javascript - Google Visualization API — percentage bars - Stack Overflow

I'm using the Google visualization API barchart. I need to be able to do a few things with the fol

I'm using the Google visualization API barchart. I need to be able to do a few things with the following code:

  1. Display Percentages along the bottom. If there is 213 total, 81 should show 38%

/

  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ["dummy","Question 1", "Question 2", " Question 3"],
        [0,81, 122, 10 ]
    ]);

    var options = {
      title: '',
      vAxis: {
        title: '',
        gridlines : {
          count : 5,
          color: 'white'
        }
      },
      hAxis: {
        title: '',
        format : '#%',
        gridlines : {
          count : 5,
          color: 'white'
        }
      },
      colors: ['#be1e2d', '#74b749', '#0daed3', '#ffb400', '#f63131'],
      legend : {
        position: 'bottom'
      }
    };

    var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
    chart.draw(data, options);

I'm using the Google visualization API barchart. I need to be able to do a few things with the following code:

  1. Display Percentages along the bottom. If there is 213 total, 81 should show 38%

http://jsfiddle/wesbos/EQ4kc/

  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = google.visualization.arrayToDataTable([
        ["dummy","Question 1", "Question 2", " Question 3"],
        [0,81, 122, 10 ]
    ]);

    var options = {
      title: '',
      vAxis: {
        title: '',
        gridlines : {
          count : 5,
          color: 'white'
        }
      },
      hAxis: {
        title: '',
        format : '#%',
        gridlines : {
          count : 5,
          color: 'white'
        }
      },
      colors: ['#be1e2d', '#74b749', '#0daed3', '#ffb400', '#f63131'],
      legend : {
        position: 'bottom'
      }
    };

    var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
    chart.draw(data, options);
Share Improve this question edited Dec 29, 2013 at 6:39 Kara 6,22616 gold badges53 silver badges58 bronze badges asked Sep 19, 2013 at 17:20 wesboswesbos 26.4k31 gold badges108 silver badges144 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

In order to do that, you have to translate your data into percents. You can use a DataView to handle this:

var view = new google.visualization.DataView(data);
view.setColumns([0, {
    type: 'number',
    label: data.getColumnLabel(1),
    calc: function (dt, row) {
        var val = dt.getValue(row, 1);
        for (var i = 1, total = 0, cols = dt.getNumberOfColumns(); i < cols; i++) {
            total += dt.getValue(row, i);
        }
        var percent = val / total;
        // what you do here depends on how you want to display the data in the tooltips

        // option 1: use the value of the data point:
        return {v: percent, f: dt.getFormattedValue(row, 1)};


        // option 2: use the percent:
        return {v: percent, f: (percent * 100).toFixed(2) + '%'};
    }
}, {
    type: 'number',
    label: data.getColumnLabel(2),
    calc: function (dt, row) {
        var val = dt.getValue(row, 2);
        for (var i = 1, total = 0, cols = dt.getNumberOfColumns(); i < cols; i++) {
            total += dt.getValue(row, i);
        }
        var percent = val / total;
        // what you do here depends on how you want to display the data in the tooltips

        // option 1: use the value of the data point:
        return {v: percent, f: dt.getFormattedValue(row, 2)};


        // option 2: use the percent:
        return {v: percent, f: (percent * 100).toFixed(2) + '%'};
    }
}, {
    type: 'number',
    label: data.getColumnLabel(3),
    calc: function (dt, row) {
        var val = dt.getValue(row, 3);
        for (var i = 1, total = 0, cols = dt.getNumberOfColumns(); i < cols; i++) {
            total += dt.getValue(row, i);
        }
        var percent = val / total;
        // what you do here depends on how you want to display the data in the tooltips

        // option 1: use the value of the data point:
        return {v: percent, f: dt.getFormattedValue(row, 3)};


        // option 2: use the percent:
        return {v: percent, f: (percent * 100).toFixed(2) + '%'};
    }
}]);

Then you draw the chart with the view instead of data:

chart.draw(view, options);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745674047a4639575.html

相关推荐

  • javascript - Google Visualization API — percentage bars - Stack Overflow

    I'm using the Google visualization API barchart. I need to be able to do a few things with the fol

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信