当悬停和阴影在线时Chart.js垂直线

问题描述 投票:0回答:1

所以,我使用Chart.js作为我的项目,这就是我在PSD中看到的。 enter image description here

好。我开始探索这个问题,并且实际上找到了我的问题的答案。分别。

对于垂直线 - 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个回答
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中按照您的问题的完整代码

© www.soinside.com 2019 - 2024. All rights reserved.