我正在尝试使用预定的数据(数据库中的AKA)在canvas.js中创建饼图。
在Canvasjs方面,有一个有用的tutoriol将多个数据放入图形中(使用弱类型数组),但是我不知道如何为图例执行此操作。我已经尝试了一些方法,但是似乎还没有完成。

我的代码将发布在下面,如果有人知道我如何创建图例或替代它的东西,我将不胜感激!

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

最佳答案

您做得不错,但是将标签放置在错误的数组上

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

您可以找到更多示例here

10-04 22:34
查看更多