1

Cleanup of grapher

This commit is contained in:
Alex Yatskov 2014-10-31 11:00:43 +09:00
parent 6e30c691a5
commit e18efdf0a2

View File

@ -97,6 +97,11 @@
return value; return value;
}; };
this.include = function(range) {
this.start = Math.min(this.start, range.start);
this.end = Math.max(this.end, range.end);
};
} }
@ -226,134 +231,87 @@
// Grapher // Grapher
// //
grapher.Grapher = function(canvas, range, columnWidth, useLocalScale, useRelativeScale) { grapher.Grapher = function(params) {
this.setColumns = function(columns) { this.setColumns = function(columns) {
this.clearColumns();
var scale = 0; var scale = 0;
if (!useLocalScale) { if (!useLocalScale) {
var hintData = {}; var hintData = _.pluck(columns, 'hints');
_.each(columns, function(columnValue, columnName) { scale = this.computeGlobalScale(hintData);
hintData[columnName] = columnValue.hints || [];
});
scale = this.getGlobalScale(hintData);
} }
var index = 0; for (var name in columns) {
var that = this; var data = columns[name];
_.each(columns, function(columnValue, columnName) {
if (useLocalScale) { if (useLocalScale) {
scale = that.getLocalScale(columnValue.hints); scale = this.computeLocalScale(column.hints);
} }
that.columns.push(new Column(that.canvas, columnName, columnValue, scale, that.range)); var column = this.columns[name];
that.indexMap[columnName] = index++; if (column) {
column.update({
data: data,
scale: scale
}); });
};
this.clearColumns = function() {
_.each(this.columns, function(column) {
column.clearShapes();
});
this.columns = [];
this.indexMap = {};
};
this.updateColumns = function(data) {
var scale = 0;
if (!this.useLocalScale) {
scale = this.getGlobalScale(hintData);
} }
else {
var that = this; this.columns.push(new Column({
_.each(data, function(entry, name) { canvas: this.canvas,
if (that.useLocalScale) { data: column,
scale = that.getLocalScale(entry.hints); name: name,
range: this.range,
scale: scale,
}));
}
} }
var index = that.getColumnIndex(name);
var column = that.columns[index];
column.updateParams(entry, scale);
});
}; };
this.setUseLocalScale = function(useLocalScale) { this.setUseLocalScale = function(useLocalScale) {
if (useLocalScale != this.useLocalScale) { if (useLocalScale != this.useLocalScale) {
this.useLocalScale = useLocalScale; this.useLocalScale = useLocalScale;
this.invalidateHints(); this.updateColumns(this.columnData);
} }
}; };
this.setUseRelativeScale = function(useRelativeScale) { this.setUseRelativeScale = function(useRelativeScale) {
if (useRelativeScale != this.useRelativeScale) { if (useRelativeScale != this.useRelativeScale) {
this.useRelativeScale = useRelativeScale; this.useRelativeScale = useRelativeScale;
this.invalidateHints(); this.updateColumns(this.columnData);
} }
}; };
this.invalidateHints = function() {
var hintData = {};
_.each(this.columns, function(column) {
hintData[column.name] = column.hints;
});
this.setColumnHints(hintData);
};
this.setValueChangedListener = function(listener) { this.setValueChangedListener = function(listener) {
_.each(this.columns, function(column) { for (var name in this.columns) {
column.onValueChanged = listener; this.columns[name].onValueChanged = listener;
}); }
}; };
this.getLocalScale = function(hints) { this.computeLocalScale = function(hints) {
var counts = _.pluck(hints, 'count'); var counts = _.pluck(hints, 'count');
var min = this.useRelativeScale ? _.min(counts) : 0; var min = this.useRelativeScale ? _.min(counts) : 0;
return new Range(min, _.max(counts)); return new Range(min, _.max(counts));
}; };
this.getGlobalScale = function(hintData) { this.computeGlobalScale = function(hintData) {
var that = this;
var globalScale = null; var globalScale = null;
for (var i = 0, count = hintData.length; i < count; ++i) {
_.each(hintData, function(hints) { var localScale = this.computeLocalScale(hintData[i]);
var localScale = that.getLocalScale(hints);
if (globalScale) { if (globalScale) {
globalScale.includeRange(localScale); globalScale.include(localScale);
} }
else { else {
globalScale = localScale; globalScale = localScale;
} }
}); }
return globalScale; return globalScale;
}; };
this.getColumnCount = function() { this.canvas = params.canvas;
return this.columns.length; this.columnData = null;
}; this.columnPadding = 10;
this.columnRange = new Range(-1.0, 1.0);
this.getColumnIndex = function(name) { this.columnWidth = 100;
return this.indexMap[name]; this.columns = {};
}; this.useLocalScale = params.useLocalScale || true;
this.useRelativeScale = params.useRelativeScale || true;
this.getColumnName = function(index) {
return this.columns[index].name;
};
this.getColumnNames = function() {
return _.pluck(this.columns, 'name');
};
this.useLocalScale = useLocalScale;
this.useRelativeScale = useRelativeScale;
this.columnWidth = columnWidth;
this.canvas = null;
this.range = new Range(range.min, range.max);
this.padding = 10;
this.indexMap = {};
this.columns = [];
}; };
}(window.grapher = window.grapher || {})); }(window.grapher = window.grapher || {}));