1
restaurant-search/client/scripts/grapher.js

381 lines
12 KiB
JavaScript
Raw Normal View History

2014-10-03 02:43:56 +00:00
/*
The MIT License (MIT)
Copyright (c) 2014 Alex Yatskov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
2014-09-27 01:10:45 +00:00
(function(grapher) {
2014-09-28 07:58:56 +00:00
'use strict';
2014-09-27 01:10:45 +00:00
//
// Coord
//
2014-09-27 01:05:48 +00:00
2014-09-27 01:10:45 +00:00
function Coord(x, y) {
this.x = x;
this.y = y;
}
2014-09-27 01:05:48 +00:00
2014-09-27 01:10:45 +00:00
//
// Rect
//
2014-09-27 01:05:48 +00:00
2014-09-27 01:10:45 +00:00
function Rect(left, top, width, height) {
this.left = left;
this.top = top;
this.width = width;
this.height = height;
this.right = left + width;
this.bottom = top + height;
2014-09-27 01:05:48 +00:00
2014-09-27 01:10:45 +00:00
this.contains = function(coord) {
var contained =
coord.x >= this.left &&
coord.x < this.right &&
coord.y >= this.top &&
coord.y < this.bottom;
2014-09-27 01:05:48 +00:00
2014-09-27 01:10:45 +00:00
return contained;
};
2014-09-27 01:05:48 +00:00
2014-09-27 01:10:45 +00:00
this.intersection = function(rect) {
var left = Math.max(this.left, rect.left);
var top = Math.max(this.top, rect.top);
var right = Math.min(this.right, rect.right);
var bottom = Math.min(this.bottom, rect.bottom);
2014-09-27 01:05:48 +00:00
2014-09-27 01:10:45 +00:00
return new Rect(left, top, right - left, bottom - top);
};
}
2014-09-27 01:05:48 +00:00
2014-09-27 01:10:45 +00:00
//
// Range
//
2014-09-27 01:05:48 +00:00
2014-09-27 01:10:45 +00:00
function Range(start, end) {
this.start = Math.min(start, end);
this.end = Math.max(start, end);
2014-09-27 01:05:48 +00:00
2014-11-05 07:47:20 +00:00
this.contains = function(value) {
2014-09-27 01:10:45 +00:00
return value >= this.start && value <= this.end;
};
2014-09-27 01:05:48 +00:00
this.length = function() {
2014-09-27 01:10:45 +00:00
return this.end - this.start;
};
2014-09-27 01:05:48 +00:00
2014-09-27 01:10:45 +00:00
this.clamp = function(value) {
if (value < this.start) {
return this.start;
}
2014-09-27 01:05:48 +00:00
2014-09-27 01:10:45 +00:00
if (value > this.end) {
return this.end;
}
2014-09-27 01:05:48 +00:00
2014-09-27 01:10:45 +00:00
return value;
2014-09-28 07:58:56 +00:00
};
2014-10-31 02:00:43 +00:00
this.include = function(range) {
this.start = Math.min(this.start, range.start);
this.end = Math.max(this.end, range.end);
};
this.offset = function(value, clamp) {
if (clamp) {
value = this.clamp(value);
}
return (value - this.start) / this.length();
};
2014-09-27 01:05:48 +00:00
}
2014-07-08 04:35:52 +00:00
2014-09-27 01:10:45 +00:00
//
// Column
//
2014-07-08 04:35:52 +00:00
2014-10-31 02:15:51 +00:00
function Column(params) {
2014-10-31 08:00:12 +00:00
var _emptyColor = '#eeeeec';
var _strokeColor = '#d3d7cf';
var _tickColor = '#888a85';
var _fillColorNeg = '#3465a4';
var _fillColorPos = '#cc0000';
var _handleColorNeg = '#204a87';
var _handleColorPos = '#a40000';
var _desatOffset = -0.3;
var _handleSize = 10;
var _densitySize = 10;
var _panelSize = 20;
var _tickSize = 5;
var _width = 125;
var _height = 500;
var _padding = 10;
2014-11-01 06:43:03 +00:00
var _canvas = params.canvas;
var _name = params.name;
var _data = params.data;
var _scale = params.scale;
var _range = params.range;
var _steps = params.steps;
var _index = params.index;
var _elements = {};
function createShapes() {
_elements.gradient = _canvas.gradient(decimateHints());
_elements.backdrop = _canvas.rect(
2014-11-05 07:48:54 +00:00
_tickSize, 0, _width - (_densitySize + _tickSize), _height - _panelSize
2014-11-01 06:43:03 +00:00
).attr({'stroke': '#d3d7cf', 'fill': '#eeeeec'});
2014-11-05 07:48:54 +00:00
_elements.density = _canvas.rect(
_width - _densitySize, 0, _densitySize, _height - _panelSize
).attr({'stroke': '#d3d7cf', 'fill': _elements.gradient});
_elements.panel = _canvas.rect( _tickSize,
_height - _panelSize, _width - _tickSize, _panelSize
).attr({'fill': '#d3d7cf'});
_elements.label = _canvas.text(
_tickSize + (_width - _tickSize) / 2, _height - _panelSize / 2, _name
).attr({'dominant-baseline': 'middle', 'text-anchor': 'middle'});
var range = computeIndicatorRange();
_elements.indicator = _canvas.rect(
2014-11-05 07:48:54 +00:00
_tickSize, range.start, _width - (_densitySize + _tickSize), (range.end - range.start)
).attr({'fill': computeFillColor()});
2014-11-01 06:43:03 +00:00
2014-11-05 07:47:20 +00:00
if (_range.contains(0.0)) {
var origin = valueToIndicator(0.0);
2014-11-05 07:48:54 +00:00
_elements.tick = _canvas.line( 0, origin, _tickSize, origin
2014-11-05 07:47:20 +00:00
).attr({'stroke': '#888a85'});
}
2014-11-01 06:43:03 +00:00
_elements.group = _canvas.group(
_elements.backdrop,
_elements.indicator,
2014-11-01 06:43:03 +00:00
_elements.density,
_elements.panel,
_elements.tick,
_elements.label
);
_elements.group.transform(Snap.format('t{x},{y}', {x: _index * (_width + _padding), y: 0}));
}
2014-10-31 08:00:12 +00:00
function updateShapes() {
2014-11-05 07:47:20 +00:00
_elements.gradient = _canvas.gradient(decimateHints());
_elements.density.attr('fill', _elements.gradient);
2014-11-01 06:43:03 +00:00
2014-11-05 07:47:20 +00:00
var range = computeIndicatorRange();
_elements.indicator.params({
'y': range.start,
'height': range.end - range.start,
'fill': computeFillColor()
});
2014-10-31 08:00:12 +00:00
}
2014-07-08 04:35:52 +00:00
2014-10-31 08:00:12 +00:00
function decimateHints() {
2014-10-31 06:58:25 +00:00
var colorStops = 'l(0,0,0,1)';
2014-10-31 07:08:47 +00:00
2014-10-31 08:00:12 +00:00
var groups = groupHints();
2014-10-31 02:15:51 +00:00
for (var i = 0, count = groups.length; i < count; ++i) {
var groupSize = groups[i];
2014-09-27 01:10:45 +00:00
var colorPercent = 0;
if (_scale.length() > 0) {
colorPercent = Math.max(0, groupSize - _scale.start) / _scale.length();
2014-09-27 01:10:45 +00:00
}
2014-07-08 04:35:52 +00:00
2014-09-27 01:10:45 +00:00
var colorByte = 0xff - Math.min(0xff, Math.round(0xff * colorPercent));
var colorObj = tinycolor({ r: colorByte, g: colorByte, b: colorByte });
var colorStr = colorObj.toHexString();
2014-07-08 04:35:52 +00:00
2014-10-31 06:58:25 +00:00
colorStops += colorStr;
if (i + 1 < count) {
colorStops += '-';
}
2014-10-31 02:15:51 +00:00
}
2014-07-08 04:35:52 +00:00
2014-09-27 01:10:45 +00:00
return colorStops;
2014-10-31 08:00:12 +00:00
}
2014-07-08 04:35:52 +00:00
2014-10-31 08:00:12 +00:00
function groupHints() {
var stepSize = _range.length() / _steps;
2014-07-08 04:35:52 +00:00
2014-09-27 01:10:45 +00:00
var hintGroups = [];
2014-10-31 08:00:12 +00:00
for (var i = 0; i < _steps; ++i) {
var stepMax = _range.end - stepSize * i;
2014-09-27 01:10:45 +00:00
var stepMin = stepMax - stepSize;
2014-07-08 04:35:52 +00:00
2014-09-27 01:10:45 +00:00
var hintCount = 0;
2014-10-31 08:00:12 +00:00
for (var j = 0, count = _data.hints.length; j < count; ++j) {
var hint = _data.hints[j];
2014-09-27 01:10:45 +00:00
if (hint.sample > stepMin && hint.sample <= stepMax) {
hintCount += hint.count;
}
2014-09-28 07:58:56 +00:00
}
2014-07-08 04:35:52 +00:00
2014-09-27 01:10:45 +00:00
hintGroups.push(hintCount);
}
2014-07-08 04:35:52 +00:00
2014-09-27 01:10:45 +00:00
return hintGroups;
2014-10-31 08:00:12 +00:00
}
2014-07-08 04:35:52 +00:00
2014-11-01 06:43:03 +00:00
function setClampedValue(value, final) {
_data.value = _range.clamp(value);
2014-07-08 04:35:52 +00:00
2014-11-01 06:43:03 +00:00
if (final && onValueChanged) {
onValueChanged(_name, _data.value);
2014-09-27 01:10:45 +00:00
}
2014-07-08 04:35:52 +00:00
2014-10-31 08:00:12 +00:00
updateShapes();
}
2014-07-08 04:35:52 +00:00
2014-10-31 08:00:12 +00:00
function valueColorAdjust(color, offset) {
2014-09-27 01:10:45 +00:00
var colorObj = tinycolor(color);
var rangeEnd = _data.value >= 0.0 ? _range.end : _range.start;
2014-10-31 08:00:12 +00:00
var rangeMid = (_range.start + _range.end) / 2.0;
var rangeRat = (_data.value - rangeMid) / (rangeEnd - rangeMid);
2014-09-27 01:10:45 +00:00
var desatVal = Math.max(0.0, 1.0 - rangeRat + offset) * 100.0;
return colorObj.desaturate(desatVal).toHexString();
2014-10-31 08:00:12 +00:00
}
2014-10-31 08:00:12 +00:00
function computeFillColor() {
var color = _data.value >= 0.0 ? _fillColorPos : _fillColorNeg;
2014-10-31 08:00:12 +00:00
return valueColorAdjust(color, _desatOffset);
}
2014-10-31 08:00:12 +00:00
function computeHandleColor() {
var color = _data.value >= 0.0 ? _handleColorPos : _handleColorNeg;
2014-10-31 08:00:12 +00:00
return valueColorAdjust(color, _desatOffset);
}
function computeIndicatorRange() {
return new Range(valueToIndicator(0.0), valueToIndicator(_data.value));
}
function valueToIndicator(scalar) {
var box = _elements.backdrop.getBBox();
var ratio = box.height / _range.length();
var offset = _range.offset(scalar, true);
return box.y + box.height * (1.0 - offset);
}
2014-10-31 08:00:12 +00:00
this.update = function(data, scale) {
_data = data;
_scale = scale;
updateShapes();
2014-09-27 01:10:45 +00:00
};
2014-11-01 06:43:03 +00:00
createShapes();
2014-07-08 04:35:52 +00:00
}
2014-09-27 01:10:45 +00:00
//
// Grapher
//
2014-07-08 04:35:52 +00:00
2014-10-31 02:00:43 +00:00
grapher.Grapher = function(params) {
2014-10-31 07:32:10 +00:00
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;
2014-10-31 07:36:20 +00:00
var _onValueChanged = params.onValueChanged;
2014-10-31 07:32:10 +00:00
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;
}
2014-09-27 01:10:45 +00:00
this.setColumns = function(columns) {
var scale = 0;
2014-10-31 07:32:10 +00:00
if (!_useLocalScale) {
2014-10-31 02:00:43 +00:00
var hintData = _.pluck(columns, 'hints');
2014-10-31 07:32:10 +00:00
scale = computeGlobalScale(hintData);
2014-09-27 01:10:45 +00:00
}
2014-07-08 04:35:52 +00:00
2014-10-31 06:25:59 +00:00
var index = 0;
2014-10-31 02:00:43 +00:00
for (var name in columns) {
2014-10-31 07:32:10 +00:00
var data = _data[name] = columns[name];
if (_useLocalScale) {
scale = computeLocalScale(data.hints);
2014-09-27 01:10:45 +00:00
}
2014-07-08 04:35:52 +00:00
2014-10-31 07:32:10 +00:00
var column = _columns[name];
2014-10-31 02:00:43 +00:00
if (column) {
2014-10-31 02:15:51 +00:00
column.update(data, scale);
2014-09-27 01:10:45 +00:00
}
2014-10-31 02:00:43 +00:00
else {
2014-10-31 07:32:10 +00:00
_columns[name] = new Column({
2014-10-31 07:36:20 +00:00
onValueChanged: _onValueChanged,
steps: _steps,
range: _range,
canvas: _canvas,
data: data,
name: name,
scale: scale,
index: index++,
2014-10-31 02:51:11 +00:00
});
2014-10-31 02:00:43 +00:00
}
}
2014-09-28 07:58:56 +00:00
};
2014-07-08 04:35:52 +00:00
2014-09-27 01:10:45 +00:00
this.setUseLocalScale = function(useLocalScale) {
2014-10-31 07:32:10 +00:00
if (useLocalScale != _useLocalScale) {
_useLocalScale = useLocalScale;
this.setColumns(_data);
2014-09-27 01:10:45 +00:00
}
2014-09-28 07:58:56 +00:00
};
2014-07-08 04:35:52 +00:00
2014-09-27 01:10:45 +00:00
this.setUseRelativeScale = function(useRelativeScale) {
2014-10-31 07:32:10 +00:00
if (useRelativeScale != _useRelativeScale) {
_useRelativeScale = useRelativeScale;
this.setColumns(_data);
2014-09-27 01:10:45 +00:00
}
2014-09-28 07:58:56 +00:00
};
};
2014-09-27 01:10:45 +00:00
}(window.grapher = window.grapher || {}));