score:16
Accepted answer
yes!
you could accomplish the same stroke shadow effect for line chart with chartjs v2 in the following way ...
let draw = chart.controllers.line.prototype.draw;
chart.controllers.line = chart.controllers.line.extend({
draw: function() {
draw.apply(this, arguments);
let ctx = this.chart.chart.ctx;
let _stroke = ctx.stroke;
ctx.stroke = function() {
ctx.save();
ctx.shadowcolor = '#e56590';
ctx.shadowblur = 10;
ctx.shadowoffsetx = 0;
ctx.shadowoffsety = 4;
_stroke.apply(this, arguments)
ctx.restore();
}
}
});
let ctx = document.getelementbyid("canvas").getcontext('2d');
let mychart = new chart(ctx, {
type: 'line',
data: {
labels: ["january", "february", "march", "april", "may", "june", "july"],
datasets: [{
label: "my first dataset",
data: [65, 59, 80, 81, 56, 55, 40],
bordercolor: '#ffb88c',
pointbackgroundcolor: "#fff",
pointbordercolor: "#ffb88c",
pointhoverbackgroundcolor: "#ffb88c",
pointhoverbordercolor: "#fff",
pointradius: 4,
pointhoverradius: 4,
fill: false
}]
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.5.0/chart.min.js"></script>
<canvas id="canvas" width="600" height="300" style="background-color:#fff"></canvas>
score:1
the proper answer is given on this issue at the github page for chart.js by etimberg and ashot-kr.
see this fiddle for the work in action, the red line is the shadow.
adopted from the fiddle, giving a proper shadow, include this code:
(function()
{
var shadowlineelement = chart.elements.line.extend({
draw: function()
{
var ctx = this._chart.ctx;
var originalstroke = ctx.stroke;
ctx.stroke = function()
{
ctx.save();
ctx.shadowcolor = 'rgba(0,0,0,0.4)';
ctx.shadowblur = 2;
ctx.shadowoffsetx = 0.5;
ctx.shadowoffsety = 0.5;
originalstroke.apply(this, arguments);
ctx.restore();
};
chart.elements.line.prototype.draw.apply(this, arguments);
ctx.stroke = originalstroke;
}
});
chart.defaults.shadowline = chart.defaults.line;
chart.controllers.shadowline = chart.controllers.line.extend({
datasetelementtype: shadowlineelement
});
})();
and then change the dataset type from type: 'line'
to type: 'shadowline'
.
Source: stackoverflow.com
Related Query
- Chartjs v2 stroke shadow
- Chartjs - pointColor to follow current color of gradient stroke
- Removing ChartJS 2 border and shadow from point style legend
- How to add ChartJS code in Html2Pdf to view image
- Use transparent stroke color with chartjs pie chart
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- Chartjs not working with d3 from csv source
- ChartJS adding shadow color to grid and custom x-axis labels
- ChartJS have xAxes labels match data source
- Updating Chartjs to 2.5 with custom code
- add shadow for line in vue chartjs
- Chartjs 2 Line Graph Single Stroke Between Datasets
- Calling data from outside of Chartjs code
- VueJS + Chartjs - Chart only renders after code change
- ChartJS 2.0 differences in code help needed
- My Chartjs is not updating after setstate full code attached Reactjs Update
- Chartjs doc examples are lightning fast but same code is slow when reproducing
- 'require is not defined' error when attempting to use chartjs in javascript code
- How to run Chart.js samples using source code
- Code for Chartjs Typescript definition file
- Dynamically update values of a chartjs chart
- Different color for each bar in a bar chart; ChartJS
- Hide points in ChartJS LineGraph
- ChartJS Line Charts - remove color underneath lines
- Skip decimal points on y-axis in chartJS
- How to set ChartJS Y axis title?
- Chartjs Bar Chart showing old data when hovering
- Chartjs change grid line color
- Chart area background color chartjs
- Custom Legend with ChartJS v2.0
More Query from same tag
- Chart.js line split color
- Angular 2+: How to load a data to chartjs graph in every second in Angular
- How to use chart.js script to draw multiple pie Charts
- Vue3 does not show line-chart
- Displaying every nth label on the x-axis
- Chart.js returns a console error and does not display the chart when using variables as data input
- Chart not rendering w/Chart.js on button click
- add a unit to label with chartjs plugin datalabels
- Vuejs parameters in props
- Returning fetched JSON correctly but can't get it into Chart.JS
- How to dynamically call function with a for loop within Chart.js (JS)
- How to add images as labels to Canvas Charts using chart.js
- Vue ChartKick - different colours for bar chart
- Chart.js chart not displaying until I open the browser's console
- How to edit the ID of each button in a class if a condition is true in JavaScript?
- Chart.js add border around line chart, and yAxis unit title ([s])
- How to convert pandas dataframe to specific json format
- Chart.js : How I change the x axes ticks labels alignment in any sizes?
- donut chart not rendering with csv data chart.js
- How to push datasets dynamically for chart.js (bar chart)?
- Export chart.js canvas to prawn-pdf in rails
- How to pass Arrays from backing bean to JavaScript to draw a chart using Chart.js in an Ajax call
- Make a Histogram in Chart.js
- Chart.js chart onclick disables legend onclick
- How can I add padding between tick labels?
- Chart.js v2 - combined stacked bar chart and 2 unstacked lines
- Chart isn't updating with Response data (Chart.js 3.2.1, ng2-charts 3.0.0-beta.9)
- chartjs-plugin-annotation won't draw anything
- Change <p> and <h> element when selecting something from dropdown
- Horizontally align Chart.js y axes