【问题标题】:Google pie chart: rounding issue with 2 decimal percentages谷歌饼图:带有 2 个小数百分比的四舍五入问题
【发布时间】:2016-05-25 10:04:45
【问题描述】:

我在使用 Google 图表时遇到问题。我想要一个饼图,显示带有标签图例的所有值,包括低于 0.1% 的非常小的值。

假设我有以下数据:

    var data = google.visualization.arrayToDataTable([
      ['Task', 'Hours per Day'],
      ['Work',     66],
      ['Sleep',    33.95],
      ['Eat', 0.05]
    ]);

(最多匹配 100 个)

现在的问题是,在计算百分比时,Google Charts 会将值四舍五入,导致 33.95 变为 34,因此 0.05 变为 0。

此处示例:https://jsfiddle.net/4qe7h21s/

有什么方法可以让图表引擎创建带有 2 位小数而不是 1 位小数的百分比?

【问题讨论】:

    标签: javascript charts google-visualization pie-chart


    【解决方案1】:

    因为没有选项可以更改百分比的格式或精度

    首先,需要设置
    sliceVisibilityThreshold: 0.0001

    所以'Eat' 不属于'Other'

    接下来,在数据中提供自定义格式的值
    {v: 66, f: '66.00%'}

    pieSliceTexttooltip.text 设置为'value'

    由于'Eat' 的切片太小,请使用
    tooltip.trigger: 'selection'

    看下面的例子,切片显示正确的百分比

    单击'Eat' 的图例项以查看工具提示

    google.charts.load('current', {
      callback: function () {
        new google.visualization.PieChart(document.getElementById('chart_div')).draw(
          google.visualization.arrayToDataTable([
            ['Task',  'Hours per Day'],
            ['Work',  {v: 66, f: '66.00%'}],
            ['Sleep', {v: 33.95, f: '33.95%'}],
            ['Eat',   {v: 0.05, f: '0.05%'}]
          ]),
          {
            height: 400,
            legend: {
              position: 'right'
            },
            pieSliceText: 'value',
            sliceVisibilityThreshold: 0.0001,
            title: 'My Daily Activities',
            tooltip: {
              showColorCode: true,
              text: 'value',
              trigger: 'selection'
            },
            width: 600
          }
        );
      },
      packages: ['corechart', 'table']
    });
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="chart_div"></div>

    【讨论】:

    • 像魅力一样工作!正是我需要的。所以基本上是提供预定义格式值的技巧。
    【解决方案2】:

    我仍然需要在悬停时显示工具提示,但不仅仅是在点击时,所以我找到了解决方案,使用处理事件 API:https://developers.google.com/chart/interactive/docs/events。这里有一点改进,适用于切片和图例悬停:

    function mouseOverHandler(selection) {
       chart.setSelection([selection]);
    }
    
    function mouseOutHandler() {
       chart.setSelection();
    }
    
    google.visualization.events
      .addListener(chart, 'onmouseover', mouseOverHandler);
    google.visualization.events
      .addListener(chart, 'onmouseout', mouseOutHandler);
    

    这是一个完整的工作示例(基于@WhiteHat 的 sn-p):

    google.charts.load('current', {
      callback: function () {
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(
          google.visualization.arrayToDataTable([
            ['Task',  'Hours per Day'],
            ['Work',  {v: 66, f: '66.00%'}],
            ['Sleep', {v: 33.95, f: '33.95%'}],
            ['Eat',   {v: 0.05, f: '0.05%'}]
          ]),
          {
            height: 400,
            legend: {
              position: 'right'
            },
            pieSliceText: 'value',
            sliceVisibilityThreshold: 0.0001,
            title: 'My Daily Activities',
            tooltip: {
              showColorCode: true,
              text: 'value',
              trigger: 'selection'
            },
            width: 600
          }
        );
    
        function mouseOverHandler(selection) {
           chart.setSelection([selection]);
        }
    
        function mouseOutHandler() {
           chart.setSelection();
        }
    
        google.visualization.events
          .addListener(chart, 'onmouseover', mouseOverHandler);
        google.visualization.events
          .addListener(chart, 'onmouseout', mouseOutHandler);
      },
      packages: ['corechart', 'table']
    });
    #chart_div svg g:last-child > g:last-child {
        pointer-events: none;
    }
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <div id="chart_div"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      • 2013-11-19
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多