简体   繁体   English

如何向Chart.js饼图添加笔触?

[英]How can I add stroke to Chart.js Pie Charts?

I want each section in the pie chart to have its own stroke color. 我希望饼图中的每个部分都有其自己的笔触颜色。 Right now, it only seems to do a global stroke--can I get a stroke over each pie segment? 现在,它似乎只是全局笔画-我可以在每个饼图片段上笔画吗? I've tried the following, with no luck: 我尝试了以下方法,但没有运气:

 Chart.defaults.global.segmentShowStroke = true; var ctx = document.getElementById("myChart").getContext("2d"); var data = [{ value: 300, color: "#F7464A", highlight: "#FF5A5E", stroke: "#FF0000", // Doesn't work label: "Red" }, { value: 50, color: "#46BFBD", highlight: "#5AD3D1", strokeColor: "#00FF00", // Doesn't work label: "Green" }, { value: 100, color: "#FDB45C", highlight: "#FFC870", segmentStrokeColor: "#88FF00", // Doesn't work label: "Yellow" }]; /** * Whether or not segmentStrokeColor is enabled, * the previous stroke options do not work. */ var options = { segmentStrokeColor: "black" // Works. Defaults to "white" if removed }; var myPieChart = new Chart(ctx).Pie(data, options); 
 canvas { border: 1px solid black; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.1/Chart.min.js"></script> <canvas id="myChart" width="150" height="150"></canvas> 

Note that even if segmentStrokeColor is removed from the options variable, the previous attempts to color the stroke of each pie segment do not work. 请注意,即使从options变量中删除了segmentStrokeColor ,上一次为每个扇形段的笔触着色的尝试也无效。

Unlike Pie and Bar charts, the Pie one doesn't support a strokeColor per segment, it only uses a global one. 与饼图和条形图不同,饼图不支持每个段的strokeColor ,它仅使用全局图。

The only way to achieve what you want is to extend the Pie chart, copying the addData method and using segment.strokeColor instead of this.options.segmentStrokeColor : 达到你想要的东西的唯一方法是延长饼图,复制addData方法和使用segment.strokeColor代替this.options.segmentStrokeColor的:

Chart.types.Pie.extend({
    name: 'SegmentStrokeColorPie',
    addData : function(segment, atIndex, silent){
      ...
      [Copy all the addData method here, only change the line below]
      ...
          strokeColor : segment.strokeColor,
      ...
      ...
    }
});

var data = [
    {
        value: 300,
        color:"#F7464A",
        highlight: "#FF5A5E",
        label: "Red",
        strokeColor: "#FF0000"
    },
    {
        value: 50,
        color: "#46BFBD",
        highlight: "#5AD3D1",
        label: "Green",
        strokeColor: "#00FF00"
    }
];

new Chart(ctx).SegmentStrokeColorPie(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM