2013-03-06 50 views
0

我试图改变Dygraph GVizChart列的可见性。Dygraphs setVisibility of column

这工作:

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

回答

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); 
}