admin管理员组文章数量:1310306
I'm using chart.js
to build a line chart. I was wondering if it is possible to add a drop shadow to the line chart I'm displaying. To clarify I mean add a drop shadow to the line itself. Like this:
Here is the code and code snippets.
// Line Chart
var ctx = document.getElementById("salesData").getContext("2d");
var gradient = ctx.createLinearGradient(0,0,700,0);
gradient.addColorStop(0, 'rgba(255, 204, 128, 1)');
gradient.addColorStop(0.5, 'rgba(255, 152, 0, 1)');
gradient.addColorStop(1, 'rgba(239, 108, 0, 1)');
var salesData = {
labels: ["1", "2", "3", "4", "5", "6", "7", "8"],
datasets: [
{
label: "Front",
fillColor: "rgba(0, 0, 0, 0)",
strokeColor: gradient,
pointColor: gradient,
pointStrokeColor: "#202b33",
pointHighlightStroke: "rgba(225,225,225,0.9)",
data: [0, 10, 40, 48, 55, 64, 55, 72]
}
]
};
window.myLineChart = new Chart(ctx).Line(salesData, {
pointDotRadius : 0,
pointDotStrokeWidth : 0,
datasetStrokeWidth : 4,
scaleShowVerticalLines: true,
scaleGridLineWidth : 2,
scaleShowGridLines : true,
scaleGridLineColor : "rgba(225, 255, 255, 0.02)",
scaleOverride: true,
scaleSteps: 12,
scaleStepWidth: 10,
scaleStartValue: 0,
responsive: true
});
<script src=".js/1.0.1/Chart.min.js"></script>
<canvas id="salesData"></canvas>
I'm using chart.js
to build a line chart. I was wondering if it is possible to add a drop shadow to the line chart I'm displaying. To clarify I mean add a drop shadow to the line itself. Like this:
Here is the code and code snippets.
// Line Chart
var ctx = document.getElementById("salesData").getContext("2d");
var gradient = ctx.createLinearGradient(0,0,700,0);
gradient.addColorStop(0, 'rgba(255, 204, 128, 1)');
gradient.addColorStop(0.5, 'rgba(255, 152, 0, 1)');
gradient.addColorStop(1, 'rgba(239, 108, 0, 1)');
var salesData = {
labels: ["1", "2", "3", "4", "5", "6", "7", "8"],
datasets: [
{
label: "Front",
fillColor: "rgba(0, 0, 0, 0)",
strokeColor: gradient,
pointColor: gradient,
pointStrokeColor: "#202b33",
pointHighlightStroke: "rgba(225,225,225,0.9)",
data: [0, 10, 40, 48, 55, 64, 55, 72]
}
]
};
window.myLineChart = new Chart(ctx).Line(salesData, {
pointDotRadius : 0,
pointDotStrokeWidth : 0,
datasetStrokeWidth : 4,
scaleShowVerticalLines: true,
scaleGridLineWidth : 2,
scaleShowGridLines : true,
scaleGridLineColor : "rgba(225, 255, 255, 0.02)",
scaleOverride: true,
scaleSteps: 12,
scaleStepWidth: 10,
scaleStartValue: 0,
responsive: true
});
<script src="http://cdnjs.cloudflare./ajax/libs/Chart.js/1.0.1/Chart.min.js"></script>
<canvas id="salesData"></canvas>
Share
Improve this question
edited Oct 13, 2015 at 13:49
Andy Holmes
asked Oct 13, 2015 at 13:39
Andy HolmesAndy Holmes
8,08710 gold badges54 silver badges91 bronze badges
2 Answers
Reset to default 6Extending the chart and overriding the draw
function would be one (plicated) way to do this.
An easier way would be to duplicate the chart canvas with all the other elements (grid lines, scale labels...) hidden, style the line differently (thicker and in a gray shade). Then position this duplicate canvas under and to the bottom and right of the original canvas.
CSS
.shadowParent {
position: relative;
}
.shadowParent canvas.firstShadow {
position: absolute;
left: 10px;
top: 30px;
z-index: -1;
}
HTML
<div class="shadowParent">
<canvas id="myChartShadow" class="firstShadow" width="600"></canvas>
<canvas id="myChart" width="600"></canvas>
</div>
Script
...
var ctxShadow = document.getElementById("myChartShadow").getContext("2d");
var dataShadow = JSON.parse(JSON.stringify(data));
dataShadow.datasets[0].strokeColor = "rgba(220,220,220,0.2)"
new Chart(ctxShadow).Line(dataShadow, {
datasetStrokeWidth: 10,
datasetFill: false,
pointDot: false,
showTooltips: false,
});
If your shadow isn't blurry enough you could add one more layer
CSS
.shadowParent canvas.secondShadow {
position: absolute;
left: 10px;
top: 30px;
z-index: -1;
}
HTML
<div class="shadowParent">
<canvas id="myChartShadow2" class="secondShadow" width="600"></canvas>
...
Script
var ctxShadow2 = document.getElementById("myChartShadow2").getContext("2d");
var dataShadow2 = JSON.parse(JSON.stringify(data));
dataShadow2.datasets[0].strokeColor = "rgba(220,220,220,0.1)"
new Chart(ctxShadow2).Line(dataShadow2, {
datasetStrokeWidth: 20,
datasetFill: false,
pointDot: false,
showTooltips: false,
scaleFontColor: "rgba(0,0,0,0)",
scaleLineColor: "rgba(0,0,0,0)",
scaleShowGridLines: false,
datasetFill: false,
});
Note that the scale aligns with the first shadow (it gives it a more 3D feel), but you can move it to the first layer if the scale is important (vs. this being more of an rough look kind of graph)
Fiddle - http://jsfiddle/fjyj1021/
You can use filters:
g.ct-series {filter:url(#shadow);}
<defs>
<filter id="shadow" x="-50%" y="-50%" width="200%" height="200%">
<feDropShadow dx="2" dy="2" stdDeviation="6" flood-opacity="0.80" />
</filter>
</defs>
本文标签: javascriptIs it possible to add a drop shadow to chartjs line chartStack Overflow
版权声明:本文标题:javascript - Is it possible to add a drop shadow to chart.js line chart? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741828709a2399801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论