我正在使用谷歌圖表(特別是折線圖)。圖表正確顯示。現在我正在嘗試添加交互,當系列的標籤被點擊時,系列會消失並出現。谷歌折線圖說未捕獲TypeError:不能調用方法'getSelection'null
我的問題是,該方法getSelection()拋出錯誤
Cannot call method 'getSelection' of null
這是我的代碼部分:
var jsonData = $.ajax({
url: 'http://localhost:9580/LocalApp/api/Data?Number=16846851',
type: 'GET',
dataType: 'json',
success: function (values) {
// now we need to build the map data, loop over each result
// create columns array
var columns = [{ id: 'datetime', label: 'Date and Time', type: 'datetime' },
{ id: 'x', label: 'X', type: 'number' },
{ id: 'y', label: 'Y', type: 'number' },
{ id: 'z', label: 'Z', type: 'number' },
{ id: 'total', label: 'Total', type: 'number' }];
var chartData = new google.visualization.DataTable(
{
cols: columns
});
// Add empty rows
chartData.addRows(values.ac.length);
$.each(values.ac, function (indexInArray, ac) {
chartData.setCell(indexInArray, 0, new Date(acc.dateTime));
chartData.setCell(indexInArray, 1, ac.x);
chartData.setCell(indexInArray, 2, ac.y);
chartData.setCell(indexInArray, 3, ac.z);
chartData.setCell(indexInArray, 4, ac.total);
});
var options = {
title: 'Data',
pointSize:4,
legend: { position: 'bottom' },
series: {
0: { color: 'blue', lineWidth: 2 },
1: { color: 'red', lineWidth: 2 },
2: { color: 'green', lineWidth: 2 },
3: { color: 'yellow', lineWidth: 2 }
}
};
function showHideSeries(newChart) {
var sel = newChart.getSelection(); **--> error**
// if selection length is 0, we deselected an element
if (sel.length > 0) {
// if row is undefined, we clicked on the legend
if (sel[0].row == null) {
var col = sel[0].column;
if (columns[col] == col) {
// hide the data series
columns[col] = {
label: data.getColumnLabel(col),
type: data.getColumnType(col),
calc: function() {
return null;
}
};
// grey out the legend entry
series[col - 1].color = '#CCCCCC';
}
else {
// show the data series
columns[col] = col;
series[col - 1].color = null;
}
var view = new google.visualization.DataView(chartData);
view.setColumns(columns);
newChart.draw(view, options);
}
}
}
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
google.visualization.events.addListener(chart, 'select', showHideSeries(chart));
chart.draw(chartData, options);
任何想法,爲什麼我收到錯誤? 謝謝!吉列爾莫。
'google.visualization.events.addListener(圖表,「選擇」,showHideSeries(圖));'線的外觀不正確。 '(圖表)'不應該在那裏,因爲'showHideSeries'不會返回一個函數來執行事件。 –