javascript - Previous Rows/Markers disappear once new rows/markers are created in Google Scatter Chart -
i creating google scatter chart. want dynamically update markers/rows. problem while being updated dynamically, each time new row created previous 1 disappears. want see new rows old ones. rows being dynamically updated combobox item selection. however, previous row disappears when new combobox item selected. want see row on chart. code is::
<script type="text/javascript"> var i=0; google.load("visualization", "1", {packages:["corechart"]}); google.setonloadcallback(drawchart); function drawchart() { var data = new google.visualization.datatable(); var struser=[0]; var struser2=[0]; if(i==0) { var struser=[0]; var struser2=[0]; } else { var a= document.getelementbyid("combo1"); struser[i] = parseint(a.options[a.selectedindex].value); var b= document.getelementbyid("combo2"); struser2[i] = parseint(b.options[b.selectedindex].value); } var options = { title: 'x vs. y ', legend: { position: 'top', maxlines: 150 }, haxis: {title: 'x', minvalue: 0, maxvalue: 15}, vaxis: {title: 'y', minvalue: 0, maxvalue: 15}, }; data.addcolumn('number', 'x'); data.addcolumn('number', 'y'); var chart = new google.visualization.scatterchart(document.getelementbyid('chart_div'));; for(j=0;j<=i;j++) { data.addrows(i+1); data.addrows([[struser[j],struser2[j]]]); } i=i+1; chart.draw(data, options); } html: <body> <select name="combo1" id="combo1" onchange="drawchart()"> <option value="0">x</option> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> <select name="combo2" id="combo2" onchange="drawchart()"> <option value="0">y</option> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> </body>
you create new datatable, keep previous values must store datatable somewhere when call drawchart
first time , use stored datatable in subsequent calls.
example(stores datatable property of node draw chart)
var i=0; google.load("visualization", "1", {packages:["corechart"]}); google.setonloadcallback(drawchart); function drawchart() { var container=document.getelementbyid('chart_div'); if(!container.datatable){ container.datatable = new google.visualization.datatable(); container.datatable.addcolumn('number', 'x'); container.datatable.addcolumn('number', 'y'); } data=container.datatable var struser=[0]; var struser2=[0]; if(i==0) { var struser=[0]; var struser2=[0]; } else { var a= document.getelementbyid("combo1"); struser[i] = parseint(a.options[a.selectedindex].value); var b= document.getelementbyid("combo2"); struser2[i] = parseint(b.options[b.selectedindex].value); } var options = { title: 'x vs. y ', legend: { position: 'top', maxlines: 150 }, haxis: {title: 'x', minvalue: 0, maxvalue: 15}, vaxis: {title: 'y', minvalue: 0, maxvalue: 15}, }; var chart = new google.visualization.scatterchart(container); for(j=0;j<=i;j++) { data.addrows([[struser[j],struser2[j]]]); } i=i+1; chart.draw(data, options); }
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['corechart']}]}"></script> <select name="combo1" id="combo1" onchange="drawchart()"> <option value="0">x</option> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> <select name="combo2" id="combo2" onchange="drawchart()"> <option value="0">y</option> <option value="0">0</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> <div id="chart_div" ></div>
Comments
Post a Comment