【问题标题】:Parsing Json to render multiline highstock charts using Javascript解析 Json 以使用 Javascript 呈现多行 highstock 图表
【发布时间】:2018-08-21 01:56:16
【问题描述】:

我正在尝试解析来自 R Dataframe 的类似 JSON 输出,以呈现为多行 highstock 图表。我是 javascripting 和 highcharts 的新手,所以在阅读了 highstocks 的文档后,我仍然无法按照我的意愿渲染图表。

我有来自 R 数据框的输出格式的 JSON 文件,(示例)-

[[
 {
   "Server": "AAAA",
   "Date": "9/1/2017",
   "Hits": 455
 },
 {
   "Server": "AAAA",
   "Date": "9/2/2017",
   "Hits": 457
 },
 {
   "Server": "AAAA",
   "Date": "9/3/2017",
   "Hits": 658
 },
 {
   "Server": "AAAA",
   "Date": "9/4/2017",
   "Hits": 645
 },
 {
   "Server": "AAAA",
   "Date": "9/5/2017",
   "Hits": 578
 },
 {
   "Server": "AAAA",
   "Date": "9/6/2017",
   "Hits": 425
 },
 {
   "Server": "AAAA",
   "Date": "9/7/2017",
   "Hits": 487
 },
 {
   "Server": "AAAA",
   "Date": "9/8/2017",
   "Hits": 499
 },
 {
   "Server": "AAAA",
   "Date": "9/9/2017",
   "Hits": 600
 },
 {
   "Server": "AAAA",
   "Date": "9/10/2017",
   "Hits": 567
 },
 {
   "Server": "BBBB",
   "Date": "9/1/2017",
   "Hits": 486
 },
 {
   "Server": "BBBB",
   "Date": "9/2/2017",
   "Hits": 595
 },
 {
   "Server": "BBBB",
   "Date": "9/3/2017",
   "Hits": 509
 },
 {
   "Server": "BBBB",
   "Date": "9/4/2017",
   "Hits": 460
 },
 {
   "Server": "BBBB",
   "Date": "9/5/2017",
   "Hits": 351
 },
 {
   "Server": "BBBB",
   "Date": "9/6/2017",
   "Hits": 488
 },
 {
   "Server": "BBBB",
   "Date": "9/7/2017",
   "Hits": 693
 },
 {
   "Server": "BBBB",
   "Date": "9/8/2017",
   "Hits": 478
 },
 {
   "Server": "BBBB",
   "Date": "9/9/2017",
   "Hits": 662
 },
 {
   "Server": "BBBB",
   "Date": "9/10/2017",
   "Hits": 401
 },
 {
   "Server": "CCCC",
   "Date": "9/1/2017",
   "Hits": 477
 },
 {
   "Server": "CCCC",
   "Date": "9/2/2017",
   "Hits": 474
 },
 {
   "Server": "CCCC",
   "Date": "9/3/2017",
   "Hits": 396
 },
 {
   "Server": "CCCC",
   "Date": "9/4/2017",
   "Hits": 372
 },
 {
   "Server": "CCCC",
   "Date": "9/5/2017",
   "Hits": 398
 },
 {
   "Server": "CCCC",
   "Date": "9/6/2017",
   "Hits": 605
 },
 {
   "Server": "CCCC",
   "Date": "9/7/2017",
   "Hits": 415
 },
 {
   "Server": "CCCC",
   "Date": "9/8/2017",
   "Hits": 522
 },
 {
   "Server": "CCCC",
   "Date": "9/9/2017",
   "Hits": 385
 },
 {
   "Server": "CCCC",
   "Date": "9/10/2017",
   "Hits": 378
 }
]]

我尝试的 Javascript 代码如下所示。尝试按服务器对 json 进行分组,使其具有 highstock 图表所需的形式。 [[“名称”:“AAAA”,“数据”:[[9/1/2017,455],[9/2/2017,457]...],“名称”:“BBBB,“数据”: [[9/1/2017,486],...]...]]

var result = [];

/**
 * Create the chart when all data is loaded
 * @returns {undefined}
 */
function createChart() {

    Highcharts.stockChart('container', {

        rangeSelector: {
            selected: 4
        },

        yAxis: {
            labels: {
                formatter: function () {
                    return (this.value > 0 ? ' + ' : '') + this.value + '%';
                }
            },
            plotLines: [{
                value: 0,
                width: 2,
                color: 'silver'
            }]
        },

        plotOptions: {
            series: {
                compare: 'percent',
                showInNavigator: true
            }
        },

        tooltip: {
            pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
            valueDecimals: 2,
            split: true
        },

        series: result
    });
}

$.getJSON('data.json',    function (data) {
        result = data.reduce(function (r, a) {
        r[a.Server] = r[a.Server] || [];
        r[a.Server].push(a);
        return r;
    }, Object.create(null));
        };

            createChart();

    });
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<script src="https://code.highcharts.com/stock/modules/export-data.js"></script>


<div id="container" style="height: 400px; min-width: 310px"></div>

通过使用 console.log(),json 被分组,但图表似乎仍然没有生成。花了三天多的时间阅读 SO .. 尝试了各种技巧,但似乎仍然无法弄清楚。任何人都可以为我指出正确的方向,了解如何阅读 json,解析它,以便 highstocks 渲染会很棒!使用https://www.highcharts.com/stock/demo/compare 处的模板生成与此类似的图。数据集是动态过滤的,因此无法将名称字段预填充为在 highcharts 中找到的演示代码。日期也生成为 UNIX 时间。

【问题讨论】:

  • x 数据(日期)需要以 1970 年以来的毫秒数为单位。您可以使用 new Date('your date here').getTIme()

标签: javascript r json highcharts


【解决方案1】:

我创建了一个简单的示例,如何根据您的 JSON 数据创建具有多个系列的 Highstock 图表:

function createChart(series) {
    Highcharts.stockChart('container', {
        series: series
    });
}

$.getJSON('https://api.myjson.com/bins/1d5580', function(data) {
    var series = [],
        name,
        newData = [];

    Highcharts.each(data[0], function(el, i) {
        if (!name) {
            name = el.Server;
        }
        if (name === el.Server) {
            newData.push({
                x: Number(moment(el.Date).format('X')),
                y: el.Hits
            });
        } else {
            series.push({
                name: name,
                data: newData
            });
            name = el.Sever;
            newData = [];
            newData.push({
                x: Number(moment(el.Date).format('X')),
                y: el.Hits
            });
        }
    });
    series.push({
        name: name,
        data: newData
    });
    createChart(series);
});

现场演示:http://jsfiddle.net/BlackLabel/qrxszc0b/

文档:https://www.highcharts.com/docs/working-with-data/data-intro

【讨论】:

  • 谢谢!!!像魅力一样工作。我有超过 100000 个数据点,因此必须将 plotOptions 中的 turboThreshold 设置为 0 以禁用检查。
  • 快速问题,当我在开头或结尾没有双 [[ 时,我的 json 不会解析。它仅适用于 [[ ... ]]。我从服务器输出的 json 格式为 [{}{}]。我必须手动将 json 更改为 [[{}{}{}]] 才能正常工作。我可以不使用 [[{}{}]] 而只使用 [{}{}]。当我按原样加载它时,它不会渲染。感谢您的任何指点!
  • 只需要在'Highcharts.each'方法中将'data[0]'改为'data'即可。
  • 太棒了! ..我开始了解这些究竟是如何工作的!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 2014-06-12
  • 2014-11-08
相关资源
最近更新 更多