Chart.js在饼图上显示标签

时间:2022-12-04 10:26:07

I recently updated my charts.js library to the most updated version (2.5.0). This version doesn't show the labels on the chart.

我最近将我的charts.js库更新为最新版本(2.5.0)。此版本不显示图表上的标签。

I have an example of working one on fiddler: http://jsfiddle.net/g6fajwg8 .

我有一个在提琴手上工作的例子:http://jsfiddle.net/g6fajwg8。

However, I defined my chart exactly as in the example but still can not see the labels on the chart.

但是,我在示例中完全定义了我的图表,但仍无法看到图表上的标签。

Note: There are a lot of questions like this on Google and * but most of them are about previous versions which is working well on them.

注意:在Google和*上有很多这样的问题,但大多数问题都与之前的版本有关。

var config = {
    type: 'pie',
    data: {
        datasets: [{
            data: [
              1200,
              1112,
              533,
              202,
              105,
            ],
            backgroundColor: [
              "#F7464A",
              "#46BFBD",
              "#FDB45C",
              "#949FB1",
              "#4D5360",
            ],
            label: 'Dataset 1'
        }],
        labels: [
          "Red",
          "Green",
          "Yellow",
          "Grey",
          "Dark Grey"
        ]
    },
    options: {
        responsive: true,
        legend: {
            position: 'top',
        },
        title: {
            display: true,
            text: 'Chart.js Doughnut Chart'
        },
        animation: {
            animateScale: true,
            animateRotate: true
        }
    }
};

window.pPercentage = new Chart(ChartContext, config);

Chart.js在饼图上显示标签

1 个解决方案

#1


35  

It seems like there is no such build in option.

似乎没有这样的构建选项。

However, there is special library for this option, it calls: "Chart PieceLabel".

但是,这个选项有一个特殊的库,它调用:“Chart PieceLabel”。

Here is their demo.

这是他们的演示。

After you add their script to your project, you might want to add another option, called: "pieceLabel", and define the properties values as you like:

将脚本添加到项目后,可能需要添加另一个名为“pieceLabel”的选项,并根据需要定义属性值:

pieceLabel: {
    // mode 'label', 'value' or 'percentage', default is 'percentage'
    mode: (!mode) ? 'value' : mode,

    // precision for percentage, default is 0
    precision: 0,

    // font size, default is defaultFontSize
    fontSize: 18,

    // font color, default is '#fff'
    fontColor: '#fff',

    // font style, default is defaultFontStyle
    fontStyle: 'bold',

    // font family, default is defaultFontFamily
    fontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
}

#1


35  

It seems like there is no such build in option.

似乎没有这样的构建选项。

However, there is special library for this option, it calls: "Chart PieceLabel".

但是,这个选项有一个特殊的库,它调用:“Chart PieceLabel”。

Here is their demo.

这是他们的演示。

After you add their script to your project, you might want to add another option, called: "pieceLabel", and define the properties values as you like:

将脚本添加到项目后,可能需要添加另一个名为“pieceLabel”的选项,并根据需要定义属性值:

pieceLabel: {
    // mode 'label', 'value' or 'percentage', default is 'percentage'
    mode: (!mode) ? 'value' : mode,

    // precision for percentage, default is 0
    precision: 0,

    // font size, default is defaultFontSize
    fontSize: 18,

    // font color, default is '#fff'
    fontColor: '#fff',

    // font style, default is defaultFontStyle
    fontStyle: 'bold',

    // font family, default is defaultFontFamily
    fontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif"
}