Better data hiding
This commit is contained in:
parent
b5b6b39401
commit
b7eb24bae4
@ -284,71 +284,24 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
grapher.Grapher = function(params) {
|
grapher.Grapher = function(params) {
|
||||||
this.setColumns = function(columns) {
|
var _canvas = params.canvas;
|
||||||
var scale = 0;
|
var _columns = {};
|
||||||
if (!useLocalScale) {
|
var _data = {};
|
||||||
var hintData = _.pluck(columns, 'hints');
|
var _range = new Range(-1.0, 1.0);
|
||||||
scale = this.computeGlobalScale(hintData);
|
var _steps = params.steps || 20;
|
||||||
}
|
var _useLocalScale = params.useLocalScale || true;
|
||||||
|
var _useRelativeScale = params.useRelativeScale || true;
|
||||||
|
|
||||||
var index = 0;
|
function computeLocalScale(hints) {
|
||||||
for (var name in columns) {
|
|
||||||
var data = this.data[name] = columns[name];
|
|
||||||
if (useLocalScale) {
|
|
||||||
scale = this.computeLocalScale(data.hints);
|
|
||||||
}
|
|
||||||
|
|
||||||
var column = this.columns[name];
|
|
||||||
if (column) {
|
|
||||||
column.update(data, scale);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this.columns[name] = new Column({
|
|
||||||
canvas: this.canvas,
|
|
||||||
width: this.columnWidth,
|
|
||||||
height: this.columnHeight,
|
|
||||||
padding: this.columnPadding,
|
|
||||||
steps: this.steps,
|
|
||||||
range: this.range,
|
|
||||||
data: data,
|
|
||||||
name: name,
|
|
||||||
scale: scale,
|
|
||||||
index: index++,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
this.setUseLocalScale = function(useLocalScale) {
|
|
||||||
if (useLocalScale != this.useLocalScale) {
|
|
||||||
this.useLocalScale = useLocalScale;
|
|
||||||
this.setColumns(this.data);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
this.setUseRelativeScale = function(useRelativeScale) {
|
|
||||||
if (useRelativeScale != this.useRelativeScale) {
|
|
||||||
this.useRelativeScale = useRelativeScale;
|
|
||||||
this.setColumns(this.data);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
this.setValueChangedListener = function(listener) {
|
|
||||||
for (var name in this.columns) {
|
|
||||||
this.columns[name].onValueChanged = listener;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
this.computeLocalScale = function(hints) {
|
|
||||||
var counts = _.pluck(hints, 'count');
|
var counts = _.pluck(hints, 'count');
|
||||||
var min = this.useRelativeScale ? _.min(counts) : 0;
|
var min = _useRelativeScale ? _.min(counts) : 0;
|
||||||
return new Range(min, _.max(counts));
|
return new Range(min, _.max(counts));
|
||||||
};
|
}
|
||||||
|
|
||||||
this.computeGlobalScale = function(hintData) {
|
function computeGlobalScale(hintData) {
|
||||||
var globalScale = null;
|
var globalScale = null;
|
||||||
for (var i = 0, count = hintData.length; i < count; ++i) {
|
for (var i = 0, count = hintData.length; i < count; ++i) {
|
||||||
var localScale = this.computeLocalScale(hintData[i]);
|
var localScale = computeLocalScale(hintData[i]);
|
||||||
if (globalScale) {
|
if (globalScale) {
|
||||||
globalScale.include(localScale);
|
globalScale.include(localScale);
|
||||||
}
|
}
|
||||||
@ -358,14 +311,58 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
return globalScale;
|
return globalScale;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setColumns = function(columns) {
|
||||||
|
var scale = 0;
|
||||||
|
if (!_useLocalScale) {
|
||||||
|
var hintData = _.pluck(columns, 'hints');
|
||||||
|
scale = computeGlobalScale(hintData);
|
||||||
|
}
|
||||||
|
|
||||||
|
var index = 0;
|
||||||
|
for (var name in columns) {
|
||||||
|
var data = _data[name] = columns[name];
|
||||||
|
if (_useLocalScale) {
|
||||||
|
scale = computeLocalScale(data.hints);
|
||||||
|
}
|
||||||
|
|
||||||
|
var column = _columns[name];
|
||||||
|
if (column) {
|
||||||
|
column.update(data, scale);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
_columns[name] = new Column({
|
||||||
|
canvas: _canvas,
|
||||||
|
steps: _steps,
|
||||||
|
range: _range,
|
||||||
|
data: data,
|
||||||
|
name: name,
|
||||||
|
scale: scale,
|
||||||
|
index: index++,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.canvas = params.canvas;
|
this.setUseLocalScale = function(useLocalScale) {
|
||||||
this.columns = {};
|
if (useLocalScale != _useLocalScale) {
|
||||||
this.data = {};
|
_useLocalScale = useLocalScale;
|
||||||
this.range = new Range(-1.0, 1.0);
|
this.setColumns(_data);
|
||||||
this.steps = params.steps || 20;
|
}
|
||||||
this.useLocalScale = params.useLocalScale || true;
|
};
|
||||||
this.useRelativeScale = params.useRelativeScale || true;
|
|
||||||
|
this.setUseRelativeScale = function(useRelativeScale) {
|
||||||
|
if (useRelativeScale != _useRelativeScale) {
|
||||||
|
_useRelativeScale = useRelativeScale;
|
||||||
|
this.setColumns(_data);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.setValueChangedListener = function(listener) {
|
||||||
|
for (var name in _columns) {
|
||||||
|
_columns[name].onValueChanged = listener;
|
||||||
|
}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}(window.grapher = window.grapher || {}));
|
}(window.grapher = window.grapher || {}));
|
||||||
|
Loading…
Reference in New Issue
Block a user