• <legend id='95dI7'><style id='95dI7'><dir id='95dI7'><q id='95dI7'></q></dir></style></legend>
  • <i id='95dI7'><tr id='95dI7'><dt id='95dI7'><q id='95dI7'><span id='95dI7'><b id='95dI7'><form id='95dI7'><ins id='95dI7'></ins><ul id='95dI7'></ul><sub id='95dI7'></sub></form><legend id='95dI7'></legend><bdo id='95dI7'><pre id='95dI7'><center id='95dI7'></center></pre></bdo></b><th id='95dI7'></th></span></q></dt></tr></i><div id='95dI7'><tfoot id='95dI7'></tfoot><dl id='95dI7'><fieldset id='95dI7'></fieldset></dl></div>

      1. <small id='95dI7'></small><noframes id='95dI7'>

        <tfoot id='95dI7'></tfoot>
          <bdo id='95dI7'></bdo><ul id='95dI7'></ul>
      2. 删除 Chart.js 中饼图上的切片 onClick 事件

        时间:2023-11-01

        <small id='uiaOH'></small><noframes id='uiaOH'>

      3. <legend id='uiaOH'><style id='uiaOH'><dir id='uiaOH'><q id='uiaOH'></q></dir></style></legend>
        • <bdo id='uiaOH'></bdo><ul id='uiaOH'></ul>

          • <tfoot id='uiaOH'></tfoot>
              <tbody id='uiaOH'></tbody>
            <i id='uiaOH'><tr id='uiaOH'><dt id='uiaOH'><q id='uiaOH'><span id='uiaOH'><b id='uiaOH'><form id='uiaOH'><ins id='uiaOH'></ins><ul id='uiaOH'></ul><sub id='uiaOH'></sub></form><legend id='uiaOH'></legend><bdo id='uiaOH'><pre id='uiaOH'><center id='uiaOH'></center></pre></bdo></b><th id='uiaOH'></th></span></q></dt></tr></i><div id='uiaOH'><tfoot id='uiaOH'></tfoot><dl id='uiaOH'><fieldset id='uiaOH'></fieldset></dl></div>
                • 本文介绍了删除 Chart.js 中饼图上的切片 onClick 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对 Chart.js 上的饼图有疑问.

                  I have a question regarding piecharts on Chart.js.

                  单击切片时删除切片的最佳方法是什么?我知道方法 getSegmentsAtEvent() 可用于读取切片属性.可以找出我正在单击的切片循环遍历切片对象,直到找到匹配项.有没有更简单的方法来实现它?

                  What would be the best way to remove a slice when clicking on it? I'm aware the method getSegmentsAtEvent() can be used to read the slice properties. It's possible to find out which slicing I'm clicking looping through the slices object until a match is found. Is there a simpler way to achieve it?

                  tks

                  推荐答案

                  这可以通过以下函数实现:getSegmentsAtEvent(event)removeData( index ) Chart.js API

                  This can be achieved using the functions: getSegmentsAtEvent(event) and removeData( index ) Chart.js API

                  使用 getSegmentsAtEvent 可以恢复被点击的段.

                  With getSegmentsAtEvent you can recover the segment that has been clicked.

                  下一步,是在图表中找到切片的索引.要进行搜索,您可以遍历图表的所有当前段并在找到时调用 removeData.(我觉得没有办法直接知道索引)

                  The next step, is to find the index of the slice in the chart. To do the search, you can iterate through all the current segments of the chart and call removeData when it's found. (I think there is no way to directly know the index)

                  var segments = myChart.segments;
                  for (var index = 0; index < segments.length; index++) {
                      if (activeLabel == segments[index].label) {
                          myChart.removeData(index);
                      }
                  }
                  

                  完整演示:

                  <html>
                      <head>
                          <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.2.js"></script>
                          <script type="text/javascript" src="Chart.js"></script>
                          <script type="text/javascript">
                              var data = [
                                  {
                                      value: 300,
                                      color:"#F7464A",
                                      highlight: "#FF5A5E",
                                      label: "Red"
                                  },
                                  {
                                      value: 50,
                                      color: "#46BFBD",
                                      highlight: "#5AD3D1",
                                      label: "Green"
                                  },
                                  {
                                      value: 100,
                                      color: "#FDB45C",
                                      highlight: "#FFC870",
                                      label: "Yellow"
                                  }
                              ];
                  
                              $(document).ready( 
                                  function () {
                                      var ctx = document.getElementById("myChart").getContext("2d");
                                      var myChart = new Chart(ctx).Pie(data);
                  
                                      $("#myChart").click( 
                                          function(evt){
                                              var activePoints = myChart.getSegmentsAtEvent(evt);
                                              var activeLabel = activePoints[0].label;
                                              var segments = myChart.segments;
                                              for (var index = 0; index < segments.length; index++) {
                                                  if (activeLabel == segments[index].label) {
                                                      myChart.removeData(index);
                                                  }
                                              }
                                          }
                                      );
                                  }
                              );
                          </script>
                      </head>
                      <body>
                          <canvas id="myChart" width="400" height="400"></canvas>
                      </body>
                  </html>
                  

                  这篇关于删除 Chart.js 中饼图上的切片 onClick 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:自动删除 Chart.js 中的旧数据点 下一篇:Chart.js - 如何将数组集合推送到数据集中

                  相关文章

                  <legend id='uUgFe'><style id='uUgFe'><dir id='uUgFe'><q id='uUgFe'></q></dir></style></legend>

                    1. <i id='uUgFe'><tr id='uUgFe'><dt id='uUgFe'><q id='uUgFe'><span id='uUgFe'><b id='uUgFe'><form id='uUgFe'><ins id='uUgFe'></ins><ul id='uUgFe'></ul><sub id='uUgFe'></sub></form><legend id='uUgFe'></legend><bdo id='uUgFe'><pre id='uUgFe'><center id='uUgFe'></center></pre></bdo></b><th id='uUgFe'></th></span></q></dt></tr></i><div id='uUgFe'><tfoot id='uUgFe'></tfoot><dl id='uUgFe'><fieldset id='uUgFe'></fieldset></dl></div>
                        <bdo id='uUgFe'></bdo><ul id='uUgFe'></ul>
                    2. <small id='uUgFe'></small><noframes id='uUgFe'>

                    3. <tfoot id='uUgFe'></tfoot>