目前无法创建在 google 可视化中显示的图表。您可以使用DataTable Roles创建误差线,但BarChart
不支持注释(这意味着您不能在图表上显示您发布的示例中的文本)。
您可以摆弄 a ComboChart
,它可以支持注释,但是您会遇到柱状图(而不是条形图)。
这是条形图的代码:
function drawVisualization() {
// Create and populate the data table.
var data = new google.visualization.DataTable();
data.addColumn({type:'string', label:'label'});
data.addColumn({type:'number', label:'value', pattern:'#.#%'});
data.addColumn({type:'number', role:'interval', pattern:'#.#%'}); // interval role col.
data.addColumn({type:'number', role:'interval', pattern:'#.#%'}); // interval role col.
data.addColumn({type:'string', role:'annotation'}); // annotation role col. -- not enabled for bar charts
data.addColumn({type:'string', role:'annotationText'}); // annotationText col. -- not enabled for bar charts
data.addRows([
['1', 0.074, 0.055, 0.098, 'A', '7.4% (-1.9/2.4)'],
['2', 0.04, 0.027, 0.059, 'B', '4.0% (-1.3/1.9)'],
['3', 0.117, 0.093, 0.146, 'C', '11.7% (-2.4/2.9)'],
['4', 0.217, 0.185, 0.252, 'D', '21.7% (-3.2/3.5)'],
['5', 0.552, 0.511, 0.592, 'E', '55.2% (-4.1/4.0)'],
]);
// Create and draw the visualization.
new google.visualization.BarChart(document.getElementById('visualization')).
draw(data,
{title:"SubPopulation B",
width:600, height:400,
vAxis: {title: "Importance"},
hAxis: {title: "Percent", format:'#%'},
}
);
}
这是组合图版本的代码:
function drawVisualization() {
// Create and populate the data table.
var data = new google.visualization.DataTable();
data.addColumn({type:'string', label:'label'});
data.addColumn({type:'number', label:'value', pattern:'#.#%'});
data.addColumn({type:'number', label:'line', pattern:'#.#%'});
data.addColumn({type:'number', role:'interval', pattern:'#.#%'}); // interval role col.
data.addColumn({type:'number', role:'interval', pattern:'#.#%'}); // interval role col.
data.addColumn({type:'string', role:'annotation'}); // annotation role col. -- not enabled for bar charts
data.addColumn({type:'string', role:'annotationText'}); // annotationText col. -- not enabled for bar charts
data.addRows([
['1', 0.074, 0.074, 0.055, 0.098, '7.4% (-1.9/2.4)', '7.4% (-1.9/2.4)'],
['2', 0.040, 0.040, 0.027, 0.059, '4.0% (-1.3/1.9)', '4.0% (-1.3/1.9)'],
['3', 0.117, 0.117, 0.093, 0.146, '11.7% (-2.4/2.9)', '11.7% (-2.4/2.9)'],
['4', 0.217, 0.217, 0.185, 0.252, '21.7% (-3.2/3.5)', '21.7% (-3.2/3.5)'],
['5', 0.552, 0.552, 0.511, 0.592, '55.2% (-4.1/4.0)', '55.2% (-4.1/4.0)'],
]);
// Create and draw the visualization.
var ac = new google.visualization.ComboChart(document.getElementById('visualization'));
ac.draw(data, {
title : 'Subpopulation B',
width: 600,
height: 400,
vAxis: {title: "Percentage", format:'#%'},
hAxis: {title: "Importance"},
seriesType: "bars",
series: {1: {type: "line"}}
});
}
您可以使用选项隐藏线条,使其看起来更漂亮一些,但总的来说它看起来很相似(它不像您的示例那么漂亮)。
如果这些都不适合您,那么您将需要编写自定义 javascript 以BarChart
手动添加工具提示(注释)。我不知道如何(因为我不是 javascript 专家),所以如果上述解决方法还不够好,我会留给你。