在带有标签的图表js中创建饼图

时间:2019-02-25 11:04:24

标签: javascript chart.js

我正在尝试使用Chart js创建一个饼图。我的数据如下。

data: {
    labels: ["Green", "Blue", "Gray", "Purple", "Yellow", "Red"],
    datasets: [{
      backgroundColor: ["#7ECFA2", "#866B42", "#77FF81", "#F127F8", "#9647BC", "#74CB15"],
      data: [2, 9, 2, 1, 1, 1]
    }]
  }

使用此数据,我可以创建一个饼图。但是在这里,我的要求是,不是将鼠标悬停在饼图上,而是可以直接在饼图上显示label。这是我当前的working fiddle

我不介意图例是否被禁用,如果我在饼图本身上获取了图例值。

请让我知道如何获得它。

谢谢

1 个答案:

答案 0 :(得分:2)

这个github问题是关于如何始终在图表js V2中显示工具提示的。

https://github.com/chartjs/Chart.js/issues/1861

您需要为图表创建一个插件。为了获得更完整的答案,我只需从上面链接中的jsfiddle复制代码。考虑到您的HTML中有图表:

<canvas id="canvas"></canvas>

然后您制作数据以及图表的插件:

    var ctx = document.getElementById("canvas").getContext("2d");

    var data = {
        labels: [
            "Red",
            "Green",
            "Yellow"
        ],
        datasets: [
            {
                data: [300, 50, 100],
                backgroundColor: [
                    "#FF6384",
                    "#36A2EB",
                    "#FFCE56"
                ],
                hoverBackgroundColor: [
                    "#FF6384",
                    "#36A2EB",
                    "#FFCE56"
                ]
            }]
    };

    Chart.pluginService.register({
        beforeRender: function (chart) {
            if (chart.config.options.showAllTooltips) {
                // create an array of tooltips
                // we can't use the chart tooltip because there is only one tooltip per chart
                chart.pluginTooltips = [];
                chart.config.data.datasets.forEach(function (dataset, i) {
                    chart.getDatasetMeta(i).data.forEach(function (sector, j) {
                        chart.pluginTooltips.push(new Chart.Tooltip({
                            _chart: chart.chart,
                            _chartInstance: chart,
                            _data: chart.data,
                            _options: chart.options.tooltips,
                            _active: [sector]
                        }, chart));
                    });
                });

                // turn off normal tooltips
                chart.options.tooltips.enabled = false;
            }
        },
        afterDraw: function (chart, easing) {
            if (chart.config.options.showAllTooltips) {
                // we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once
                if (!chart.allTooltipsOnce) {
                    if (easing !== 1)
                        return;
                    chart.allTooltipsOnce = true;
                }

                // turn on tooltips
                chart.options.tooltips.enabled = true;
                Chart.helpers.each(chart.pluginTooltips, function (tooltip) {
                    tooltip.initialize();
                    tooltip.update();
                    // we don't actually need this since we are not animating tooltips
                    tooltip.pivot();
                    tooltip.transition(easing).draw();
                });
                chart.options.tooltips.enabled = false;
            }
        }
    })

    var myPieChart = new Chart(ctx, {
        type: 'pie',
        data: data,
        options: {
            showAllTooltips: true
        }
    });

如果您想了解有关如何创建图表js插件的更多信息,我发现这些链接非常有用:

https://www.chartjs.org/docs/latest/developers/plugins.html

https://levelup.gitconnected.com/how-to-write-your-own-chartjs-plugin-e46199ae5734

https://blog.larapulse.com/javascript/creating-chart-js-plugins