2016-10-19 7 views
0

現在、私はHighstock/Highchartsで作業していますが、私が持っているjsonファイルでデータを読み込むことはできません。Highstock/Highchartsからの奇妙なエラーメッセージの取得

highstock.js:456 Uncaught TypeError: f.data.slice is not a function 

そして、私はこの機能によってJSONデータを取得しようとしている:私は、データを取得しようとしているとき、それは私が前に見たことがない、コンソールでこのエラーメッセージを表示します。イムは、私のdata.jsonのURLを入力するとき私は、このエラーメッセージが出てい:

$.each(names, function (i, name) { 

     var dataUrl = 'http://local.priceservice.dev/js/data.json'; 

     $.getJSON(dataUrl, function (data) { 

      seriesOptions[i] = { 
       name: name, 
       data: data 
      }; 

      // As we're loading the data asynchronously, we don't know what order it will arrive. So 
      // we keep a counter and create the chart when all the data is loaded. 
      seriesCounter += 1; 

      if (seriesCounter === names.length) { 
       createChart(); 
      } 
      console.log(data) 
     }); 
    }); 

私は前にそのメッセージを見たことがないと私は周りに検索するようにしようとしましたが、私は持っていた誰かを見つけることができません私と同じ問題。

JSON:

{ 
    "priceSeries": { 
     "2016-10-19T12:51:51.000Z": [[1451606400000, 18.0], [1454284800000, 19.0], [1456790400000, 17.0], [1456790400000, 15.0], [1459468800000, 15.0], [1462060800000, 15.0], [1464739200000, 15.0], [1467331200000, 15.0], [1470009600000, 15.0], [1472688000000, 15.0], [1475280000000, 15.0], [1477958400000, 15.0], [1480550400000, 15.0], [1483228800000, 15.0], [1485907200000, 15.0], [1488326400000, 15.0]], 
     "2016-11-19T12:51:51.000Z": [[1451606400000, 19.0], [1454284800000, 20.0], [1456790400000, 18.0], [1456790400000, 16.0], [1459468800000, 16.0], [1462060800000, 16.0], [1464739200000, 16.0], [1467331200000, 16.0], [1470009600000, 16.0], [1472688000000, 16.0], [1475280000000, 16.0], [1477958400000, 16.0], [1480550400000, 16.0], [1483228800000, 16.0], [1485907200000, 16.0], [1488326400000, 16.0]] 
    } 
} 

Highstock/Highchartsコード:

$(function() { 
    var seriesOptions = [], 
     seriesCounter = 0, 
     names = ['MSFT', 'AAPL', 'GOOG']; 
    /** 
    * Create the chart when all data is loaded 
    * @returns {undefined} 
    */ 
    function createChart() { 

     $('#container').highcharts('StockChart', { 

      chart: { 
       zoomType: 'x' 
      }, 
      legend: { 
       enabled: true 
      }, 

      rangeSelector: { 
       buttons: [{ 
        type: 'hour', 
        count: 1, 
        text: '1h' 
      }, { 
        type: 'day', 
        count: 1, 
        text: '1d' 
      }, { 
        type: 'week', 
        count: 1, 
        text: '1w' 
      }, { 
        type: 'month', 
        count: 1, 
        text: '1m' 
      }, { 
        type: 'year', 
        count: 1, 
        text: '1y' 
      }, { 
        type: 'all', 
        text: 'All' 
      }], 
       inputEnabled: false, // it supports only days 
       selected: 4 // all 
      }, 
      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: seriesOptions 
     }); 
    } 

    $.each(names, function (i, name) { 

     var dataUrl = 'http://local.priceservice.dev/js/data.json'; 

     $.getJSON(dataUrl, function (data) { 

      seriesOptions[i] = { 
       name: name, 
       data: data 
      }; 

      // As we're loading the data asynchronously, we don't know what order it will arrive. So 
      // we keep a counter and create the chart when all the data is loaded. 
      seriesCounter += 1; 

      if (seriesCounter === names.length) { 
       createChart(); 
      } 
      console.log(data) 
     }); 
    }); 
}); 

事前にありがとうございます! :)

答えて

1

データは配列でなければなりません。

series: [{ 
    name: ..., 
    data: { // <- you put an object instead of an array with points 
     "priceSeries": ... 
    } 
}] 

上記のコードスライスがこの

のように、オブジェクトの呼び出しであることを原因:これは次のようにあなたのシリーズに見える

series: [{ 
    data: [...] // <- has to be an array 
}] 

:シリーズは、このような形式のデータを有する少なくとも一つのオブジェクトが必要です

var someObj = {}; 
someObj.slice(); // Uncaught TypeError: someObj.slice is not a function(…) 

あなたはこの方法で行く必要があります。助け答え、のためhttp://jsfiddle.net/r3xfxymm/1/

+0

感謝を! – RasmusGlenvig

関連する問題