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

    1. <tfoot id='mqpOe'></tfoot>

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

      • <bdo id='mqpOe'></bdo><ul id='mqpOe'></ul>

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

        使用 ChartJS 单击更改点颜色

        Change point color on click using ChartJS(使用 ChartJS 单击更改点颜色)

          • <legend id='G4C95'><style id='G4C95'><dir id='G4C95'><q id='G4C95'></q></dir></style></legend>
              <tbody id='G4C95'></tbody>

                <tfoot id='G4C95'></tfoot>
                  <bdo id='G4C95'></bdo><ul id='G4C95'></ul>

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

                  <i id='G4C95'><tr id='G4C95'><dt id='G4C95'><q id='G4C95'><span id='G4C95'><b id='G4C95'><form id='G4C95'><ins id='G4C95'></ins><ul id='G4C95'></ul><sub id='G4C95'></sub></form><legend id='G4C95'></legend><bdo id='G4C95'><pre id='G4C95'><center id='G4C95'></center></pre></bdo></b><th id='G4C95'></th></span></q></dt></tr></i><div id='G4C95'><tfoot id='G4C95'></tfoot><dl id='G4C95'><fieldset id='G4C95'></fieldset></dl></div>
                • 本文介绍了使用 ChartJS 单击更改点颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  目前,当您单击单个点(折线图上)时,我可以更改它的颜色,但它会立即变回以前的颜色,我该如何防止这种情况发生?

                  Currently I'm able to change the color of an individual point (on a line chart) when you click on it but it changes back immediately to previous color, how can I prevent this?

                  这是我的功能:

                  var options = {
                    onClick: function(e){
                      var element = this.getElementAtEvent(e);
                      if (element.length > 0) {
                        element[0]._view.backgroundColor = '#FFF';
                        this.update();
                      }
                  }
                  

                  我在这里发现了同样的问题 https://github.com/chartjs/Chart.js/issues/2989 显然这家伙能够管理它,但我认为该代码不再兼容.

                  I found this same issue here https://github.com/chartjs/Chart.js/issues/2989 and apparently the guy was able to manage it but I think that code is no longer compatible.

                  我正在使用 ChartJS v2.5.0.

                  I'm using ChartJS v2.5.0.

                  推荐答案

                  以下方法利用:

                  • pointBackgroundColor 数据集属性,一个保存当前点颜色的数组.单击某个点时,关联的数组值将更改为 white 并更新图表.
                  • onClick 图表选项,一个如果事件是'mouseup' 或'click' 类型时调用的函数".它在图表的上下文中调用并传递事件和一组活动元素."
                  • the pointBackgroundColor dataset property, an array which will hold the current colors of points. When a point is clicked, the associated array value will be changed to white and the chart will be updated.
                  • the onClick chart option, a function that is "called if the event is of type 'mouseup' or 'click'." It is "called in the context of the chart and passed the event and an array of active elements."

                  更多信息请参见 docs.

                  长篇大论代码:

                  var ctx = document.getElementById("myChart");
                  var myChart = new Chart(ctx, {
                    type: 'line',
                    data: {
                      labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
                      datasets: [{
                        label: '# of Votes',
                        data: [72, 49, 43, 49, 35, 82],
                        pointBackgroundColor: ["red", "blue", "yellow", "green", "purple", "orange"]
                      }]
                    },
                    options: {
                      onClick: function(evt, activeElements) {
                        var elementIndex = activeElements[0]._index;
                        this.data.datasets[0].pointBackgroundColor[elementIndex] = 'white';
                        this.update();
                      }
                    }
                  });

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
                  <canvas id="myChart" width="400" height="400"></canvas>

                  还有一个可以玩的小提琴.

                  这篇关于使用 ChartJS 单击更改点颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  在开发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 在一年的第一天返回前一年)

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

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