设法解决了http://jsfiddle.net/vivekbhusal/3n78tc8p/
@juvian 给出的解决方案也很好
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['subscribers', 'administrators', 'Authors','Publishers'],
[120, 2, 0, 5],
]);
var options = {
isStacked: true,
title: '',
vAxis: {
textPosition : 'none',
title: '',
gridlines : {
count : 1,
color: 'white'
}
},
hAxis: {
title: '',
format : '#%',
'gridlines':{
'color':'transparent',
},
'baselineColor':'transparent',
'textStyle':{
'fontSize':'11',
}
},
colors: ['#be1e2d', '#74b749', '#0daed3', '#ffb400', '#f63131'],
legend : {
position: 'bottom'
}
};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
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;
return {v: percent, f: dt.getFormattedValue(row, 2)};
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;
return {v: percent, f: dt.getFormattedValue(row, 3)};
return {v: percent, f: (percent * 100).toFixed(2) + '%'};
}
}]);
chart.draw(view, options);
}