use*_*613 2 javascript charts chart.js chart.js2
所以,我将 Chart.js 用于我的项目,这就是我在 PSD 中看到的。
好的。我开始探索这个问题,实际上找到了我的问题的答案。分别地。
对于垂直线 -使用 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
]
}
]
}
})
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
您可以使用 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();
}
}
});
Run Code Online (Sandbox Code Playgroud)
要创建垂直线,您也可以使用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();
}
}
});
Run Code Online (Sandbox Code Playgroud)
在我的Fiddle 中按照您的问题的完整代码进行操作
归档时间: |
|
查看次数: |
4430 次 |
最近记录: |