Makanz / chartjs-plugin-trendline

This plugin draws an linear trendline in your Chart. Made for Chart.js > 3.0
MIT License
80 stars 57 forks source link

Bug in 2.0.3: Error when first data point is 0, "The provided double value is non-finite" #79

Open tsnork opened 11 months ago

tsnork commented 11 months ago

when the first value in data array is 0

let data = {
    "labels": [
        202303,
        202304,
        202305,
        202306
    ],
    "datasets": [
        {
            "data": [
                0,
                1,
                3,
                6
            ],
            "label": "Test",
            "borderColor": "#409b8c",
            "trendlineLinear": {
                "style": "#409b8c",
                "lineStyle": "dotted",
                "width": 1,
                "projection": false
            }
        }
    ]
};

the following error gets thrown:

Uncaught TypeError: Failed to execute 'createLinearGradient' on 'CanvasRenderingContext2D': The provided double value is non-finite. at addFitter (chartjs-plugin-trendline.js:136:24)

Changing the first value to any int > 0 solves this issue, every other data point can be 0. Values < 0 cause this error as well.

let data = {
    "labels": [
        202303,
        202304,
        202305,
        202306
    ],
    "datasets": [
        {
            "data": [
                2
                0,
                3,
                6
            ],
            "label": "Test",
            "borderColor": "#409b8c",
            "trendlineLinear": {
                "style": "#409b8c",
                "lineStyle": "dotted",
                "width": 1,
                "projection": false
            }
        }
    ]
};

Here's a codepen with the reproducible issue:

https://codepen.io/tsnork/pen/RwqqaJg

Thx for your work with this library!

Makanz commented 10 months ago

I removed the "logarithmic" setting, and that made the chart start working. Now, I need to keep looking into why logarithmic scale caused this problem. @tsnork