简体   繁体   English

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

[英]Rendering Chart.js Bubble Chart Using Array Data

I have three arrays that I am parsing in from an XML file, detailed below: 我从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"]

Let's say these are called arrayX, arrayY and arrayR. 假设这些被称为arrayX,arrayY和arrayR。 How would I go about using these to render a bubble chart in Chart.js? 我将如何使用它们在Chart.js中呈现气泡图? I have the code to create a simple bubble chart here: 我具有在此处创建简单气泡图的代码:

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
                                    }
                                }]
                            }
                        }
                    });

Note the format of the arrays can be changed if need be, so that just the values are used. 请注意,如果需要,可以更改数组的格式,以便仅使用值。

Since your are getting data dynamically, just iterate over your data and build a chartData object in the format that chart.js requires. 由于您正在动态获取数据,因此只需遍历数据并以chartData的格式构建chartData对象即可。 Once you have assembled your data, just use that in your chart definition. 组装好数据后,只需在图表定义中使用它即可。 See the below example 请参阅以下示例

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  
           }
         }],
       }
     }
   }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM