gpt4 book ai didi

javascript - Chartjs - 点颜色以跟随渐变笔划的当前颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:44:36 25 4
gpt4 key购买 nike

我刚刚使用 chartjs 创建了折线图图书馆,我设法用渐变颜色画笔画。这里很简单 fiddle例如我到目前为止所做的。

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

有什么想法可以实现吗?

谢谢!

最佳答案

更新

来自@AndyHolmes 的问题 Is it possible to add a drop shadow to chart.js line chart?

不需要原始解决方案(扩展)。所需要的只是简单的

  ...
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/


enter image description here

关于javascript - Chartjs - 点颜色以跟随渐变笔划的当前颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31896466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com