2011-10-03 27 views
0

我想將我的YUI2基於Flash的圖表轉換爲純JavaScript實現。我已經使用YUI DataSource定義了一個數據源,但是我一直無法從中提取我需要的數據來填充我的圖表。 我的代碼如下所示:使用YUI數據源填充HighCharts折線圖

function setupChart(e) { 

var dataSource = new YAHOO.util.DataSource(document.location.href + '/index/charts'); 
dataSource.responseType = YAHOO.util.DataSource.TYPE_JSARRAY; 
dataSource.responseSchema = { 
fields: ['date', 'cust_view', 'cust_upd', 'notes', 'mydata_comp', 'mydata_not_completable'] }; 

var mychart = new Highcharts.Chart({ 
chart: { 
    renderTo: 'chart', 
    defaultSeriesType: 'line' 
}, 
title: { text: null }, 
xAxis: { 
    type: 'datetime', 
    tickInterval: 7 * 24 * 3600 * 1000 // one week 
}, 

yAxis: { 
    title: { text: null } 
}, 

series: [ 
    { name: 'Viewed', data: 'cust_view' } 
    { name: 'Updated', data: 'cust_upd' } 
    { name: 'Notes Created', data: 'notes' }, 
    { name: 'myData Completions', data: 'mydata_comp' }, 
    { name: 'myData marked as Incompletable', data: 'mydata_not_completable' } ] 


}); 
} 

所以,我知道該數據源擁有我想要的價值,但我不知道如何格式化HighCharts語法把它弄出來的存在。

關於SO的第一個問題,抱歉,如果不清楚。

+0

歡迎StackOverflow上。請務必提供您認爲有幫助的答案(包括解答其他人的問題)。對於您自己的問題,請記住「檢查」解決問題的答案。 –

回答

0

您錯過了從服務器檢索數據的步驟。

這是通過Ajax調用完成的。該數據將在您提供的回調函數中可用。您將在回調函數中創建圖表不在主線中。例如

 dataSource.sendRequest("get_data.php", 
      { 
      success: function (req,res) { 
        // res.results holds the results. Use 
         // firebug to see its format 
         // The format is an array of JS objects. 
         // Each element of the array is an object with 
         // properties from your fields configuration. 
         // 
         // Eg 
      [ 
      { date: "xxx", cust_view: "red",  notes: "apples", ... }, 
      { date: "xxx", cust_view: "green", notes: "vegetable", ... }, 
      { date: "xxx", cust_view: "cherries", notes: "fruit", ... }, 
      ... 
      ]        
         // create the Highcharts.chart and add the data to it 
         // here 
        } 

       failure: function() { 
         alert("Couldn't contact server"); 
         } 
      }); 

見的例子來自YUI docs

+0

IndexController中有一個名爲chartsAction的函數,它在我的代碼的第二行中被引用。它從MySQL視圖中提取數據值。我不確定get_data.php文件中發生了什麼。我正在嘗試你的代碼。 〜謝謝 – mintwhip

+0

get_data.php只是一個示例url。它看起來像你的url是'document.location.href +'/ index/charts''因爲你已經設置了它,所以當你調用sendRequest方法時你可能會使用null。看到文檔,我沒有使用過這部分數據Source –

+0

非常感謝Larry。 '空'工作,我打電話給我現在需要的價值觀。我在這方面掙扎得太久了。如果你不介意的話,我可能會寫一篇博文。 :) – mintwhip