Chartjs v2中风阴影

时间:2017-04-16 16:10:38

标签: javascript html chart.js

我想在线图上使用笔触。但我找到的每个解决方案都只适用于chartjs v1。

他们的最新解决方案是什么?

这就是我用chartjs v1设计的东西,但就像我说的那样,我发现没有办法用版本2来做。

jsfiddle

Chart.types.Line.extend({
  name: "LineAlt",
  initialize: function () {
    Chart.types.Line.prototype.initialize.apply(this, arguments);

    var ctx = this.chart.ctx;
    var originalStroke = ctx.stroke;
    ctx.stroke = function () {
      ctx.save();
      ctx.shadowColor = '#E56590';
      ctx.shadowBlur = 10;
      ctx.shadowOffsetX = 0;
      ctx.shadowOffsetY = 4;
      originalStroke.apply(this, arguments)
      ctx.restore();
    }
  }
});

var data = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [
    {
      label: "My First dataset",
      fillColor: "#fff",
      strokeColor: "#ffb88c",
      pointColor: "#fff",
      pointStrokeColor: "#ffb88c",
      pointHighlightFill: "#ffb88c",
      pointHighlightStroke: "#fff",
      data: [65, 59, 80, 81, 56, 55, 40]
    }
  ]
};


var ctx = document.getElementById("canvas").getContext("2d");
var myChart = new Chart(ctx).LineAlt(data, {
  datasetFill: false
});

HTML:

<canvas id="canvas" width="600" height="300" style="background-color:#fff"></canvas>

2 个答案:

答案 0 :(得分:7)

是的!

您可以通过以下方式使用 ChartJS v2 为折线图完成相同的描边阴影效果...

&#13;
&#13;
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 = '#E56590';
            ctx.shadowBlur = 10;
            ctx.shadowOffsetX = 0;
            ctx.shadowOffsetY = 4;
            _stroke.apply(this, arguments)
            ctx.restore();
        }
    }
});

let ctx = document.getElementById("canvas").getContext('2d');
let myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [{
            label: "My First dataset",
            data: [65, 59, 80, 81, 56, 55, 40],
            borderColor: '#ffb88c',
            pointBackgroundColor: "#fff",
            pointBorderColor: "#ffb88c",
            pointHoverBackgroundColor: "#ffb88c",
            pointHoverBorderColor: "#fff",
            pointRadius: 4,
            pointHoverRadius: 4,
            fill: false
        }]
    }
});
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<canvas id="canvas" width="600" height="300" style="background-color:#fff"></canvas>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

正确的答案由etimberg和Ashot-KR在this issue at the github page for Chart.js上给出。

有关实际工作,请参见this fiddle,红线是阴影。

从小提琴中采用,给出适当的阴影,包括以下代码:

(function()
{
    var ShadowLineElement = Chart.elements.Line.extend({
        draw: function()
        {
            var ctx = this._chart.ctx;
            var originalStroke = ctx.stroke;
            ctx.stroke = function()
            {
                ctx.save();
                ctx.shadowColor = 'rgba(0,0,0,0.4)';
                ctx.shadowBlur = 2;
                ctx.shadowOffsetX = 0.5;
                ctx.shadowOffsetY = 0.5;
                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
    });
})();

然后将数据集类型从type: 'line'更改为type: 'ShadowLine'