在Chartjs中单击条形图中的每个条形后,如何启动模态?如何获取其数据?

时间:2019-01-20 09:24:44

标签: javascript chart.js

因此,我使用 chartjs 在javascript中创建图表,我遇到一个问题,即如何选择条形图中的每个条形并使用其数据启动模态。

我已经在options之后尝试了onClick事件,但是我认为我的模式错误。我对chartjs不太了解,我认为我需要帮助。这是我的代码:

  var ctx = document.getElementById("myChart2").getContext('2d');
        var myChart = new Chart(ctx, {
            type: 'horizontalBar',
            data: {
                labels: outlet,
                datasets: [{
                    label: "Players",
                    data: total_outlet,
                    backgroundColor: [
                        "#f38b4a",
                        "#56d798",
                        "#ff8397",
                        "#6970d5",
                        "#ff0000",
                        "#339966",
                        "#004d00",
                        "#003366",
                        "#e62e00",
                        "#666633",
                        "#996633"
                    ],
                    hoverBackgroundColor: [
                        "#f38b4a",
                        "#56d798",
                        "#ff8397",
                        "#6970d5",
                        "#ff0000",
                        "#339966",
                        "#004d00",
                        "#003366",
                        "#e62e00",
                        "#666633",
                        "#996633"
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    xAxes: [{
                        ticks: {
                           min: 0,
                           fontColor: "white"
                        },
                        gridLines: {
                            display:false
                        }
                    }],
                    yAxes:[{
                        ticks: {
                        fontColor: "white"
                        },
                        gridLines: {
                            display:false
                        }
                    }]
                },

                title: {
                    display: true,
                    text: 'Top 10 Outlet with Active UGL Terminals',
                    fontColor: "white"
                },
                legend: {
                    display:false,
                    labels: {
                        fontColor: "white"

                    }
                }



            }
                    });

1 个答案:

答案 0 :(得分:0)

这非常简单,documented at the Chart.js API page。以下是根据官方示例改编的有效代码段:

let canvas = document.getElementById('canvas');

let myChart = new Chart(canvas, {
  type: 'bar',
  data: {
    labels: ['a', 'b', 'c', 'd'],
    datasets: [{
      data: [0, 2, 4, 8]
    }]
  }
});

canvas.addEventListener('click', function(evt) {
  var firstPoint = myChart.getElementAtEvent(evt)[0];
  if (firstPoint) {
    var label = myChart.data.labels[firstPoint._index];
    var value = myChart.data.datasets[firstPoint._datasetIndex].data[firstPoint._index];

    alert('Label: ' + label + "\nValue: " + value);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.bundle.min.js"></script>
<canvas id="canvas"></canvas>