Dynamically Updating Data Series Legend - CanvasJS

Dynamically Updating Data Series Legend - CanvasJS

by canvasjs

HTML

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

JavaScript

var chart = new CanvasJS.Chart("chartContainer",
	{      
      data: [
      {
        type: "column",
        showInLegend: true,        
        toolTipContent: "{legendText}",
        dataPoints: [
        { x: 10, y: 71 },
        { x: 20, y: 55},
        { x: 30, y: 50 },
        { x: 40, y: 65 },
        { x: 50, y: 95 },
        { x: 60, y: 68 },
        { x: 70, y: 28 },
        { x: 80, y: 34 },
        { x: 90, y: 14}
        ]
      },
      {
        type: "column",
        showInLegend: true,
        toolTipContent: "{legendText}",
        dataPoints: [
        { x: 10, y: 41 },
        { x: 20, y: 65},
        { x: 30, y: 40 },
        { x: 40, y: 75 },
        { x: 50, y: 35 },
        { x: 60, y: 88 },
        { x: 70, y: 28 },
        { x: 80, y: 44 },
        { x: 90, y: 24}
        ]
      }
      ]
    });

 chart.render();

var updateCount = 0;
setInterval(function(){  
    chart.options.data[0].legendText = "DS1 Update " + (updateCount + 1);
    chart.options.data[1].legendText = "DS2 Update " + (updateCount + 1);
    chart.render();
    
    updateCount++;
}, 2000);