Connecting points between missing data in chart.js

后端 未结 4 2064
你的背包
你的背包 2020-12-20 12:28

I\'m using chart.js and I have some missing data between multiple day entries at certain points in my chart. I\'ve assigned these values null, but would like the chart to dr

4条回答
  •  一生所求
    2020-12-20 12:38

    I had the same problem and i've modified this version like this:

                        var lastPoint = null;
                    helpers.each(dataset.points, function (point, index) {
    
                        if (!point.ignore && dataset.skipNullValues && lastPoint) {
                            ctx.beginPath();
                            ctx.moveTo(lastPoint.x, lastPoint.y);
    
                            if (this.options.bezierCurve) {
                                ctx.bezierCurveTo(
                                    lastPoint.controlPoints.outer.x,
                                    lastPoint.controlPoints.outer.y,
                                    point.controlPoints.inner.x,
                                    point.controlPoints.inner.y,
                                    point.x,
                                    point.y
                                );
                            } else {
                                ctx.lineTo(point.x, point.y);
                            }
                            ctx.stroke();
                        }
    
                        if (index > 0 && !dataset.points[index - 1].ignore && !point.ignore) {
                            if (this.options.bezierCurve) {
                                ctx.bezierCurveTo(
                                    dataset.points[index - 1].controlPoints.outer.x,
                                    dataset.points[index - 1].controlPoints.outer.y,
                                    point.controlPoints.inner.x,
                                    point.controlPoints.inner.y,
                                    point.x,
                                    point.y
                                );
                            }
                            else {
                                ctx.lineTo(point.x, point.y);
                            }
    
                            lastPoint = point;
                        }
                        else if (index === 0 || !point.ignore) {
                            ctx.moveTo(point.x, point.y);
    
                            if (!point.ignore) {
                                lastPoint = point;
                            }
                        }
    
                    }, this);
    

    For a better structure i have set a property for the dataset called "skipNullValues":

                var datasetObject = {
                    label: dataset.label || null,
                    fillColor: dataset.fillColor,
                    strokeColor: dataset.strokeColor,
                    pointColor: dataset.pointColor,
                    pointStrokeColor: dataset.pointStrokeColor,
                    tooltip: dataset.tooltip,
                    line: dataset.line,
                    fill: dataset.fill,
                    points: [],
                    skipNullValues: dataset.skipNullValues
                };
    

    Here is the full working version!

    Maybe there is a better solution, but for my uses it works.

    Let me know if it is working for you!

提交回复
热议问题