Chartjs - pointColor to follow current color of gradient stroke(Chartjs - pointColor 跟随渐变描边的当前颜色)
问题描述
我刚刚使用
I just created Line Chart using chartjs library and I managed to make stroke in gradient color. Here is simple fiddle example what I did so far.
What I need next, is to make pointColor
to follow gradient stroke
and to pick up the current color of it's position. Like on this photo.
Any ideas how to achieve that?
Thanks!
Update
From @AndyHolmes question at Is it possible to add a drop shadow to chart.js line chart?
The original solution (extending) is not required. All that is required is simply
...
pointColor: gradientstroke
...
Original Solution
Just extend the line and update the point colors. You could do it in the draw function, but it would be efficient (when you have animation enabled) to do it in initialize function
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();
}
});
The pointColor and pointStrokeColor in the lineChartData are actually not required. Note that you could also override the pointHighlightStroke and pointHighlightFill the same way if you wanted to.
You call it like so
new Chart(ctx).LineAlt(...
Fiddle - http://jsfiddle.net/w2nh153d/
这篇关于Chartjs - pointColor 跟随渐变描边的当前颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Chartjs - pointColor 跟随渐变描边的当前颜色


基础教程推荐
- 在for循环中使用setTimeout 2022-01-01
- 动态更新多个选择框 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06