sgratzl / chartjs-chart-boxplot

Chart.js Box Plots and Violin Plot Charts
https://www.sgratzl.com/chartjs-chart-boxplot/
MIT License
107 stars 24 forks source link

Tooltip displaying NaN for values >999 #9

Closed Djones4822 closed 3 years ago

Djones4822 commented 3 years ago

Tooltips are showing NaN for values > 999, I think it's because the number is formatted as a string before it is passed to the formatter again. I can avoid this by writing my own label callback and doing my own formatting/handling.

To Reproduce

create project, run npm install --save @sgratzl/chartjs-chart-boxplot, download the items sample and utils sample files, update script src in items.html and update line 19 to something like:

const random = (min, max) => () => b() * ((max || 10000) - (min || 1000)) + (min || 1000);

Screenshots

image

Context

Djones4822 commented 3 years ago

From what I've found digging into index.umd.js - during the getLabelAndValue definition inside of StatsBase$1 there is a call to _transformStats which we are passing vScale.getLabelForValue as the mapping function - this is where the number is initially formatted with commas.

Then during _toStringStats the string value is passed to helpers.formatNumber which yields the NaN.

Haven't found a great solution given that parts of the world use commas as thousands-separators, and others use it as the decimal separator.

Since my project does not chart user-input and I'm in the US - I can safely treat commas as thousand separators, my revised _toStringStats() method is now:

        _toStringStats(b) {
            const f = (v) => (v == null ? 'NaN' : helpers.formatNumber((typeof v === 'string' ? v.replace(',','') : v), this.chart.options.locale, {}));
            //const f = (v) => (v);
            return `(min: ${f(b.min)}, 25% quantile: ${f(b.q1)}, median: ${f(b.median)}, mean: ${f(b.mean)}, 75% quantile: ${f(b.q3)}, max: ${f(b.max)})`;
        }
sgratzl commented 3 years ago

should be fixed with v3.5.1 of this plugin