Getting chartjs chart to work in custom component

Hi all,

I am trying to get this chart animation to work in a custom component in retool, but for the life of me I cannot get it to load. It just shows a blank screen.

I tried using retool.subscribe but with no luck.

Any suggestions or ideas welcome :slight_smile:
Thanks!

window.Retool.subscribe(function(model) {

////from CHARTupdateChart(dataLength);
window.onload = function () {

    var dps = []; // dataPoints

var chart = new CanvasJS.Chart("chartContainer", {
title :{
text: "Dynamic Data"
},
data: [{
type: "line",
dataPoints: dps
}]
});

var xVal = 0;
var yVal = 100;
var updateInterval = 50;
var dataLength = 200; // number of dataPoints visible at any point

var updateChart = function (count) {

count = count || 1;

for (var j = 0; j < count; j++) {
yVal = yVal + Math.round(5 + Math.random() *(-5-5));
dps.push({
x: xVal,
y: yVal
});
xVal++;
}

if (dps.length > dataLength) {
dps.shift();
}

chart.render();
};

updateChart(dataLength);
setInterval(function(){updateChart()}, updateInterval);}

})


</script>

<script>

</script>

it work for me.

just modify window.onload to window.onclick, you can't listen to the document load event in custom component.
and then click that. the chart will appear.

<!DOCTYPE HTML>
<html>
<head>
<script>
window.onclick = function () {

var dps = []; // dataPoints
var chart = new CanvasJS.Chart("chartContainer", {
  title :{
    text: "Dynamic Data"
  },
  data: [{
    type: "line",
    dataPoints: dps
  }]
});

var xVal = 0;
var yVal = 100; 
var updateInterval = 1000;
var dataLength = 20; // number of dataPoints visible at any point

var updateChart = function (count) {

  count = count || 1;

  for (var j = 0; j < count; j++) {
    yVal = yVal +  Math.round(5 + Math.random() *(-5-5));
    dps.push({
      x: xVal,
      y: yVal
    });
    xVal++;
  }

  if (dps.length > dataLength) {
    dps.shift();
  }

  chart.render();
};

updateChart(dataLength);
setInterval(function(){updateChart()}, updateInterval);

}
</script>
</head>
<body>
<div id="chartContainer" style="height: 370px; width:100%;"></div>
<script src="https://cdn.canvasjs.com/canvasjs.min.js"></script>
</body>
</html>

or if you don't want to use click event.
just move the script to bottom of html, and remove window.onload= function () {}

<!DOCTYPE HTML>
<html>
<head>

</head>
<body>
<div id="chartContainer" style="height: 370px; width:100%;"></div>
<script src="https://cdn.canvasjs.com/canvasjs.min.js"></script>
</body>
  <script>


var dps = []; // dataPoints
var chart = new CanvasJS.Chart("chartContainer", {
  title :{
    text: "Dynamic Data"
  },
  data: [{
    type: "line",
    dataPoints: dps
  }]
});

var xVal = 0;
var yVal = 100; 
var updateInterval = 1000;
var dataLength = 20; // number of dataPoints visible at any point

var updateChart = function (count) {

  count = count || 1;

  for (var j = 0; j < count; j++) {
    yVal = yVal +  Math.round(5 + Math.random() *(-5-5));
    dps.push({
      x: xVal,
      y: yVal
    });
    xVal++;
  }

  if (dps.length > dataLength) {
    dps.shift();
  }

  chart.render();
};

updateChart(dataLength);
setInterval(function(){updateChart()}, updateInterval);


</script>
</html>

Thanks AnsonHwang, that works perfectly! You're a lifesaver :slight_smile:

3 posts were split to a new topic: Animated Chart in Retool