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>
4

1 回答 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

于 2014-07-28T15:06:54.763 回答