每个酒吧的Chartjs图例

lymnna71  于 12个月前  发布在  Chart.js
关注(0)|答案(1)|浏览(114)

我正在尝试创建一个数据和标签都是随机的水平条形图。我还想为每个条形图添加一个图例,类似于圆环图。目标是通过选择其相应的图例文本来删除特定项目。我正在考虑创建多个数据集的选项,但我不确定这是否是最佳方法。我使用的是Chart.js/2.7.2/Chart.min.js
我希望我可以为每个酒吧创建图例,请提出一些没有多个数据集的好主意
下面的代码我试图创建基于标签和值的新数据集:

const newDatasets = labels.map((label, index) => ({
  label: label,
  data: values[index],
}));

字符串
下面是我的实际代码:

const labels = category;
const values = count;

const data = {
    labels: labels,
    datasets: [{

        data: values,
        backgroundColor: getBackgroundColor(labels, reportColor[0]),
        borderWidth: 2,
        borderRadius: 19,
        borderSkipped: false,
        barPercentage: 1,
        categoryPercentage: 0.1
    }]
    
};

const config = {
    type: 'horizontalBar',
    data,
    options: {
        plugins: {
    datalabels: {
        color: 'white',
        font: {
            weight: 'bold'
        },
    },
},  
        responsive: true,
        maintainAspectRatio: false,
        indexAxis: 'y',
        legend: { display: false },
        tooltips: {
            cornerRadius: 10
        },
        scales: {
            xAxes: [{
                barThickness: 6,
                gridLines: {
                    display: false,
                    drawBorder: false
                },
                scaleLabel: {
                    display: true,
                    labelString: yAxisLabel,
                    fontSize: 20
                },
                ticks: {
                    /*beginAtZero: true,*/
                    userCallback: function(label, index, labels) {
                        if (Math.trunc(label) === label) {
                            return label;
                        }
                    }
                },
            }],
            yAxes: [{
                barPercentage: 1.0,
                barThickness: 20,
                gridLines: {
                    display: false,
                    drawBorder: false
                },
                scaleLabel: {
                    display: true,
                    labelString: xAxisLabel,
                    fontSize: 20
                },
            }]
        },
    },
};

horizontalBarChart = new Chart(
    document.getElementById("horizontalBarChart"),
    config
);


enter image description here
Bar Image

jtoj6r0c

jtoj6r0c1#

据我所知,您希望为每个小节生成Horizontal bar chart with random value
这里我已经做了你想要的东西与价值的每一个酒吧内的酒吧。
由于您使用的Chart.js/2.7.2/Chart.min.js有时不起作用,因此建议您使用https://cdn.jsdelivr.net/npm/chart.js,对于datatabels plugin,请使用https://cdn.jsdelivr.net/npm/ [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection)

// Setup
      var data = {
        labels: ['Playing', 'Not Playing'], // This is the label of the chart's Axis
        datasets: [ // datasets consists data of each label
          {
            label: 'Yes',
            backgroundColor: 'rgba(78, 136, 250, 1)',
            data: [(Math.random()  95 + 5).toFixed(2), (Math.random()  95 + 5).toFixed(2)], // for random value
            borderRadius: 14,
            barPercentage: 0.9, // Adjust this value to change the width of bars (its value is from 0 to 1)
            
            // Below datalabels are to show the values of the bars at the top-bottom wherever we wants
            datalabels: {
              color: 'white', // color of the data labels
            }
          },
          {
            label: 'No',
            backgroundColor: 'rgba(241, 156, 23, 1)',
            data: [(Math.random()  95 + 5).toFixed(2), (Math.random()  95 + 5).toFixed(2)], // for random value
            borderRadius: 14,
            barPercentage: 0.9, // Adjust this value to change the width of bars (its value is from 0 to 1)
            
            // Below datalabels are to show the values of the bars at the top-bottom wherever we wants
            datalabels: {
              color: 'royalblue', // color of the data labels
            }
          },
        ]
      };
  
      // Configuration
      var options = {
        indexAxis: 'y',
        responsive: true,
        maintainAspectRatio: true,
        plugins: {
          legend: { // legends are the labels of the graph
            position: 'top', // using position parameter we can adjust position of the legend in the chart area (Vertical)
            align: 'end', // align the legend at the end of the chart (Horizontal)
            labels: {
              boxWidth: 15,
              boxHeight: 6,
              useBorderRadius: true,
              borderRadius: 3,
              color: '#02d0d0',
              font: { 
                size: 16,
              },
              padding: 30, // spacing between the legend lables in px
            },
            onHover: function(event, legendItem) {
              document.getElementById('myChart').style.cursor = 'pointer'; // to show the cursor on hover of the legend
            },
          },
          tooltips: {
            // enabled: true,
            mode: "index",
            intersect: false,
          },

          // To Customise the data labels
          datalabels: {
            formatter: (value) => {
              return value + '%';
            },
            font: { 
              weight: 'bold',
            }
          },              
        },

        layout: {
          padding: 20,
        },

        scales: {
          y: {
            min: 0,
            max: 100,
            ticks: {
              beginAtZero: true,
              stepSize: 25,
            },
            scaleLabel: { 
              display: false,
              labelString: 'Percentage',
            },
          },
        },
      };
  
      // Create chart
      var ctx = document.getElementById('myChart').getContext('2d'); // ctx is nothing but the small for of the word "ConTeXt" and we can use any thing as this is a name of the variable.
      
      // Config
      var myChart = new Chart(ctx, {
        type: 'bar',
        data: data, // gets the data from the var data
        options: options,
        plugins: [ChartDataLabels], // add this as it activates the datalabel plugin
      });

字符串
请参考下面的JSFiddle链接以获取演示。JSFiddle Link
希望这对你有帮助。
谢谢.

相关问题