【问题标题】:Chart.js - Doughnut show tooltips always?Chart.js - 甜甜圈总是显示工具提示?
【发布时间】:2014-10-28 22:48:33
【问题描述】:

使用 Chart.js 库时,我可以毫无问题地在我的页面上添加多个甜甜圈。

http://www.chartjs.org/docs/#doughnut-pie-chart

但我找不到始终显示工具提示的方法 - 不仅是在将鼠标悬停在甜甜圈上时。有人知道这是否可能吗?

【问题讨论】:

    标签: jquery charts


    【解决方案1】:

    我今天遇到了同样的问题,通过添加选项 onAnimationComplte 和 tooltipevents 很容易解决。

    onAnimationComplete 调用该方法来显示工具提示,就像悬停事件一样。 通常您在 tooltipevents 中定义事件以显示工具提示,但我们需要删除它们并传递一个空数组。

    注意:(http://www.chartjs.org/docs/#doughnut-pie-chart)。

    Javascript:

    var options = 
    {
        tooltipTemplate: "<%= value %>",
    
        onAnimationComplete: function()
        {
            this.showTooltip(this.segments, true);
    
            //Show tooltips in bar chart (issue: multiple datasets doesnt work http://jsfiddle.net/5gyfykka/14/)
            //this.showTooltip(this.datasets[0].bars, true);
    
            //Show tooltips in line chart (issue: multiple datasets doesnt work http://jsfiddle.net/5gyfykka/14/)
            //this.showTooltip(this.datasets[0].points, true);  
        },
    
        tooltipEvents: [],
    
        showTooltips: true
    }
    
    var context = $('#chart').get(0).getContext('2d');
    var chart = new Chart(context).Pie(data, options);  
    

    HTML:

    <div id="chartContainer">
        <canvas id="chart" width="200" height="200"></canvas>
    </div>
    

    示例数据:

    var data = [
        {
            value: 300,
            color:"#F7464A",
            highlight: "#FF5A5E"
        },
        {
            value: 50,
            color: "#46BFBD",
            highlight: "#5AD3D1"
        },
        {
            value: 100,
            color: "#FDB45C",
            highlight: "#FFC870"
        }
    ]
    

    JSFiddle 饼图: http://jsfiddle.net/5gyfykka/

    JSFiddle BAR/LINE: http://jsfiddle.net/5gyfykka/14/

    【讨论】:

    • 你从哪里得到图表对象?您是否先创建图表,然后设置选项?
    • @kentverger 是的,我更改了代码,以便您了解我是如何解决的。
    • 只是在那个例子的基础上,换掉“chart.showTooltip(chart.segments, true);”用“this.showTooltip(this.segments, true);” ...那么您无需担心命名约定 :)
    • @manish,是的,这是可能的,请检查这个小提琴:jsfiddle.net/5gyfykka/14。多个数据集还没有工作,但我会在不久的将来尝试解决这个问题。
    • @adelriosantiago 这种方法不适用于 2.x 版本。我正在研究使用 2.x 版本的 AngularChart 并且有类似的问题。
    【解决方案2】:

    我扩展了 Kapi 的方法,因此当您将鼠标悬停在它上面时,您仍然可以保留默认功能,例如颜色更改,而当您将鼠标悬停在某个部分上时,它将隐藏其余部分。我认为它看起来更好。

    var options =
    {
        onAnimationComplete: function () {
            this.showTooltip(this.segments, true);
        },        
    }
    
    var ctx = document.getElementById("Chart").getContext("2d");
    var myPieChart = new Chart(ctx).Pie(data, options);
    
    $("#Chart").on('mouseleave', function (){
        myPieChart.showTooltip(myPieChart.segments, true);
    });
    

    【讨论】:

    • 我尝试使用 Chart(ctx2).Doughnut 但在 on 函数中出现错误,因为未定义
    • 是的,这不适用于 2.1.3 版本。我们需要使用 1.0.1 版本
    • @Raghu,对那些使用 chart.js 2.3.x 的人有什么建议吗?
    • @Fernando stackoverflow.com/questions/36992922/… ,看看链接是否有帮助。
    【解决方案3】:

    如果有人试图隐藏某些片段工具提示;在 tooltipTemplate 中执行:

    tooltipTemplate : "<%  var percentage = Math.round(circumference / 6.283 * 100); if (percentage >8)%><%= percentage %>%";
    

    例如,此代码检查 %value 并仅显示高于 8% 的值以减少混乱

    【讨论】:

      【解决方案4】:

      如果您只想显示一个工具提示,则必须使用此代码。这是第一段的示例。

      chart.showTooltip([chart.segments[0]], true);
      

      函数 showTooltip 只接受二维数组作为第一个参数。

      【讨论】:

        【解决方案5】:

        您可以通过编写自己的支持 ChartJS 版本 > 2.1.5 的插件来做到这一点。

        在您的脚本中包含以下代码:

        // Show tooltips always even the stats are zero
        
        Chart.pluginService.register({
          beforeRender: function(chart) {
            if (chart.config.options.showAllTooltips) {
              // create an array of tooltips
              // we can't use the chart tooltip because there is only one tooltip per chart
              chart.pluginTooltips = [];
              chart.config.data.datasets.forEach(function(dataset, i) {
                chart.getDatasetMeta(i).data.forEach(function(sector, j) {
                  chart.pluginTooltips.push(new Chart.Tooltip({
                    _chart: chart.chart,
                    _chartInstance: chart,
                    _data: chart.data,
                    _options: chart.options.tooltips,
                    _active: [sector]
                  }, chart));
                });
              });
        
              // turn off normal tooltips
              chart.options.tooltips.enabled = false;
            }
          },
          afterDraw: function(chart, easing) {
            if (chart.config.options.showAllTooltips) {
              // we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once
              if (!chart.allTooltipsOnce) {
                if (easing !== 1)
                  return;
                chart.allTooltipsOnce = true;
              }
        
              // turn on tooltips
              chart.options.tooltips.enabled = true;
              Chart.helpers.each(chart.pluginTooltips, function(tooltip) {
                tooltip.initialize();
                tooltip.update();
                // we don't actually need this since we are not animating tooltips
                tooltip.pivot();
                tooltip.transition(easing).draw();
              });
              chart.options.tooltips.enabled = false;
            }
          }
        });
        
        // Show tooltips always even the stats are zero
        

        然后只需在要显示所有可用工具提示的任何图表的选项中使用以下行。

        showAllTooltips: true
        

        下面给出了工作小提琴

        // Show tooltips always even the stats are zero
        
        Chart.pluginService.register({
          beforeRender: function(chart) {
            if (chart.config.options.showAllTooltips) {
              // create an array of tooltips
              // we can't use the chart tooltip because there is only one tooltip per chart
              chart.pluginTooltips = [];
              chart.config.data.datasets.forEach(function(dataset, i) {
                chart.getDatasetMeta(i).data.forEach(function(sector, j) {
                  chart.pluginTooltips.push(new Chart.Tooltip({
                    _chart: chart.chart,
                    _chartInstance: chart,
                    _data: chart.data,
                    _options: chart.options.tooltips,
                    _active: [sector]
                  }, chart));
                });
              });
        
              // turn off normal tooltips
              chart.options.tooltips.enabled = false;
            }
          },
          afterDraw: function(chart, easing) {
            if (chart.config.options.showAllTooltips) {
              // we don't want the permanent tooltips to animate, so don't do anything till the animation runs atleast once
              if (!chart.allTooltipsOnce) {
                if (easing !== 1)
                  return;
                chart.allTooltipsOnce = true;
              }
        
              // turn on tooltips
              chart.options.tooltips.enabled = true;
              Chart.helpers.each(chart.pluginTooltips, function(tooltip) {
                tooltip.initialize();
                tooltip.update();
                // we don't actually need this since we are not animating tooltips
                tooltip.pivot();
                tooltip.transition(easing).draw();
              });
              chart.options.tooltips.enabled = false;
            }
          }
        });
        
        // Show tooltips always even the stats are zero
        
        
        var canvas = $('#myCanvas2').get(0).getContext('2d');
        var doughnutChart = new Chart(canvas, {
          type: 'doughnut',
          data: {
            labels: [
              "Success",
              "Failure"
            ],
            datasets: [{
              data: [45, 9],
              backgroundColor: [
                "#1ABC9C",
                "#566573"
              ],
              hoverBackgroundColor: [
                "#148F77",
                "#273746"
              ]
            }]
          },
          options: {
            // In options, just use the following line to show all the tooltips
            showAllTooltips: true
          }
        });
        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.bundle.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        
        <div>
             <canvas id="myCanvas2" width="350" height="296"></canvas>
        </div>

        Working JSFIDDLE here.

        【讨论】:

        • 你让我开心;)
        • 数据隐藏时如何隐藏标签?我点击数据集按钮,饼图隐藏,但标签仍然可见。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-13
        相关资源
        最近更新 更多