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

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

我正在尝试使用图表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

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

请告诉我如何获得它。

谢谢

javascript chart.js
1个回答
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

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