使用数组数据渲染Chart.js气泡图

时间:2017-03-09 10:27:44

标签: javascript charts chart.js

我从XML文件中解析了三个数组,详情如下:

["x": "23.561799", "x": "-10.713591", "x": "-20.516543", "x": "27.352751", "x": "-21.090982"]
["y": "-5.777557", "y": "-24.425175", "y": "9.131939", "y": "7.052970", "y": "-26.059631"]
["r": "10.000000", "r": "10.000000", "r": "10.000000", "r": "10.000000", "r": "10.000000"]

假设这些被称为arrayX,arrayY和arrayR。我如何使用这些来在Chart.js中渲染气泡图? 我有代码在这里创建一个简单的气泡图:

var ctx = document.getElementById("myChart");
                    var myChart = new Chart(ctx, {
                        type: 'bubble',
                        data: {
                            labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
                            datasets: [
                            {
                                label: 'Gaze Map Month 1',
                                data: [
                                {
                                    x: 23,
                                    y: -10,
                                    r: 10
                                },
                                {
                                    x: -10.713591,
                                    y: -24.425175,
                                    r: 3
                                },
                                {
                                    x: -20.516543,
                                    y: 9.131939,
                                    r: 36
                                },
                                {
                                    x: 27.352751,
                                    y: 7.052970,
                                    r: 19
                                },
                                {
                                    x: -21.090982,
                                    y: -26.059631,
                                    r: 2
                                }

                                ],
                                backgroundColor:"#FF6384",
                                hoverBackgroundColor: "#FF6384",
                            }]
                        },
                        options: {
                            scales: {
                                yAxes: [{
                                    ticks: {
                                        beginAtZero:true,
                                        min: -30,
                                        max: 30  
                                    }
                                }],
                                xAxes: [{
                                    ticks: {
                                        beginAtZero:true,
                                        min: -30,
                                        max: 30
                                    }
                                }]
                            }
                        }
                    });

请注意,如果需要,可以更改数组的格式,以便只使用这些值。

1 个答案:

答案 0 :(得分:2)

由于您正在动态获取数据,因此只需迭代数据并以chart.js所需的格式构建chartData对象。汇总数据后,只需在图表定义中使用它即可。见下面的例子

var xArray = ["x": "23.561799", "x": "-10.713591", "x": "-20.516543", "x": "27.352751", "x": "-21.090982"];
var yArray = ["y": "-5.777557", "y": "-24.425175", "y": "9.131939", "y": "7.052970", "y": "-26.059631"];
var rArray = ["r": "10.000000", "r": "10.000000", "r": "10.000000", "r": "10.000000", "r": "10.000000"];

var chartData = [];

xArray.forEach(function(e, i) {
  chartData.push({
    x: parseFloat(e),
    y: parseFloat(yArray[i]),
    r: parseFloat(rArray[i]),
  });
});

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
  type: 'bubble',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [
      {
         label: 'Gaze Map Month 1',
         data: chartData,
         backgroundColor:"#FF6384",
         hoverBackgroundColor: "#FF6384",
      }
    ]
  },
  options: {
     scales: {
       yAxes: [{
         ticks: {
           beginAtZero:true,
            min: -30,
            max: 30  
           }
         }],
       }
     }
   }
});