我正在尝试更改Dygraph GVizChart列的可见性。
这有效:
function drawChart() {
data = getData();
window.chart1 = new Dygraph.GVizChart(
document.getElementById('dygraphs')).draw(data, {
});
}
这也有效:
function drawChart() {
data = getData();
window.chart1 = new Dygraph.GVizChart(
document.getElementById('dygraphs')).draw(data, {
visibility: [false, true, true, true]
});
}
但在drawChart
内,在该代码之后,当我添加以下行时,
function drawChart() {
data = getData();
window.chart1 = new Dygraph.GVizChart(
document.getElementById('dygraphs')).draw(data, {
});
window.chart1.setVisibility(0, true);
window.chart1.setVisibility(1, false);
}
我收到错误:Uncaught TypeError: Cannot call method 'setVisibility' of undefined. drawChart
在阅读this question之后,我想在执行时可能chart1
尚未就绪。所以我添加了这个功能:
function showChange() {
alert('show chart1:' + window.chart1);
window.chart1.setVisibility(3, false);
}
<a href="#" onclick='showChange();return false;'>showChange</a>
但是当我点击showChange
链接时,我收到同样的错误:Uncaught TypeError: Cannot call method 'setVisibility' of undefined
警报窗口显示show chart1: undefined
答案 0 :(得分:1)
new Dygraph.GVizChart()
返回一个对象。 .draw()
没有。
你想要更像的东西:
window.chart1 = new Dygraph.GVizChart(document.getElementById('dygraphs'));
window.chart1.draw(data, {});
你也会遇到问题,因为dygraphs GViz包装器没有setVisibility()
方法。您需要在底层的Dygraph对象中获取代码才能工作:
function showChange() {
alert('show chart1:' + window.chart1);
window.chart1.date_graph.setVisibility(3, false);
}