Skip to content

Commit eb9bb44

Browse files
committed
Added error message when both round and precision used
1 parent faedd19 commit eb9bb44

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

examples/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,12 @@ <h1>Precision - Small</h1>
682682
new Chartkick.ColumnChart("precision-small", [["A",1.2345],["B",0.12345],["C",0.012345]], {precision: 3});
683683
</script>
684684

685+
<h1>Round and Precision</h1>
686+
<div id="round-precision" style="height: 300px;"></div>
687+
<script>
688+
new Chartkick.ColumnChart("round-precision", [["A",1.2345],["B",0.12345],["C",0.012345]], {round: 2, precision: 3});
689+
</script>
690+
685691
<h1>Null Data</h1>
686692
<div id="null-data" style="height: 300px;"></div>
687693
<script>

src/helpers.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ function formatValue(pre, value, options, axis) {
235235

236236
let suffix = options.suffix || "";
237237
let precision = options.precision;
238+
let round = options.round;
238239

239240
if (options.byteScale) {
240241
let baseValue = axis ? options.byteScale : value;
@@ -254,23 +255,27 @@ function formatValue(pre, value, options, axis) {
254255
suffix = " bytes";
255256
}
256257

257-
if (precision === undefined) {
258+
if (precision === undefined && round === undefined) {
258259
precision = 3;
259260
}
260261
}
261262

263+
if (precision !== undefined && round !== undefined) {
264+
throw Error("Use either round or precision, not both");
265+
}
266+
262267
if (!axis) {
263268
if (precision !== undefined) {
264269
value = value.toPrecision(precision);
265270
value = parseFloat(value).toString(); // no insignificant zeros
266271
}
267272

268-
if (options.round !== undefined) {
269-
if (options.round < 0) {
270-
let num = Math.pow(10, -1 * options.round);
273+
if (round !== undefined) {
274+
if (round < 0) {
275+
let num = Math.pow(10, -1 * round);
271276
value = parseInt((1.0 * value / num).toFixed(0)) * num;
272277
} else {
273-
value = parseFloat(value.toFixed(options.round));
278+
value = parseFloat(value.toFixed(round));
274279
}
275280
}
276281
}

0 commit comments

Comments
 (0)