【问题标题】:How to display Tooltip without hovering pie chart with Chart.JS如何在不使用 Chart.JS 悬停饼图的情况下显示工具提示
【发布时间】:2018-11-21 09:47:02
【问题描述】:

我将 AdminLTE 和 chart.js 用于饼图。问题是,我可以在不悬停鼠标的情况下使饼图中每个弧线的文本可见吗? 我不使用图例,因为有些图表中有很多标签。

如果您有任何其他方式来显示所有文本标签,我将不胜感激。

这是我所有饼图的当前脚本

<script>    
                $(function () {
                    //-------------
                    //- PIE CHART -
                    //-------------
                    // Get context with jQuery - using jQuery's .get() method.
                    var pieChartCanvas = $('#pieChart').get(0).getContext('2d')
                    var pieChart       = new Chart(pieChartCanvas)
                    var PieData        = [<?php echo $isiData; ?>]

                    var pieOptions     = {                      
                    //Boolean - Whether we should show a stroke on each segment
                    segmentShowStroke    : true,
                    //String - The colour of each segment stroke
                    segmentStrokeColor   : '#fff',
                    //Number - The width of each segment stroke
                    segmentStrokeWidth   : 2,
                    //Number - The percentage of the chart that we cut out of the middle
                    percentageInnerCutout: 0, // This is 0 for Pie charts
                    //Number - Amount of animation steps
                    animationSteps       : 150,
                    //String - Animation easing effect
                    animationEasing      : 'easeOutBack',
                    //Boolean - Whether we animate the rotation of the Doughnut
                    animateRotate        : true,
                    //Boolean - Whether we animate scaling the Doughnut from the centre
                    animateScale         : false,
                    //Boolean - whether to make the chart responsive to window resizing
                    responsive           : true,
                    // Boolean - whether to maintain the starting aspect ratio or not when responsive, if set to false, will take up entire container
                    maintainAspectRatio  : true,

                    //String - A legend template
                    legendTemplate       : '<ul class="<%=name.toLowerCase()%>-legend"><% for (var i=0; i<segments.length; i++){%><li><span style="background-color:<%=segments[i].fillColor%>"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>'
                    }

                    //Create pie or douhnut chart
                    // You can switch between pie and douhnut using the method below.
                    pieChart.Doughnut(PieData, pieOptions)
                })  
                </script>
<canvas id="pieChart" style="height:400px;"></canvas>

【问题讨论】:

    标签: javascript php chart.js adminlte


    【解决方案1】:

    我在谷歌上玩得很开心。

    基本上其他开发人员解决您的问题的方式是创建一个插件,使所有工具提示在渲染后显示

    我找到了一个解决这个问题的小提琴..

    小提琴不是我的..

    感谢 Suhaib Janjua

    // 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>

    【讨论】:

    • 这很棒,但是 chart.bundle.min.js 无法工作,因为 AdminLTE 正在使用 chart.min.js,我尝试同时使用它们但不工作,如果我只使用chart.bundle.min 另一个图表消失了。我使用了 AdminLTE 示例图表中的确切示例,我如何解决这个问题
    • @GisellAnnisa 这是否意味着您无法重构代码以使用 chart.bundle.min.js,因为您基于 AdminLTE 文档?...我确实认为这会是一个问题如果您还可以尝试了解管理员 LTE 是如何尝试显示图表的。只是为了以后您在 ChartJs 或 AdminLTE 更新时不会感到困惑。在限制你的事情上,我真的无法帮助你。 AdminLTE 只是像其他人一样使用 chartJS。也许您可以尝试了解它的工作原理并尝试对其进行更改以满足您的需求。 (我还是会想办法帮你)
    【解决方案2】:

    我使用 onclick 事件和引导模式来解决这个问题并禁用工具提示。

            ,onClick: function(c,i) {
                e = i[0];
                var x_value = this.data.labels[e._index];
                var ID = x_value;
                var Type =1;
    
    
                    $.ajax({
                                url: 'getsearchresults.asmx/ChartDetayGetir',
                                data: "{ 'ID': '" + ID + "',type:'"+Type+"'}",
                                dataType: "json",
                                type: "POST",
                                contentType: "application/json; charset=utf-8",
                                success: function (data) {
    
                                document.getElementById("modalheader").innerHTML = x_value;
    
                                document.getElementById("modalbody").innerHTML = data.d;
    
                                $('#myModal').modal();
    
                                        },
                                error: function (response) {
                                    alert(response.responseText);
                                },
                                failure: function (response) {
                                    alert('Failure');
                                }
                            });
    
    
    
    
    
    
            }
    

    【讨论】:

    • 他们更像是,“如果用户不想点击那些饼图怎么办,他们现在怎么知道橙色意味着什么?”所以也许他们希望图表总是显示工具提示。我在charts.js 中搜索,但找不到如何使工具提示始终可见。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多