BarChart: Round floating-point numbers to 2 digits by default

This commit is contained in:
Aaron Lindsay 2017-01-11 14:18:19 -05:00
parent b578f3250d
commit a0534f78a8

View File

@ -11,6 +11,9 @@ module.exports = React.createClass({
* {'Label': 'bar', 'Value': 8} * {'Label': 'bar', 'Value': 8}
* ]; * ];
*/ */
var roundTo = 2;
if (this.props.roundTo)
roundTo = this.props.roundTo;
var rows = []; var rows = [];
if (this.props.data.length >= 1) { if (this.props.data.length >= 1) {
@ -26,15 +29,16 @@ module.exports = React.createClass({
for (var i = 0; i < this.props.data.length; i++) { for (var i = 0; i < this.props.data.length; i++) {
var rowData = this.props.data[i]; var rowData = this.props.data[i];
var value = parseFloat((rowData.Value).toFixed(roundTo))
if ((max - min) == 0.0) if ((max - min) == 0.0)
var percent = 100; var percent = 100;
else if (min < 0) else if (min < 0)
var percent = 100*(parseFloat(rowData.Value)-min)/(max-min); var percent = 100*(value-min)/(max-min);
else else
var percent = 100*parseFloat(rowData.Value)/max; var percent = 100*value/max;
rows.push(( rows.push((
<tr key={i}> <tr key={i}>
<td style={{'width': '20%'}}>{rowData.Label + " (" + rowData.Value + ")"}</td> <td style={{'width': '20%'}}>{rowData.Label + " (" + value + ")"}</td>
<td style={{'width': '80%'}}><div style={{'width': percent + "%", 'backgroundColor': 'steelblue'}}>&nbsp;</div></td> <td style={{'width': '80%'}}><div style={{'width': percent + "%", 'backgroundColor': 'steelblue'}}>&nbsp;</div></td>
</tr> </tr>
)); ));