Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are quite a few things wrong with the code block you have posted including scoping and synchronization issues - rather than try and kludge it to make it work I've rewritten it and commented as to why I've done it this way:</p> <pre><code>/** * A JS class that you can instansiate with the "new" construct * on page/api load - this keeps everything self-contained and * for the most part, out of the global scope. */ function ChartObject(){ // internal function - neater way to grab elements function $(x){ return document.getElementById(x); } // some default data this.data = [['x', 'Metric Title'], ['start',0]]; // read values from our DOM elements and add to our data array this.update = function(){ var dis = $('distanceField').value.trim(), res = parseInt($('resultField').value, 10); if(!isNaN(res) &amp;&amp; !!dis){ this.data.push([dis, res]); this.draw(); } else { alert('invalid entry'); } }; // redraw our graph based on our current data array this.draw = function(){ new google.visualization.LineChart($('visualization')).draw( google.visualization.arrayToDataTable(this.data), { curveType : "function", width : 450, height : 300 } ); } // the following is executed once when the ChartObject is first called // draw the object with initial data this.draw(); // add listener to button that user can click to "update" graph // this could be adapted to work with "onkeyup" rather than trying // to hack with "setTimeout" $('addRecord').onclick = function(){ myChartObject.update(); } }; </code></pre> <p><a href="http://jsfiddle.net/Pe6xV/" rel="nofollow"><strong>jsFiddle: working example</strong></a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload