2013-03-06 51 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); 
}