因此,我将Chart.js用于我的项目,这就是我在PSD中看到的。
好的。我开始探索这个问题,并实际上找到了我的问题的答案。另外。
对于垂直线-Moving vertical line when hovering over the chart using chart.js
对于阴影-https://jsfiddle.net/dces93wv/或https://github.com/chartjs/Chart.js/issues/4977
但是几个小时以来,我一直无法弄清楚如何结合这两种方法。 :(
const ShadowLineElement = Chart.elements.Line.extend({
draw () {
const { ctx } = this._chart
const originalStroke = ctx.stroke
ctx.stroke = function () {
ctx.save()
ctx.shadowColor = 'red'
ctx.shadowBlur = 0
ctx.shadowOffsetX = 0
ctx.shadowOffsetY = 8
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
})
new Chart(document.getElementById('canvas'), {
type: 'ShadowLine',
data: {
datasets: [
{
label: 'somedata',
fill: false,
borderColor: 'green',
data: [
10, 20
]
}
]
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js"></script>
<p>
<b style="color: red">red</b> is shadow
</p>
<canvas id="canvas" width="200" height="100"></canvas>
答案 0 :(得分:1)
您可以使用Chart Js库(docs)自定义各种内容。
要向图表线添加阴影,可以使用Chart.controllers.line
并创建一个绘制阴影的函数。
阴影示例:
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 = '#000000';
ctx.shadowBlur = 10;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 4;
_stroke.apply(this, arguments)
ctx.restore();
}
}
});
要创建垂直线,您可以使用Chart.defaults.LineWithLine
并创建一个函数来绘制垂直线。
示例:
Chart.defaults.LineWithLine = Chart.defaults.line;
Chart.controllers.LineWithLine = Chart.controllers.line.extend({
draw: function(ease) {
Chart.controllers.line.prototype.draw.call(this, ease);
if (this.chart.tooltip._active && this.chart.tooltip._active.length) {
var activePoint = this.chart.tooltip._active[0],
ctx = this.chart.ctx,
x = activePoint.tooltipPosition().x,
topY = this.chart.scales['y-axis-0'].top,
bottomY = this.chart.scales['y-axis-0'].bottom;
// draw line
ctx.save();
ctx.beginPath();
ctx.moveTo(x, topY);
ctx.lineTo(x, bottomY);
ctx.lineWidth = 2;
ctx.strokeStyle = '#07C';
ctx.stroke();
ctx.restore();
}
}
});
在我的Fiddle
中关注您问题的完整代码