如何为条形图创建图例?

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

如何创建如下图例:

enter image description here

到目前为止,我有以下内容:

enter image description here

我无法弄清楚如何为图例指定标签。他们也没有提供例子。

https://www.chartjs.org/docs/latest/configuration/legend.html

到目前为止,我有以下代码:

var topClickSourcesChart = new Chart('dashboard-click-source-chart', {
    type: 'bar',
    data: {
      labels: ["Facebook","Google","NONE",],
      datasets: [{
        data: [10,5,3,],
        backgroundColor: ['#4D90C3', '#866DB2', '#EA6F98', '#61BDF6', '#768BB7'],
      }],
    },
    options: {
        scales: {
            xAxes: [{
                display: false,
            }]
        },
        legend: {
            position: 'right',
            labels: {
                boxWidth: 10,
            }
        }
    }
});
javascript charts chart.js
2个回答
4
投票

要显示图例,您需要设置labeldataset属性,因此可以通过从代码创建图表来构建您期望的输出类型,如下所示。小提琴 - > https://jsfiddle.net/6nkcx8sq/

var topClickSourcesChart = new Chart('dashboard-click-source-chart', {
    type: 'bar',
    data: {
      labels: ["Number of users"],
      datasets: [{
        label: 'Facebook',
        data: [10],
        backgroundColor: '#4D90C3',
      },
      {
        label: 'Instagram',
        data: [15],
        backgroundColor: '#866DB2',
      },
      {
        label: 'Twitter',
        data: [7],
        backgroundColor: '#EA6F98',
      }],
    },
    options: {
        scales: {
            xAxes: [{
                display: false,
            }],
            yAxes: [{
            ticks: {
                beginAtZero: true
            }
        }]
        },
        legend: {
            position: 'right',
            labels: {
                boxWidth: 10,
            }
        }
    }
});

0
投票

我能够通过以下方式获得它,但由于backgroundColor必须在dataset内部,因此循环的屁股很痛苦。

var topClickSourceChart = new Chart('dashboard-click-source-chart', {
    type: 'bar',
    data: {
      labels: ["Facebook","Google","NONE",],
      datasets: [
          {label: "Facebook", data: [10]},{label: "Google", data: [5]},{label: "NONE", data: [3]},
      ]
    },
    options: {
        scales: {
            xAxes: [{
                display: false,
            }],
            yAxes: [{
                ticks: {
                    beginAtZero: true,
                }
            }]
        },
        legend: {
            position: 'right',
            labels: {
                boxWidth: 10,
            }
        }
    }
});
if (topClickSourceChart.data.datasets.length > 0) topClickSourceChart.data.datasets[0].backgroundColor = '#4D90C3';
if (topClickSourceChart.data.datasets.length > 1) topClickSourceChart.data.datasets[1].backgroundColor = '#866DB2';
if (topClickSourceChart.data.datasets.length > 2) topClickSourceChart.data.datasets[2].backgroundColor = '#EA6F98';
if (topClickSourceChart.data.datasets.length > 3) topClickSourceChart.data.datasets[3].backgroundColor = '#61BDF6';
if (topClickSourceChart.data.datasets.length > 4) topClickSourceChart.data.datasets[4].backgroundColor = '#768BB7';

这是JSP / JSTL循环:

data: {
  labels: [<c:forEach items="${clickSources}" var="cs">"${cs.key}",</c:forEach>],
  datasets: [
      <c:forEach items="${clickSources}" var="cs">{label: "${cs.key}", data: [${cs.value}]},</c:forEach>
  ]
},

我仍然找不到一种方法来使条形的顶部四舍五入。

enter image description here

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