javascript - 从远程 JSON 文件绘制 Highcharts 图表

标签 javascript jquery json highcharts

我正在尝试使用 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
        }]

最佳答案

您应该使用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>

关于javascript - 从远程 JSON 文件绘制 Highcharts 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47984506/

相关文章:

javascript - Highcharts - 柱形图重绘动画

javascript - 来自文本文件的 AJAX 请求

javascript - JQuery UI - 将 Draggable 附加到 Droppable

arrays - 使用 jsoncpp 迭代一组 JSON 对象

python - 用于模拟响应的 Python Quick Rest API

javascript - 如何使用 JavaScript 或 jQuery 构建内嵌评级?

php - javascript错误地确定了夏令时线(2006年的示例)

javascript - Rail 3.2 与 JavaScript

jquery - 如何使用手动工具滚动 html 页面?

json - meteor `Deps.autorun` 与 `Collection.observe`