Me gustaría actualizar/modificar las opciones de un gráfico de Google existente. Digamos que quiero aplicar nuevas opciones a un gráfico existente al hacer clic en un botón.
Cuando se hace clic en el botón "Modificar gráfico", las opciones del gráfico se actualizan (por ejemplo, se establecen nuevos colores).
Lo extraño es que el tipo de gráfico se cambia de un gráfico combinado a un gráfico de líneas. ¿Por qué está pasando esto? Incluso si paso chartType: 'ComboChart' en las opciones, el tipo de gráfico aún cambia.
Ayuda muy apreciada.
function nuChart(d, t, a, h, x, y, st, is) { let obj = document.getElementById(d); if (obj == null) return; a = eval(a); if (a.length === 0) { return; } try { google.charts.load('current', { 'packages': ['corechart'] }); } catch (error) { return; } google.charts.setOnLoadCallback(drawVisualization); if (a == '') { return; } function drawVisualization() { if (a === undefined) { return; } let data = google.visualization.arrayToDataTable(a); let wrapper = new google.visualization.ChartWrapper({ chartType: t, dataTable: data, containerId: d, options: { title: h, vAxis: { title: y }, hAxis: { title: x }, seriesType: st, isStacked: is, } }); wrapper.draw(); window[d + '_wrapper'] = wrapper; } } function nuTestChart() { let a = [ ['Month', 'Shane', 'Dave', 'Adam', 'Paul', 'Chris'], ['2019', 100, 200, 300, 400, 500], ['2020', 165, 238, 322, 498, 550], ['2021', 165, 938, 522, 998, 450], ['2022', 135, 1120, 599, 1268, 288] ]; nuChart('google_chart', 'ComboChart', a, 'title', '', '', 'bars', false); } nuTestChart(); function nuModifyChart() { let wrapper = window["google_chart_wrapper"]; wrapper.setOptions({ colors: ['#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6'] }); wrapper.draw(); } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <button onclick="nuModifyChart()">Modify Chart</button> <div id="google_chart" data-nu-tab="8" data-nu-form="" class="nuHtml" data-nu-access="0" style="top: 38px; width: 450px; height: 192px; position: absolute; visibility: visible;"></div>El tipo de gráfico no se cambia, pero el tipo de serie (que sigue siendo un misterio). Si configura el "tipo de serie" nuevamente en las opciones, se solucionará.
wrapper.setOptions({ colors: ['#e0440e', '#e6693e', '#ec8f6e', '#f3b49f', '#f6c7b6'] ,seriesType: wrapper.m.seriesType });