[view/chart][m]: (refs #11) chart now has functioning line graph (much still to do!).
This commit is contained in:
75
src/view.js
75
src/view.js
@@ -119,7 +119,9 @@ recline.FlotGraph = Backbone.View.extend({
|
|||||||
<ul> \
|
<ul> \
|
||||||
<li class="editor-type"> \
|
<li class="editor-type"> \
|
||||||
<label>Graph Type</label> \
|
<label>Graph Type</label> \
|
||||||
<select></select> \
|
<select> \
|
||||||
|
<option value="line">Line</option> \
|
||||||
|
</select> \
|
||||||
</li> \
|
</li> \
|
||||||
<li class="editor-group"> \
|
<li class="editor-group"> \
|
||||||
<label>Group Column (x-axis)</label> \
|
<label>Group Column (x-axis)</label> \
|
||||||
@@ -150,28 +152,61 @@ recline.FlotGraph = Backbone.View.extend({
|
|||||||
</div> \
|
</div> \
|
||||||
',
|
',
|
||||||
|
|
||||||
|
initialize: function(options, chart) {
|
||||||
|
this.el = $(this.el);
|
||||||
|
this.chart = chart;
|
||||||
|
this.chartConfig = {
|
||||||
|
group: null,
|
||||||
|
series: [],
|
||||||
|
graphType: 'line'
|
||||||
|
};
|
||||||
|
var self = this;
|
||||||
|
this.model.getRows().then(function(rows) {
|
||||||
|
self._currentRows = rows;
|
||||||
|
self.render()
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
events: {
|
events: {
|
||||||
'change select': 'onEditorSubmit'
|
// 'change select': 'onEditorSubmit'
|
||||||
},
|
},
|
||||||
|
|
||||||
onEditorSubmit: function(e) {
|
onEditorSubmit: function(e) {
|
||||||
var select = this.el.find('.editor-group select');
|
var select = this.el.find('.editor-group select');
|
||||||
|
this._getEditorData();
|
||||||
|
this.plot.setData(this.createSeries());
|
||||||
|
this.plot.resize();
|
||||||
|
this.plot.setupGrid();
|
||||||
|
this.plot.draw();
|
||||||
|
},
|
||||||
|
_getEditorData: function() {
|
||||||
|
$editor = this
|
||||||
|
var series = this.$series.map(function () {
|
||||||
|
return $(this).val();
|
||||||
|
});
|
||||||
|
this.chartConfig.series = $.makeArray(series)
|
||||||
|
this.chartConfig.group = this.el.find('.editor-group select').val();
|
||||||
},
|
},
|
||||||
|
|
||||||
initialize: function(options, chart) {
|
|
||||||
this.el = $(this.el);
|
|
||||||
this.chart = chart;
|
|
||||||
this.render();
|
|
||||||
},
|
|
||||||
toTemplateJSON: function() {
|
toTemplateJSON: function() {
|
||||||
return this.model.toJSON();
|
return this.model.toJSON();
|
||||||
},
|
},
|
||||||
|
|
||||||
render: function() {
|
render: function() {
|
||||||
htmls = $.mustache(this.template, this.toTemplateJSON());
|
htmls = $.mustache(this.template, this.toTemplateJSON());
|
||||||
$(this.el).html(htmls);
|
$(this.el).html(htmls);
|
||||||
|
// now set a load of stuff up
|
||||||
this.$graph = this.el.find('.panel.graph');
|
this.$graph = this.el.find('.panel.graph');
|
||||||
|
// event approach did not seem to work
|
||||||
|
this.$series = this.el.find('.editor-series select');
|
||||||
|
this.$seriesClone = this.$series.parent().clone();
|
||||||
|
var self = this;
|
||||||
|
this.el.find('form select').change(function() {
|
||||||
|
self.onEditorSubmit.apply(self, arguments)
|
||||||
|
});
|
||||||
return this;
|
return this;
|
||||||
},
|
},
|
||||||
|
|
||||||
createPlot: function () {
|
createPlot: function () {
|
||||||
// only lines for the present
|
// only lines for the present
|
||||||
options = {
|
options = {
|
||||||
@@ -183,12 +218,13 @@ recline.FlotGraph = Backbone.View.extend({
|
|||||||
},
|
},
|
||||||
|
|
||||||
createSeries: function () {
|
createSeries: function () {
|
||||||
var series = [], view = this;
|
var self = this;
|
||||||
if (this.chart) {
|
var series = [];
|
||||||
$.each(this.chart.series, function (seriesIndex, field) {
|
if (this.chartConfig) {
|
||||||
|
$.each(this.chartConfig.series, function (seriesIndex, field) {
|
||||||
var points = [];
|
var points = [];
|
||||||
$.each(view.data, function (index) {
|
$.each(self._currentRows, function (index) {
|
||||||
var x = this[view.chart.groups], y = this[field];
|
var x = this[self.chartConfig.group], y = this[field];
|
||||||
if (typeof x === 'string') {
|
if (typeof x === 'string') {
|
||||||
x = index;
|
x = index;
|
||||||
}
|
}
|
||||||
@@ -198,5 +234,20 @@ recline.FlotGraph = Backbone.View.extend({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
return series;
|
return series;
|
||||||
|
},
|
||||||
|
|
||||||
|
// TODO: finish porting this function
|
||||||
|
addSeries: function () {
|
||||||
|
var element = this.seriesClone.clone(),
|
||||||
|
label = element.find('label'),
|
||||||
|
index = this.series.length;
|
||||||
|
|
||||||
|
this.el.$series.parent().find('ul').append(element);
|
||||||
|
this.updateSeries();
|
||||||
|
|
||||||
|
label.append('<a href="#remove">Remove</a>');
|
||||||
|
label.find('span').text(String.fromCharCode(this.series.length + 64));
|
||||||
|
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user