• <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 事件

        Remove slice onClick events on Pie Charts in Chart.js(删除 Chart.js 中饼图上的切片 onClick 事件)

        <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 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                  getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)
                    <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>
                            <tbody id='uUgFe'></tbody>

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

                          • <tfoot id='uUgFe'></tfoot>