我已经使用谷歌图表(谷歌可视化)成功实施了AreaChart,但现在我需要在谷歌AreaChart中添加字幕。这里是我试过的代码。
var options = {
width: 310,
height: 260,
chart: {
title: 'Company Performance',
subtitle: 'Sales, Expenses, and Profit: 2014-2017',
},
backgroundColor: '#000000'
};
答案 0 :(得分:2)
chart.subtitle
仅适用于材料图表
材料图表使用packages: ['bar', 'line', 'scatter']
和namespce - > google.charts
遗憾的是,没有材料版本的区域图...
chart.subtitle
不可用于核心图表
核心图表使用packages: ['corechart']
和namespce - > google.visualization
但是当'ready'
事件触发
请参阅以下工作代码段...
google.charts.load('current', {
callback: function () {
var data = new google.visualization.DataTable({
"cols": [
{"label": "Country", "type": "string"},
{"label": "Value", "type": "number"}
],
"rows": [
{"c": [{"v": "Canada"}, {"v": 33}]},
{"c": [{"v": "Mexico"}, {"v": 33}]},
{"c": [{"v": "USA"}, {"v": 34}]}
]
});
var container = document.getElementById('chart_div');
var chart = new google.visualization.AreaChart(container);
var options = {
height: 400,
legend: {
position: 'labeled'
},
sliceVisibilityThreshold: 0,
title: 'Title',
width: 600
};
google.visualization.events.addListener(chart, 'ready', function () {
Array.prototype.forEach.call(container.getElementsByTagName('text'), function(label) {
if (label.innerHTML === options.title) {
var subtitle = label.parentNode.appendChild(label.cloneNode(true));
subtitle.innerHTML = 'Subtitle';
subtitle.setAttribute('y', parseFloat(label.getAttribute('y')) + 20);
}
});
});
chart.draw(data, options);
},
packages:['corechart']
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>