Class for multiple containers on a page. TODO : Window resizing handling.

This commit is contained in:
Phyks 2014-04-19 09:25:40 +02:00
parent dd37d440b3
commit a78a635057
2 changed files with 188 additions and 184 deletions

View File

@ -13,32 +13,114 @@
* --------------------------------------------------------------------------------- * ---------------------------------------------------------------------------------
*/ */
var Timeline = {};
Timeline.ns = "http://www.w3.org/2000/svg";
Timeline.xlinkns = "http://www.w3.org/1999/xlink";
Timeline.marginBottom = 10; /* Initialization :
Timeline.marginTop = 15; * arg is an object with :
Timeline.marginLeft = 10; * id = id of the parent block
Timeline.marginRight = 10; * height / width = size of the svg
Timeline.rounded = false; * grid = small / big / both
Timeline.x_axis = false; * x_axis = true / false to show or hide x axis
Timeline.fill = true; * line = none / line / dashed to choose line type
Timeline.line = 'line'; * rounded = true / false to use splines to smoothen the graph
Timeline.dashed_style = '5, 5'; * x_callback = function(args) { } or false is called to display the legend on the x axis
* fill = true / false to fill below the graph or not
*/
function Timeline(arg) {
this.ns = "http://www.w3.org/2000/svg";
this.xlinkns = "http://www.w3.org/1999/xlink";
Timeline.parent_holder = false; this.marginBottom = 10;
Timeline.holder = false; this.marginTop = 15;
Timeline.g = false; this.marginLeft = 10;
Timeline.axis = false; this.marginRight = 10;
Timeline.graphs = []; this.rounded = false;
Timeline.raw_points = []; this.x_axis = false;
Timeline.x_callback = false; this.fill = true;
this.line = 'line';
this.dashed_style = '5, 5';
this.parent_holder = false;
this.holder = false;
this.g = false;
this.axis = false;
this.graphs = [];
this.raw_points = [];
this.x_callback = false;
if(!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image", "1.1")) {
alert("ERROR : Your browser does not support embedded SVG.");
}
this.parent_holder = document.getElementById(arg.id);
var svg = this.createElement('svg:svg', { 'width': arg.width, 'height': arg.height });
svg.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', this.xlinkns);
this.parent_holder.appendChild(svg);
this.holder = this.parent_holder.querySelector('svg');
defs = this.createElement('defs', {});
this.holder.appendChild(defs);
if(arg.grid === 'small' || arg.grid === 'both') {
var small_grid_pattern = this.createElement('pattern', { 'id': 'smallGrid', 'width': 8, 'height': 8, 'patternUnits': 'userSpaceOnUse' });
var small_grid_path = this.createElement('path', { 'd': 'M 8 0 L 0 0 0 8', 'fill': 'none', 'stroke': 'gray', 'stroke-width': '0.5' });
small_grid_pattern.appendChild(small_grid_path);
defs.appendChild(small_grid_pattern);
}
if(arg.grid === 'big' || arg.grid === 'both') {
var grid_pattern = this.createElement('pattern', { 'id': 'grid', 'width': 80, 'height': 80, 'patternUnits': 'userSpaceOnUse' });
if(arg.grid === 'both') {
var grid_rect = this.createElement('rect', {'width': 80, 'height': 80, 'fill': 'url(#smallGrid)' });
grid_pattern.appendChild(grid_rect);
}
var grid_path = this.createElement('path', {'d': 'M 80 0 L 0 0 0 80', 'fill': 'none', 'stroke': 'gray', 'stroke-width': '1'});
grid_pattern.appendChild(grid_path);
defs.appendChild(grid_pattern);
}
this.grid = arg.grid;
var marker = this.createElement('marker', {'id': 'markerArrow', 'markerWidth': 13, 'markerHeight': 13, 'refX': 2, 'refY': 6, 'orient': 'auto' });
var marker_path = this.createElement('path', {'d': 'M2,2 L2,11 L10,6 L2,2', 'fill': 'gray' });
marker.appendChild(marker_path);
defs.appendChild(marker);
this.g = this.createElement('g', {'transform': 'translate(0, ' + this.parent_holder.offsetHeight + ') scale(1, -1)'});
this.holder.appendChild(this.g);
if(arg.x_axis === true) {
this.axis = this.createElement('line', {'x1': this.marginLeft, 'y1': this.parent_holder.offsetHeight / 2 + 1.5, 'x2': this.parent_holder.offsetWidth - 13 - this.marginRight, 'y2': this.parent_holder.offsetHeight / 2 + 1.5, 'stroke': 'gray', 'stroke-width': 3, 'marker-end': 'url("#markerArrow")'});
this.g.appendChild(this.axis);
}
if(this.grid !== "none") {
var grid = this.createElement('rect', {'width': '100%', 'height': '100%'});
if(this.grid === 'big' || this.grid === 'both') {
grid.setAttribute('fill', 'url(#grid)');
}
else {
grid.setAttribute('fill', 'url(#smallGrid)');
}
this.g.appendChild(grid);
}
this.rounded = arg.rounded;
this.x_axis = arg.x_axis;
this.line = arg.line;
this.fill = arg.fill;
this.x_callback = arg.x_callback;
}
// Create an element "element" with the attributes "attrs" // Create an element "element" with the attributes "attrs"
Timeline.createElement = function (element, attrs) { Timeline.prototype.createElement = function (element, attrs) {
var el = document.createElementNS(Timeline.ns, element); var el = document.createElementNS(this.ns, element);
for(attr in attrs) { for(attr in attrs) {
el.setAttribute(attr, attrs[attr]); el.setAttribute(attr, attrs[attr]);
} }
@ -47,18 +129,18 @@ Timeline.createElement = function (element, attrs) {
}; };
// Check wether the element "element" has class "class" // Check wether the element "element" has class "class"
Timeline.hasClass = function (element, cls) { Timeline.prototype.hasClass = function (element, cls) {
return (' ' + element.getAttribute('class') + ' ').indexOf(' ' + cls + ' ') > -1; return (' ' + element.getAttribute('class') + ' ').indexOf(' ' + cls + ' ') > -1;
}; };
// Add a new graph to the Timeline // Add a new graph to the Timeline
Timeline.addGraph = function (graph, color) { Timeline.prototype.addGraph = function (graph, color) {
Timeline.graphs[graph] = color; this.graphs[graph] = color;
}; };
// Test wether a graph of name "graph" already exists // Test wether a graph of name "graph" already exists
Timeline.hasGraph = function (graph) { Timeline.prototype.hasGraph = function (graph) {
if(typeof(Timeline.graphs[graph]) === 'undefined') { if(typeof(this.graphs[graph]) === 'undefined') {
return false; return false;
} }
else { else {
@ -67,22 +149,22 @@ Timeline.hasGraph = function (graph) {
}; };
// Clear the specified graph data, or completely clear all the graph data // Clear the specified graph data, or completely clear all the graph data
Timeline.clearGraph = function (graph) { Timeline.prototype.clearGraph = function (graph) {
if(typeof(graph) === 'undefined') { if(typeof(graph) === 'undefined') {
Timeline.raw_points = []; this.raw_points = [];
Timeline.graphs = []; this.graphs = [];
} }
else { else {
for(var i = 0; i < Timeline.raw_points.length; i++) { for(var i = 0; i < this.raw_points.length; i++) {
if(Timeline.raw_points[i].graph === graph) { if(this.raw_points[i].graph === graph) {
Timeline.raw_points[i] = undefined; this.raw_points[i] = undefined;
} }
} }
} }
}; };
// Add points to the specified graph // Add points to the specified graph
Timeline.addPoints = function (graph, data) { Timeline.prototype.addPoints = function (graph, data) {
for(var point = 0; point < data.length; point++) { for(var point = 0; point < data.length; point++) {
var insert = {'graph': graph, 'x': data[point].x, 'y': data[point].y}; var insert = {'graph': graph, 'x': data[point].x, 'y': data[point].y};
if(typeof(data[point].label) !== 'undefined') { if(typeof(data[point].label) !== 'undefined') {
@ -97,10 +179,10 @@ Timeline.addPoints = function (graph, data) {
else { else {
insert.click = false; insert.click = false;
} }
Timeline.raw_points.push(insert); this.raw_points.push(insert);
} }
Timeline.raw_points.sort(function (a, b) { this.raw_points.sort(function (a, b) {
if(a.x < b.x) { if(a.x < b.x) {
return -1; return -1;
} }
@ -114,23 +196,24 @@ Timeline.addPoints = function (graph, data) {
}; };
// Compute new coordinates, knowing the min and max value to fit the graph in the container // Compute new coordinates, knowing the min and max value to fit the graph in the container
Timeline.newCoordinate = function(value, min, max, minValue, maxValue) { Timeline.prototype.newCoordinate = function(value, min, max, minValue, maxValue) {
var a = (maxValue - minValue) / (max - min); var a = (maxValue - minValue) / (max - min);
return a *(value - min) + minValue; return a *(value - min) + minValue;
}; };
// Compute new X and Y values // Compute new X and Y values
Timeline.getNewXY = function (minX, maxX, minY, maxY) { Timeline.prototype.getNewXY = function (minX, maxX, minY, maxY) {
var obj = this;
return function (x, y) { return function (x, y) {
return { return {
'x': Timeline.newCoordinate(x, minX, maxX, Timeline.marginLeft, Timeline.parent_holder.offsetWidth - Timeline.marginRight), 'x': obj.newCoordinate(x, minX, maxX, obj.marginLeft, obj.parent_holder.offsetWidth - obj.marginRight),
'y': Timeline.newCoordinate(y, minY, maxY, 2*Timeline.marginBottom, Timeline.parent_holder.offsetHeight - Timeline.marginTop) 'y': obj.newCoordinate(y, minY, maxY, 2*obj.marginBottom, obj.parent_holder.offsetHeight - obj.marginTop)
}; };
}; };
}; };
// Get the necessary control points to smoothen the graph, is rounded is true // Get the necessary control points to smoothen the graph, is rounded is true
Timeline.getControlPoints = function (data) { Timeline.prototype.getControlPoints = function (data) {
// From http://www.particleincell.com/wp-content/uploads/2012/06/bezier-spline.js // From http://www.particleincell.com/wp-content/uploads/2012/06/bezier-spline.js
var p1 = new Array(); var p1 = new Array();
var p2 = new Array(); var p2 = new Array();
@ -185,90 +268,9 @@ Timeline.getControlPoints = function (data) {
return {p1:p1, p2:p2}; return {p1:p1, p2:p2};
}; };
/* Initialization :
* arg is an object with :
* id = id of the parent block
* height / width = size of the svg
* grid = small / big / both
* x_axis = true / false to show or hide x axis
* line = none / line / dashed to choose line type
* rounded = true / false to use splines to smoothen the graph
* x_callback = function(args) { } or false is called to display the legend on the x axis
* fill = true / false to fill below the graph or not
*/
Timeline.init = function (arg) {
if(!document.implementation.hasFeature("http://www.w3.org/TR/Timeline11/feature#Image", "1.1")) {
alert("ERROR : Your browser does not support embedded Timeline.");
}
Timeline.parent_holder = document.getElementById(arg.id);
var svg = Timeline.createElement('svg:svg', { 'width': arg.width, 'height': arg.height });
svg.setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', Timeline.xlinkns);
Timeline.parent_holder.appendChild(svg);
Timeline.holder = Timeline.parent_holder.querySelector('svg');
defs = Timeline.createElement('defs', {});
Timeline.holder.appendChild(defs);
if(arg.grid === 'small' || arg.grid === 'both') {
var small_grid_pattern = Timeline.createElement('pattern', { 'id': 'smallGrid', 'width': 8, 'height': 8, 'patternUnits': 'userSpaceOnUse' });
var small_grid_path = Timeline.createElement('path', { 'd': 'M 8 0 L 0 0 0 8', 'fill': 'none', 'stroke': 'gray', 'stroke-width': '0.5' });
small_grid_pattern.appendChild(small_grid_path);
defs.appendChild(small_grid_pattern);
}
if(arg.grid === 'big' || arg.grid === 'both') {
var grid_pattern = Timeline.createElement('pattern', { 'id': 'grid', 'width': 80, 'height': 80, 'patternUnits': 'userSpaceOnUse' });
if(arg.grid === 'both') {
var grid_rect = Timeline.createElement('rect', {'width': 80, 'height': 80, 'fill': 'url(#smallGrid)' });
grid_pattern.appendChild(grid_rect);
}
var grid_path = Timeline.createElement('path', {'d': 'M 80 0 L 0 0 0 80', 'fill': 'none', 'stroke': 'gray', 'stroke-width': '1'});
grid_pattern.appendChild(grid_path);
defs.appendChild(grid_pattern);
}
Timeline.grid = arg.grid;
var marker = Timeline.createElement('marker', {'id': 'markerArrow', 'markerWidth': 13, 'markerHeight': 13, 'refX': 2, 'refY': 6, 'orient': 'auto' });
var marker_path = Timeline.createElement('path', {'d': 'M2,2 L2,11 L10,6 L2,2', 'fill': 'gray' });
marker.appendChild(marker_path);
defs.appendChild(marker);
Timeline.g = Timeline.createElement('g', {'transform': 'translate(0, ' + Timeline.parent_holder.offsetHeight + ') scale(1, -1)'});
Timeline.holder.appendChild(Timeline.g);
if(arg.x_axis === true) {
Timeline.axis = Timeline.createElement('line', {'x1': Timeline.marginLeft, 'y1': Timeline.parent_holder.offsetHeight / 2 + 1.5, 'x2': Timeline.parent_holder.offsetWidth - 13 - Timeline.marginRight, 'y2': Timeline.parent_holder.offsetHeight / 2 + 1.5, 'stroke': 'gray', 'stroke-width': 3, 'marker-end': 'url("#markerArrow")'});
Timeline.g.appendChild(Timeline.axis);
}
if(Timeline.grid !== "none") {
var grid = Timeline.createElement('rect', {'width': '100%', 'height': '100%'});
if(Timeline.grid === 'big' || Timeline.grid === 'both') {
grid.setAttribute('fill', 'url(#grid)');
}
else {
grid.setAttribute('fill', 'url(#smallGrid)');
}
Timeline.g.appendChild(grid);
}
Timeline.rounded = arg.rounded;
Timeline.x_axis = arg.x_axis;
Timeline.line = arg.line;
Timeline.fill = arg.fill;
Timeline.x_callback = arg.x_callback;
};
// Get the scale so that graph fits with window // Get the scale so that graph fits with window
Timeline.scale = function(data) { Timeline.prototype.scale = function(data) {
var empty = true; var empty = true;
for(graph in data) { for(graph in data) {
empty = false; empty = false;
@ -299,12 +301,12 @@ Timeline.scale = function(data) {
} }
// Scale the grid, if needed // Scale the grid, if needed
var scale = Timeline.getNewXY(minX, maxX, minY, maxY); var scale = this.getNewXY(minX, maxX, minY, maxY);
var tmp = scale(Math.pow(10, Math.floor(Math.log(maxX - minX) / Math.log(10))), Math.pow(10, Math.floor(Math.log(maxY - minY) / Math.log(10)))); var tmp = scale(Math.pow(10, Math.floor(Math.log(maxX - minX) / Math.log(10))), Math.pow(10, Math.floor(Math.log(maxY - minY) / Math.log(10))));
var origin = scale(0, 0); var origin = scale(0, 0);
var coordinates = {'x': tmp.x - origin.x, 'y': tmp.y - origin.y }; var coordinates = {'x': tmp.x - origin.x, 'y': tmp.y - origin.y };
if(Timeline.grid === 'big' || Timeline.grid === 'both') { if(this.grid === 'big' || this.grid === 'both') {
var grid = Timeline.holder.getElementById('grid'); var grid = this.holder.getElementById('grid');
grid.setAttribute('width', coordinates.x); grid.setAttribute('width', coordinates.x);
grid.setAttribute('height', coordinates.y); grid.setAttribute('height', coordinates.y);
var big_coords = scale(Math.floor(minX / Math.pow(10, Math.floor(Math.log(maxX - minX) / Math.log(10)))) * Math.pow(10, Math.floor(Math.log(maxX - minX) / Math.log(10))), Math.floor(minY / Math.pow(10, Math.floor(Math.log(maxY - minY) / Math.log(10)))) * Math.pow(10, Math.floor(Math.log(maxY - minY) / Math.log(10)))); var big_coords = scale(Math.floor(minX / Math.pow(10, Math.floor(Math.log(maxX - minX) / Math.log(10)))) * Math.pow(10, Math.floor(Math.log(maxX - minX) / Math.log(10))), Math.floor(minY / Math.pow(10, Math.floor(Math.log(maxY - minY) / Math.log(10)))) * Math.pow(10, Math.floor(Math.log(maxY - minY) / Math.log(10))));
@ -312,18 +314,18 @@ Timeline.scale = function(data) {
grid.setAttribute('x', big_coords.x); grid.setAttribute('x', big_coords.x);
grid.querySelector('path').setAttribute('d', 'M '+coordinates.x+' 0 L 0 0 0 '+coordinates.y); grid.querySelector('path').setAttribute('d', 'M '+coordinates.x+' 0 L 0 0 0 '+coordinates.y);
if(Timeline.grid === 'both') { if(this.grid === 'both') {
grid.querySelector('rect').setAttribute('width', coordinates.x); grid.querySelector('rect').setAttribute('width', coordinates.x);
grid.querySelector('rect').setAttribute('height', coordinates.y); grid.querySelector('rect').setAttribute('height', coordinates.y);
} }
} }
if(Timeline.grid === 'small' || Timeline.grid === 'both') { if(this.grid === 'small' || this.grid === 'both') {
coordinates.x = coordinates.x / 10; coordinates.x = coordinates.x / 10;
coordinates.y = coordinates.y / 10; coordinates.y = coordinates.y / 10;
var grid = Timeline.holder.getElementById('smallGrid'); var grid = this.holder.getElementById('smallGrid');
grid.setAttribute('width', coordinates.x); grid.setAttribute('width', coordinates.x);
grid.setAttribute('height', coordinates.y); grid.setAttribute('height', coordinates.y);
if(Timeline.grid === 'small') { if(this.grid === 'small') {
var small_coords = scale(Math.floor(minX / Math.pow(10, Math.floor(Math.log(maxX - minX) / Math.log(10)))) * Math.pow(10, Math.floor(Math.log(maxX - minX) / Math.log(10))), Math.floor(minY / Math.pow(10, Math.floor(Math.log(maxY - minY) / Math.log(10)))) * Math.pow(10, Math.floor(Math.log(maxY - minY) / Math.log(10)))); var small_coords = scale(Math.floor(minX / Math.pow(10, Math.floor(Math.log(maxX - minX) / Math.log(10)))) * Math.pow(10, Math.floor(Math.log(maxX - minX) / Math.log(10))), Math.floor(minY / Math.pow(10, Math.floor(Math.log(maxY - minY) / Math.log(10)))) * Math.pow(10, Math.floor(Math.log(maxY - minY) / Math.log(10))));
grid.setAttribute('y', small_coords.y); grid.setAttribute('y', small_coords.y);
grid.setAttribute('x', small_coords.x); grid.setAttribute('x', small_coords.x);
@ -332,41 +334,42 @@ Timeline.scale = function(data) {
} }
/* Draw axis */ /* Draw axis */
if(Timeline.x_axis === true) { if(this.x_axis === true) {
y = scale(0, 0).y; y = scale(0, 0).y;
Timeline.axis.setAttribute('y1', y); this.axis.setAttribute('y1', y);
Timeline.axis.setAttribute('y2', y); this.axis.setAttribute('y2', y);
} }
return scale; return scale;
}; };
// Draw graphs // Draw graphs
Timeline.draw = function() { Timeline.prototype.draw = function() {
var scale = Timeline.scale(Timeline.raw_points); var scale = this.scale(this.raw_points);
var points = [], path; var points = [], path;
var px, py; var px, py;
var element; var element;
var obj = this;
for(var point = 0; point < Timeline.raw_points.length; point++) { for(var point = 0; point < this.raw_points.length; point++) {
var tmp = scale(Timeline.raw_points[point].x, Timeline.raw_points[point].y); var tmp = scale(this.raw_points[point].x, this.raw_points[point].y);
points.push({'id': point, 'x': tmp.x, 'y': tmp.y, 'graph': Timeline.raw_points[point].graph, 'click': Timeline.raw_points[point].click, 'label': Timeline.raw_points[point].label}); points.push({'id': point, 'x': tmp.x, 'y': tmp.y, 'graph': this.raw_points[point].graph, 'click': this.raw_points[point].click, 'label': this.raw_points[point].label});
} }
// Draw each graph // Draw each graph
for(var graph in Timeline.graphs) { for(var graph in this.graphs) {
var filtered_points = points.filter(function(el) { return el.graph == graph; }); var filtered_points = points.filter(function(el) { return el.graph == graph; });
path = ''; path = '';
// Draw line // Draw line
if(Timeline.rounded === true) { if(this.rounded === true) {
var x = new Array(), y = new Array(); var x = new Array(), y = new Array();
for(var point = 0; point < filtered_points.length; point++) { for(var point = 0; point < filtered_points.length; point++) {
x.push(filtered_points[point].x); x.push(filtered_points[point].x);
y.push(filtered_points[point].y); y.push(filtered_points[point].y);
} }
px = Timeline.getControlPoints(x); px = this.getControlPoints(x);
py = Timeline.getControlPoints(y); py = this.getControlPoints(y);
for(var point = 0; point < filtered_points.length - 1; point++) { for(var point = 0; point < filtered_points.length - 1; point++) {
path += 'C '+px.p1[point]+' '+py.p1[point]+' '+px.p2[point]+' '+py.p2[point]+' '+filtered_points[point+1].x+' '+filtered_points[point+1].y+' '; path += 'C '+px.p1[point]+' '+py.p1[point]+' '+px.p2[point]+' '+py.p2[point]+' '+filtered_points[point+1].x+' '+filtered_points[point+1].y+' ';
} }
@ -377,25 +380,25 @@ Timeline.draw = function() {
} }
} }
if(Timeline.line !== 'none') { if(this.line !== 'none') {
element = Timeline.createElement('path', {'class': 'line', 'stroke': Timeline.graphs[graph], 'stroke-width': 2, 'fill': 'none', 'd': 'M '+filtered_points[0].x+' '+filtered_points[0].y+' '+path}); element = this.createElement('path', {'class': 'line', 'stroke': this.graphs[graph], 'stroke-width': 2, 'fill': 'none', 'd': 'M '+filtered_points[0].x+' '+filtered_points[0].y+' '+path});
if(Timeline.line === 'dashed') { if(this.line === 'dashed') {
element.setAttribute('style', 'stroke-dasharray: '+Timeline.dashed_style); element.setAttribute('style', 'stroke-dasharray: '+this.dashed_style);
} }
Timeline.g.appendChild(element); this.g.appendChild(element);
} }
// Draw fill // Draw fill
if(Timeline.fill) { if(this.fill) {
element = Timeline.createElement('path', {'class': 'graph', 'fill': Timeline.graphs[graph], 'opacity': '0.25', 'stroke': 'none', 'd': 'M '+filtered_points[0].x+' '+2*Timeline.marginBottom+' L '+filtered_points[0].x+' '+filtered_points[0].y+' '+ path + ' L '+filtered_points[filtered_points.length - 1].x+' '+2*Timeline.marginBottom+' Z' }); element = this.createElement('path', {'class': 'graph', 'fill': this.graphs[graph], 'opacity': '0.25', 'stroke': 'none', 'd': 'M '+filtered_points[0].x+' '+2*this.marginBottom+' L '+filtered_points[0].x+' '+filtered_points[0].y+' '+ path + ' L '+filtered_points[filtered_points.length - 1].x+' '+2*this.marginBottom+' Z' });
Timeline.g.insertBefore(element, Timeline.g.querySelectorAll('.over')[0]); this.g.insertBefore(element, this.g.querySelectorAll('.over')[0]);
} }
} }
// Hover effect // Hover effect
var prev = 0; var prev = 0;
for(var point = 0; point < points.length;) { for(var point = 0; point < points.length;) {
var rect = Timeline.createElement('rect', {'class': 'over', 'id': 'over_'+point, 'y': 0, 'fill': 'white', 'opacity': 0, 'height': '100%'}); var rect = this.createElement('rect', {'class': 'over', 'id': 'over_'+point, 'y': 0, 'fill': 'white', 'opacity': 0, 'height': '100%'});
var currents = [point]; var currents = [point];
var next = point + 1; var next = point + 1;
@ -420,47 +423,47 @@ Timeline.draw = function() {
} }
if(point == points.length - 1) { if(point == points.length - 1) {
rect.setAttribute('width', Timeline.parent_holder.offsetWidth - (points[point].x + points[point - 1].x)/2 + 1); rect.setAttribute('width', this.parent_holder.offsetWidth - (points[point].x + points[point - 1].x)/2 + 1);
} }
else if(point == 0) { else if(point == 0) {
rect.setAttribute('width', (points[1].x + points[0].x)/2 + Timeline.marginLeft + 1); rect.setAttribute('width', (points[1].x + points[0].x)/2 + this.marginLeft + 1);
} }
else { else {
rect.setAttribute('width', (points[next].x - points[prev].x)/2 + 1); rect.setAttribute('width', (points[next].x - points[prev].x)/2 + 1);
} }
Timeline.g.appendChild(rect); this.g.appendChild(rect);
rect.addEventListener('mouseover', (function(arg) { rect.addEventListener('mouseover', (function(arg) {
return function() { return function() {
for(var i = 0; i < arg.length; i++) { for(var i = 0; i < arg.length; i++) {
Timeline.holder.getElementById('point_'+arg[i]).setAttribute('r', '6'); obj.holder.getElementById('point_'+arg[i]).setAttribute('r', '6');
Timeline.holder.getElementById('label_'+arg[i]).setAttribute('display', 'block'); obj.holder.getElementById('label_'+arg[i]).setAttribute('display', 'block');
} }
}; };
})(currents)); })(currents));
rect.addEventListener('mouseout', function() { rect.addEventListener('mouseout', function() {
// Reinitialize all states // Reinitialize all states
[].forEach.call(Timeline.holder.querySelectorAll('.point'), function(el) { [].forEach.call(obj.holder.querySelectorAll('.point'), function(el) {
el.setAttribute('r', '4'); el.setAttribute('r', '4');
}); });
[].forEach.call(Timeline.holder.querySelectorAll('.label'), function(el) { [].forEach.call(obj.holder.querySelectorAll('.label'), function(el) {
el.setAttribute('display', 'none'); el.setAttribute('display', 'none');
}); });
}); });
if(Timeline.x_callback !== false && points[point].x + 2.5 < Timeline.parent_holder.offsetWidth - Timeline.marginRight) { if(this.x_callback !== false && points[point].x + 2.5 < this.parent_holder.offsetWidth - this.marginRight) {
element = Timeline.createElement('text', {'class': 'legend_x', 'fill': 'gray', 'transform': 'translate(0, ' + Timeline.parent_holder.offsetHeight + ') scale(1, -1)'}); element = this.createElement('text', {'class': 'legend_x', 'fill': 'gray', 'transform': 'translate(0, ' + this.parent_holder.offsetHeight + ') scale(1, -1)'});
element.appendChild(document.createTextNode(Timeline.x_callback(Timeline.raw_points[point].x))); element.appendChild(document.createTextNode(this.x_callback(this.raw_points[point].x)));
Timeline.g.appendChild(element); this.g.appendChild(element);
element.setAttribute('x', points[point].x - element.getBoundingClientRect().width / 2 + 2.5); element.setAttribute('x', points[point].x - element.getBoundingClientRect().width / 2 + 2.5);
var y_zero = scale(0, 0).y; var y_zero = scale(0, 0).y;
element.setAttribute('y', Timeline.parent_holder.offsetHeight - Timeline.marginBottom - y_zero); element.setAttribute('y', this.parent_holder.offsetHeight - this.marginBottom - y_zero);
element = Timeline.createElement('line', {'class': 'legend_x', 'stroke': 'gray', 'stroke-width': 2, 'x1': points[point].x, 'x2': points[point].x, 'y1': y_zero - 5, 'y2': y_zero + 5}); element = this.createElement('line', {'class': 'legend_x', 'stroke': 'gray', 'stroke-width': 2, 'x1': points[point].x, 'x2': points[point].x, 'y1': y_zero - 5, 'y2': y_zero + 5});
Timeline.g.appendChild(element); this.g.appendChild(element);
} }
prev = next - 1; prev = next - 1;
@ -468,12 +471,12 @@ Timeline.draw = function() {
} }
// Draw points and labels // Draw points and labels
for(var graph in Timeline.graphs) { for(var graph in this.graphs) {
var filtered_points = points.filter(function(el) { return el.graph == graph; }); var filtered_points = points.filter(function(el) { return el.graph == graph; });
for(var point = 0; point < filtered_points.length; point++) { for(var point = 0; point < filtered_points.length; point++) {
element = Timeline.createElement('circle', {'class': 'point', 'id': 'point_'+filtered_points[point].id, 'cx': filtered_points[point].x, 'cy': filtered_points[point].y, 'r': 4, 'fill': '#333', 'stroke': Timeline.graphs[graph], 'stroke-width': 2}); element = this.createElement('circle', {'class': 'point', 'id': 'point_'+filtered_points[point].id, 'cx': filtered_points[point].x, 'cy': filtered_points[point].y, 'r': 4, 'fill': '#333', 'stroke': this.graphs[graph], 'stroke-width': 2});
Timeline.g.insertBefore(element, Timeline.g.querySelectorAll('.label')[0]); this.g.insertBefore(element, this.g.querySelectorAll('.label')[0]);
if(filtered_points[point].click !== false) { if(filtered_points[point].click !== false) {
element.onclick = filtered_points[point].click; element.onclick = filtered_points[point].click;
@ -481,63 +484,63 @@ Timeline.draw = function() {
element.addEventListener('mouseover', function() { element.addEventListener('mouseover', function() {
this.setAttribute('r', '6'); this.setAttribute('r', '6');
Timeline.holder.getElementById(this.getAttribute('id').replace('point', 'label')).setAttribute('display', 'block'); obj.holder.getElementById(this.getAttribute('id').replace('point', 'label')).setAttribute('display', 'block');
}); });
if(filtered_points[point].label !== '') { if(filtered_points[point].label !== '') {
var g = Timeline.createElement('g', { 'class': 'label', 'id': 'label_'+filtered_points[point].id, 'transform': 'translate(0, ' + Timeline.parent_holder.offsetHeight + ') scale(1, -1)'}); var g = this.createElement('g', { 'class': 'label', 'id': 'label_'+filtered_points[point].id, 'transform': 'translate(0, ' + this.parent_holder.offsetHeight + ') scale(1, -1)'});
Timeline.g.appendChild(g); this.g.appendChild(g);
g.addEventListener('mouseover', function() { g.addEventListener('mouseover', function() {
Timeline.holder.getElementById(this.getAttribute('id').replace('label', 'point')).setAttribute('r', '6'); obj.holder.getElementById(this.getAttribute('id').replace('label', 'point')).setAttribute('r', '6');
this.setAttribute('display', 'block'); this.setAttribute('display', 'block');
}); });
element = Timeline.createElement('text', {}); element = this.createElement('text', {});
var text = filtered_points[point].label.replace('</sup>', '<sup>').split('<sup>'); var text = filtered_points[point].label.replace('</sup>', '<sup>').split('<sup>');
for(var i = 0; i < text.length; i++) { for(var i = 0; i < text.length; i++) {
text[i] = text[i].replace(/(<([^>]+)>)/ig,"").replace('%y', Timeline.raw_points[filtered_points[point].id].y).replace('%x', Timeline.raw_points[filtered_points[point].id].x); text[i] = text[i].replace(/(<([^>]+)>)/ig,"").replace('%y', this.raw_points[filtered_points[point].id].y).replace('%x', this.raw_points[filtered_points[point].id].x);
if(i % 2 == 0) { if(i % 2 == 0) {
element.appendChild(document.createTextNode(text[i])); element.appendChild(document.createTextNode(text[i]));
} }
else { else {
var tmp = Timeline.createElement('tspan', {'dy': '-5'}); var tmp = this.createElement('tspan', {'dy': '-5'});
tmp.appendChild(document.createTextNode(text[i])); tmp.appendChild(document.createTextNode(text[i]));
element.appendChild(tmp); element.appendChild(tmp);
} }
} }
path = Timeline.createElement('path', {'stroke': 'black', 'stroke-width': 2, 'fill': 'white', 'opacity': 0.5}); path = this.createElement('path', {'stroke': 'black', 'stroke-width': 2, 'fill': 'white', 'opacity': 0.5});
// Append here to have them with the good z-index, update their attributes later // Append here to have them with the good z-index, update their attributes later
g.appendChild(path); g.appendChild(path);
g.appendChild(element); g.appendChild(element);
var x_text = filtered_points[point].x - element.getBoundingClientRect().width / 2; var x_text = filtered_points[point].x - element.getBoundingClientRect().width / 2;
var y_text = Timeline.parent_holder.offsetHeight - filtered_points[point].y - 20; var y_text = this.parent_holder.offsetHeight - filtered_points[point].y - 20;
var element_width = element.getBoundingClientRect().width; var element_width = element.getBoundingClientRect().width;
var element_height = element.getBoundingClientRect().height; var element_height = element.getBoundingClientRect().height;
if(filtered_points[point].x - element.getBoundingClientRect().width / 2 < 0) { if(filtered_points[point].x - element.getBoundingClientRect().width / 2 < 0) {
x_text = filtered_points[point].x + 20; x_text = filtered_points[point].x + 20;
y_text = Timeline.parent_holder.offsetHeight - filtered_points[point].y + 5; y_text = this.parent_holder.offsetHeight - filtered_points[point].y + 5;
path.setAttribute('d', 'M '+(x_text - 5)+' '+(y_text + 5)+' L '+(x_text - 5)+' '+(y_text - element_height/2 + 7.5)+' L '+(x_text - 10)+' '+(y_text - element_height/2 + 5)+' L '+(x_text - 5)+' '+(y_text - element_height/2 + 2.5)+' L '+(x_text - 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text + 5)+' Z'); path.setAttribute('d', 'M '+(x_text - 5)+' '+(y_text + 5)+' L '+(x_text - 5)+' '+(y_text - element_height/2 + 7.5)+' L '+(x_text - 10)+' '+(y_text - element_height/2 + 5)+' L '+(x_text - 5)+' '+(y_text - element_height/2 + 2.5)+' L '+(x_text - 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text + 5)+' Z');
} }
else if(filtered_points[point].y + element.getBoundingClientRect().height + 12 > Timeline.parent_holder.offsetHeight) { else if(filtered_points[point].y + element.getBoundingClientRect().height + 12 > this.parent_holder.offsetHeight) {
x_text = filtered_points[point].x + 20; x_text = filtered_points[point].x + 20;
y_text = Timeline.parent_holder.offsetHeight - filtered_points[point].y + 5; y_text = this.parent_holder.offsetHeight - filtered_points[point].y + 5;
path.setAttribute('d', 'M '+(x_text - 5)+' '+(y_text + 5)+' L '+(x_text - 5)+' '+(y_text - element_height/2 + 7.5)+' L '+(x_text - 10)+' '+(y_text - element_height/2 + 5)+' L '+(x_text - 5)+' '+(y_text - element_height/2 + 2.5)+' L '+(x_text - 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text + 5)+' Z'); path.setAttribute('d', 'M '+(x_text - 5)+' '+(y_text + 5)+' L '+(x_text - 5)+' '+(y_text - element_height/2 + 7.5)+' L '+(x_text - 10)+' '+(y_text - element_height/2 + 5)+' L '+(x_text - 5)+' '+(y_text - element_height/2 + 2.5)+' L '+(x_text - 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text + 5)+' Z');
if(x_text + element_width > Timeline.parent_holder.offsetWidth) { if(x_text + element_width > this.parent_holder.offsetWidth) {
x_text = filtered_points[point].y - element_width - 20; x_text = filtered_points[point].y - element_width - 20;
y_text = Timeline.parent_holder.offsetHeight - filtered_points[point].y + 5; y_text = this.parent_holder.offsetHeight - filtered_points[point].y + 5;
path.setAttribute('d', 'M '+(x_text - 5)+' '+(y_text + 5)+' L '+(x_text - 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height/2 + 2.5)+' L '+(x_text + element_width + 10)+' '+(y_text - element_height/2 + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height/2 + 7.5)+' L '+(x_text + element_width + 5)+' '+(y_text + 5)+' Z'); path.setAttribute('d', 'M '+(x_text - 5)+' '+(y_text + 5)+' L '+(x_text - 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height/2 + 2.5)+' L '+(x_text + element_width + 10)+' '+(y_text - element_height/2 + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height/2 + 7.5)+' L '+(x_text + element_width + 5)+' '+(y_text + 5)+' Z');
} }
} }
else if(filtered_points[point].x + element_width / 2 + 12 > Timeline.parent_holder.offsetWidth) { else if(filtered_points[point].x + element_width / 2 + 12 > this.parent_holder.offsetWidth) {
x_text = filtered_points[point].x - element_width - 20; x_text = filtered_points[point].x - element_width - 20;
y_text = Timeline.parent_holder.offsetHeight - filtered_points[point].y + 5; y_text = this.parent_holder.offsetHeight - filtered_points[point].y + 5;
path.setAttribute('d', 'M '+(x_text - 5)+' '+(y_text + 5)+' L '+(x_text - 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height/2 + 2.5)+' L '+(x_text + element_width + 10)+' '+(y_text - element_height/2 + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height/2 + 7.5)+' L '+(x_text + element_width + 5)+' '+(y_text + 5)+' Z'); path.setAttribute('d', 'M '+(x_text - 5)+' '+(y_text + 5)+' L '+(x_text - 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height/2 + 2.5)+' L '+(x_text + element_width + 10)+' '+(y_text - element_height/2 + 5)+' L '+(x_text + element_width + 5)+' '+(y_text - element_height/2 + 7.5)+' L '+(x_text + element_width + 5)+' '+(y_text + 5)+' Z');
} }
else { else {
@ -553,6 +556,7 @@ Timeline.draw = function() {
} }
}; };
// TODO
var old = window.onresize || function () {}; var old = window.onresize || function () {};
window.onresize = function() { window.onresize = function() {
old(); old();

2
timeline.min.js vendored

File diff suppressed because one or more lines are too long