ChartJs如何从多个日期集中获取单击数据集栏的方式 [英] ChartJs how to get from mulitiple dateset which dataset bar is clicked

查看:57
本文介绍了ChartJs如何从多个日期集中获取单击数据集栏的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为每个栏添加一个唯一的URL,然后单击其中一个栏打开该URL。但就我而言,我有两个数据集。我正在使用onClick事件,该事件返回两个数据集的活动记录。

I want add a unique url to each bar and open that url on click one of bar. But in my case i have two datasets. I am using onClick event which return active record of both dataset.

我正在使用以下代码。它将返回两个数据集对象...应返回只有一个数据集。

I am using following code.. It will return both dataset object...it should return only one dataset.

<script>
        var barChartData = {
            labels: ["Jan","Feb","March"],
            datasets: [{
                label : "Quoted",
                backgroundColor : "#FF7228",
                data : [50,20,70],
            },
            {
                label : "Accepted",
                backgroundColor : "#FFCC8C",
                data :  [30,10,20],
            }, 
            ]

        };

        window.onload = function() {
            var ctx = document.getElementById("canvas").getContext("2d");
            window.myBar = new Chart(ctx, {
                type: 'bar',
                data: barChartData,
                options: {
                    events:['click'],
                    onClick:function(c,i){
                            console.log(this.active);
                            }
                        },
                    responsive: true,
                    legend: {
                        position: 'top',
                    },
                }
            });
        };
 </script>


推荐答案

您可以使用> getElementAtEvent() 方法,仅返回一个数据集。然后,您可以应用进一步的逻辑来检测单击了哪个条。

You can use getElementAtEvent() method to get only one dataset returned. Then you can apply further logic to detect which bar is clicked on.

ᴅᴇᴍᴏ

var barChartData = {
   labels: ["Jan", "Feb", "March"],
   datasets: [{
      label: "Quoted",
      backgroundColor: "#FF7228",
      data: [50, 20, 70],
   }, {
      label: "Accepted",
      backgroundColor: "#FFCC8C",
      data: [30, 10, 20],
   }]
};

var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx, {
   type: 'bar',
   data: barChartData,
   options: {
      onClick: function(e) {
         var bar = this.getElementAtEvent(e)[0];
         var index = bar._index;
         var datasetIndex = bar._datasetIndex;

         // check which bar is clicked
         if (index == 0 && datasetIndex == 0) alert('First BAR Clicked!');
         else if (index == 0 && datasetIndex == 1) alert('Second BAR Clicked!');
         else if (index == 1 && datasetIndex == 0) alert('Third BAR Clicked!');
         else if (index == 1 && datasetIndex == 1) alert('Fourth BAR Clicked!');
         else if (index == 2 && datasetIndex == 0) alert('Fifth BAR Clicked!');
         else if (index == 2 && datasetIndex == 1) alert('Sixth BAR Clicked!');
      }
   },
   responsive: true,
   legend: {
      position: 'top',
   },
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="canvas"></canvas>

这篇关于ChartJs如何从多个日期集中获取单击数据集栏的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆