2017-06-05 2 views
1

私は、サーバーからこのようなデータを取得する:アレイをハイチャートに表示するにはどうすればいいですか? [JS]

data":[[1496640705,1583360,1583360,1583370,1583360],[1496640720,1583360,1583350,1583360,1583345],[1496640735,1583350,1583320,1583400,1583320]] 

私の質問、どのように私はHighchartsでこのデータを表示することができますか?すべての配列の最初の要素はX軸の日付であり、Y軸の各配列の最後のデータのみが必要です。ハイチャートのためにこれらの2つの要素を選択するにはどうすればよいですか?

+0

ようこそ。 '<>'スニペットエディタを使って追加できる[mcve]が好きであることを確認するには[help]にアクセスしてください。 – mplungjan

+0

以前の回答に加えて、現在のデータフォーマットを使用し、 Highchartsパラメーターで配列の値:http://jsfiddle.net/hy7bywqh/ –

答えて

1

highchartのすべてのチャートが異なるway.Createでデータを受け入れる二つの別々のアレイXAXIS & Y軸データから配列&をhighchartに価値を提供します。

var data = [ 
 
    [1496640705, 1583360, 1583360, 1583370, 1583360], 
 
    [1496640720, 1583360, 1583350, 1583360, 1583345], 
 
    [1496640735, 1583350, 1583320, 1583400, 1583320] 
 
    ], 
 
    xAxis = [], 
 
    yAxis = []; 
 

 

 
data.forEach(function(item) { 
 
    xAxis.push(item[0]); 
 
    yAxis.push(item[item.length - 1]) 
 
}) 
 

 
console.log(xAxis, yAxis)

1

サーバーからあなたのデータは、データの.so(highcharts用)ミリ秒の形式で日付が含まれている必要があります

var data = [ 
    [1496640705000, 1583360, 1583360, 1583370, 1583360], 
    [1496640720000, 1583360, 1583350, 1583360, 1583345], 
    [1496640735000, 1583350, 1583320, 1583400, 1583320] 
    ], 
//For highcharts data should be formatted as [[x,y],[x,y],...] 
seresData=[] 
data.forEach(function(item) { 
seresData.push([item[0],item[item.length - 1]]) 
}) 
console.log(seresData) 

デモコード

var data = [ 
 
    [1496640705000, 1583360, 1583360, 1583370, 1583360], 
 
    [1496640720000, 1583360, 1583350, 1583360, 1583345], 
 
    [1496640735000, 1583350, 1583320, 1583400, 1583320] 
 
    ], 
 

 
    seresData = [] 
 
data.forEach(function(item) { 
 
    seresData.push([item[0], item[item.length - 1]]) 
 
}) 
 
//console.log(seresData) 
 

 
Highcharts.chart('container', { 
 
    chart: { 
 
    type: 'spline' 
 
    }, 
 
    title: { 
 
    text: 'Snow depth at Vikjafjellet, Norway' 
 
    }, 
 
    subtitle: { 
 
    text: 'Irregular time data in Highcharts JS' 
 
    }, 
 
    xAxis: { 
 
    type: 'datetime', 
 
    dateTimeLabelFormats: { // don't display the dummy year 
 
     month: '%e. %b', 
 
     year: '%b' 
 
    }, 
 
    title: { 
 
     text: 'Date' 
 
    } 
 
    }, 
 
    yAxis: { 
 
    title: { 
 
     text: 'Snow depth (m)' 
 
    }, 
 
    min: 0 
 
    }, 
 
    tooltip: { 
 
    headerFormat: '<b>{series.name}</b><br>', 
 
    pointFormat: '{point.x:%e. %b}: {point.y:.2f} m' 
 
    }, 
 

 
    plotOptions: { 
 
    spline: { 
 
     marker: { 
 
     enabled: true 
 
     } 
 
    } 
 
    }, 
 

 
    series: [{ 
 
    name: 'Winter 2012-2013', 
 
    // Define the data points. All series have a dummy year 
 
    // of 1970/71 in order to be compared on the same x axis. Note 
 
    // that in JavaScript, months start at 0 for January, 1 for February etc. 
 
    data: seresData 
 
    }] 
 
});
<script src="https://code.highcharts.com/highcharts.js"></script> 
 
<script src="https://code.highcharts.com/modules/exporting.js"></script> 
 
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
のようにする必要があります

関連する問題