Creating and Updating Graph in HTML and Javascript -
i have website calls function randomly picks number 2 decimal places e.g.
(5.53)
here code:
randomnum = math.floor(math.random() * (1000 - 100) + 100) / 100;
(this gives me value between 1 , 10 2 decimal places)
the website counts number in increments of 0.01 , when reaches value stops , loops creating random number , starts counting again.
if (randomnum < c) { c = 0; stopcount() }
(c counter value increments 0.01 every 20ms)
i'm trying create line graph shows increments , updates, image visual aid only, not have working version.
this code below i'm using general shape, did not create code. here link source im using. http://www.chartjs.org/docs/latest/charts/line.html
function renderchart() { var ctx = document.getelementbyid("bettingarea").getcontext('2d'); var mylinechart = new chart(ctx, { type: 'line', data: { labels: [ "1", "2", "3", "4",], datasets: [{ label: 'crash graph', data: [5.44, 5.67, 5.94, 6.12,], backgroundcolor: [ 'rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', 'rgba(255, 159, 64, 0.2)' ], bordercolor: [ 'rgba(255,99,132,1)', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)', 'rgba(75, 192, 192, 1)', 'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)' ], borderwidth: 1 }] }, options: { scales: { yaxes: [{ ticks: { beginatzero:false } }] } } }); }
the same website has http://www.chartjs.org/docs/latest/developers/updates.html
which cannot seam working no matter :(
i suppose i'm asking if there easier way of doing this, , if not please can updatedata set function working
thank provided :)
update: custom pseudocode (i imagine working so)
generate random = randomnum start timer update canvasgraph x.seconds , y.timer timer = timer + 0.01 if timer > randomnum stop.timer end loop else continue timer start loop again
this basic form of code, , acts visual aid people trying visualize code, know i'm not using correct pseudocode terminology
if understand correctly, after :
var mylinechart, c = 0, label = 0, randomnum, interval; function renderchart() { var ctx = document.getelementbyid("bettingarea").getcontext('2d'); mylinechart = new chart(ctx, { type: 'line', data: { labels: ["1", "2", "3", "4"], datasets: [{ label: 'crash graph', data: [5.44, 5.67, 5.94, 6.12], backgroundcolor: [ 'rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', 'rgba(255, 159, 64, 0.2)' ], bordercolor: [ 'rgba(255,99,132,1)', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)', 'rgba(75, 192, 192, 1)', 'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)' ], borderwidth: 1 }] }, options: { scales: { yaxes: [{ ticks: { beginatzero: false } }] } } }); } function updatechart(chart) { randomnum = math.floor(math.random() * (1000 - 100) + 100) / 100; chart.data.labels = []; chart.data.datasets[0].data = []; interval = setinterval(function() { if (c < randomnum) { c += 0.01; label++; //update chart chart.data.labels.push(label.tostring()); chart.data.datasets[0].data.push(+c.tofixed(2)); chart.update(); } else { clearinterval(interval); c = 0; label = 0; //updatechart(mylinechart) /* maybe call function again */ } }, 20); } window.onload = renderchart;
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.6.0/chart.min.js"></script> <canvas id="bettingarea"></canvas><br> <button onclick="updatechart(mylinechart)">update chart</button>
correct me if wrong :)
Comments
Post a Comment