简体   繁体   中英

Google Visualization API: Column Chart %

How do I get data displayed as a percentage on hover? I can get the gridlines to show percentages, but the hover always shows unformatted data: 1 vs 100% .

图表

var data = google.visualization.arrayToDataTable([
        ['Who', 'Percentage'],
        ['Unlike Me', 0],
        ['Like Me', 1]
    ]);
var options = {
      vAxis: { format:'#%'}, // Makes gridlines have percentages (correct)
      format: '#%' // Does nothing
};

var div = $('#graph').get([0]);
var chart = new google.visualization.ColumnChart(div);
chart.draw(data, options);

Figured it out: you have to use tooltip "data roles" :

data.addColumn('string', 'Who'); // Implicit domain column.
data.addColumn('number', 'Percentage'); // Implicit data column.
data.addColumn({type:'string', role:'tooltip'}); // Tooltip with percentages 
data.addRows([
    ['Like Me', 0, 0 * 100 + '%'],
    ['Unlike Me', 1, 1 * 100 + '%']
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM