代码之家  ›  专栏  ›  技术社区  ›  Chloe

如何为条形图创建图例?

  •  0
  • Chloe  · 技术社区  · 5 年前

    如何创建如下图例:

    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,
                }
            }
        }
    });
    
    0 回复  |  直到 5 年前
        1
  •  4
  •   Kunal Khivensara    5 年前

    要显示图例,您需要设置 label a的性质 dataset ,因此可以通过从下面所示的代码创建图表来生成所需的输出类型。小提琴-> 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,
                }
            }
        }
    });
    
        2
  •  0
  •   Chloe    5 年前

    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';
    

    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