2013-02-15 15 views
2

我一直抨擊我的頭3天試圖加載數據到Highstock圖表的所有三種方法。有沒有辦法導入數據而不使用PHP?我在沒有安裝PHP的IIS服務器上。有沒有辦法在不使用PHP的情況下將數據加載到Highstock?

當我使用$ j.getJSON()方法指向http://www.highcharts.com/samples/data/jsonp.php它工作正常。然而,當我開始嘗試添加我自己的數據時(我嘗試過使用CSV和XML),我可以通過螢火蟲控制檯看到所有內容都沒有問題,但我得到了一個奇怪的結果。

http://i.imgur.com/qj5y8bM.png

我能請得到一個Stockchart圖表樣本(3個積點)。我可以找到的所有樣本都是爲了一個Highchart條形圖。這與我在StockChart中所做的完全不同。請,我需要一個示例CSV和示例JSON來基於此。有人請幫忙。

回答

0

您應該可以使用數組或命名值對象將數據加載到Highcharts/Highstock對象中。 highcharts.com上的PHP示例URL返回Highcharts/Highstock可以理解的JSON數據,但您可以使用任何返回JSON數據的數據源。

作爲一個例子,這裏是使用一個簡單的數組爲系列數據的一些代碼:

$(function() { 
    window.chart = new Highcharts.StockChart({ 
     chart : { 
      renderTo : 'container' 
     }, 
     title : { 
      text : 'My chart' 
     }, 
     series : [{ 
      name : 'My Series', 
      data : [ 
       [Date.UTC(2006, 0, 29, 0, 0, 0), 30.14], 
       [Date.UTC(2006, 0, 29, 1, 0, 0), 34.76], 
       [Date.UTC(2006, 0, 29, 2, 0, 0), 34.34], 
       [Date.UTC(2006, 0, 29, 3, 0, 0), 33.9] 
      ] 
     }] 
    }); 
}); 

這裏有一個活樣本:http://jsfiddle.net/fF9Ru/1/

如果你有一個現有的數據源,請張貼它返回的數據樣本。如果數據格式不正確,圖表可能無法呈現。

+0

謝謝傑里米。我得到了所有這些,但是你的示例在你的函數中使用了手工製作的數據。我如何將上面的數據放在CSV中?您是否有適當格式的CSV示例。我真的在這個黑暗中:( – Daveable 2013-02-15 22:38:30

+0

請看看裏卡多的解決方案 - 他提供了一個jQuery示例讀取CSV,然後使用它作爲數據數組。 – Jeremy 2013-02-19 04:48:02

+0

Sooo,我嘗試了不同的東西。使用你的數據以上: 在data.json文件中,只是指向它。然而,它仍然鎖定圖表。我看到(通過控制檯)它拉動data.json文件就好了,但圖表本身如果Stockchart可以直接處理這些數據,它不應該在.json文件中工作嗎? – Daveable 2013-02-19 15:35:27

3

看看docs,有一些演示和解釋如何做到這一點。

第一個,創建csv。

Categories,Apples,Pears,Oranges,Bananas 
John,8,4,6,5 
Jane,3,4,2,3 
Joe,86,76,79,77 
Janet,3,16,13,15 

,定義基本圖表選項。

var options = { 
    chart: { 
     renderTo: 'container', 
     defaultSeriesType: 'column' 
    }, 
    title: { 
     text: 'Fruit Consumption' 
    }, 
    xAxis: { 
     categories: [] 
    }, 
    yAxis: { 
     title: { 
      text: 'Units' 
     } 
    }, 
    series: [] 
}; 

第三,處理數據。

$.get('data.csv', function(data) { 
    // Split the lines 
    var lines = data.split('\n'); 

    // Iterate over the lines and add categories or series 
    $.each(lines, function(lineNo, line) { 
     var items = line.split(','); 

     // header line containes categories 
     if (lineNo == 0) { 
      $.each(items, function(itemNo, item) { 
       if (itemNo > 0) options.xAxis.categories.push(item); 
      }); 
     } 

     // the rest of the lines contain data with their name in the first position 
     else { 
      var series = { 
       data: [] 
      }; 
      $.each(items, function(itemNo, item) { 
       if (itemNo == 0) { 
        series.name = item; 
       } else { 
        series.data.push(parseFloat(item)); 
       } 
      }); 

      options.series.push(series); 

     } 

    }); 

    // Create the chart 
    var chart = new Highcharts.Chart(options); 
}); 

here is the result

相關問題