如何在canvas函数中拟合数组数据?

时间:2017-08-19 06:15:03

标签: jquery ajax

This is final output.

var dps =[]; 
$(document).ready(function(){
    $.ajax({
        type:'post',
        url:'data.php',
        success:function(response){
            var result= JSON.parse(response);
            console.log(result); //var count = Object.keys(result).length;
            //console.log(count);

            for(var i=0; i<result.day.length;i++){
              dps.push({"label":result.day[i], "y":result.amount[i]});
            }
            console.log(dps);
            DrawHigh(dps);
            //DrawCanvas(dps);
        }
    });
});
function DrawHigh(data){
    Highcharts.setOptions(Highcharts.theme);
    Highcharts.chart('chartContainer', {
        xAxis: {
          categories: , 
          title: {
              enabled: true,
              text: '<b>Purchase</b>',
              style: {
                  fontWeight: 'normal'
                    }
            }
         },
       legend: {
          enabled: false
            },
        credits: {
          enabled: false
            },
      series: [{
          data: ,
           }]
     });
} 

这是我的代码。我想渲染var dps = []数组数据来绘制Highchart。因为在高图中我们必须将数据串联起来,现在相同的数据我是如何串联的。

This is the output of console.log(dps); These are the values contain by dps array.我想绘制这个dps数组数据,但是如何使用这些数据来绘制Highchart。

1 个答案:

答案 0 :(得分:0)

根据this basic example (on JSFiddle)documentation

dps.push({"label":result.day[i], "y":result.amount[i]});

应该是:

dps.push({"name":result.day[i], "data":result.amount[i]});

然后您可以将dps数组传递给DrawHigh(data)函数。

series: data  // Where data is dps, an array of objects having the right property names.

<小时/> 的修改

在你的ajax成功回调中,用这个替换for循环:

var xAxisCat = [];
var seriesData = [];

for(var i=0; i<result.day.length;i++){
  xAxisCat.push(result.day[i])
  seriesData.push(result.amount[i]);
}

然后将这两个数组发送到HightCharts,如下所示:

var xAxis = {categories: xAxisCat};
var series = [{data: seriesData, , name: "My graph"}];

DrawHigh(xAxis, series);

function DrawHigh(xAxis, series){中,现在有两个参数,只需xAxis: xAxisseries: series。正确形成数据就像这样传递它。

以下是CodePen,其结果正确,但使用了&#34;模拟&#34; 数据(因为我无法访问您的data.php)我上面提出的循环产生了。