简体   繁体   English

在运行时将Google Chart容器更改为另一个

[英]Change Google Chart container to another during runtime

Like in this answer: 像这样的答案:

how to clone and re draw google chart in another div? 如何在另一个div中克隆并重新绘制Google图表?

However, I don't want to clone the chart itself, but move him to another div in runtime. 但是,我不想克隆图表本身,而是在运行时将其移至另一个div。

here are some options... 这里有一些选择...

1) if using the ChartWrapper class, 1)如果使用ChartWrapper类,
simply use setContainerId to change the div id 只需使用setContainerId更改div ID
then redraw the chart 然后重绘图表

see following working snippet... 请参阅以下工作片段...

 google.charts.load('current', { callback: drawChart, packages: ['controls', 'corechart'] }); function drawChart() { var data = new google.visualization.DataTable(); data.addColumn('number', 'x'); data.addColumn('number', 'y'); data.addRows([ [0, 0], [3, 3], [6, 6] ]); var chartWrapper = new google.visualization.ChartWrapper({ chartType: 'LineChart', containerId: 'chart-left', dataTable: data, options: { backgroundColor: 'transparent', theme: 'maximized' } }); chartWrapper.draw(); document.getElementById('switch').addEventListener('click', function () { var containerId = (chartWrapper.getContainerId() === 'chart-left') ? 'chart-right' : 'chart-left'; chartWrapper.setContainerId(containerId); chartWrapper.draw(); }, false); } 
 .chart { display: inline-block; height: 160px; width: 160px; vertical-align: top; } .chart:first-child { background: cyan; } .chart:last-child { background: yellow; } 
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div> <input id="switch" type="button" value="Switch"/> </div> <div> <div class="chart" id="chart-left"></div> <div class="chart" id="chart-right"></div> </div> 


2) if using a standard chart class, such as LineChart , 2)如果使用标准图表类,例如LineChart
you will need to re-create the chart using the new container, 您将需要使用新容器重新创建图表,
then draw 然后画

you will have to clear the previous chart 您将必须清除先前的图表
you can use chart method --> clearChart 您可以使用图表方法-> clearChart
or simply clear the div --> div.innerHTML = '' 或者只是清除div.innerHTML = '' > div.innerHTML = ''

see following working snippet... 请参阅以下工作片段...

 google.charts.load('current', { callback: drawChart, packages: ['controls', 'corechart'] }); function drawChart() { var data = new google.visualization.DataTable(); data.addColumn('number', 'x'); data.addColumn('number', 'y'); data.addRows([ [0, 0], [3, 3], [6, 6] ]); var container = ''; document.getElementById('switch').addEventListener('click', drawChart, false); drawChart(); function drawChart() { if (container !== '') { document.getElementById(container).innerHTML = ''; } container = (container === 'chart-left') ? 'chart-right' : 'chart-left'; var chart = new google.visualization.ComboChart( document.getElementById(container) ); chart.draw(data, { backgroundColor: 'transparent', theme: 'maximized' }); } } 
 .chart { display: inline-block; height: 160px; width: 160px; vertical-align: top; } .chart:first-child { background: cyan; } .chart:last-child { background: yellow; } 
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div> <input id="switch" type="button" value="Switch"/> </div> <div> <div class="chart" id="chart-left"></div> <div class="chart" id="chart-right"></div> </div> 


3) you could also use static method --> google.visualization.drawChart 3)您也可以使用静态方法-> google.visualization.drawChart

see following working snippet... 请参阅以下工作片段...

 google.charts.load('current', { callback: drawChart, packages: ['controls', 'corechart'] }); function drawChart() { var data = new google.visualization.DataTable(); data.addColumn('number', 'x'); data.addColumn('number', 'y'); data.addRows([ [0, 0], [3, 3], [6, 6] ]); var container = ''; document.getElementById('switch').addEventListener('click', drawChart, false); drawChart(); function drawChart() { if (container !== '') { document.getElementById(container).innerHTML = ''; } container = (container === 'chart-left') ? 'chart-right' : 'chart-left'; google.visualization.drawChart({ containerId: container, dataTable: data, chartType: 'LineChart', options: { backgroundColor: 'transparent', theme: 'maximized' } }); } } 
 .chart { display: inline-block; height: 160px; width: 160px; vertical-align: top; } .chart:first-child { background: cyan; } .chart:last-child { background: yellow; } 
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div> <input id="switch" type="button" value="Switch"/> </div> <div> <div class="chart" id="chart-left"></div> <div class="chart" id="chart-right"></div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM