1

Better data hiding

This commit is contained in:
Alex Yatskov 2014-10-31 16:32:10 +09:00
parent b5b6b39401
commit b7eb24bae4

View File

@ -284,32 +284,58 @@
// //
grapher.Grapher = function(params) { grapher.Grapher = function(params) {
var _canvas = params.canvas;
var _columns = {};
var _data = {};
var _range = new Range(-1.0, 1.0);
var _steps = params.steps || 20;
var _useLocalScale = params.useLocalScale || true;
var _useRelativeScale = params.useRelativeScale || true;
function computeLocalScale(hints) {
var counts = _.pluck(hints, 'count');
var min = _useRelativeScale ? _.min(counts) : 0;
return new Range(min, _.max(counts));
}
function computeGlobalScale(hintData) {
var globalScale = null;
for (var i = 0, count = hintData.length; i < count; ++i) {
var localScale = computeLocalScale(hintData[i]);
if (globalScale) {
globalScale.include(localScale);
}
else {
globalScale = localScale;
}
}
return globalScale;
}
this.setColumns = function(columns) { this.setColumns = function(columns) {
var scale = 0; var scale = 0;
if (!useLocalScale) { if (!_useLocalScale) {
var hintData = _.pluck(columns, 'hints'); var hintData = _.pluck(columns, 'hints');
scale = this.computeGlobalScale(hintData); scale = computeGlobalScale(hintData);
} }
var index = 0; var index = 0;
for (var name in columns) { for (var name in columns) {
var data = this.data[name] = columns[name]; var data = _data[name] = columns[name];
if (useLocalScale) { if (_useLocalScale) {
scale = this.computeLocalScale(data.hints); scale = computeLocalScale(data.hints);
} }
var column = this.columns[name]; var column = _columns[name];
if (column) { if (column) {
column.update(data, scale); column.update(data, scale);
} }
else { else {
this.columns[name] = new Column({ _columns[name] = new Column({
canvas: this.canvas, canvas: _canvas,
width: this.columnWidth, steps: _steps,
height: this.columnHeight, range: _range,
padding: this.columnPadding,
steps: this.steps,
range: this.range,
data: data, data: data,
name: name, name: name,
scale: scale, scale: scale,
@ -320,52 +346,23 @@
}; };
this.setUseLocalScale = function(useLocalScale) { this.setUseLocalScale = function(useLocalScale) {
if (useLocalScale != this.useLocalScale) { if (useLocalScale != _useLocalScale) {
this.useLocalScale = useLocalScale; _useLocalScale = useLocalScale;
this.setColumns(this.data); this.setColumns(_data);
} }
}; };
this.setUseRelativeScale = function(useRelativeScale) { this.setUseRelativeScale = function(useRelativeScale) {
if (useRelativeScale != this.useRelativeScale) { if (useRelativeScale != _useRelativeScale) {
this.useRelativeScale = useRelativeScale; _useRelativeScale = useRelativeScale;
this.setColumns(this.data); this.setColumns(_data);
} }
}; };
this.setValueChangedListener = function(listener) { this.setValueChangedListener = function(listener) {
for (var name in this.columns) { for (var name in _columns) {
this.columns[name].onValueChanged = listener; _columns[name].onValueChanged = listener;
} }
}; };
this.computeLocalScale = function(hints) {
var counts = _.pluck(hints, 'count');
var min = this.useRelativeScale ? _.min(counts) : 0;
return new Range(min, _.max(counts));
};
this.computeGlobalScale = function(hintData) {
var globalScale = null;
for (var i = 0, count = hintData.length; i < count; ++i) {
var localScale = this.computeLocalScale(hintData[i]);
if (globalScale) {
globalScale.include(localScale);
}
else {
globalScale = localScale;
}
}
return globalScale;
};
this.canvas = params.canvas;
this.columns = {};
this.data = {};
this.range = new Range(-1.0, 1.0);
this.steps = params.steps || 20;
this.useLocalScale = params.useLocalScale || true;
this.useRelativeScale = params.useRelativeScale || true;
}; };
}(window.grapher = window.grapher || {})); }(window.grapher = window.grapher || {}));