【问题标题】:Display item in legend even if value = 0 with Google Charts Tools Pie Chart即使 value = 0 使用 Google Charts Tools 饼图也可以在图例中显示项目
【发布时间】:2012-05-02 21:42:02
【问题描述】:

我正在使用 Google 图表工具,特别是 Pie Chart

自然,如果一个项目的值为 0,它就不会显示在饼图中(因为它占据了饼图的 0%)。但是,它也不会显示在图例中。

如何操作初始化选项以在图例中仍然显示 0 值项目,以便用户可以看到该项目存在,它只是具有 0 值?

【问题讨论】:

    标签: javascript google-visualization


    【解决方案1】:

    sliceVisibilityThreshold 设置为零将解决您的问题。

    function drawVisualization() {
      // Create and populate the data table.
      var data = google.visualization.arrayToDataTable([
        ['Task', 'Hours per Day'],
        ['Work', 11],
        ['Eat', 0],
        ['Commute', 2],
        ['Watch TV', 2],
        ['Sleep', 7]
      ]);
    
      // Create and draw the visualization.
      new google.visualization.PieChart(document.getElementById('visualization')).
          draw(data, {title:"So, how was your day?",
                     sliceVisibilityThreshold:0
                     });
    }
    ​
    

    【讨论】:

    • 正是我想要的——只是无法理解 API 中的描述!谢谢!
    • 设置 sliceVisibilityThreshold=0 如果至少一个图表数据字段的数字非零,但如果所有字段都为零,则没有显示图例。在这种情况下我们如何显示图例?
    • @ocanal - 对于 GoogleVisualr::Image::PieChar,我们如何隐藏值为 0 的图例?
    • @ocanal 感谢您的帮助
    • 我已尝试应用此方法,但我的饼图中的图例仍仅限于大于零的值。您有任何故障排除提示吗?我可以更改标题等其他选项,当我记录 sliceVisibilityThreshold 的值时显示它为零 (0)...
    【解决方案2】:

    我最近添加了 Google 图表,但在其中添加零值时遇到了问题。
    感谢@ocanal,我使用了 sliceVisibilityThreshold:0,但以其他方式。

    <script type="text/javascript">
            google.charts.load('current', {'packages':['corechart']});
          google.charts.setOnLoadCallback(drawChart);
    
          function drawChart() {
    
            var data = google.visualization.arrayToDataTable([
              ['Task', 'Hours per Day'],
              ['B-TRIPS',     <?php echo $arr_get_a_org['total_trips']; ?>],
              ['Reimbursed',      <?php echo $arr_get_a_org['reimbursed_trips']; ?>],
              ['Approved',  <?php echo $arr_get_a_org['approved_trips']; ?>],
              ['Pending', <?php echo $arr_get_a_org['pending_trips']; ?>]
             // ['Sleep',    <?php echo $arr_get_a_org['total_trips']; ?>]
            ]);
    
            var options = {
              title: 'OVERVIEW',
              backgroundColor:'#e2e1e0',
              pieSliceText:'value',
              sliceVisibilityThreshold :0
    
            };
    
            var chart = new google.visualization.PieChart(document.getElementById('piechart'));
    
            chart.draw(data, options);
          }
    </script>
    

    随着选项定义方式的改变,更多信息请查看Google Chart site

    【讨论】:

      【解决方案3】:

      我所做的草图很多,但产生了我正在寻找的结果。我正在使用 Google Apps 脚本编辑器本身并使用 excel 公式来强制显示 0%。

      =IF(&lt;formula&gt; = 0%, 1E-20%, &lt;formula&gt;)

      基本上,在 excel 列值中,如果值为 0(或在我的情况下为 0%),我会将该单元格替换为 1-E20%。它模拟了一个实际值(因此显示在图例中),但其他值仍将加起来为 100%,因为该值非常小,不会影响饼图。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-13
        • 1970-01-01
        • 1970-01-01
        • 2019-08-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多