2011-09-23 70 views
0

我有一個MySQL數據庫中的一些價格數據,我想顯示使用HighCharts/HighStock但我不知道我怎麼能真正去獲取這個數據從MySQL (通過DBslayer作爲JSON層)並顯示在高圖表中(我在他們的網站上找到的示例沒有幫助,並且搜索周圍沒有好的內容)。JSON,HighCharts/HighStock,DBSlayer - 數據顯示

所以基本上系統是這樣的:

MySQL <---> DBSlayer Service <---> JSON Requests to DBSlayer <---> Web page with charts - send query to DBSLayer 

MySQL的查看簡而言之DBSlayer查詢看起來是這樣的:

DATE TIME | Symbol1 | Price 1 | Symbol2 | Price 2 | Price3 
2011-09-01| ABC  | 12.3 | XYZ  | 67.8 | 0.0852 

還是一個更好的例子是從查詢返回的JSON到DBSlayer:

{"RESULT" : {"HEADER" : ["id" , , "authorID" , "msgDate" , "obj_obj" , "obj_subj" , "obj_diff" , "subj_pos" , "subj_neg" , "subj_diff" , "pos_lex" , "neg_lex" ] , 
    "ROWS" : [["4e0f1c393bfbb6aa4b7278c2" , "27" , "2011-06-30 13:59:47" , 0.0275171 , 0.972483 , -0.944966 , 0.993814 , 0.00618577 , 0.987628 , 1 , 0 ] , 
    ["4e0f1c393bfbb6aa4b7278c3" , "36324" , "2011-06-30 13:59:31" , 0.364953 , 0.635047 , -0.270095 , 0.0319281 , 0.968072 , -0.936144 , 3 , 1 ] , 
    ["4e0f1c393bfbb6aa4b7278c4" , "12134" , "2011-06-30 13:59:28" , 0.0112589 , 0.988741 , -0.977482 , 0.857735 , 0.142265 , 0.715469 , 1 , 1 ] ] , 
    "TYPES" : ["MYSQL_TYPE_VAR_STRING" , "MYSQL_TYPE_VAR_STRING" , "MYSQL_TYPE_DATETIME" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_DOUBLE" , "MYSQL_TYPE_LONG" } , "SERVER" : "db-webPrices"} 

我應該如何將此部署到High Charts?我應該首先使用Node.js來包裝查詢(有Node.js到DBSlayer庫,但他們不工作與最新版本的Node.js

我將如何使用JQuery來獲取此數據和格式HighStock圖表像這樣的:http://www.highcharts.com/stock/demo/multiple-series/gray

基本HighCharts使用CSV文件作爲數據源演示看起來像這樣:

$(function() { 
     var seriesOptions = [], 
      yAxisOptions = [], 
      seriesCounter = 0, 
      names = ['DJI', 'SENTIMENT', 'GOOG', 'GS', 'SENTIMENT-Z', 'DJI-Z'], 
      colors = Highcharts.getOptions().colors; 

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

      $.get(name +'.csv', function(csv, state, xhr) { 

       // inconsistency 
       if (typeof csv != 'string') { 
        csv = xhr.responseText; 
       } 

       // parse the CSV data 
       var data = [], header, comment = /^#/, x; 

       $.each(csv.split('\n'), function(i, line){ 
        if (!comment.test(line)) { 
         if (!header) { 
          header = line; 
         } 
         else { 
          var point = line.split(';'), date = point[0].split('-'); 

          x = Date.UTC(date[2], date[1] - 1, date[0]); 

          if (point.length > 1) { 
           // use point[4], the close value 
           data.push([ 
            x, 
            parseFloat(point[4]) 
           ]); 
          } 
         } 
        } 
       }); 

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

       // create one y axis for each series in order to be able to compare them 
       yAxisOptions[i] = { 
        alternateGridColor: null, 
        gridLineWidth: i ? 0 : 1, // only grid lines for the first series 
        opposite: i ? true : false, 
        minorGridLineWidth: 0, 
        title: { 
         text: name, 
         style: { 
          color: colors[i] 
         } 
        }, 
        lineWidth: 2, 
        lineColor: colors[i] 
       }; 

       // 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++; 

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



     // create the chart when all data is loaded 
     function createChart() { 

      chart = new Highcharts.StockChart({ 
       chart: { 
        renderTo: 'container', 
        alignTicks: false 
       }, 

       rangeSelector: { 
        selected: 1 
       }, 

       title: { 
        text: null 
       }, 

       xAxis: { 
        type: 'datetime', 
        maxZoom: 14 * 24 * 3600000, // fourteen days 
        title: { 
         text: null 
        } 
       }, 
       yAxis: yAxisOptions, 

       series: seriesOptions 
      }); 
     } 

    }); 
    </script> 
    <script type="text/javascript" src="js/themes/gray.js"></script> 

任何示例/代碼將不勝感激

!乾杯!

回答

0

如果您查看您提供的演示頁面的源代碼,可以看到它是如何處理CSV數據的。 JSON結果更容易使用。

更新:documentation向您顯示有關數據外觀的更多信息。

+1

是的,謝謝,我可以看到這一點,已經使用了CSV的例子,它的一切工作,但CSV並不是實時的,所以它沒有多大幫助。我不知道如何使用JSON結果。如果你能提供一個例子說明如何獲取JSON結果並將其解析爲highCharts(最好是刷新),那就太棒了! – NightWolf

+0

$ .get指向一個URL來檢索csv文件。在你的情況下,我會指向一個返回你的json的url,它不必指向一個靜態文件。關於如何做到這一點,highchart網站上有樣本。文檔和支持非常好,所以請查看論壇。另外檢查[這篇文章](http://stackoverflow.com/questions/4210879/reload-chart-data-via-json-with-highcharts)。 – pritaeas