Google Charts:如何获取饼图中每个切片的百分比值?

时间:2014-09-23 15:55:47

标签: javascript charts google-visualization

我想得到每个切片的百分比值,此刻我能够获得所选标签的名称,但我想得到每个标签的百分比值。我找不到这样做的方法,这可能吗?

http://jsfiddle.net/oaobxych/4/

function selectHandler() {
          var selectedItem = chart.getSelection()[0];
          if (selectedItem) {
            var version = data.getValue(selectedItem.row, 0);
            alert('The user selected ' + version);
          }
        }

1 个答案:

答案 0 :(得分:2)

可能你必须计算它。

    var total = 0;
    for (var f = 0; f < data.getNumberOfRows(); f++){
        total += data.getValue(f, 1);
    }
      var selectedItem = chart.getSelection()[0];
      if (selectedItem) {
        var percentage = (data.getValue(selectedItem.row,1)*100/total).toFixed(1);
        var version = data.getValue(selectedItem.row, 0);
        alert('The user selected ' + version + ' a ' + percentage + '% of total');
      }
    }

http://jsfiddle.net/oaobxych/7/