1👍
✅
You can’t do that directly. The code is executed only once when the request is sent to server.
In order to achieve your goals, you need create a server side code snippet to generate data. For example, if you prefer Razor Page
, create a OnGetRefresh()
handler as below:
public IActionResult OnGetRefresh()
{
return new JsonResult(this.RNG());
}
private int RNG(){
Random rnd = new Random();
int num = rnd.Next(1, 10);
return num;
}
And then pull data when onRefresh
using Ajax or websocket
. Here’s a demo that uses Ajax to get the data from server:
function onRefresh(chart) {
function pushData(yData){
chart.config.data.datasets.forEach(function(dataset) {
dataset.data.push({ x: Date.now(), y:yData });
});
}
var xhr = new XMLHttpRequest();
// you might want to custom the url
var handlerUrl = '/Index?handler=Refresh';
xhr.open('GET', handlerUrl , true);
xhr.onreadystatechange = function(){
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status == 200) {
pushData(xhr.responseText);
return;
}
console.log("error happens: receives a status code ",xhr.status)
}
}
xhr.send();
}
A Working Demo Using your config:
Source:stackexchange.com