简体   繁体   English

如何格式化Highcharts以获取特定的json数据?

[英]How to format Highcharts to get specific json data?

I have been spending some time trying to figure this out. 我一直在花一些时间试图解决这个问题。 I normally research instead of asking questions, but I am completely stumped. 我通常会研究而不是问问题,但我完全陷入了困境。 I am trying to create a Highchart with data from the forecast.io api. 我正在尝试使用Forecast.io api中的数据创建一个Highchart。 Specifically, the hourly temperature and minutely precipitation. 具体而言,每小时的温度和每分钟的沉淀。

For those who don't know. 对于那些不知道的人。 To call forecast.io you need your own API key. 要调用Forecast.io,您需要自己的API密钥。 I don't mind sharing mine right now, because you can automatically change it whenever you want. 我现在不介意分享我的,因为您可以随时更改它。 Here is a link to the json that results from calling a specific location (the actual json list would take up too much space and cause clutter for my question) 这是指向特定位置的json的链接(实际的json列表会占用太多空间,导致我的问题变得混乱)

https://api.forecast.io/forecast/87a7dd82a91b0b765d2576872f2a3826/34.6507,-82.8612 https://api.forecast.io/forecast/87a7dd82a91b0b765d2576872f2a3826/34.6507,-82.8612

To call the hourly temperature: 调用小时温度:

  • data.hourly.data[0].temperature (current hour) data.hourly.data [0]。温度(当前小时)
  • data.hourly.data[2].temperature ( two hours form now, you get the point..) data.hourly.data [2] .temperature(现在两个小时了,你知道了..)

I just have no clue how to include this with the highcharts javascript. 我只是不知道如何在highcharts javascript中包含此内容。 Here is a sample line chart from Highcharts that I slightly modified but don't know where to go from here. 这是Highcharts的示例折线图,我对其进行了一些修改,但不知道从何而来。 I have tried so many different ways. 我尝试了很多不同的方法。

$(function () {
$('#container').highcharts({
    chart: {
        type: 'spline'
    },
    title: {
        text: 'Temp'
    },
    subtitle: {
        text: ''
    },
    xAxis: {
        type: 'datetime',
        labels: {
            overflow: 'justify'
        }
    },
    yAxis: {
        title: {
            text: ''
        },
        min: 0,
        minorGridLineWidth: 0,
        gridLineWidth: 0,
        alternateGridColor: null,
        plotBands: [{ // Light air
            from: 0.3,
            to: 1.5,
            color: 'rgba(68, 170, 213, 0.1)',
            label: {
                text: '',
                style: {
                    color: '#606060'
                }
            }
        }, { // Light breeze
            from: 1.5,
            to: 3.3,
            color: 'rgba(0, 0, 0, 0)',
            label: {
                text: '',
                style: {
                    color: '#606060'
                }
            }
        }, { // Gentle breeze
            from: 3.3,
            to: 5.5,
            color: 'rgba(68, 170, 213, 0.1)',
            label: {
                text: '',
                style: {
                    color: '#606060'
                }
            }
        }, { // Moderate breeze
            from: 5.5,
            to: 8,
            color: 'rgba(0, 0, 0, 0)',
            label: {
                text: '',
                style: {
                    color: '#606060'
                }
            }
        }, { // Fresh breeze
            from: 8,
            to: 11,
            color: 'rgba(68, 170, 213, 0.1)',
            label: {
                text: '',
                style: {
                    color: '#606060'
                }
            }
        }, { // Strong breeze
            from: 11,
            to: 14,
            color: 'rgba(0, 0, 0, 0)',
            label: {
                text: '',
                style: {
                    color: '#606060'
                }
            }
        }, { // High wind
            from: 14,
            to: 15,
            color: 'rgba(68, 170, 213, 0.1)',
            label: {
                text: '',
                style: {
                    color: '#606060'
                }
            }
        }]
    },
    tooltip: {
        valueSuffix: ' m/s'
    },
    plotOptions: {
        spline: {
            lineWidth: 4,
            states: {
                hover: {
                    lineWidth: 5
                }
            },
            marker: {
                enabled: false
            },
            tickInterval: 3600000, // one hour

        }
    },
    series: [{
        name: 'Temperature',
        data: []

    }],
    navigation: {
        menuItemStyle: {
            fontSize: '10px'
        }
    }
});
 $.ajax({
  url: "https://api.forecast.io/forecast/87a7dd82a91b0b765d2576872f2a3826/53.479324,-2.248485",
  jsonp: "callback",
  dataType: "jsonp",
  success: function(data) {

  }
  });
});

http://jsfiddle.net/nn51895/kto8yt3r/ http://jsfiddle.net/nn51895/kto8yt3r/

Any help would be appreciated. 任何帮助,将不胜感激。

Also, I have read through the Highcharts documentation, and samples. 另外,我已经阅读了Highcharts文档和示例。 I couldn't figure it out. 我不知道。 I must be missing something. 我肯定错过了什么。

Here you go, i have created a function which takes the dataseries and the initial time as parameter and plot them using highcharts. 在这里,我创建了一个函数,该函数将数据序列和初始时间作为参数,并使用highcharts对其进行绘制。

The ajax request is fetching hourly temperature like this: ajax请求正在获取每小时温度,如下所示:

 $.ajax({
  url: "https://api.forecast.io/forecast/87a7dd82a91b0b765d2576872f2a3826/53.479324,-2.248485",
  jsonp: "callback",
  dataType: "jsonp",
  success: function(odata) {
  var dataArr = new Array();
  var timeint = odata.hourly.data[0].time; 
  for(var i=0; i<odata.hourly.data.length; i++)
      dataArr.push(odata.hourly.data[i].temperature);
  plotLine(dataArr, timeint)
  }
  });

http://jsfiddle.net/kto8yt3r/3/ http://jsfiddle.net/kto8yt3r/3/

You can always format the xAxis, yAxis and tooltip data according to your need. 您始终可以根据需要设置xAxis,yAxis和工具提示数据的格式。

Hope this help! 希望有帮助!

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

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