0

I'm trying to create a pie chart in canvasjs, using predetermined data (AKA from the DB). On the side of Canvasjs, there is a helpful tutoriol to put multiple data in a graph (using a weakly typed array), but I don't know how to do this for the legend. I have tried something, but it doesn't seem to quite work yet.

My code will be posted below, if anyone knows how I could create a legend or something that substitutes it, I'd be very grateful!

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
window.onload = function () {

   var dps = [{y: 10}, {y: 13}, {y: 18}, {y: 20}];
  var bmp = [{x:"Wii U"}, { x: "3DS"}, { x: "PS3"}, { x: "Xbox One"}];
  //dataPoints. 

  var chart = new CanvasJS.Chart("chartContainer",{
    title :{
        text: "Test title"
    },
    data: [{
      type: "pie",
        dataPoints : dps,
      indexLabels : bmp
    }],
    legendText: bmp
  });

  chart.render();
  var xVal = dps.length + 1;
  var yVal = 15;    
  var updateInterval = 1000;

  var updateChart = function () {


    yVal = yVal +  Math.round(5 + Math.random() *(-5-5));
    dps.push({x: xVal,y: yVal});

    xVal++;
    if (dps.length >  10 )
    {
        dps.shift();                
    }

    chart.render();     

// update chart after specified time. 

};   
}
</script>
    <script type="text/javascript" src="/assets/script/canvasjs.min.js"></script>
</head>
<body>
<div id="chartContainer" style="height: 300px; width: 100%;">
</div>
</body>
</html>

1 Answer 1

1

You done good job, but you placed Labels to wrong array

var dps = [
    {y: 10, legendText:"Wii U", label: "Wii U 10%"}, 
    {y: 13, legendText:"3DS", label: "3DS 13%"}, 
    {y: 18, legendText:"PS3", label: "PS3 18%"}, 
    {y: 20, legendText:"Xbox One", label: "Xbox One 20%"}
];

DEMO

You can find more examples here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.