[#334,flot,bugfix][s]: find and resolve issue with no ticks on x-axis if values are floats - fixes #334.

* Change to explicitly signal the Labels / Dates case (where x values are indices into records array)
* No longer always set xticks - let flot do this except in indices case
This commit is contained in:
Rufus Pollock
2013-03-08 10:30:02 +00:00
parent cf115ef79a
commit 7ec2fdb278

View File

@@ -159,14 +159,19 @@ my.Flot = Backbone.View.extend({
var xtype = xfield.get('type'); var xtype = xfield.get('type');
var isDateTime = (xtype === 'date' || xtype === 'date-time' || xtype === 'time'); var isDateTime = (xtype === 'date' || xtype === 'date-time' || xtype === 'time');
if (this.model.records.models[parseInt(x, 10)]) { if (this.xvaluesAreIndex) {
x = this.model.records.models[parseInt(x, 10)].get(this.state.attributes.group); x = parseInt(x, 10);
if (isDateTime) { // HACK: deal with bar graph style cases where x-axis items were strings
x = new Date(x).toLocaleDateString(); // In this case x at this point is the index of the item in the list of
} // records not its actual x-axis value
} else if (isDateTime) { x = this.model.records.models[x].get(this.state.attributes.group);
x = new Date(parseInt(x, 10)).toLocaleDateString();
} }
if (isDateTime) {
x = new Date(x).toLocaleDateString();
}
// } else if (isDateTime) {
// x = new Date(parseInt(x, 10)).toLocaleDateString();
// }
return x; return x;
}, },
@@ -201,31 +206,16 @@ my.Flot = Backbone.View.extend({
var xaxis = {}; var xaxis = {};
xaxis.tickFormatter = tickFormatter; xaxis.tickFormatter = tickFormatter;
// calculate the x-axis ticks // for labels case we only want ticks at the label intervals
// // HACK: however we also get this case with Date fields. In that case we
// the number of ticks should be a multiple of the number of points so that // could have a lot of values and so we limit to max 30 (we assume)
// each tick lines up with a point if (this.xvaluesAreIndex) {
if (numPoints) { var numTicks = Math.min(this.model.records.length, 15);
var ticks = [], var increment = this.model.records.length / numTicks;
maxTicks = 10, var ticks = [];
x = 1, for (i=0; i<numTicks; i++) {
i = 0; ticks.push(parseInt(i*increment));
// show all ticks in bar graphs
// for other graphs only show up to maxTicks ticks
if (self.state.attributes.graphType !== 'bars') {
while (x <= maxTicks) {
if ((numPoints / x) <= maxTicks) {
break;
}
x = x + 1;
}
} }
for (i = 0; i < numPoints; i = i + x) {
ticks.push(i);
}
xaxis.ticks = ticks; xaxis.ticks = ticks;
} }
@@ -310,6 +300,7 @@ my.Flot = Backbone.View.extend({
createSeries: function() { createSeries: function() {
var self = this; var self = this;
self.xvaluesAreIndex = false;
var series = []; var series = [];
_.each(this.state.attributes.series, function(field) { _.each(this.state.attributes.series, function(field) {
var points = []; var points = [];
@@ -323,11 +314,13 @@ my.Flot = Backbone.View.extend({
var isDateTime = (xtype === 'date' || xtype === 'date-time' || xtype === 'time'); var isDateTime = (xtype === 'date' || xtype === 'date-time' || xtype === 'time');
if (isDateTime) { if (isDateTime) {
self.xvaluesAreIndex = true;
x = index; x = index;
} else if (typeof x === 'string') { } else if (typeof x === 'string') {
x = parseFloat(x); x = parseFloat(x);
if (isNaN(x)) { if (isNaN(x)) { // assume this is a string label
x = index; x = index;
self.xvaluesAreIndex = true;
} }
} }