从远程JSON文件绘制Highcharts图

问题描述 投票:0回答:2

我试图使用Highcharts JS绘制图形。我有一个JSON文件,比如

[{"receive_date":"2013-11-04","responses":"2"}]

JSON: - https://api.myjson.com/bins/gdpu7

receive_date应为X轴,responses应为Y轴。我正在加载远程JSON数据并将其传递给Highcharts。但是,建议的方法是分配X轴receive_date键并分配Y轴responses键。

JSFiddle: -

http://jsfiddle.net/273x2f13/1/

// Create the chart
$.getJSON("https://api.myjson.com/bins/gdpu7", function(data){ 
    chart: {
        type: 'column'
    },
    title: {
        text: 'Date Vs Rsponses'
    },
    subtitle: {
        text: 'Chart View Here'
    },
    xAxis: {
        type: 'category'
    },
    yAxis: {
        title: {
            text: 'Responses'
        }

    },
    legend: {
        enabled: false
    },
    plotOptions: {
        series: {
            borderWidth: 0,
            dataLabels: {
                enabled: true,
                format: '{point.y:.1f}%'
            }
        }
    },

    tooltip: {
        headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
        pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
    },

    series: [{
        x: 'receive_date',
        y: 'responses'
        colorByPoint: true,
        data: data
    }]


});

我用它来给它X轴和Y轴值。但是,这不正确。

series: [{
            x: 'receive_date',
            y: 'responses'
            colorByPoint: true,
            data: data
        }]
javascript jquery json highcharts
2个回答
1
投票

你应该使用Array#map

例如:

xAxis: {
  type: 'category',
  categories: data.map(function(x) {
    return x.receive_date;
  })
},

还有这个:

series: [{
  colorByPoint: true,
  data: data.map(function(x) {
    return x.responses * 1; // Convert to a number.
  })
}]

像这样的东西:

$(function() {
  $.getJSON("https://api.myjson.com/bins/gdpu7", function(data) {
    console.log(data);
    Highcharts.chart('container', {
      chart: {
        type: 'column'
      },
      title: {
        text: 'Date Vs Rsponses'
      },
      subtitle: {
        text: 'Chart View Here'
      },
      xAxis: {
        type: 'category',
        categories: data.map(function(x) {
          return x.receive_date;
        })
      },
      yAxis: {
        title: {
          text: 'Responses'
        }
      },
      legend: {
        enabled: false
      },
      plotOptions: {
        series: {
          borderWidth: 0,
          dataLabels: {
            enabled: true,
            format: '{point.y:.1f}'
          }
        }
      },
      tooltip: {
        headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
        pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}</b> of total<br/>'
      },
      series: [{
        colorByPoint: true,
        data: data.map(function(x) {
          return x.responses * 1;
        })
      }]
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto;"></div>

0
投票

另一种方法是将x轴的类型更改为'datetime'并将receive_date属性转换为时间戳:

$.getJSON("https://api.myjson.com/bins/gdpu7", function(data) {

  var chart = Highcharts.chart('container', {
    xAxis: {
      type: 'datetime'
    },

    series: [{
      data: data.map(function(p) {
        var ymd = p.receive_date.split("-");
        return [Date.UTC(ymd[0], ymd[2] - 1, ymd[1]), parseInt(p.responses)];
      })
    }]
  });
});

现场演示:http://jsfiddle.net/kkulig/da6Lejy7/

© www.soinside.com 2019 - 2024. All rights reserved.