Chartjs - pointColor 跟随渐变描边的当前颜色

Chartjs - pointColor to follow current color of gradient stroke

我刚刚使用 chartjs library and I managed to make stroke in gradient color. Here is simple fiddle 示例创建了折线图,这是我目前所做的。

接下来我需要做的是让 pointColor 跟随渐变 stroke 并获取其位置的当前颜色。喜欢这个 photo.

有什么实现方法吗?

谢谢!

更新

来自@AndyHolmes 的问题

不需要原解(扩展)。所需要的只是

  ...
  pointColor: gradientstroke
  ...

原解

只需延长线并更新点颜色即可。您可以在 draw 函数中执行此操作,但在初始化函数中执行此操作会很有效(当您启用动画时)

Chart.types.Line.extend({
    name: "LineAlt",
    initialize: function (data) {
        Chart.types.Line.prototype.initialize.apply(this, arguments);

        var ctx = this.chart.ctx;
        // draw a line with the gradient, we use this to get each points color
        ctx.fillStyle = data.datasets[0].strokeColor;
        ctx.fillRect(0, 0, this.chart.width, 1);

        this.datasets.forEach(function (dataset) {
            dataset.points.forEach(function (point) {
                // get the color from the gradient drew above
                var imageData = ctx.getImageData(point.x, 0, 1, 1);
                var color = 'rgba(' + imageData.data[0] + ', ' + imageData.data[1] + ', ' + imageData.data[2] + ', ' + imageData.data[3] + ')';

                // the _saved is used by the tooltip refresh to draw the point when you mouseout
                point.fillColor = color;
                point._saved.fillColor = color;
                point.strokeColor = color;
                point._saved.strokeColor = color;
            })
        })

        // we need to call the render function again to overwrite what was drawn in the initialize render (otherwise we don't see the changed color when animation is false)
        // this also wipes out the reference gradient
        this.render();
    }
});

lineChartData中的pointColor和pointStrokeColor其实不是必须的。请注意,您也可以根据需要以相同的方式覆盖 pointHighlightStroke 和 pointHighlightFill。

你这样称呼它

new Chart(ctx).LineAlt(...

Fiddle - http://jsfiddle.net/w2nh153d/