Help: Shadows on line charts #9966
-
I'm trying to draw a line chart with shadows. The only way I have found is to override let draw = Chart.controllers.line.prototype.draw;
Chart.helpers.extend(Chart.controllers.line.prototype,{
draw: function () {
draw.apply(this, arguments);
let ctx = this.chart.ctx;
let _stroke = ctx.stroke;
ctx.stroke = function () {
ctx.save();
ctx.shadowColor = "rgba(39, 127, 254, 1)";
ctx.shadowBlur = 5;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 4;
_stroke.apply(this, arguments);
ctx.restore();
};
},
}); Is there any way to draw shadows only for lines? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I guess this behaviour is comming from that you override the canvas stroke function with your own function where you give it a shadow. If you dont do this it works fine: https://jsfiddle.net/Leelenaleee/x7ao5419/5/ class lineWithShadow extends Chart.controllers.line {
draw() {
const ctx = this._ctx;
ctx.shadowColor = "rgba(39, 127, 254, 1)";
ctx.shadowBlur = 5;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 4;
super.draw();
}
}
lineWithShadow.id = 'lineWithShadow';
Chart.register(lineWithShadow);
const options = {
type: 'lineWithShadow',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'pink'
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderColor: 'orange'
}
]
},
options: {
scales: {}
}
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options); |
Beta Was this translation helpful? Give feedback.
-
Thanks for the help! I really appreciate it :) I tried this way but the shadow didn't render @LeeLenaleee is there any way without doing by classes |
Beta Was this translation helpful? Give feedback.
-
I had tried this way, it's working fine let draw = Chart.controllers.line.prototype.draw;
Chart.helpers.extend(Chart.controllers.line.prototype, {
draw: function () {
let ctx = this.chart.ctx;
ctx.save();
ctx.shadowColor = "rgba(54, 54, 54, 0.2)";
ctx.shadowBlur = 5;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 4;
draw.apply(this, arguments);
ctx.restore();
},
}); By using this way, shadows won't appear on gridlines, also if we are using an |
Beta Was this translation helpful? Give feedback.
I guess this behaviour is comming from that you override the canvas stroke function with your own function where you give it a shadow. If you dont do this it works fine: https://jsfiddle.net/Leelenaleee/x7ao5419/5/