【问题标题】:Chart.js vertical line when hovering and shadow on lineChart.js 悬停时垂直线和在线阴影
【发布时间】:2019-03-04 19:21:18
【问题描述】:

所以,我在我的项目中使用 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>

【问题讨论】:

    标签: javascript charts chart.js chart.js2


    【解决方案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 中关注您问题的完整代码

    【讨论】:

    • @user2164613 如果这对您有帮助,请接受作为答案。
    • 如何在draw 方法(draw: function() {) 中访问图表options 对象?谢谢!
    猜你喜欢
    • 2018-04-20
    • 2012-06-20
    • 1970-01-01
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多