我正在使用Flot在我的Sproutcore應用程序上顯示圖表。
要使用它,你需要在frameworks文件夾內創建一個flot目錄,其中包括jquery.flot.js文件(我還包括jquery.flot.pie.js文件)和一個core.js文件與此內容:
sc_require('jquery.flot.js');
sc_require('jquery.flot.pie.js');
Flot = SC.Object.create({
plot: $.plot
}) ;
然後,你需要這個新的庫添加到您的構建文件:
config :yourapp,
:required => ['flot']
要在應用中展示您的圖表,你可以使用我發來解決這個自定義視圖與Flot:
GX.FlotView = SC.View.extend({
classNames: ['flot'],
//ex: [{ data: [[1326366000000, 1500], [1326452400000, 600]], label: 'title of the serie' }, ...]
data: [],
/*
ex: {
legend: { show: true },
series: line, points,
xaxis: { mode: "time", timeformat: "%d/%m/%y", }
grid: { backgroundColor: { colors: ["#FFF", "#fefefe"]}},
}
*/
options: [],
render: function(context, firstTime) {
var frame = this.get('frame'),
data = this.get('data'),
options = this.get('options');
// To avoid an error with flot, we check if width and height > 0
if(frame.width > 0 && frame.height > 0 && data && data.length) {
var layer = this.get('layer'),
plot = Flot.plot(layer, data, options);
}
},
viewDidResize: function() {
this.invokeLast(function() {
if (this.get('isVisibleInWindow')) this.set('layerNeedsUpdate', YES);
});
}.observes('data', 'data.[]'),
});
然後,您只需將數據和選項屬性與數據綁定即可。