我正在嘗試向Google燭臺圖表添加一些註釋。我注意到有人已經問過同樣的問題(Adding annotations to Google Candlestick chart)。用戶Aperçu回答了一個詳細的解決方案,以擴展圖表並添加註釋,因爲圖表沒有內置任何此類功能。但是,當我嘗試此解決方案時,出現「TypeError:document.querySelectorAll(...) [0]是未定義」向Google燭臺圖表添加註釋(發佈解決方案觸發器TypeError)
這是我的代碼:
chartPoints = [
['Budget', 0, 0, 9999, 9999, 'foo1'],
['Sales', 0, 0, 123, 123, 'foo2'],
['Backlog', 123, 123, 456, 456, 'foo3'],
['Hard Forecast', 456, 456, 789, 789, 'foo4'],
['Sales to Budget', 789, 789, 1000, 1000, 'foo5']
];
var data = google.visualization.arrayToDataTable(chartPoints, true);
data.setColumnProperty(5, 'role', 'annotation');
var options = {
legend: 'none',
bar: { groupWidth: '40%', width: '100%' },
candlestick: {
fallingColor: { strokeWidth: 0, fill: '#a52714' },
risingColor: { strokeWidth: 0, fill: '#0f9d58' }
}
};
var chart = new google.visualization.CandlestickChart(document.getElementById('chart_div'));
chart.draw(data, options);
// attempt to use Aperçu's solution
const bars = document.querySelectorAll('#chart_div svg > g:nth-child(5) > g')[0].lastChild.children // this triggers a TypeError
for (var i = 0 ; i < bars.length ; i++) {
const bar = bars[i]
const { top, left, width } = bar.getBoundingClientRect()
const hint = document.createElement('div')
hint.style.top = top + 'px'
hint.style.left = left + width + 5 + 'px'
hint.classList.add('hint')
hint.innerText = rawData.filter(t => t[1])[i][0]
document.getElementById('chart_div').append(hint)
}
我希望圖表的最後一塊數據的顯示欄(即,旁邊的‘foo1’,‘foo2的’,等等)
檢查[這個答案](https://stackoverflow.com/a/41857399/5090771)... – WhiteHat
嗨,這是一個非常長的答案,我不確定我需要改變它以使其與我的代碼一起工作。我嘗試在底部粘貼(從var waterFallChart = new google.visualization.ChartWrapper({到最後減去jquery的東西),但是我得到錯誤ReferenceError:dataChart沒有被定義,只是需要很多東西你可以向我解釋一下,我可以重構這個,使它與上面的代碼一起工作嗎? – ag415
我試圖將dataChart更改爲chartPoints,但現在我得到錯誤:未知的頭文件類型:0 – ag415