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

400 lines
13 KiB
JavaScript
Raw Normal View History

2014-10-03 02:43:56 +00:00
/*
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
*
* 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-10-03 02:43:56 +00:00
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
//
// Range
//
2014-09-27 01:05:48 +00:00
2014-11-08 02:48:10 +00:00
function Range(min, max) {
this.min = Math.min(min, max);
this.max = Math.max(min, max);
2014-09-27 01:05:48 +00:00
2014-11-05 07:47:20 +00:00
this.contains = function(value) {
2014-11-08 02:48:10 +00:00
return value >= this.min && value <= this.max;
2014-09-27 01:10:45 +00:00
};
2014-09-27 01:05:48 +00:00
this.length = function() {
2014-11-08 02:48:10 +00:00
return this.max - this.min;
2014-09-27 01:10:45 +00:00
};
2014-09-27 01:05:48 +00:00
2014-09-27 01:10:45 +00:00
this.clamp = function(value) {
2014-11-08 02:48:10 +00:00
if (value < this.min) {
return this.min;
2014-09-27 01:10:45 +00:00
}
2014-09-27 01:05:48 +00:00
2014-11-08 02:48:10 +00:00
if (value > this.max) {
return this.max;
2014-09-27 01:10:45 +00:00
}
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) {
2014-11-08 02:48:10 +00:00
this.min = Math.min(this.min, range.min);
this.max = Math.max(this.max, range.max);
2014-10-31 02:00:43 +00:00
};
2014-11-05 08:48:07 +00:00
this.offset = function(value) {
2014-11-08 02:48:10 +00:00
return (value - this.min) / this.length();
};
2014-11-05 08:48:07 +00:00
this.project = function(value) {
2014-11-08 02:48:10 +00:00
return this.min + this.length() * value;
2014-11-05 08:48:07 +00:00
};
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) {
2015-03-21 11:46:20 +00:00
var _backdropColor = '#eeeeec';
var _borderColor = '#babdb6';
var _fillColorNeg = '#3465a4';
var _fillColorPos = '#cc0000';
var _panelColor = '#babdb6';
var _tickColor = '#888a85';
var _densitySize = 10;
var _desatOffset = -0.3;
var _height = 500;
var _padding = 5;
var _panelSize = 20;
var _tickSize = 5;
var _width = 100;
var _easeTime = 400;
2014-10-31 08:00:12 +00:00
2014-11-09 03:31:52 +00:00
var _animation = null;
var _canvas = params.canvas;
var _data = params.data;
2014-11-05 09:31:00 +00:00
var _index = params.index;
var _name = params.name;
2014-11-09 02:10:12 +00:00
var _valueAnimated = params.data.value;
2014-11-05 09:31:00 +00:00
var _onValueChanged = params.onValueChanged;
var _range = params.range;
2014-11-05 09:31:00 +00:00
var _scale = params.scale;
var _steps = params.steps;
2014-11-05 11:14:15 +00:00
var _elements = {};
2014-11-01 06:43:03 +00:00
function createShapes() {
2014-11-05 08:48:07 +00:00
// backdrop
2014-11-01 06:43:03 +00:00
_elements.backdrop = _canvas.rect(
2014-11-05 07:48:54 +00:00
_tickSize, 0, _width - (_densitySize + _tickSize), _height - _panelSize
2014-11-05 11:14:15 +00:00
).attr({cursor: 'crosshair', stroke: _borderColor, fill: _backdropColor}).click(clicked);
2014-11-01 06:43:03 +00:00
2014-11-05 08:48:07 +00:00
// density
2014-11-05 07:48:54 +00:00
_elements.density = _canvas.rect(
_width - _densitySize, 0, _densitySize, _height - _panelSize
2014-11-05 11:14:15 +00:00
).attr({stroke: _borderColor});
2014-11-05 07:48:54 +00:00
2014-11-05 08:48:07 +00:00
// panel
2014-11-05 07:48:54 +00:00
_elements.panel = _canvas.rect( _tickSize,
_height - _panelSize, _width - _tickSize, _panelSize
2014-11-05 11:14:15 +00:00
).attr({fill: _panelColor});
2014-11-05 07:48:54 +00:00
2014-11-05 08:48:07 +00:00
// label
2014-11-05 07:48:54 +00:00
_elements.label = _canvas.text(
_tickSize + (_width - _tickSize) / 2, _height - _panelSize / 2, _name
).attr({'dominant-baseline': 'middle', 'text-anchor': 'middle'});
2014-11-05 08:48:07 +00:00
// indicator
2014-11-08 11:10:51 +00:00
var range = computeIndicatorRange(_data.value);
_elements.indicator = _canvas.rect(
2014-11-08 02:48:10 +00:00
_tickSize, range.min, _width - (_densitySize + _tickSize), (range.max - range.min)
2014-11-10 10:31:42 +00:00
).attr({cursor: 'crosshair', fill: computeIndicatorColor(_data.value)}).click(clicked);
2014-11-01 06:43:03 +00:00
2014-11-05 08:48:07 +00:00
// tick
2014-11-05 07:47:20 +00:00
if (_range.contains(0.0)) {
var origin = valueToIndicator(0.0);
2014-11-05 09:31:00 +00:00
_elements.tick = _canvas.line(0, origin, _width - _densitySize, origin
2014-11-05 11:14:15 +00:00
).attr({stroke: _tickColor});
2014-11-05 07:47:20 +00:00
}
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
);
2014-11-05 11:14:15 +00:00
_elements.group.transform(
Snap.format('t{x},{y}', {x: _index * (_width + _padding), y: 0})
);
2014-11-08 11:10:51 +00:00
updateDensity();
2014-11-01 06:43:03 +00:00
}
2014-10-31 08:00:12 +00:00
2014-11-08 11:10:51 +00:00
function updateIndicator(value) {
var range = computeIndicatorRange(value);
2014-11-05 08:48:07 +00:00
_elements.indicator.attr({
2014-11-08 02:48:10 +00:00
y: range.min,
height: range.max - range.min,
2014-11-08 11:10:51 +00:00
fill: computeIndicatorColor(value)
2014-11-05 07:47:20 +00:00
});
2014-11-09 02:10:12 +00:00
_valueAnimated = value;
2014-10-31 08:00:12 +00:00
}
2014-07-08 04:35:52 +00:00
2014-11-08 11:10:51 +00:00
function updateDensity() {
2015-03-21 11:46:20 +00:00
var fill = _backdropColor;
if (_data.hints.length > 0) {
fill = _canvas.gradient(decimateHints());
}
_elements.density.attr({fill: fill});
2014-11-08 11:10:51 +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) {
2014-11-08 02:48:10 +00:00
colorPercent = Math.max(0, groupSize - _scale.min) / _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) {
2014-11-08 02:48:10 +00:00
var stepMax = _range.max - stepSize * i;
2014-09-27 01:10:45 +00:00
var stepMin = stepMax - stepSize;
2014-07-08 04:35:52 +00:00
2015-03-21 11:46:20 +00:00
var hintValue = 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) {
2015-03-21 11:46:20 +00:00
hintValue += hint.rating;
2014-09-27 01:10:45 +00:00
}
2014-09-28 07:58:56 +00:00
}
2014-07-08 04:35:52 +00:00
2015-03-21 11:46:20 +00:00
hintGroups.push(hintValue);
2014-09-27 01:10:45 +00:00
}
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-05 11:14:15 +00:00
function updateValue(value) {
2014-11-08 11:10:51 +00:00
_data.value = _range.clamp(value);
if (_onValueChanged) {
2014-11-08 11:10:51 +00:00
_onValueChanged(_name, _data.value);
2014-09-27 01:10:45 +00:00
}
2014-11-10 10:14:45 +00:00
animateIndicator(_valueAnimated, _data.value);
2014-11-08 10:53:13 +00:00
}
2014-11-08 11:10:51 +00:00
function animateIndicator(valueOld, valueNew) {
2014-11-09 03:31:52 +00:00
if (valueOld === valueNew) {
return;
}
if (_animation !== null) {
_animation.stop();
}
_animation = Snap.animate(
2014-11-08 11:10:51 +00:00
valueOld,
valueNew,
2014-11-08 10:53:13 +00:00
function(value) {
2014-11-08 11:10:51 +00:00
updateIndicator(value);
2014-11-08 10:53:13 +00:00
},
_easeTime,
2014-11-10 10:14:45 +00:00
mina.easeinout,
2014-11-08 10:53:13 +00:00
function() {
2014-11-09 03:31:52 +00:00
_animation = null;
2014-11-08 10:53:13 +00:00
}
);
2014-10-31 08:00:12 +00:00
}
2014-07-08 04:35:52 +00:00
2014-11-08 11:10:51 +00:00
function valueColorAdjust(value, color, offset) {
2014-09-27 01:10:45 +00:00
var colorObj = tinycolor(color);
2014-11-08 11:10:51 +00:00
var rangeEnd = value >= 0.0 ? _range.max : _range.min;
2014-11-08 02:48:10 +00:00
var rangeMid = (_range.min + _range.max) / 2.0;
2014-11-08 11:10:51 +00:00
var rangeRat = (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-11-08 11:10:51 +00:00
function computeIndicatorColor(value) {
var color = value >= 0.0 ? _fillColorPos : _fillColorNeg;
return valueColorAdjust(value, color, _desatOffset);
2014-10-31 08:00:12 +00:00
}
2014-11-08 11:10:51 +00:00
function computeIndicatorRange(value) {
return new Range(valueToIndicator(0.0), valueToIndicator(value));
}
function valueToIndicator(scalar) {
var box = _elements.backdrop.getBBox();
2014-11-05 08:48:07 +00:00
var offset = _range.offset(scalar);
return box.y + box.height * (1.0 - offset);
}
2014-11-05 08:48:07 +00:00
function indicatorToValue(scalar) {
var box = _elements.backdrop.getBBox();
var range = new Range(box.y, box.y + box.height);
return -_range.project(range.offset(scalar));
}
function clicked(event, x, y) {
var rect = _canvas.node.getBoundingClientRect();
updateValue(indicatorToValue(y - rect.top));
2014-11-05 08:48:07 +00:00
}
2014-10-31 08:00:12 +00:00
this.update = function(data, scale) {
2014-11-08 11:10:51 +00:00
_scale = scale;
2014-11-08 10:53:13 +00:00
2014-11-10 10:14:45 +00:00
if (_.has(data, 'value')) {
_data.value = data.value;
animateIndicator(_valueAnimated, _data.value);
}
if (_.has(data, 'hints')) {
_data.hints = data.hints;
updateDensity();
}
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) {
var _canvas = params.canvas;
var _columns = {};
var _data = {};
var _range = new Range(params.range.min || -1.0, params.range.max || 1.0);
var _steps = params.steps || 20;
var _useLocalScale = params.useLocalScale || false;
var _displayType = params.displayType || 'density';
var _onValueChanged = params.onValueChanged;
2014-10-31 07:32:10 +00:00
2015-03-21 11:46:20 +00:00
function processHintParameters(columns) {
var displayTypes = {compatibility: 'compatibility', density: 'count'};
var statKey = displayTypes[_displayType];
for (var name in columns) {
var column = columns[name];
for (var i = 0, count = column.hints.length; i < count; ++i) {
column.hints[i].rating = column.hints[i].stats[statKey];
}
}
2014-10-31 07:32:10 +00:00
}
2015-03-21 11:52:47 +00:00
function computeLocalScale(columnData) {
var ratings = _.map(columnData.hints, function(hint) {
2015-03-21 11:46:20 +00:00
return hint.rating;
});
return new Range(0, _.max(ratings));
2015-03-21 11:46:20 +00:00
}
2015-03-21 11:52:47 +00:00
function computeGlobalScale(columnsData) {
2014-10-31 07:32:10 +00:00
var globalScale = null;
2015-03-22 02:19:09 +00:00
for (var name in columnsData) {
var localScale = computeLocalScale(columnsData[name]);
2014-10-31 07:32:10 +00:00
if (globalScale) {
globalScale.include(localScale);
}
else {
globalScale = localScale;
}
}
return globalScale;
}
2015-03-21 11:52:47 +00:00
this.setColumns = function(columnsData) {
processHintParameters(columnsData);
2015-03-21 11:46:20 +00:00
var scale = null;
2014-10-31 07:32:10 +00:00
if (!_useLocalScale) {
2015-03-21 11:52:47 +00:00
scale = computeGlobalScale(columnsData);
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;
2015-03-21 11:52:47 +00:00
for (var name in columnsData) {
var columnData = _data[name] = columnsData[name];
2014-10-31 07:32:10 +00:00
if (_useLocalScale) {
2015-03-21 11:52:47 +00:00
scale = computeLocalScale(columnData);
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) {
2015-03-21 11:52:47 +00:00
column.update(columnData, 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,
2015-03-21 11:52:47 +00:00
data: columnData,
2014-10-31 07:36:20 +00:00
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
2015-03-21 11:46:20 +00:00
this.setDisplayType = function(displayType) {
if (displayType != _displayType) {
_displayType = displayType;
this.setColumns(_data);
}
};
2014-09-28 07:58:56 +00:00
};
2014-09-27 01:10:45 +00:00
}(window.grapher = window.grapher || {}));