diff --git a/dist/vis.js b/dist/vis.js index 439353b1..8704181c 100644 --- a/dist/vis.js +++ b/dist/vis.js @@ -5530,16 +5530,14 @@ Linegraph.prototype.setData = function() { var dataset = this._extractData(datapoints); var data = dataset.data; - console.log("height",data,datapoints, dataset); - this.yAxis.setRange({start:dataset.range.low,end:dataset.range.high}); this.yAxis.repaint(); data = this.yAxis.convertValues(data); var d, d2, d3; - d = this._catmullRom(data,0.5); - d3 = this._catmullRom(data,0); - d2 = this._catmullRom(data,1); + d = this._catmullRom(data,0.5); // centripetal + d3 = this._catmullRom(data,0); // uniform + d2 = this._catmullRom(data,1); // chordial // var data2 = []; diff --git a/src/3dgraph/doc/.gitignore b/src/3dgraph/doc/.gitignore new file mode 100644 index 00000000..30d1e49a --- /dev/null +++ b/src/3dgraph/doc/.gitignore @@ -0,0 +1 @@ +jsdoc diff --git a/src/3dgraph/doc/default.css b/src/3dgraph/doc/default.css new file mode 100644 index 00000000..f0c251df --- /dev/null +++ b/src/3dgraph/doc/default.css @@ -0,0 +1,87 @@ +html, body { + width: 100%; + height: 100%; + padding: 0; + margin: 0; +} + +body, td, th { + font-family: arial, sans-serif; + font-size: 11pt; + color: #4D4D4D; + line-height: 1.7em; +} + +#container { + margin: 0 auto; + padding-bottom: 50px; + width: 900px; +} + +h1 { + font-size: 180%; + font-weight: bold; + padding: 0; + margin: 1em 0 1em 0; +} + +h2 { + padding-top: 20px; + padding-bottom: 10px; + border-bottom: 1px solid #a0c0f0; + color: #2B7CE9; +} + +h3 { + font-size: 140%; +} + + +a { + color: #2B7CE9; + text-decoration: none; +} +a:visited { + color: #2E60A4; +} +a:hover { + color: red; + text-decoration: underline; +} + +hr { + border: none 0; + border-top: 1px solid #abc; + height: 1px; +} + +pre { + display: block; + font-size: 10pt; + line-height: 1.5em; + font-family: monospace; +} + +pre, code { + background-color: #f5f5f5; +} + +table +{ + border-collapse: collapse; +} + +th { + font-weight: bold; + border: 1px solid lightgray; + background-color: #E5E5E5; + text-align: left; + vertical-align: top; + padding: 5px; +} + +td { + border: 1px solid lightgray; + padding: 5px; + vertical-align: top; +} diff --git a/src/3dgraph/doc/example.html b/src/3dgraph/doc/example.html new file mode 100644 index 00000000..b09d4929 --- /dev/null +++ b/src/3dgraph/doc/example.html @@ -0,0 +1,68 @@ + + + + Graph 3D demo + + + + + + + + + + +
+ + diff --git a/src/3dgraph/doc/graph3d.png b/src/3dgraph/doc/graph3d.png new file mode 100644 index 00000000..72fc45cd Binary files /dev/null and b/src/3dgraph/doc/graph3d.png differ diff --git a/src/3dgraph/doc/graph3d120x60.png b/src/3dgraph/doc/graph3d120x60.png new file mode 100644 index 00000000..9d44dd30 Binary files /dev/null and b/src/3dgraph/doc/graph3d120x60.png differ diff --git a/src/3dgraph/doc/index.html b/src/3dgraph/doc/index.html new file mode 100644 index 00000000..d822f0ad --- /dev/null +++ b/src/3dgraph/doc/index.html @@ -0,0 +1,770 @@ + + + + Graph3d documentation + + + + + + + +
+ +

Graph3d documentation

+ + + + + + + + + + + + + + + +
AuthorJos de Jong, Almende B.V.
WebpageChap Links Library
License Apache License, Version 2.0
+ + +

Contents

+ + +

Overview

+

+ Graph3d is an interactive visualization chart to draw data in a three dimensional + graph. You can freely move and zoom in the graph by dragging and scrolling in the + window. + Graph3d also supports animation of a graph. +

+ +

+ The graph works smooth on any modern browser for data up to 10.000 points. +

+ +

+ The Graph is developed as a Google Visualization Chart in javascript. + + It runs on all modern browsers without additional requirements. + Graph is tested on Firefox 3.6+, Safari 5.0+, Chrome 6.0+, Opera 10.6+, + Internet Explorer 9+. +

+ +

+ To get started with Graph3d, download + graph3d.zip, + and unzip the contents in a subfolder graph3d on your site. + + Examples can be found in the + examples directory. + + The possiblities of Graph3d can be tested on the + playground. +

+ +

Example

+

+ Here a graph example. Click and drag to move the graph, scroll to zoom the graph. +

+ +

+ +

+
+<!DOCTYPE HTML PUBLIC
+     "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
+<html>
+  <head>
+    <title>Graph 3D demo</title>
+
+    <style>
+      body {font: 10pt arial;}
+    </style>
+
+    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
+    <script type="text/javascript" src="../graph3d.js"></script>
+
+    <script type="text/javascript">
+      var data = null;
+      var graph = null;
+
+      google.load("visualization", "1");
+
+      // Set callback to run when API is loaded
+      google.setOnLoadCallback(drawVisualization);
+
+      function custom(x, y) {
+        return Math.sin(x/50) * Math.cos(y/50) * 50 + 50;
+      }
+
+      // Called when the Visualization API is loaded.
+      function drawVisualization() {
+        // Create and populate a data table.
+        data = new google.visualization.DataTable();
+        data.addColumn('number', 'x');
+        data.addColumn('number', 'y');
+        data.addColumn('number', 'value');
+
+        // create some nice looking data with sin/cos
+        var steps = 25;  // number of datapoints will be steps*steps
+        var axisMax = 314;
+        axisStep = axisMax / steps;
+        for (var x = 0; x < axisMax; x+=axisStep) {
+          for (var y = 0; y < axisMax; y+=axisStep) {
+            var value = custom(x,y);
+            data.addRow([x, y, value]);
+          }
+        }
+
+        // specify options
+        options = {width:  "400px",
+                   height: "400px",
+                   style: "surface",
+                   showPerspective: true,
+                   showGrid: true,
+                   showShadow: false,
+                   keepAspectRatio: true,
+                   verticalRatio: 0.5
+                   };
+
+        // Instantiate our graph object.
+        graph = new links.Graph3d(document.getElementById('mygraph'));
+
+        // Draw our graph with the created data and options
+        graph.draw(data, options);
+      }
+   </script>
+  </head>
+
+  <body>
+    <div id="mygraph"></div>
+  </body>
+</html>
+
+ + +

Loading

+

+ Graph3d is no built-in visualization of Google. + + To load Graph3d, download the file + graph3d.zip, + and unzip it in your html page such that the files are located in a subfolder graph3d. + Include the google API and the following files in the head of your html code: +

+ +
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
+<script type="text/javascript" src="graph/graph3d.js"></script>
+ +

+ The google visualization needs to be loaded in order to use DataTable. +

+
google.load("visualization", "1");
+google.setOnLoadCallback(drawVisualization);
+function drawVisualization() {
+  // load data and create the graph here
+}
+
+ +The class name of the Graph3d is links.Graph3d +
var graph = new links.Graph3d(container);
+ +After being loaded, the graph can be drawn via the function draw(), +provided with data and options. +
graph.draw(data, options);
+where data is a DataTable, and options is a name-value map in the JSON format. + +

Data Format

+

+ Graph3d requires a data table with tree to five columns, depending on the chosen style + and animation. + + The first three columns must contain the location coordinates for x-axis, + y-axis, and z-axis. + + The forth column is optional, and can contain a data value. + + The last column (this can be the fourth or fifth column) and can contain + filter values used for animation. +

+ +

+ Note that the column labels can be changed (for example instead of 'value' + one can use 'Temperature'). +

+ + +

Definition

+

+ The data columns are defined as: +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeRequiredDescription
xnumberyesLocation on the x-axis.
ynumberyesLocation on the y-axis.
znumberyesLocation on the z-axis.
valuenumbernoThe data value, required for graph styles dot-color and + dot-size. +
filtervalueanytypenoFilter values used for the animation. + This column may have any type, such as a number, string, or Date.
+ +

Construction of 3D data

+

+ Graph3d supports the following styles to display data in three dimensions: + dot, dot-line, line, grid, + and surface. + + The data table for these styles is constructed as follows. +

+ +
+var data = new google.visualization.DataTable();
+data.addColumn('number', 'x');
+data.addColumn('number', 'y');
+data.addColumn('number', 'z');  // the data value, visualized as a height at the z-axis
+                                // and by a color.
+
+data.addRow([2.3, 5.2, 102.1]);
+// ...
+
+ + +

Construction of 4D data

+

+ Graph3d supports the following styles to display data in four dimensions: + dot-color, dot-size. + + The data table for these styles is constructed as follows. +

+ +
+var data = new google.visualization.DataTable();
+data.addColumn('number', 'x');
+data.addColumn('number', 'y');
+data.addColumn('number', 'z');
+data.addColumn('number', 'value');  // the data value, visualized by a color or size
+
+data.addRow([2.3, 5.2, 102.1, 45.2]);
+// ...
+
+ + +

Construction of animation data

+

+ If an extra column with filter values is provided in the data table, Graph3d + will use these values for animation. + + The filter values are grouped and each group represents one animation frame. + + When animating, Graph3d loops through the distinct filter values, + and draws all data points which have the current filter value. +

+

+ For example, to create an animation with three frames, first add all datapoints + for the first frame with a filtervalue 1. Next, add the datapoints for the + second frame and give them a filtervalue 2. Last, add the datapoints for the + third frame and give them a filtervalue of 3. Now, when the Graph3d is loaded, + a slider with buttons play/next/previous will be drawn below the graph, where one can loop through the + three frames. +

+ +

+ To create an animation, add an extra column to the data table. + This column may have any type (number, date, string, ...). +

+ +
+var data = new google.visualization.DataTable();
+data.addColumn('number', 'x');
+data.addColumn('number', 'y');
+data.addColumn('number', 'value');
+data.addColumn('number', 'filtervalue'); // Optional column with filter values for animation
+
+data.addRow([2.3, 5.2, 102.1, 23]);
+// ...
+
+ + +

Configuration Options

+ +

+ Options can be used to customize the graph. Options are defined as a JSON object. + All options are optional. +

+ +
+options = {
+    width:  "100%",
+    height: "400px",
+    style: "surface"
+};
+
+ +

+ The following options are available. +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDefaultDescription
animationIntervalnumber1000The animation interval in milliseconds. This determines how fast + the animation runs.
animationPreloadbooleanfalseIf false, the animation frames are loaded as soon as they are requested. + if animationPreload is true, the graph will automatically load + all frames in the background, resulting in a smoother animation as soon as + all frames are loaded. The load progress is shown on screen.
animationAutoStartbooleanfalseIf true, the animation starts playing automatically after the graph + is created.
backgroundColorstring or Object"white"The background color for the main area of the chart. + Can be either a simple HTML color string, for example: 'red' or '#00cc00', + or an object with the following properties.
backgroundColor.strokestring"gray"The color of the chart border, as an HTML color string.
backgroundColor.strokeWidthnumber1The border width, in pixels.
backgroundColor.fillstring"white"The chart fill color, as an HTML color string.
cameraPositionObject{"horizontal": 1.0, "vertical": 0.5, "distance": 1.7}Set the initial rotation and position of the camera. + The object cameraPosition contains three parameters: + horizontal, vertical, and distance. + Parameter horizontal is a value in radians and can have any + value (but normally in the range of 0 and 2*Pi). + Parameter vertical is a value in radians between 0 and 0.5*Pi. + Parameter distance is the (normalized) distance from the + camera to the center of the graph, in the range of 0.71 to 5.0. A + larger distance puts the graph further away, making it smaller. + All parameters are optional. +
heightstring"400px"The height of the graph in pixels or as a percentage.
keepAspectRatiobooleantrueIf keepAspectRatio is true, the x-axis and the y-axis + keep their aspect ratio. If false, the axes are scaled such that they + both have the same, maximum with.
showAnimationControlsbooleantrueIf true, animation controls are created at the bottom of the Graph. + The animation controls consists of buttons previous, start/stop, next, + and a slider showing the current frame. + Only applicable when the provided data contains an animation.
showGridbooleantrueIf true, grid lines are draw in the x-y surface (the bottom of the 3d + graph).
showPerspectivebooleantrueIf true, the graph is drawn in perspective: points and lines which + are further away are drawn smaller. + Note that the graph currently does not support a gray colored bottom side + when drawn in perspective. +
showShadowbooleanfalseShow shadow on the graph.
stylestring"dot"The style of the 3d graph. Available styles: + bar, + bar-color, + bar-size, + dot, + dot-line, + dot-color, + dot-size, + line, + grid, + or surface
tooltipboolean | functionfalseShow a tooltip showing the values of the hovered data point. + The contents of the tooltip can be customized by providing a callback + function as tooltip. In this case the function is called + with an object containing parameters x, + y, and z argument, + and must return a string which may contain HTML. +
valueMaxnumbernoneThe maximum value for the value-axis. Only available in combination + with the styles dot-color and dot-size.
valueMinnumbernoneThe minimum value for the value-axis. Only available in combination + with the styles dot-color and dot-size.
verticalRationumber0.5A value between 0.1 and 1.0. This scales the vertical size of the graph + When keepAspectRatio is set to false, and verticalRatio is set to 1.0, + the graph will be a cube.
widthstring"400px"The width of the graph in pixels or as a percentage.
xBarWidthnumbernoneThe width of bars in x direction. By default, the width is equal to the distance + between the data points, such that bars adjoin each other. + Only applicable for styles "bar" and "bar-color".
xCenterstring"55%"The horizontal center position of the graph, as a percentage or in + pixels.
xMaxnumbernoneThe maximum value for the x-axis.
xMinnumbernoneThe minimum value for the x-axis.
xStepnumbernoneStep size for the grid on the x-axis.
yBarWidthnumbernoneThe width of bars in y direction. By default, the width is equal to the distance + between the data points, such that bars adjoin each other. + Only applicable for styles "bar" and "bar-color".
yCenterstring"45%"The vertical center position of the graph, as a percentage or in + pixels.
yMaxnumbernoneThe maximum value for the y-axis.
yMinnumbernoneThe minimum value for the y-axis.
yStepnumbernoneStep size for the grid on the y-axis.
zMinnumbernoneThe minimum value for the z-axis.
zMaxnumbernoneThe maximum value for the z-axis.
zStepnumbernoneStep size for the grid on the z-axis.
+ + +

Methods

+

+ Graph3d supports the following methods. +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
MethodReturn TypeDescription
animationStart()noneStart playing the animation. + Only applicable when animation data is available.
animationStop()noneStop playing the animation. + Only applicable when animation data is available.
draw(data, options)noneLoads data, sets the provided options, and draws the 3d graph.
getCameraPosition()An object with parameters horizontal, + vertical and distanceReturns an object with parameters horizontal, + vertical and distance, + which each one of them is a number, representing the rotation and position + of the camera.
redraw()noneRedraw the graph. Useful after the camera position is changed externally, + when data is changed, or when the layout of the webpage changed.
setSize(width, height)noneParameters width and height are strings, + containing a new size for the graph. Size can be provided in pixels + or in percentages.
setCameraPosition (pos){"horizontal": 1.0, "vertical": 0.5, "distance": 1.7}Set the rotation and position of the camera. Parameter pos + is an object which contains three parameters: horizontal, + vertical, and distance. + Parameter horizontal is a value in radians and can have any + value (but normally in the range of 0 and 2*Pi). + Parameter vertical is a value in radians between 0 and 0.5*Pi. + Parameter distance is the (normalized) distance from the + camera to the center of the graph, in the range of 0.71 to 5.0. A + larger distance puts the graph further away, making it smaller. + All parameters are optional. +
+ +

Events

+

+ Graph3d fires events after the camera position has been changed. + The event can be catched by creating a listener. + Here an example on how to catch a camerapositionchange event. +

+ +
+function oncamerapositionchange(event) {
+  alert("The camera position changed to:\n" +
+        "Horizontal: " + event.horizontal + "\n" +
+        "Vertical: " + event.vertical + "\n" +
+        "Distance: " + event.distance);
+}
+
+google.visualization.events.addListener(mygraph, 'camerapositionchange', oncamerapositionchange);
+
+ +

+ The following events are available. +

+ + + + + + + + + + + + + + + + + + + + + + + +
nameDescriptionProperties
camerapositionchangeThe camera position changed. Fired after the user modified the camera position + by moving (dragging) the graph, or by zooming (scrolling), + but not after a call to setCameraPosition method. + The new camera position can be retrieved by calling the method + getCameraPosition. +
    +
  • horizontal: Number. The horizontal angle of the camera.
  • +
  • vertical: Number. The vertical angle of the camera.
  • +
  • distance: Number. The distance of the camera to the center of the graph.
  • +
+
readyThe graph is ready for external method calls. + If you want to interact with the graph, and call methods after you draw it, + you should set up a listener for this event before you call the draw method, + and call them only after the event was fired.none
+ +

Data Policy

+

+ All code and data are processed and rendered in the browser. No data is sent to any server. +

+ +
+ + diff --git a/src/3dgraph/doc/prettify/lang-apollo.js b/src/3dgraph/doc/prettify/lang-apollo.js new file mode 100644 index 00000000..bfc0014c --- /dev/null +++ b/src/3dgraph/doc/prettify/lang-apollo.js @@ -0,0 +1,2 @@ +PR.registerLangHandler(PR.createSimpleLexer([["com",/^#[^\r\n]*/,null,"#"],["pln",/^[\t\n\r \xA0]+/,null,"\t\n\r \u00a0"],["str",/^\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)/,null,'"']],[["kwd",/^(?:ADS|AD|AUG|BZF|BZMF|CAE|CAF|CA|CCS|COM|CS|DAS|DCA|DCOM|DCS|DDOUBL|DIM|DOUBLE|DTCB|DTCF|DV|DXCH|EDRUPT|EXTEND|INCR|INDEX|NDX|INHINT|LXCH|MASK|MSK|MP|MSU|NOOP|OVSK|QXCH|RAND|READ|RELINT|RESUME|RETURN|ROR|RXOR|SQUARE|SU|TCR|TCAA|OVSK|TCF|TC|TS|WAND|WOR|WRITE|XCH|XLQ|XXALQ|ZL|ZQ|ADD|ADZ|SUB|SUZ|MPY|MPR|MPZ|DVP|COM|ABS|CLA|CLZ|LDQ|STO|STQ|ALS|LLS|LRS|TRA|TSQ|TMI|TOV|AXT|TIX|DLY|INP|OUT)\s/, +null],["typ",/^(?:-?GENADR|=MINUS|2BCADR|VN|BOF|MM|-?2CADR|-?[1-6]DNADR|ADRES|BBCON|[SE]?BANK\=?|BLOCK|BNKSUM|E?CADR|COUNT\*?|2?DEC\*?|-?DNCHAN|-?DNPTR|EQUALS|ERASE|MEMORY|2?OCT|REMADR|SETLOC|SUBRO|ORG|BSS|BES|SYN|EQU|DEFINE|END)\s/,null],["lit",/^\'(?:-*(?:\w|\\[\x21-\x7e])(?:[\w-]*|\\[\x21-\x7e])[=!?]?)?/],["pln",/^-*(?:[!-z_]|\\[\x21-\x7e])(?:[\w-]*|\\[\x21-\x7e])[=!?]?/i],["pun",/^[^\w\t\n\r \xA0()\"\\\';]+/]]),["apollo","agc","aea"]) \ No newline at end of file diff --git a/src/3dgraph/doc/prettify/lang-css.js b/src/3dgraph/doc/prettify/lang-css.js new file mode 100644 index 00000000..61157f38 --- /dev/null +++ b/src/3dgraph/doc/prettify/lang-css.js @@ -0,0 +1,2 @@ +PR.registerLangHandler(PR.createSimpleLexer([["pln",/^[ \t\r\n\f]+/,null," \t\r\n\u000c"]],[["str",/^\"(?:[^\n\r\f\\\"]|\\(?:\r\n?|\n|\f)|\\[\s\S])*\"/,null],["str",/^\'(?:[^\n\r\f\\\']|\\(?:\r\n?|\n|\f)|\\[\s\S])*\'/,null],["lang-css-str",/^url\(([^\)\"\']*)\)/i],["kwd",/^(?:url|rgb|\!important|@import|@page|@media|@charset|inherit)(?=[^\-\w]|$)/i,null],["lang-css-kw",/^(-?(?:[_a-z]|(?:\\[0-9a-f]+ ?))(?:[_a-z0-9\-]|\\(?:\\[0-9a-f]+ ?))*)\s*:/i],["com",/^\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\//], +["com",/^(?: + + + + + + + + + +
+ + diff --git a/src/3dgraph/examples/example14_styles.html b/src/3dgraph/examples/example14_styles.html new file mode 100644 index 00000000..d1f1f447 --- /dev/null +++ b/src/3dgraph/examples/example14_styles.html @@ -0,0 +1,131 @@ + + + + Graph 3D styles + + + + + + + + + + + +

+ +

+ +

+ +

+ +

+ +

+

+ +

+ +
+ +
+ + diff --git a/src/3dgraph/examples/example15_tooltips.html b/src/3dgraph/examples/example15_tooltips.html new file mode 100644 index 00000000..ceaa1bdb --- /dev/null +++ b/src/3dgraph/examples/example15_tooltips.html @@ -0,0 +1,114 @@ + + + + Graph 3D tooltips + + + + + + + + + + + +

+ +

+ +
+ +
+ + diff --git a/src/3dgraph/examples/index.html b/src/3dgraph/examples/index.html new file mode 100644 index 00000000..b0092210 --- /dev/null +++ b/src/3dgraph/examples/index.html @@ -0,0 +1,28 @@ + + + + + +
+ +

Examples

+ +

example01_basis.html

+

example02_camera.html

+

example03_filter.html

+

example04_animate.html

+

example05_datasource.html

+

example06_line.html

+

example07_internet_explorer_9.html

+

example08_moving_dots.html

+

example09_dot_cloud_colors.html

+

example10_dot_cloud_size.html

+

example11_datasource_refresh.html

+

example12_datastream.html

+

example13_mobile.html

+

example14_styles.html

+

example15_tooltips.html

+ +
+ + diff --git a/src/3dgraph/graph3d.js b/src/3dgraph/graph3d.js new file mode 100644 index 00000000..b0b47dba --- /dev/null +++ b/src/3dgraph/graph3d.js @@ -0,0 +1,3270 @@ +/** + * @file graph3d.js + * + * @brief + * Graph3d is an interactive google visualization chart to draw data in a + * three dimensional graph. You can freely move and zoom in the graph by + * dragging and scrolling in the window. Graph3d also supports animation. + * + * Graph3d is part of the CHAP Links library. + * + * Graph3d is tested on Firefox 3.6, Safari 5.0, Chrome 6.0, Opera 10.6, and + * Internet Explorer 9+. + * + * @license + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy + * of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + * + * Copyright (C) 2010-2014 Almende B.V. + * + * @author Jos de Jong, jos@almende.org + * @date 2014-05-27 + * @version 1.4 + */ + +/* + TODO + - add options to add text besides the circles/dots + + - add methods getAnimationIndex, getAnimationCount, setAnimationIndex, setAnimationNext, setAnimationPrev, ... + - add extra examples to the playground + - make default dot color customizable, and also the size, min size and max size of the dots + - calculating size of a dot with style dot-size is not created well. + - problem when animating and there is only one group + - enable gray bottom side of the graph + - add options to customize the color and with of the lines (when style:"line") + - add an option to draw multiple lines in 3d + - add options to draw dots in 3d, with a value represented by a radius or color + - create a function to export as png + window.open(graph.frame.canvas.toDataURL("image/png")); + http://www.nihilogic.dk/labs/canvas2image/ + - option to show network: dots connected by a line. The width or color of a line + can represent a value + + BUGS + - when playing, and you change the data, something goes wrong and the animation starts playing 2x, and cannot be stopped + - opera: right aligning the text on the axis does not work + + DOCUMENTATION + http://en.wikipedia.org/wiki/3D_projection + + */ + + +/** + * Declare a unique namespace for CHAP's Common Hybrid Visualisation Library, + * "links" + */ +if (typeof links === 'undefined') { + links = {}; + // important: do not use var, as "var links = {};" will overwrite + // the existing links variable value with undefined in IE8, IE7. +} + +/** + * @constructor links.Graph3d + * The Graph is a visualization Graphs on a time line + * + * Graph is developed in javascript as a Google Visualization Chart. + * + * @param {Element} container The DOM element in which the Graph will + * be created. Normally a div element. + */ +links.Graph3d = function (container) { + // create variables and set default values + this.containerElement = container; + this.width = "400px"; + this.height = "400px"; + this.margin = 10; // px + this.defaultXCenter = "55%"; + this.defaultYCenter = "50%"; + + this.style = links.Graph3d.STYLE.DOT; + this.showPerspective = true; + this.showGrid = true; + this.keepAspectRatio = true; + this.showShadow = false; + this.showGrayBottom = false; // TODO: this does not work correctly + this.showTooltip = false; + this.verticalRatio = 0.5; // 0.1 to 1.0, where 1.0 results in a "cube" + + this.animationInterval = 1000; // milliseconds + this.animationPreload = false; + + this.camera = new links.Graph3d.Camera(); + this.eye = new links.Point3d(0, 0, -1); // TODO: set eye.z about 3/4 of the width of the window? + + this.dataTable = null; // The original data table + this.dataPoints = null; // The table with point objects + + // the column indexes + this.colX = undefined; + this.colY = undefined; + this.colZ = undefined; + this.colValue = undefined; + this.colFilter = undefined; + + this.xMin = 0; + this.xStep = undefined; // auto by default + this.xMax = 1; + this.yMin = 0; + this.yStep = undefined; // auto by default + this.yMax = 1; + this.zMin = 0; + this.zStep = undefined; // auto by default + this.zMax = 1; + this.valueMin = 0; + this.valueMax = 1; + this.xBarWidth = 1; + this.yBarWidth = 1; + // TODO: customize axis range + + // constants + this.colorAxis = "#4D4D4D"; + this.colorGrid = "#D3D3D3"; + this.colorDot = "#7DC1FF"; + this.colorDotBorder = "#3267D2"; + + // create a frame and canvas + this.create(); +}; + +/** + * @class Camera + * The camera is mounted on a (virtual) camera arm. The camera arm can rotate + * The camera is always looking in the direction of the origin of the arm. + * This way, the camera always rotates around one fixed point, the location + * of the camera arm. + * + * Documentation: + * http://en.wikipedia.org/wiki/3D_projection + */ +links.Graph3d.Camera = function () { + this.armLocation = new links.Point3d(); + this.armRotation = {}; + this.armRotation.horizontal = 0; + this.armRotation.vertical = 0; + this.armLength = 1.7; + + this.cameraLocation = new links.Point3d(); + this.cameraRotation = new links.Point3d(0.5*Math.PI, 0, 0); + + this.calculateCameraOrientation(); +}; + +/** + * Set the location (origin) of the arm + * @param {number} x Normalized value of x + * @param {number} y Normalized value of y + * @param {number} z Normalized value of z + */ +links.Graph3d.Camera.prototype.setArmLocation = function(x, y, z) { + this.armLocation.x = x; + this.armLocation.y = y; + this.armLocation.z = z; + + this.calculateCameraOrientation(); +}; + +/** + * Set the rotation of the camera arm + * @param {number} horizontal The horizontal rotation, between 0 and 2*PI. + * Optional, can be left undefined. + * @param {number} vertical The vertical rotation, between 0 and 0.5*PI + * if vertical=0.5*PI, the graph is shown from the + * top. Optional, can be left undefined. + */ +links.Graph3d.Camera.prototype.setArmRotation = function(horizontal, vertical) { + if (horizontal !== undefined) { + this.armRotation.horizontal = horizontal; + } + + if (vertical !== undefined) { + this.armRotation.vertical = vertical; + if (this.armRotation.vertical < 0) this.armRotation.vertical = 0; + if (this.armRotation.vertical > 0.5*Math.PI) this.armRotation.vertical = 0.5*Math.PI; + } + + if (horizontal !== undefined || vertical !== undefined) { + this.calculateCameraOrientation(); + } +}; + +/** + * Retrieve the current arm rotation + * @return {object} An object with parameters horizontal and vertical + */ +links.Graph3d.Camera.prototype.getArmRotation = function() { + var rot = {}; + rot.horizontal = this.armRotation.horizontal; + rot.vertical = this.armRotation.vertical; + + return rot; +}; + +/** + * Set the (normalized) length of the camera arm. + * @param {number} length A length between 0.71 and 5.0 + */ +links.Graph3d.Camera.prototype.setArmLength = function(length) { + if (length === undefined) + return; + + this.armLength = length; + + // Radius must be larger than the corner of the graph, + // which has a distance of sqrt(0.5^2+0.5^2) = 0.71 from the center of the + // graph + if (this.armLength < 0.71) this.armLength = 0.71; + if (this.armLength > 5.0) this.armLength = 5.0; + + this.calculateCameraOrientation(); +}; + +/** + * Retrieve the arm length + * @return {number} length + */ +links.Graph3d.Camera.prototype.getArmLength = function() { + return this.armLength; +}; + +/** + * Retrieve the camera location + * @return {links.Point3d} cameraLocation + */ +links.Graph3d.Camera.prototype.getCameraLocation = function() { + return this.cameraLocation; +}; + +/** + * Retrieve the camera rotation + * @return {links.Point3d} cameraRotation + */ +links.Graph3d.Camera.prototype.getCameraRotation = function() { + return this.cameraRotation; +}; + +/** + * Calculate the location and rotation of the camera based on the + * position and orientation of the camera arm + */ +links.Graph3d.Camera.prototype.calculateCameraOrientation = function() { + // calculate location of the camera + this.cameraLocation.x = this.armLocation.x - this.armLength * Math.sin(this.armRotation.horizontal) * Math.cos(this.armRotation.vertical); + this.cameraLocation.y = this.armLocation.y - this.armLength * Math.cos(this.armRotation.horizontal) * Math.cos(this.armRotation.vertical); + this.cameraLocation.z = this.armLocation.z + this.armLength * Math.sin(this.armRotation.vertical); + + // calculate rotation of the camera + this.cameraRotation.x = Math.PI/2 - this.armRotation.vertical; + this.cameraRotation.y = 0; + this.cameraRotation.z = -this.armRotation.horizontal; +}; + +/** + * Calculate the scaling values, dependent on the range in x, y, and z direction + */ +links.Graph3d.prototype._setScale = function() { + this.scale = new links.Point3d(1 / (this.xMax - this.xMin), + 1 / (this.yMax - this.yMin), + 1 / (this.zMax - this.zMin)); + + // keep aspect ration between x and y scale if desired + if (this.keepAspectRatio) { + if (this.scale.x < this.scale.y) { + //noinspection JSSuspiciousNameCombination + this.scale.y = this.scale.x; + } + else { + //noinspection JSSuspiciousNameCombination + this.scale.x = this.scale.y; + } + } + + // scale the vertical axis + this.scale.z *= this.verticalRatio; + // TODO: can this be automated? verticalRatio? + + // determine scale for (optional) value + this.scale.value = 1 / (this.valueMax - this.valueMin); + + // position the camera arm + var xCenter = (this.xMax + this.xMin) / 2 * this.scale.x; + var yCenter = (this.yMax + this.yMin) / 2 * this.scale.y; + var zCenter = (this.zMax + this.zMin) / 2 * this.scale.z; + this.camera.setArmLocation(xCenter, yCenter, zCenter); +}; + + +/** + * Convert a 3D location to a 2D location on screen + * http://en.wikipedia.org/wiki/3D_projection + * @param {links.Point3d} point3d A 3D point with parameters x, y, z + * @return {links.Point2d} point2d A 2D point with parameters x, y + */ +links.Graph3d.prototype._convert3Dto2D = function(point3d) { + var translation = this._convertPointToTranslation(point3d); + return this._convertTranslationToScreen(translation); +}; + +/** + * Convert a 3D location its translation seen from the camera + * http://en.wikipedia.org/wiki/3D_projection + * @param {links.Point3d} point3d A 3D point with parameters x, y, z + * @return {links.Point3d} translation A 3D point with parameters x, y, z This is + * the translation of the point, seen from the + * camera + */ +links.Graph3d.prototype._convertPointToTranslation = function(point3d) { + var ax = point3d.x * this.scale.x, + ay = point3d.y * this.scale.y, + az = point3d.z * this.scale.z, + + cx = this.camera.getCameraLocation().x, + cy = this.camera.getCameraLocation().y, + cz = this.camera.getCameraLocation().z, + + // calculate angles + sinTx = Math.sin(this.camera.getCameraRotation().x), + cosTx = Math.cos(this.camera.getCameraRotation().x), + sinTy = Math.sin(this.camera.getCameraRotation().y), + cosTy = Math.cos(this.camera.getCameraRotation().y), + sinTz = Math.sin(this.camera.getCameraRotation().z), + cosTz = Math.cos(this.camera.getCameraRotation().z), + + // calculate translation + dx = cosTy * (sinTz * (ay - cy) + cosTz * (ax - cx)) - sinTy * (az - cz), + dy = sinTx * (cosTy * (az - cz) + sinTy * (sinTz * (ay - cy) + cosTz * (ax - cx))) + cosTx * (cosTz * (ay - cy) - sinTz * (ax-cx)), + dz = cosTx * (cosTy * (az - cz) + sinTy * (sinTz * (ay - cy) + cosTz * (ax - cx))) - sinTx * (cosTz * (ay - cy) - sinTz * (ax-cx)); + + return new links.Point3d(dx, dy, dz); +}; + +/** + * Convert a translation point to a point on the screen + * @param {links.Point3d} translation A 3D point with parameters x, y, z This is + * the translation of the point, seen from the + * camera + * @return {links.Point2d} point2d A 2D point with parameters x, y + */ +links.Graph3d.prototype._convertTranslationToScreen = function(translation) { + var ex = this.eye.x, + ey = this.eye.y, + ez = this.eye.z, + dx = translation.x, + dy = translation.y, + dz = translation.z; + + // calculate position on screen from translation + var bx; + var by; + if (this.showPerspective) { + bx = (dx - ex) * (ez / dz); + by = (dy - ey) * (ez / dz); + } + else { + bx = dx * -(ez / this.camera.getArmLength()); + by = dy * -(ez / this.camera.getArmLength()); + } + + // shift and scale the point to the center of the screen + // use the width of the graph to scale both horizontally and vertically. + return new links.Point2d( + this.xcenter + bx * this.frame.canvas.clientWidth, + this.ycenter - by * this.frame.canvas.clientWidth); +}; + +/** + * Main drawing logic. This is the function that needs to be called + * in the html page, to draw the Graph. + * + * A data table with the events must be provided, and an options table. + * @param {google.visualization.DataTable} data The data containing the events + * for the Graph. + * @param {Object} options A name/value map containing settings for the Graph. + */ +links.Graph3d.prototype.draw = function(data, options) { + var cameraPosition = undefined; + + if (options !== undefined) { + // retrieve parameter values + if (options.width !== undefined) this.width = options.width; + if (options.height !== undefined) this.height = options.height; + + if (options.xCenter !== undefined) this.defaultXCenter = options.xCenter; + if (options.yCenter !== undefined) this.defaultYCenter = options.yCenter; + + if (options.style !== undefined) { + var styleNumber = this._getStyleNumber(options.style); + if (styleNumber !== -1) { + this.style = styleNumber; + } + } + if (options.showGrid !== undefined) this.showGrid = options.showGrid; + if (options.showPerspective !== undefined) this.showPerspective = options.showPerspective; + if (options.showShadow !== undefined) this.showShadow = options.showShadow; + if (options.tooltip !== undefined) this.showTooltip = options.tooltip; + if (options.showAnimationControls !== undefined) this.showAnimationControls = options.showAnimationControls; + if (options.keepAspectRatio !== undefined) this.keepAspectRatio = options.keepAspectRatio; + if (options.verticalRatio !== undefined) this.verticalRatio = options.verticalRatio; + + if (options.animationInterval !== undefined) this.animationInterval = options.animationInterval; + if (options.animationPreload !== undefined) this.animationPreload = options.animationPreload; + if (options.animationAutoStart !== undefined)this.animationAutoStart = options.animationAutoStart; + + if (options.xBarWidth !== undefined) this.defaultXBarWidth = options.xBarWidth; + if (options.yBarWidth !== undefined) this.defaultYBarWidth = options.yBarWidth; + + if (options.xMin !== undefined) this.defaultXMin = options.xMin; + if (options.xStep !== undefined) this.defaultXStep = options.xStep; + if (options.xMax !== undefined) this.defaultXMax = options.xMax; + if (options.yMin !== undefined) this.defaultYMin = options.yMin; + if (options.yStep !== undefined) this.defaultYStep = options.yStep; + if (options.yMax !== undefined) this.defaultYMax = options.yMax; + if (options.zMin !== undefined) this.defaultZMin = options.zMin; + if (options.zStep !== undefined) this.defaultZStep = options.zStep; + if (options.zMax !== undefined) this.defaultZMax = options.zMax; + if (options.valueMin !== undefined) this.defaultValueMin = options.valueMin; + if (options.valueMax !== undefined) this.defaultValueMax = options.valueMax; + + if (options.cameraPosition !== undefined) cameraPosition = options.cameraPosition; + } + + this._setBackgroundColor(options.backgroundColor); + + this.setSize(this.width, this.height); + + if (cameraPosition !== undefined) { + this.camera.setArmRotation(cameraPosition.horizontal, cameraPosition.vertical); + this.camera.setArmLength(cameraPosition.distance); + } + else { + this.camera.setArmRotation(1.0, 0.5); + this.camera.setArmLength(1.7); + } + + // draw the Graph + this.redraw(data); + + // start animation when option is true + if (this.animationAutoStart && this.dataFilter) { + this.animationStart(); + } + + // fire the ready event + google.visualization.events.trigger(this, 'ready', null); +}; + + +/** + * Set the background styling for the graph + * @param {string | {fill: string, stroke: string, strokeWidth: string}} backgroundColor + */ +links.Graph3d.prototype._setBackgroundColor = function(backgroundColor) { + var fill = "white"; + var stroke = "gray"; + var strokeWidth = 1; + + if (typeof(backgroundColor) === "string") { + fill = backgroundColor; + stroke = "none"; + strokeWidth = 0; + } + else if (typeof(backgroundColor) === "object") { + if (backgroundColor.fill !== undefined) fill = backgroundColor.fill; + if (backgroundColor.stroke !== undefined) stroke = backgroundColor.stroke; + if (backgroundColor.strokeWidth !== undefined) strokeWidth = backgroundColor.strokeWidth; + } + else if (backgroundColor === undefined) { + // use use defaults + } + else { + throw "Unsupported type of backgroundColor"; + } + + this.frame.style.backgroundColor = fill; + this.frame.style.borderColor = stroke; + this.frame.style.borderWidth = strokeWidth + "px"; + this.frame.style.borderStyle = "solid"; +}; + + +/// enumerate the available styles +links.Graph3d.STYLE = { + BAR: 0, + BARCOLOR: 1, + BARSIZE: 2, + DOT : 3, + DOTLINE : 4, + DOTCOLOR: 5, + DOTSIZE: 6, + GRID : 7, + LINE: 8, + SURFACE : 9 +}; + +/** + * Retrieve the style index from given styleName + * @param {string} styleName Style name such as "dot", "grid", "dot-line" + * @return {number} styleNumber Enumeration value representing the style, or -1 + * when not found + */ +links.Graph3d.prototype._getStyleNumber = function(styleName) { + switch (styleName) { + case "dot": return links.Graph3d.STYLE.DOT; + case "dot-line": return links.Graph3d.STYLE.DOTLINE; + case "dot-color": return links.Graph3d.STYLE.DOTCOLOR; + case "dot-size": return links.Graph3d.STYLE.DOTSIZE; + case "line": return links.Graph3d.STYLE.LINE; + case "grid": return links.Graph3d.STYLE.GRID; + case "surface": return links.Graph3d.STYLE.SURFACE; + case "bar": return links.Graph3d.STYLE.BAR; + case "bar-color": return links.Graph3d.STYLE.BARCOLOR; + case "bar-size": return links.Graph3d.STYLE.BARSIZE; + } + + return -1; +}; + +/** + * Determine the indexes of the data columns, based on the given style and data + * @param {google.visualization.DataTable} data + * @param {number} style + */ +links.Graph3d.prototype._determineColumnIndexes = function(data, style) { + if (this.style === links.Graph3d.STYLE.DOT || + this.style === links.Graph3d.STYLE.DOTLINE || + this.style === links.Graph3d.STYLE.LINE || + this.style === links.Graph3d.STYLE.GRID || + this.style === links.Graph3d.STYLE.SURFACE || + this.style === links.Graph3d.STYLE.BAR) { + // 3 columns expected, and optionally a 4th with filter values + this.colX = 0; + this.colY = 1; + this.colZ = 2; + this.colValue = undefined; + + if (data.getNumberOfColumns() > 3) { + this.colFilter = 3; + } + } + else if (this.style === links.Graph3d.STYLE.DOTCOLOR || + this.style === links.Graph3d.STYLE.DOTSIZE || + this.style === links.Graph3d.STYLE.BARCOLOR || + this.style === links.Graph3d.STYLE.BARSIZE) { + // 4 columns expected, and optionally a 5th with filter values + this.colX = 0; + this.colY = 1; + this.colZ = 2; + this.colValue = 3; + + if (data.getNumberOfColumns() > 4) { + this.colFilter = 4; + } + } + else { + throw "Unknown style '" + this.style + "'"; + } +}; + +/** + * Initialize the data from the data table. Calculate minimum and maximum values + * and column index values + * @param {google.visualization.DataTable} data The data containing the events + * for the Graph. + * @param {number} style Style number + */ +links.Graph3d.prototype._dataInitialize = function (data, style) { + if (data === undefined || data.getNumberOfRows === undefined) + return; + + // determine the location of x,y,z,value,filter columns + this._determineColumnIndexes(data, style); + + this.dataTable = data; + this.dataFilter = undefined; + + // check if a filter column is provided + if (this.colFilter && data.getNumberOfColumns() >= this.colFilter) { + if (this.dataFilter === undefined) { + this.dataFilter = new links.Filter(data, this.colFilter, this); + + var me = this; + this.dataFilter.setOnLoadCallback(function() {me.redraw();}); + } + } + + var withBars = this.style == links.Graph3d.STYLE.BAR || + this.style == links.Graph3d.STYLE.BARCOLOR || + this.style == links.Graph3d.STYLE.BARSIZE; + + // determine barWidth from data + if (withBars) { + if (this.defaultXBarWidth !== undefined) { + this.xBarWidth = this.defaultXBarWidth; + } + else { + var dataX = data.getDistinctValues(this.colX); + this.xBarWidth = (dataX[1] - dataX[0]) || 1; + } + + if (this.defaultYBarWidth !== undefined) { + this.yBarWidth = this.defaultYBarWidth; + } + else { + var dataY = data.getDistinctValues(this.colY); + this.yBarWidth = (dataY[1] - dataY[0]) || 1; + } + } + + // calculate minimums and maximums + var xRange = data.getColumnRange(this.colX); + if (withBars) { + xRange.min -= this.xBarWidth / 2; + xRange.max += this.xBarWidth / 2; + } + this.xMin = (this.defaultXMin !== undefined) ? this.defaultXMin : xRange.min; + this.xMax = (this.defaultXMax !== undefined) ? this.defaultXMax : xRange.max; + if (this.xMax <= this.xMin) this.xMax = this.xMin + 1; + this.xStep = (this.defaultXStep !== undefined) ? this.defaultXStep : (this.xMax-this.xMin)/5; + + var yRange = data.getColumnRange(this.colY); + if (withBars) { + yRange.min -= this.yBarWidth / 2; + yRange.max += this.yBarWidth / 2; + } + this.yMin = (this.defaultYMin !== undefined) ? this.defaultYMin : yRange.min; + this.yMax = (this.defaultYMax !== undefined) ? this.defaultYMax : yRange.max; + if (this.yMax <= this.yMin) this.yMax = this.yMin + 1; + this.yStep = (this.defaultYStep !== undefined) ? this.defaultYStep : (this.yMax-this.yMin)/5; + + var zRange = data.getColumnRange(this.colZ); + this.zMin = (this.defaultZMin !== undefined) ? this.defaultZMin : zRange.min; + this.zMax = (this.defaultZMax !== undefined) ? this.defaultZMax : zRange.max; + if (this.zMax <= this.zMin) this.zMax = this.zMin + 1; + this.zStep = (this.defaultZStep !== undefined) ? this.defaultZStep : (this.zMax-this.zMin)/5; + + if (this.colValue !== undefined) { + var valueRange = data.getColumnRange(this.colValue); + this.valueMin = (this.defaultValueMin !== undefined) ? this.defaultValueMin : valueRange.min; + this.valueMax = (this.defaultValueMax !== undefined) ? this.defaultValueMax : valueRange.max; + if (this.valueMax <= this.valueMin) this.valueMax = this.valueMin + 1; + } + + // set the scale dependent on the ranges. + this._setScale(); +}; + + + +/** + * Filter the data based on the current filter + * @param {google.visualization.DataTable} data + * @return {Array} dataPoints Array with point objects which can be drawn on screen + */ +links.Graph3d.prototype._getDataPoints = function (data) { + // TODO: store the created matrix dataPoints in the filters instead of reloading each time + var x, y, i, z, obj, point; + + var dataPoints = []; + + if (this.style === links.Graph3d.STYLE.GRID || + this.style === links.Graph3d.STYLE.SURFACE) { + // copy all values from the google data table to a matrix + // the provided values are supposed to form a grid of (x,y) positions + + // create two lists with all present x and y values + var dataX = []; + var dataY = []; + for (i = 0; i < data.getNumberOfRows(); i++) { + x = data.getValue(i, this.colX) || 0; + y = data.getValue(i, this.colY) || 0; + + if (dataX.indexOf(x) === -1) { + dataX.push(x); + } + if (dataY.indexOf(y) === -1) { + dataY.push(y); + } + } + + function sortNumber(a, b) { + return a - b; + } + dataX.sort(sortNumber); + dataY.sort(sortNumber); + + // create a grid, a 2d matrix, with all values. + var dataMatrix = []; // temporary data matrix + for (i = 0; i < data.getNumberOfRows(); i++) { + x = data.getValue(i, this.colX) || 0; + y = data.getValue(i, this.colY) || 0; + z = data.getValue(i, this.colZ) || 0; + + var xIndex = dataX.indexOf(x); // TODO: implement Array().indexOf() for Internet Explorer + var yIndex = dataY.indexOf(y); + + if (dataMatrix[xIndex] === undefined) { + dataMatrix[xIndex] = []; + } + + var point3d = new links.Point3d(); + point3d.x = x; + point3d.y = y; + point3d.z = z; + + obj = {}; + obj.point = point3d; + obj.trans = undefined; + obj.screen = undefined; + obj.bottom = new links.Point3d(x, y, this.zMin); + + dataMatrix[xIndex][yIndex] = obj; + + dataPoints.push(obj); + } + + // fill in the pointers to the neighbors. + for (x = 0; x < dataMatrix.length; x++) { + for (y = 0; y < dataMatrix[x].length; y++) { + if (dataMatrix[x][y]) { + dataMatrix[x][y].pointRight = (x < dataMatrix.length-1) ? dataMatrix[x+1][y] : undefined; + dataMatrix[x][y].pointTop = (y < dataMatrix[x].length-1) ? dataMatrix[x][y+1] : undefined; + dataMatrix[x][y].pointCross = + (x < dataMatrix.length-1 && y < dataMatrix[x].length-1) ? + dataMatrix[x+1][y+1] : + undefined; + } + } + } + } + else { // "dot", "dot-line", etc. + // copy all values from the google data table to a list with Point3d objects + for (i = 0; i < data.getNumberOfRows(); i++) { + point = new links.Point3d(); + point.x = data.getValue(i, this.colX) || 0; + point.y = data.getValue(i, this.colY) || 0; + point.z = data.getValue(i, this.colZ) || 0; + + if (this.colValue !== undefined) { + point.value = data.getValue(i, this.colValue) || 0; + } + + obj = {}; + obj.point = point; + obj.bottom = new links.Point3d(point.x, point.y, this.zMin); + obj.trans = undefined; + obj.screen = undefined; + + dataPoints.push(obj); + } + } + + return dataPoints; +}; + + + + +/** + * Append suffix "px" to provided value x + * @param {int} x An integer value + * @return {string} the string value of x, followed by the suffix "px" + */ +links.Graph3d.px = function(x) { + return x + "px"; +}; + + +/** + * Create the main frame for the Graph3d. + * This function is executed once when a Graph3d object is created. The frame + * contains a canvas, and this canvas contains all objects like the axis and + * nodes. + */ +links.Graph3d.prototype.create = function () { + // remove all elements from the container element. + while (this.containerElement.hasChildNodes()) { + this.containerElement.removeChild(this.containerElement.firstChild); + } + + this.frame = document.createElement("div"); + this.frame.style.position = "relative"; + this.frame.style.overflow = "hidden"; + + // create the graph canvas (HTML canvas element) + this.frame.canvas = document.createElement( "canvas" ); + this.frame.canvas.style.position = "relative"; + this.frame.appendChild(this.frame.canvas); + //if (!this.frame.canvas.getContext) { + { + var noCanvas = document.createElement( "DIV" ); + noCanvas.style.color = "red"; + noCanvas.style.fontWeight = "bold" ; + noCanvas.style.padding = "10px"; + noCanvas.innerHTML = "Error: your browser does not support HTML canvas"; + this.frame.canvas.appendChild(noCanvas); + } + + this.frame.filter = document.createElement( "div" ); + this.frame.filter.style.position = "absolute"; + this.frame.filter.style.bottom = "0px"; + this.frame.filter.style.left = "0px"; + this.frame.filter.style.width = "100%"; + this.frame.appendChild(this.frame.filter); + + // add event listeners to handle moving and zooming the contents + var me = this; + var onmousedown = function (event) {me._onMouseDown(event);}; + var ontouchstart = function (event) {me._onTouchStart(event);}; + var onmousewheel = function (event) {me._onWheel(event);}; + var ontooltip = function (event) {me._onTooltip(event);}; + // TODO: these events are never cleaned up... can give a "memory leakage" + + links.addEventListener(this.frame.canvas, "keydown", onkeydown); + links.addEventListener(this.frame.canvas, "mousedown", onmousedown); + links.addEventListener(this.frame.canvas, "touchstart", ontouchstart); + links.addEventListener(this.frame.canvas, "mousewheel", onmousewheel); + links.addEventListener(this.frame.canvas, "mousemove", ontooltip); + + // add the new graph to the container element + this.containerElement.appendChild(this.frame); +}; + + +/** + * Set a new size for the graph + * @param {string} width Width in pixels or percentage (for example "800px" + * or "50%") + * @param {string} height Height in pixels or percentage (for example "400px" + * or "30%") + */ +links.Graph3d.prototype.setSize = function(width, height) { + this.frame.style.width = width; + this.frame.style.height = height; + + this._resizeCanvas(); +}; + +/** + * Resize the canvas to the current size of the frame + */ +links.Graph3d.prototype._resizeCanvas = function() { + this.frame.canvas.style.width = "100%"; + this.frame.canvas.style.height = "100%"; + + this.frame.canvas.width = this.frame.canvas.clientWidth; + this.frame.canvas.height = this.frame.canvas.clientHeight; + + // adjust with for margin + this.frame.filter.style.width = (this.frame.canvas.clientWidth - 2 * 10) + "px"; +}; + +/** + * Start animation + */ +links.Graph3d.prototype.animationStart = function() { + if (!this.frame.filter || !this.frame.filter.slider) + throw "No animation available"; + + this.frame.filter.slider.play(); +}; + + +/** + * Stop animation + */ +links.Graph3d.prototype.animationStop = function() { + if (!this.frame.filter || !this.frame.filter.slider) + throw "No animation available"; + + this.frame.filter.slider.stop(); +}; + + +/** + * Resize the center position based on the current values in this.defaultXCenter + * and this.defaultYCenter (which are strings with a percentage or a value + * in pixels). The center positions are the variables this.xCenter + * and this.yCenter + */ +links.Graph3d.prototype._resizeCenter = function() { + // calculate the horizontal center position + if (this.defaultXCenter.charAt(this.defaultXCenter.length-1) === "%") { + this.xcenter = + parseFloat(this.defaultXCenter) / 100 * + this.frame.canvas.clientWidth; + } + else { + this.xcenter = parseFloat(this.defaultXCenter); // supposed to be in px + } + + // calculate the vertical center position + if (this.defaultYCenter.charAt(this.defaultYCenter.length-1) === "%") { + this.ycenter = + parseFloat(this.defaultYCenter) / 100 * + (this.frame.canvas.clientHeight - this.frame.filter.clientHeight); + } + else { + this.ycenter = parseFloat(this.defaultYCenter); // supposed to be in px + } +}; + +/** + * Set the rotation and distance of the camera + * @param {Object} pos An object with the camera position. The object + * contains three parameters: + * - horizontal {number} + * The horizontal rotation, between 0 and 2*PI. + * Optional, can be left undefined. + * - vertical {number} + * The vertical rotation, between 0 and 0.5*PI + * if vertical=0.5*PI, the graph is shown from the + * top. Optional, can be left undefined. + * - distance {number} + * The (normalized) distance of the camera to the + * center of the graph, a value between 0.71 and 5.0. + * Optional, can be left undefined. + */ +links.Graph3d.prototype.setCameraPosition = function(pos) { + if (pos === undefined) { + return; + } + + if (pos.horizontal !== undefined && pos.vertical !== undefined) { + this.camera.setArmRotation(pos.horizontal, pos.vertical); + } + + if (pos.distance !== undefined) { + this.camera.setArmLength(pos.distance); + } + + this.redraw(); +}; + + +/** + * Retrieve the current camera rotation + * @return {object} An object with parameters horizontal, vertical, and + * distance + */ +links.Graph3d.prototype.getCameraPosition = function() { + var pos = this.camera.getArmRotation(); + pos.distance = this.camera.getArmLength(); + return pos; +}; + +/** + * Load data into the 3D Graph + */ +links.Graph3d.prototype._readData = function(data) { + // read the data + this._dataInitialize(data, this.style); + + if (this.dataFilter) { + // apply filtering + this.dataPoints = this.dataFilter._getDataPoints(); + } + else { + // no filtering. load all data + this.dataPoints = this._getDataPoints(this.dataTable); + } + + // draw the filter + this._redrawFilter(); +}; + + +/** + * Redraw the Graph. This needs to be executed after the start and/or + * end time are changed, or when data is added or removed dynamically. + * @param {google.visualization.DataTable} data Optional, new data table + */ +links.Graph3d.prototype.redraw = function(data) { + // load the data if needed + if (data !== undefined) { + this._readData(data); + } + + if (this.dataPoints === undefined) { + throw "Error: graph data not initialized"; + } + + this._resizeCanvas(); + this._resizeCenter(); + this._redrawSlider(); + this._redrawClear(); + this._redrawAxis(); + + if (this.style === links.Graph3d.STYLE.GRID || + this.style === links.Graph3d.STYLE.SURFACE) { + this._redrawDataGrid(); + } + else if (this.style === links.Graph3d.STYLE.LINE) { + this._redrawDataLine(); + } + else if (this.style === links.Graph3d.STYLE.BAR || + this.style === links.Graph3d.STYLE.BARCOLOR || + this.style === links.Graph3d.STYLE.BARSIZE) { + this._redrawDataBar(); + } + else { + // style is DOT, DOTLINE, DOTCOLOR, DOTSIZE + this._redrawDataDot(); + } + + this._redrawInfo(); + this._redrawLegend(); +}; + +/** + * Clear the canvas before redrawing + */ +links.Graph3d.prototype._redrawClear = function() { + var canvas = this.frame.canvas; + var ctx = canvas.getContext("2d"); + + ctx.clearRect(0, 0, canvas.width, canvas.height); +}; + + +/** + * Redraw the legend showing the colors + */ +links.Graph3d.prototype._redrawLegend = function() { + var y; + + if (this.style === links.Graph3d.STYLE.DOTCOLOR || + this.style === links.Graph3d.STYLE.DOTSIZE) { + + var dotSize = this.frame.clientWidth * 0.02; + + var widthMin, widthMax; + if (this.style === links.Graph3d.STYLE.DOTSIZE) { + widthMin = dotSize / 2; // px + widthMax = dotSize / 2 + dotSize * 2; // Todo: put this in one function + } + else { + widthMin = 20; // px + widthMax = 20; // px + } + + var height = Math.max(this.frame.clientHeight * 0.25, 100); + var top = this.margin; + var right = this.frame.clientWidth - this.margin; + var left = right - widthMax; + var bottom = top + height; + } + + var canvas = this.frame.canvas; + var ctx = canvas.getContext("2d"); + ctx.lineWidth = 1; + ctx.font = "14px arial"; // TODO: put in options + + if (this.style === links.Graph3d.STYLE.DOTCOLOR) { + // draw the color bar + var ymin = 0; + var ymax = height; // Todo: make height customizable + for (y = ymin; y < ymax; y++) { + var f = (y - ymin) / (ymax - ymin); + + //var width = (dotSize / 2 + (1-f) * dotSize * 2); // Todo: put this in one function + var hue = f * 240; + var color = this._hsv2rgb(hue, 1, 1); + + ctx.strokeStyle = color; + ctx.beginPath(); + ctx.moveTo(left, top + y); + ctx.lineTo(right, top + y); + ctx.stroke(); + } + + ctx.strokeStyle = this.colorAxis; + ctx.strokeRect(left, top, widthMax, height); + } + + if (this.style === links.Graph3d.STYLE.DOTSIZE) { + // draw border around color bar + ctx.strokeStyle = this.colorAxis; + ctx.fillStyle = this.colorDot; + ctx.beginPath(); + ctx.moveTo(left, top); + ctx.lineTo(right, top); + ctx.lineTo(right - widthMax + widthMin, bottom); + ctx.lineTo(left, bottom); + ctx.closePath(); + ctx.fill(); + ctx.stroke(); + } + + if (this.style === links.Graph3d.STYLE.DOTCOLOR || + this.style === links.Graph3d.STYLE.DOTSIZE) { + // print values along the color bar + var gridLineLen = 5; // px + var step = new links.StepNumber(this.valueMin, this.valueMax, (this.valueMax-this.valueMin)/5, true); + step.start(); + if (step.getCurrent() < this.valueMin) { + step.next(); + } + while (!step.end()) { + y = bottom - (step.getCurrent() - this.valueMin) / (this.valueMax - this.valueMin) * height; + + ctx.beginPath(); + ctx.moveTo(left - gridLineLen, y); + ctx.lineTo(left, y); + ctx.stroke(); + + ctx.textAlign = "right"; + ctx.textBaseline = "middle"; + ctx.fillStyle = this.colorAxis; + ctx.fillText(step.getCurrent(), left - 2 * gridLineLen, y); + + step.next(); + } + + ctx.textAlign = "right"; + ctx.textBaseline = "top"; + var label = this.dataTable.getColumnLabel(this.colValue); + ctx.fillText(label, right, bottom + this.margin); + } +}; + +/** + * Redraw the filter + */ +links.Graph3d.prototype._redrawFilter = function() { + this.frame.filter.innerHTML = ""; + + if (this.dataFilter) { + var options = { + 'visible': this.showAnimationControls + }; + var slider = new links.Slider(this.frame.filter, options); + this.frame.filter.slider = slider; + + // TODO: css here is not nice here... + this.frame.filter.style.padding = "10px"; + //this.frame.filter.style.backgroundColor = "#EFEFEF"; + + slider.setValues(this.dataFilter.values); + slider.setPlayInterval(this.animationInterval); + + // create an event handler + var me = this; + var onchange = function () { + var index = slider.getIndex(); + + me.dataFilter.selectValue(index); + me.dataPoints = me.dataFilter._getDataPoints(); + + me.redraw(); + }; + slider.setOnChangeCallback(onchange); + } + else { + this.frame.filter.slider = undefined; + } +}; + +/** + * Redraw the slider + */ +links.Graph3d.prototype._redrawSlider = function() { + if ( this.frame.filter.slider !== undefined) { + this.frame.filter.slider.redraw(); + } +}; + + +/** + * Redraw common information + */ +links.Graph3d.prototype._redrawInfo = function() { + if (this.dataFilter) { + var canvas = this.frame.canvas; + var ctx = canvas.getContext("2d"); + + ctx.font = "14px arial"; // TODO: put in options + ctx.lineStyle = "gray"; + ctx.fillStyle = "gray"; + ctx.textAlign = "left"; + ctx.textBaseline = "top"; + + var x = this.margin; + var y = this.margin; + ctx.fillText(this.dataFilter.getLabel() + ": " + this.dataFilter.getSelectedValue(), x, y); + } +}; + + +/** + * Redraw the axis + */ +links.Graph3d.prototype._redrawAxis = function() { + var canvas = this.frame.canvas, + ctx = canvas.getContext("2d"), + from, to, step, prettyStep, + text, xText, yText, zText, + offset, xOffset, yOffset, + xMin2d, xMax2d; + + // TODO: get the actual rendered style of the containerElement + //ctx.font = this.containerElement.style.font; + ctx.font = 24 / this.camera.getArmLength() + "px arial"; + + // calculate the length for the short grid lines + var gridLenX = 0.025 / this.scale.x; + var gridLenY = 0.025 / this.scale.y; + var textMargin = 5 / this.camera.getArmLength(); // px + var armAngle = this.camera.getArmRotation().horizontal; + + // draw x-grid lines + ctx.lineWidth = 1; + prettyStep = (this.defaultXStep === undefined); + step = new links.StepNumber(this.xMin, this.xMax, this.xStep, prettyStep); + step.start(); + if (step.getCurrent() < this.xMin) { + step.next(); + } + while (!step.end()) { + var x = step.getCurrent(); + + if (this.showGrid) { + from = this._convert3Dto2D(new links.Point3d(x, this.yMin, this.zMin)); + to = this._convert3Dto2D(new links.Point3d(x, this.yMax, this.zMin)); + ctx.strokeStyle = this.colorGrid; + ctx.beginPath(); + ctx.moveTo(from.x, from.y); + ctx.lineTo(to.x, to.y); + ctx.stroke(); + } + else { + from = this._convert3Dto2D(new links.Point3d(x, this.yMin, this.zMin)); + to = this._convert3Dto2D(new links.Point3d(x, this.yMin+gridLenX, this.zMin)); + ctx.strokeStyle = this.colorAxis; + ctx.beginPath(); + ctx.moveTo(from.x, from.y); + ctx.lineTo(to.x, to.y); + ctx.stroke(); + + from = this._convert3Dto2D(new links.Point3d(x, this.yMax, this.zMin)); + to = this._convert3Dto2D(new links.Point3d(x, this.yMax-gridLenX, this.zMin)); + ctx.strokeStyle = this.colorAxis; + ctx.beginPath(); + ctx.moveTo(from.x, from.y); + ctx.lineTo(to.x, to.y); + ctx.stroke(); + } + + yText = (Math.cos(armAngle) > 0) ? this.yMin : this.yMax; + text = this._convert3Dto2D(new links.Point3d(x, yText, this.zMin)); + if (Math.cos(armAngle * 2) > 0) { + ctx.textAlign = "center"; + ctx.textBaseline = "top"; + text.y += textMargin; + } + else if (Math.sin(armAngle * 2) < 0){ + ctx.textAlign = "right"; + ctx.textBaseline = "middle"; + } + else { + ctx.textAlign = "left"; + ctx.textBaseline = "middle"; + } + ctx.fillStyle = this.colorAxis; + ctx.fillText(" " + step.getCurrent() + " ", text.x, text.y); + + step.next(); + } + + // draw y-grid lines + ctx.lineWidth = 1; + prettyStep = (this.defaultYStep === undefined); + step = new links.StepNumber(this.yMin, this.yMax, this.yStep, prettyStep); + step.start(); + if (step.getCurrent() < this.yMin) { + step.next(); + } + while (!step.end()) { + if (this.showGrid) { + from = this._convert3Dto2D(new links.Point3d(this.xMin, step.getCurrent(), this.zMin)); + to = this._convert3Dto2D(new links.Point3d(this.xMax, step.getCurrent(), this.zMin)); + ctx.strokeStyle = this.colorGrid; + ctx.beginPath(); + ctx.moveTo(from.x, from.y); + ctx.lineTo(to.x, to.y); + ctx.stroke(); + } + else { + from = this._convert3Dto2D(new links.Point3d(this.xMin, step.getCurrent(), this.zMin)); + to = this._convert3Dto2D(new links.Point3d(this.xMin+gridLenY, step.getCurrent(), this.zMin)); + ctx.strokeStyle = this.colorAxis; + ctx.beginPath(); + ctx.moveTo(from.x, from.y); + ctx.lineTo(to.x, to.y); + ctx.stroke(); + + from = this._convert3Dto2D(new links.Point3d(this.xMax, step.getCurrent(), this.zMin)); + to = this._convert3Dto2D(new links.Point3d(this.xMax-gridLenY, step.getCurrent(), this.zMin)); + ctx.strokeStyle = this.colorAxis; + ctx.beginPath(); + ctx.moveTo(from.x, from.y); + ctx.lineTo(to.x, to.y); + ctx.stroke(); + } + + xText = (Math.sin(armAngle ) > 0) ? this.xMin : this.xMax; + text = this._convert3Dto2D(new links.Point3d(xText, step.getCurrent(), this.zMin)); + if (Math.cos(armAngle * 2) < 0) { + ctx.textAlign = "center"; + ctx.textBaseline = "top"; + text.y += textMargin; + } + else if (Math.sin(armAngle * 2) > 0){ + ctx.textAlign = "right"; + ctx.textBaseline = "middle"; + } + else { + ctx.textAlign = "left"; + ctx.textBaseline = "middle"; + } + ctx.fillStyle = this.colorAxis; + ctx.fillText(" " + step.getCurrent() + " ", text.x, text.y); + + step.next(); + } + + // draw z-grid lines and axis + ctx.lineWidth = 1; + prettyStep = (this.defaultZStep === undefined); + step = new links.StepNumber(this.zMin, this.zMax, this.zStep, prettyStep); + step.start(); + if (step.getCurrent() < this.zMin) { + step.next(); + } + xText = (Math.cos(armAngle ) > 0) ? this.xMin : this.xMax; + yText = (Math.sin(armAngle ) < 0) ? this.yMin : this.yMax; + while (!step.end()) { + // TODO: make z-grid lines really 3d? + from = this._convert3Dto2D(new links.Point3d(xText, yText, step.getCurrent())); + ctx.strokeStyle = this.colorAxis; + ctx.beginPath(); + ctx.moveTo(from.x, from.y); + ctx.lineTo(from.x - textMargin, from.y); + ctx.stroke(); + + ctx.textAlign = "right"; + ctx.textBaseline = "middle"; + ctx.fillStyle = this.colorAxis; + ctx.fillText(step.getCurrent() + " ", from.x - 5, from.y); + + step.next(); + } + ctx.lineWidth = 1; + from = this._convert3Dto2D(new links.Point3d(xText, yText, this.zMin)); + to = this._convert3Dto2D(new links.Point3d(xText, yText, this.zMax)); + ctx.strokeStyle = this.colorAxis; + ctx.beginPath(); + ctx.moveTo(from.x, from.y); + ctx.lineTo(to.x, to.y); + ctx.stroke(); + + // draw x-axis + ctx.lineWidth = 1; + // line at yMin + xMin2d = this._convert3Dto2D(new links.Point3d(this.xMin, this.yMin, this.zMin)); + xMax2d = this._convert3Dto2D(new links.Point3d(this.xMax, this.yMin, this.zMin)); + ctx.strokeStyle = this.colorAxis; + ctx.beginPath(); + ctx.moveTo(xMin2d.x, xMin2d.y); + ctx.lineTo(xMax2d.x, xMax2d.y); + ctx.stroke(); + // line at ymax + xMin2d = this._convert3Dto2D(new links.Point3d(this.xMin, this.yMax, this.zMin)); + xMax2d = this._convert3Dto2D(new links.Point3d(this.xMax, this.yMax, this.zMin)); + ctx.strokeStyle = this.colorAxis; + ctx.beginPath(); + ctx.moveTo(xMin2d.x, xMin2d.y); + ctx.lineTo(xMax2d.x, xMax2d.y); + ctx.stroke(); + + // draw y-axis + ctx.lineWidth = 1; + // line at xMin + from = this._convert3Dto2D(new links.Point3d(this.xMin, this.yMin, this.zMin)); + to = this._convert3Dto2D(new links.Point3d(this.xMin, this.yMax, this.zMin)); + ctx.strokeStyle = this.colorAxis; + ctx.beginPath(); + ctx.moveTo(from.x, from.y); + ctx.lineTo(to.x, to.y); + ctx.stroke(); + // line at xMax + from = this._convert3Dto2D(new links.Point3d(this.xMax, this.yMin, this.zMin)); + to = this._convert3Dto2D(new links.Point3d(this.xMax, this.yMax, this.zMin)); + ctx.strokeStyle = this.colorAxis; + ctx.beginPath(); + ctx.moveTo(from.x, from.y); + ctx.lineTo(to.x, to.y); + ctx.stroke(); + + // draw x-label + var xLabel = this.dataTable.getColumnLabel(this.colX); + if (xLabel.length > 0) { + yOffset = 0.1 / this.scale.y; + xText = (this.xMin + this.xMax) / 2; + yText = (Math.cos(armAngle) > 0) ? this.yMin - yOffset: this.yMax + yOffset; + text = this._convert3Dto2D(new links.Point3d(xText, yText, this.zMin)); + if (Math.cos(armAngle * 2) > 0) { + ctx.textAlign = "center"; + ctx.textBaseline = "top"; + } + else if (Math.sin(armAngle * 2) < 0){ + ctx.textAlign = "right"; + ctx.textBaseline = "middle"; + } + else { + ctx.textAlign = "left"; + ctx.textBaseline = "middle"; + } + ctx.fillStyle = this.colorAxis; + ctx.fillText(xLabel, text.x, text.y); + } + + // draw y-label + var yLabel = this.dataTable.getColumnLabel(this.colY); + if (yLabel.length > 0) { + xOffset = 0.1 / this.scale.x; + xText = (Math.sin(armAngle ) > 0) ? this.xMin - xOffset : this.xMax + xOffset; + yText = (this.yMin + this.yMax) / 2; + text = this._convert3Dto2D(new links.Point3d(xText, yText, this.zMin)); + if (Math.cos(armAngle * 2) < 0) { + ctx.textAlign = "center"; + ctx.textBaseline = "top"; + } + else if (Math.sin(armAngle * 2) > 0){ + ctx.textAlign = "right"; + ctx.textBaseline = "middle"; + } + else { + ctx.textAlign = "left"; + ctx.textBaseline = "middle"; + } + ctx.fillStyle = this.colorAxis; + ctx.fillText(yLabel, text.x, text.y); + } + + // draw z-label + var zLabel = this.dataTable.getColumnLabel(this.colZ); + if (zLabel.length > 0) { + offset = 30; // pixels. // TODO: relate to the max width of the values on the z axis? + xText = (Math.cos(armAngle ) > 0) ? this.xMin : this.xMax; + yText = (Math.sin(armAngle ) < 0) ? this.yMin : this.yMax; + zText = (this.zMin + this.zMax) / 2; + text = this._convert3Dto2D(new links.Point3d(xText, yText, zText)); + ctx.textAlign = "right"; + ctx.textBaseline = "middle"; + ctx.fillStyle = this.colorAxis; + ctx.fillText(zLabel, text.x - offset, text.y); + } +}; + +/** + * Calculate the color based on the given value. + * @param {number} H Hue, a value be between 0 and 360 + * @param {number} S Saturation, a value between 0 and 1 + * @param {number} V Value, a value between 0 and 1 + */ +links.Graph3d.prototype._hsv2rgb = function(H, S, V) { + var R, G, B, C, Hi, X; + + C = V * S; + Hi = Math.floor(H/60); // hi = 0,1,2,3,4,5 + X = C * (1 - Math.abs(((H/60) % 2) - 1)); + + switch (Hi) { + case 0: R = C; G = X; B = 0; break; + case 1: R = X; G = C; B = 0; break; + case 2: R = 0; G = C; B = X; break; + case 3: R = 0; G = X; B = C; break; + case 4: R = X; G = 0; B = C; break; + case 5: R = C; G = 0; B = X; break; + + default: R = 0; G = 0; B = 0; break; + } + + return "RGB(" + parseInt(R*255) + "," + parseInt(G*255) + "," + parseInt(B*255) + ")"; +}; + + +/** + * Draw all datapoints as a grid + * This function can be used when the style is "grid" + */ +links.Graph3d.prototype._redrawDataGrid = function() { + var canvas = this.frame.canvas, + ctx = canvas.getContext("2d"), + point, right, top, cross, + i, + topSideVisible, fillStyle, strokeStyle, lineWidth, + h, s, v, zAvg; + + + if (this.dataPoints === undefined || this.dataPoints.length <= 0) + return; // TODO: throw exception? + + // calculate the translations and screen position of all points + for (i = 0; i < this.dataPoints.length; i++) { + var trans = this._convertPointToTranslation(this.dataPoints[i].point); + var screen = this._convertTranslationToScreen(trans); + + this.dataPoints[i].trans = trans; + this.dataPoints[i].screen = screen; + + // calculate the translation of the point at the bottom (needed for sorting) + var transBottom = this._convertPointToTranslation(this.dataPoints[i].bottom); + this.dataPoints[i].dist = this.showPerspective ? transBottom.length() : -transBottom.z; + } + + // sort the points on depth of their (x,y) position (not on z) + var sortDepth = function (a, b) { + return b.dist - a.dist; + }; + this.dataPoints.sort(sortDepth); + + if (this.style === links.Graph3d.STYLE.SURFACE) { + for (i = 0; i < this.dataPoints.length; i++) { + point = this.dataPoints[i]; + right = this.dataPoints[i].pointRight; + top = this.dataPoints[i].pointTop; + cross = this.dataPoints[i].pointCross; + + if (point !== undefined && right !== undefined && top !== undefined && cross !== undefined) { + + if (this.showGrayBottom || this.showShadow) { + // calculate the cross product of the two vectors from center + // to left and right, in order to know whether we are looking at the + // bottom or at the top side. We can also use the cross product + // for calculating light intensity + var aDiff = links.Point3d.subtract(cross.trans, point.trans); + var bDiff = links.Point3d.subtract(top.trans, right.trans); + var crossproduct = links.Point3d.crossProduct(aDiff, bDiff); + var len = crossproduct.length(); + // FIXME: there is a bug with determining the surface side (shadow or colored) + + topSideVisible = (crossproduct.z > 0); + } + else { + topSideVisible = true; + } + + if (topSideVisible) { + // calculate Hue from the current value. At zMin the hue is 240, at zMax the hue is 0 + zAvg = (point.point.z + right.point.z + top.point.z + cross.point.z) / 4; + h = (1 - (zAvg - this.zMin) * this.scale.z / this.verticalRatio) * 240; + s = 1; // saturation + + if (this.showShadow) { + v = Math.min(1 + (crossproduct.x / len) / 2, 1); // value. TODO: scale + fillStyle = this._hsv2rgb(h, s, v); + strokeStyle = fillStyle; + } + else { + v = 1; + fillStyle = this._hsv2rgb(h, s, v); + strokeStyle = this.colorAxis; + } + } + else { + fillStyle = "gray"; + strokeStyle = this.colorAxis; + } + lineWidth = 0.5; + + ctx.lineWidth = lineWidth; + ctx.fillStyle = fillStyle; + ctx.strokeStyle = strokeStyle; + ctx.beginPath(); + ctx.moveTo(point.screen.x, point.screen.y); + ctx.lineTo(right.screen.x, right.screen.y); + ctx.lineTo(cross.screen.x, cross.screen.y); + ctx.lineTo(top.screen.x, top.screen.y); + ctx.closePath(); + ctx.fill(); + ctx.stroke(); + } + } + } + else { // grid style + for (i = 0; i < this.dataPoints.length; i++) { + point = this.dataPoints[i]; + right = this.dataPoints[i].pointRight; + top = this.dataPoints[i].pointTop; + + if (point !== undefined) { + if (this.showPerspective) { + lineWidth = 2 / -point.trans.z; + } + else { + lineWidth = 2 * -(this.eye.z / this.camera.getArmLength()); + } + } + + if (point !== undefined && right !== undefined) { + // calculate Hue from the current value. At zMin the hue is 240, at zMax the hue is 0 + zAvg = (point.point.z + right.point.z) / 2; + h = (1 - (zAvg - this.zMin) * this.scale.z / this.verticalRatio) * 240; + + ctx.lineWidth = lineWidth; + ctx.strokeStyle = this._hsv2rgb(h, 1, 1); + ctx.beginPath(); + ctx.moveTo(point.screen.x, point.screen.y); + ctx.lineTo(right.screen.x, right.screen.y); + ctx.stroke(); + } + + if (point !== undefined && top !== undefined) { + // calculate Hue from the current value. At zMin the hue is 240, at zMax the hue is 0 + zAvg = (point.point.z + top.point.z) / 2; + h = (1 - (zAvg - this.zMin) * this.scale.z / this.verticalRatio) * 240; + + ctx.lineWidth = lineWidth; + ctx.strokeStyle = this._hsv2rgb(h, 1, 1); + ctx.beginPath(); + ctx.moveTo(point.screen.x, point.screen.y); + ctx.lineTo(top.screen.x, top.screen.y); + ctx.stroke(); + } + } + } +}; + + +/** + * Draw all datapoints as dots. + * This function can be used when the style is "dot" or "dot-line" + */ +links.Graph3d.prototype._redrawDataDot = function() { + var canvas = this.frame.canvas; + var ctx = canvas.getContext("2d"); + var i; + + if (this.dataPoints === undefined || this.dataPoints.length <= 0) + return; // TODO: throw exception? + + // calculate the translations of all points + for (i = 0; i < this.dataPoints.length; i++) { + var trans = this._convertPointToTranslation(this.dataPoints[i].point); + var screen = this._convertTranslationToScreen(trans); + this.dataPoints[i].trans = trans; + this.dataPoints[i].screen = screen; + + // calculate the distance from the point at the bottom to the camera + var transBottom = this._convertPointToTranslation(this.dataPoints[i].bottom); + this.dataPoints[i].dist = this.showPerspective ? transBottom.length() : -transBottom.z; + } + + // order the translated points by depth + var sortDepth = function (a, b) { + return b.dist - a.dist; + }; + this.dataPoints.sort(sortDepth); + + // draw the datapoints as colored circles + var dotSize = this.frame.clientWidth * 0.02; // px + for (i = 0; i < this.dataPoints.length; i++) { + var point = this.dataPoints[i]; + + if (this.style === links.Graph3d.STYLE.DOTLINE) { + // draw a vertical line from the bottom to the graph value + //var from = this._convert3Dto2D(new links.Point3d(point.point.x, point.point.y, this.zMin)); + var from = this._convert3Dto2D(point.bottom); + ctx.lineWidth = 1; + ctx.strokeStyle = this.colorGrid; + ctx.beginPath(); + ctx.moveTo(from.x, from.y); + ctx.lineTo(point.screen.x, point.screen.y); + ctx.stroke(); + } + + // calculate radius for the circle + var size; + if (this.style === links.Graph3d.STYLE.DOTSIZE) { + size = dotSize/2 + 2*dotSize * (point.point.value - this.valueMin) / (this.valueMax - this.valueMin); + } + else { + size = dotSize; + } + + var radius; + if (this.showPerspective) { + radius = size / -point.trans.z; + } + else { + radius = size * -(this.eye.z / this.camera.getArmLength()); + } + if (radius < 0) { + radius = 0; + } + + var hue, color, borderColor; + if (this.style === links.Graph3d.STYLE.DOTCOLOR ) { + // calculate the color based on the value + hue = (1 - (point.point.value - this.valueMin) * this.scale.value) * 240; + color = this._hsv2rgb(hue, 1, 1); + borderColor = this._hsv2rgb(hue, 1, 0.8); + } + else if (this.style === links.Graph3d.STYLE.DOTSIZE) { + color = this.colorDot; + borderColor = this.colorDotBorder; + } + else { + // calculate Hue from the current value. At zMin the hue is 240, at zMax the hue is 0 + hue = (1 - (point.point.z - this.zMin) * this.scale.z / this.verticalRatio) * 240; + color = this._hsv2rgb(hue, 1, 1); + borderColor = this._hsv2rgb(hue, 1, 0.8); + } + + // draw the circle + ctx.lineWidth = 1.0; + ctx.strokeStyle = borderColor; + ctx.fillStyle = color; + ctx.beginPath(); + ctx.arc(point.screen.x, point.screen.y, radius, 0, Math.PI*2, true); + ctx.fill(); + ctx.stroke(); + } +}; + +/** + * Draw all datapoints as bars. + * This function can be used when the style is "bar", "bar-color", or "bar-size" + */ +links.Graph3d.prototype._redrawDataBar = function() { + var canvas = this.frame.canvas; + var ctx = canvas.getContext("2d"); + var i, j, surface, corners; + + if (this.dataPoints === undefined || this.dataPoints.length <= 0) + return; // TODO: throw exception? + + // calculate the translations of all points + for (i = 0; i < this.dataPoints.length; i++) { + var trans = this._convertPointToTranslation(this.dataPoints[i].point); + var screen = this._convertTranslationToScreen(trans); + this.dataPoints[i].trans = trans; + this.dataPoints[i].screen = screen; + + // calculate the distance from the point at the bottom to the camera + var transBottom = this._convertPointToTranslation(this.dataPoints[i].bottom); + this.dataPoints[i].dist = this.showPerspective ? transBottom.length() : -transBottom.z; + } + + // order the translated points by depth + var sortDepth = function (a, b) { + return b.dist - a.dist; + }; + this.dataPoints.sort(sortDepth); + + // draw the datapoints as bars + var xWidth = this.xBarWidth / 2; + var yWidth = this.yBarWidth / 2; + var dotSize = this.frame.clientWidth * 0.02; // px + for (i = 0; i < this.dataPoints.length; i++) { + var point = this.dataPoints[i]; + + // determine color + var hue, color, borderColor; + if (this.style === links.Graph3d.STYLE.BARCOLOR ) { + // calculate the color based on the value + hue = (1 - (point.point.value - this.valueMin) * this.scale.value) * 240; + color = this._hsv2rgb(hue, 1, 1); + borderColor = this._hsv2rgb(hue, 1, 0.8); + } + else if (this.style === links.Graph3d.STYLE.BARSIZE) { + color = this.colorDot; + borderColor = this.colorDotBorder; + } + else { + // calculate Hue from the current value. At zMin the hue is 240, at zMax the hue is 0 + hue = (1 - (point.point.z - this.zMin) * this.scale.z / this.verticalRatio) * 240; + color = this._hsv2rgb(hue, 1, 1); + borderColor = this._hsv2rgb(hue, 1, 0.8); + } + + // calculate size for the bar + if (this.style === links.Graph3d.STYLE.BARSIZE) { + xWidth = (this.xBarWidth / 2) * ((point.point.value - this.valueMin) / (this.valueMax - this.valueMin) * 0.8 + 0.2); + yWidth = (this.yBarWidth / 2) * ((point.point.value - this.valueMin) / (this.valueMax - this.valueMin) * 0.8 + 0.2); + } + + // calculate all corner points + var me = this; + var point3d = point.point; + var top = [ + {point: new links.Point3d(point3d.x - xWidth, point3d.y - yWidth, point3d.z)}, + {point: new links.Point3d(point3d.x + xWidth, point3d.y - yWidth, point3d.z)}, + {point: new links.Point3d(point3d.x + xWidth, point3d.y + yWidth, point3d.z)}, + {point: new links.Point3d(point3d.x - xWidth, point3d.y + yWidth, point3d.z)} + ]; + var bottom = [ + {point: new links.Point3d(point3d.x - xWidth, point3d.y - yWidth, this.zMin)}, + {point: new links.Point3d(point3d.x + xWidth, point3d.y - yWidth, this.zMin)}, + {point: new links.Point3d(point3d.x + xWidth, point3d.y + yWidth, this.zMin)}, + {point: new links.Point3d(point3d.x - xWidth, point3d.y + yWidth, this.zMin)} + ]; + + // calculate screen location of the points + top.forEach(function (obj) { + obj.screen = me._convert3Dto2D(obj.point); + }); + bottom.forEach(function (obj) { + obj.screen = me._convert3Dto2D(obj.point); + }); + + // create five sides, calculate both corner points and center points + var surfaces = [ + {corners: top, center: links.Point3d.avg(bottom[0].point, bottom[2].point)}, + {corners: [top[0], top[1], bottom[1], bottom[0]], center: links.Point3d.avg(bottom[1].point, bottom[0].point)}, + {corners: [top[1], top[2], bottom[2], bottom[1]], center: links.Point3d.avg(bottom[2].point, bottom[1].point)}, + {corners: [top[2], top[3], bottom[3], bottom[2]], center: links.Point3d.avg(bottom[3].point, bottom[2].point)}, + {corners: [top[3], top[0], bottom[0], bottom[3]], center: links.Point3d.avg(bottom[0].point, bottom[3].point)} + ]; + point.surfaces = surfaces; + + // calculate the distance of each of the surface centers to the camera + for (j = 0; j < surfaces.length; j++) { + surface = surfaces[j]; + var transCenter = this._convertPointToTranslation(surface.center); + surface.dist = this.showPerspective ? transCenter.length() : -transCenter.z; + // TODO: this dept calculation doesn't work 100% of the cases due to perspective, + // but the current solution is fast/simple and works in 99.9% of all cases + // the issue is visible in example 14, with graph.setCameraPosition({horizontal: 2.97, vertical: 0.5, distance: 0.9}) + } + + // order the surfaces by their (translated) depth + surfaces.sort(function (a, b) { + var diff = b.dist - a.dist; + if (diff) return diff; + + // if equal depth, sort the top surface last + if (a.corners === top) return 1; + if (b.corners === top) return -1; + + // both are equal + return 0; + }); + + // draw the ordered surfaces + ctx.lineWidth = 1; + ctx.strokeStyle = borderColor; + ctx.fillStyle = color; + // NOTE: we start at j=2 instead of j=0 as we don't need to draw the two surfaces at the backside + for (j = 2; j < surfaces.length; j++) { + surface = surfaces[j]; + corners = surface.corners; + ctx.beginPath(); + ctx.moveTo(corners[3].screen.x, corners[3].screen.y); + ctx.lineTo(corners[0].screen.x, corners[0].screen.y); + ctx.lineTo(corners[1].screen.x, corners[1].screen.y); + ctx.lineTo(corners[2].screen.x, corners[2].screen.y); + ctx.lineTo(corners[3].screen.x, corners[3].screen.y); + ctx.fill(); + ctx.stroke(); + } + } +}; + + +/** + * Draw a line through all datapoints. + * This function can be used when the style is "line" + */ +links.Graph3d.prototype._redrawDataLine = function() { + var canvas = this.frame.canvas, + ctx = canvas.getContext("2d"), + point, i; + + if (this.dataPoints === undefined || this.dataPoints.length <= 0) + return; // TODO: throw exception? + + // calculate the translations of all points + for (i = 0; i < this.dataPoints.length; i++) { + var trans = this._convertPointToTranslation(this.dataPoints[i].point); + var screen = this._convertTranslationToScreen(trans); + + this.dataPoints[i].trans = trans; + this.dataPoints[i].screen = screen; + } + + // start the line + if (this.dataPoints.length > 0) { + point = this.dataPoints[0]; + + ctx.lineWidth = 1; // TODO: make customizable + ctx.strokeStyle = "blue"; // TODO: make customizable + ctx.beginPath(); + ctx.moveTo(point.screen.x, point.screen.y); + } + + // draw the datapoints as colored circles + for (i = 1; i < this.dataPoints.length; i++) { + point = this.dataPoints[i]; + ctx.lineTo(point.screen.x, point.screen.y); + } + + // finish the line + if (this.dataPoints.length > 0) { + ctx.stroke(); + } +}; + +/** + * Start a moving operation inside the provided parent element + * @param {Event} event The event that occurred (required for + * retrieving the mouse position) + */ +links.Graph3d.prototype._onMouseDown = function(event) { + event = event || window.event; + + // check if mouse is still down (may be up when focus is lost for example + // in an iframe) + if (this.leftButtonDown) { + this._onMouseUp(event); + } + + // only react on left mouse button down + this.leftButtonDown = event.which ? (event.which === 1) : (event.button === 1); + if (!this.leftButtonDown && !this.touchDown) return; + + // get mouse position (different code for IE and all other browsers) + this.startMouseX = links.getMouseX(event); + this.startMouseY = links.getMouseY(event); + + this.startStart = new Date(this.start); + this.startEnd = new Date(this.end); + this.startArmRotation = this.camera.getArmRotation(); + + this.frame.style.cursor = 'move'; + + // add event listeners to handle moving the contents + // we store the function onmousemove and onmouseup in the graph, so we can + // remove the eventlisteners lateron in the function mouseUp() + var me = this; + this.onmousemove = function (event) {me._onMouseMove(event);}; + this.onmouseup = function (event) {me._onMouseUp(event);}; + links.addEventListener(document, "mousemove", me.onmousemove); + links.addEventListener(document, "mouseup", me.onmouseup); + links.preventDefault(event); +}; + + +/** + * Perform moving operating. + * This function activated from within the funcion links.Graph.mouseDown(). + * @param {Event} event Well, eehh, the event + */ +links.Graph3d.prototype._onMouseMove = function (event) { + event = event || window.event; + + // calculate change in mouse position + var diffX = parseFloat(links.getMouseX(event)) - this.startMouseX; + var diffY = parseFloat(links.getMouseY(event)) - this.startMouseY; + + var horizontalNew = this.startArmRotation.horizontal + diffX / 200; + var verticalNew = this.startArmRotation.vertical + diffY / 200; + + var snapAngle = 4; // degrees + var snapValue = Math.sin(snapAngle / 360 * 2 * Math.PI); + + // snap horizontally to nice angles at 0pi, 0.5pi, 1pi, 1.5pi, etc... + // the -0.001 is to take care that the vertical axis is always drawn at the left front corner + if (Math.abs(Math.sin(horizontalNew)) < snapValue) { + horizontalNew = Math.round((horizontalNew / Math.PI)) * Math.PI - 0.001; + } + if (Math.abs(Math.cos(horizontalNew)) < snapValue) { + horizontalNew = (Math.round((horizontalNew/ Math.PI - 0.5)) + 0.5) * Math.PI - 0.001; + } + + // snap vertically to nice angles + if (Math.abs(Math.sin(verticalNew)) < snapValue) { + verticalNew = Math.round((verticalNew / Math.PI)) * Math.PI; + } + if (Math.abs(Math.cos(verticalNew)) < snapValue) { + verticalNew = (Math.round((verticalNew/ Math.PI - 0.5)) + 0.5) * Math.PI; + } + + this.camera.setArmRotation(horizontalNew, verticalNew); + this.redraw(); + + // fire an oncamerapositionchange event + var parameters = this.getCameraPosition(); + google.visualization.events.trigger(this, 'camerapositionchange', parameters); + + links.preventDefault(event); +}; + + +/** + * Stop moving operating. + * This function activated from within the funcion links.Graph.mouseDown(). + * @param {event} event The event + */ +links.Graph3d.prototype._onMouseUp = function (event) { + this.frame.style.cursor = 'auto'; + this.leftButtonDown = false; + + // remove event listeners here + links.removeEventListener(document, "mousemove", this.onmousemove); + links.removeEventListener(document, "mouseup", this.onmouseup); + links.preventDefault(event); +}; + +/** + * After having moved the mouse, a tooltip should pop up when the mouse is resting on a data point + * @param {Event} event A mouse move event + */ +links.Graph3d.prototype._onTooltip = function (event) { + var delay = 300; // ms + var mouseX = links.getMouseX(event) - links.getAbsoluteLeft(this.frame); + var mouseY = links.getMouseY(event) - links.getAbsoluteTop(this.frame); + + if (!this.showTooltip) { + return; + } + + if (this.tooltipTimeout) { + clearTimeout(this.tooltipTimeout); + } + + // (delayed) display of a tooltip only if no mouse button is down + if (this.leftButtonDown) { + this._hideTooltip(); + return; + } + + if (this.tooltip && this.tooltip.dataPoint) { + // tooltip is currently visible + var dataPoint = this._dataPointFromXY(mouseX, mouseY); + if (dataPoint !== this.tooltip.dataPoint) { + // datapoint changed + if (dataPoint) { + this._showTooltip(dataPoint); + } + else { + this._hideTooltip(); + } + } + } + else { + // tooltip is currently not visible + var me = this; + this.tooltipTimeout = setTimeout(function () { + me.tooltipTimeout = null; + + // show a tooltip if we have a data point + var dataPoint = me._dataPointFromXY(mouseX, mouseY); + if (dataPoint) { + me._showTooltip(dataPoint); + } + }, delay); + } +}; + +/** + * Event handler for touchstart event on mobile devices + */ +links.Graph3d.prototype._onTouchStart = function(event) { + this.touchDown = true; + + var me = this; + this.ontouchmove = function (event) {me._onTouchMove(event);}; + this.ontouchend = function (event) {me._onTouchEnd(event);}; + links.addEventListener(document, "touchmove", me.ontouchmove); + links.addEventListener(document, "touchend", me.ontouchend); + + this._onMouseDown(event); +}; + +/** + * Event handler for touchmove event on mobile devices + */ +links.Graph3d.prototype._onTouchMove = function(event) { + this._onMouseMove(event); +}; + +/** + * Event handler for touchend event on mobile devices + */ +links.Graph3d.prototype._onTouchEnd = function(event) { + this.touchDown = false; + + links.removeEventListener(document, "touchmove", this.ontouchmove); + links.removeEventListener(document, "touchend", this.ontouchend); + + this._onMouseUp(event); +}; + + +/** + * Event handler for mouse wheel event, used to zoom the graph + * Code from http://adomas.org/javascript-mouse-wheel/ + * @param {event} event The event + */ +links.Graph3d.prototype._onWheel = function(event) { + if (!event) /* For IE. */ + event = window.event; + + // retrieve delta + var delta = 0; + if (event.wheelDelta) { /* IE/Opera. */ + delta = event.wheelDelta/120; + } else if (event.detail) { /* Mozilla case. */ + // In Mozilla, sign of delta is different than in IE. + // Also, delta is multiple of 3. + delta = -event.detail/3; + } + + // If delta is nonzero, handle it. + // Basically, delta is now positive if wheel was scrolled up, + // and negative, if wheel was scrolled down. + if (delta) { + var oldLength = this.camera.getArmLength(); + var newLength = oldLength * (1 - delta / 10); + + this.camera.setArmLength(newLength); + this.redraw(); + + this._hideTooltip(); + } + + // fire an oncamerapositionchange event + var parameters = this.getCameraPosition(); + google.visualization.events.trigger(this, 'camerapositionchange', parameters); + + // Prevent default actions caused by mouse wheel. + // That might be ugly, but we handle scrolls somehow + // anyway, so don't bother here.. + links.preventDefault(event); +}; + +/** + * Test whether a point lies inside given 2D triangle + * @param {links.Point2d} point + * @param {links.Point2d[]} triangle + * @return {boolean} Returns true if given point lies inside or on the edge of the triangle + * @private + */ +links.Graph3d.prototype._insideTriangle = function (point, triangle) { + var a = triangle[0], + b = triangle[1], + c = triangle[2]; + + function sign (x) { + return x > 0 ? 1 : x < 0 ? -1 : 0; + } + + var as = sign((b.x - a.x) * (point.y - a.y) - (b.y - a.y) * (point.x - a.x)); + var bs = sign((c.x - b.x) * (point.y - b.y) - (c.y - b.y) * (point.x - b.x)); + var cs = sign((a.x - c.x) * (point.y - c.y) - (a.y - c.y) * (point.x - c.x)); + + // each of the three signs must be either equal to each other or zero + return (as == 0 || bs == 0 || as == bs) && + (bs == 0 || cs == 0 || bs == cs) && + (as == 0 || cs == 0 || as == cs); +}; + +/** + * Find a data point close to given screen position (x, y) + * @param {number} x + * @param {number} y + * @return {Object | null} The closest data point or null if not close to any data point + * @private + */ +links.Graph3d.prototype._dataPointFromXY = function (x, y) { + var i, + distMax = 100, // px + dataPoint = null, + closestDataPoint = null, + closestDist = null, + center = new links.Point2d(x, y); + + if (this.style === links.Graph3d.STYLE.BAR || + this.style === links.Graph3d.STYLE.BARCOLOR || + this.style === links.Graph3d.STYLE.BARSIZE) { + // the data points are ordered from far away to closest + for (i = this.dataPoints.length - 1; i >= 0; i--) { + dataPoint = this.dataPoints[i]; + var surfaces = dataPoint.surfaces; + if (surfaces) { + for (var s = surfaces.length - 1; s >= 0; s--) { + // split each surface in two triangles, and see if the center point is inside one of these + var surface = surfaces[s]; + var corners = surface.corners; + var triangle1 = [corners[0].screen, corners[1].screen, corners[2].screen]; + var triangle2 = [corners[2].screen, corners[3].screen, corners[0].screen]; + if (this._insideTriangle(center, triangle1) || + this._insideTriangle(center, triangle2)) { + // return immediately at the first hit + return dataPoint; + } + } + } + } + } + else { + // find the closest data point, using distance to the center of the point on 2d screen + for (i = 0; i < this.dataPoints.length; i++) { + dataPoint = this.dataPoints[i]; + var point = dataPoint.screen; + if (point) { + var distX = Math.abs(x - point.x); + var distY = Math.abs(y - point.y); + var dist = Math.sqrt(distX * distX + distY * distY); + + if ((closestDist === null || dist < closestDist) && dist < distMax) { + closestDist = dist; + closestDataPoint = dataPoint; + } + } + } + } + + + return closestDataPoint; +}; + +/** + * Display a tooltip for given data point + * @param {Object} dataPoint + * @private + */ +links.Graph3d.prototype._showTooltip = function (dataPoint) { + var content, line, dot; + + if (!this.tooltip) { + content = document.createElement('div'); + content.style.position = 'absolute'; + content.style.padding = '10px'; + content.style.border = '1px solid #4d4d4d'; + content.style.color = '#1a1a1a'; + content.style.background = 'rgba(255,255,255,0.7)'; + content.style.borderRadius = '2px'; + content.style.boxShadow = '5px 5px 10px rgba(128,128,128,0.5)'; + + line = document.createElement('div'); + line.style.position = 'absolute'; + line.style.height = '40px'; + line.style.width = '0'; + line.style.borderLeft = '1px solid #4d4d4d'; + + dot = document.createElement('div'); + dot.style.position = 'absolute'; + dot.style.height = '0'; + dot.style.width = '0'; + dot.style.border = '5px solid #4d4d4d'; + dot.style.borderRadius = '5px'; + + this.tooltip = { + dataPoint: null, + dom: { + content: content, + line: line, + dot: dot + } + }; + } + else { + content = this.tooltip.dom.content; + line = this.tooltip.dom.line; + dot = this.tooltip.dom.dot; + } + + this._hideTooltip(); + + this.tooltip.dataPoint = dataPoint; + if (typeof this.showTooltip === 'function') { + content.innerHTML = this.showTooltip(dataPoint.point); + } + else { + content.innerHTML = '' + + '' + + '' + + '' + + '
x:' + dataPoint.point.x + '
y:' + dataPoint.point.y + '
z:' + dataPoint.point.z + '
'; + } + + content.style.left = '0'; + content.style.top = '0'; + this.frame.appendChild(content); + this.frame.appendChild(line); + this.frame.appendChild(dot); + + // calculate sizes + var contentWidth = content.offsetWidth; + var contentHeight = content.offsetHeight; + var lineHeight = line.offsetHeight; + var dotWidth = dot.offsetWidth; + var dotHeight = dot.offsetHeight; + + var left = dataPoint.screen.x - contentWidth / 2; + left = Math.min(Math.max(left, 10), this.frame.clientWidth - 10 - contentWidth); + + line.style.left = dataPoint.screen.x + 'px'; + line.style.top = (dataPoint.screen.y - lineHeight) + 'px'; + content.style.left = left + 'px'; + content.style.top = (dataPoint.screen.y - lineHeight - contentHeight) + 'px'; + dot.style.left = (dataPoint.screen.x - dotWidth / 2) + 'px'; + dot.style.top = (dataPoint.screen.y - dotHeight / 2) + 'px'; +}; + +/** + * Hide the tooltip when displayed + * @private + */ +links.Graph3d.prototype._hideTooltip = function () { + if (this.tooltip) { + this.tooltip.dataPoint = null; + + for (var prop in this.tooltip.dom) { + if (this.tooltip.dom.hasOwnProperty(prop)) { + var elem = this.tooltip.dom[prop]; + if (elem && elem.parentNode) { + elem.parentNode.removeChild(elem); + } + } + } + } +}; + +/** + * @prototype Point3d + * @param {Number} x + * @param {Number} y + * @param {Number} z + */ +links.Point3d = function (x, y, z) { + this.x = x !== undefined ? x : 0; + this.y = y !== undefined ? y : 0; + this.z = z !== undefined ? z : 0; +}; + +/** + * Subtract the two provided points, returns a-b + * @param {links.Point3d} a + * @param {links.Point3d} b + * @return {links.Point3d} a-b + */ +links.Point3d.subtract = function(a, b) { + var sub = new links.Point3d(); + sub.x = a.x - b.x; + sub.y = a.y - b.y; + sub.z = a.z - b.z; + return sub; +}; + +/** + * Add the two provided points, returns a+b + * @param {links.Point3d} a + * @param {links.Point3d} b + * @return {links.Point3d} a+b + */ +links.Point3d.add = function(a, b) { + var sum = new links.Point3d(); + sum.x = a.x + b.x; + sum.y = a.y + b.y; + sum.z = a.z + b.z; + return sum; +}; + +/** + * Calculate the average of two 3d points + * @param {links.Point3d} a + * @param {links.Point3d} b + * @return {links.Point3d} The average, (a+b)/2 + */ +links.Point3d.avg = function(a, b) { + return new links.Point3d( + (a.x + b.x) / 2, + (a.y + b.y) / 2, + (a.z + b.z) / 2 + ); +}; + +/** + * Calculate the cross product of the two provided points, returns axb + * Documentation: http://en.wikipedia.org/wiki/Cross_product + * @param {links.Point3d} a + * @param {links.Point3d} b + * @return {links.Point3d} cross product axb + */ +links.Point3d.crossProduct = function(a, b) { + var crossproduct = new links.Point3d(); + + crossproduct.x = a.y * b.z - a.z * b.y; + crossproduct.y = a.z * b.x - a.x * b.z; + crossproduct.z = a.x * b.y - a.y * b.x; + + return crossproduct; +}; + + +/** + * Rtrieve the length of the vector (or the distance from this point to the origin + * @return {Number} length + */ +links.Point3d.prototype.length = function() { + return Math.sqrt( + this.x * this.x + + this.y * this.y + + this.z * this.z + ); +}; + +/** + * @prototype links.Point2d + */ +links.Point2d = function (x, y) { + this.x = x !== undefined ? x : 0; + this.y = y !== undefined ? y : 0; +}; + + +/** + * @class Filter + * + * @param {google.visualization.DataTable} data The google data table + * @param {number} column The index of the column to be filtered + * @param {links.Graph} graph The graph + */ +links.Filter = function (data, column, graph) { + this.data = data; + this.column = column; + this.graph = graph; // the parent graph + + this.index = undefined; + this.value = undefined; + + // read all distinct values and select the first one + this.values = data.getDistinctValues(this.column); + if (this.values.length) { + this.selectValue(0); + } + + // create an array with the filtered datapoints. this will be loaded afterwards + this.dataPoints = []; + + this.loaded = false; + this.onLoadCallback = undefined; + + if (graph.animationPreload) { + this.loaded = false; + this.loadInBackground(); + } + else { + this.loaded = true; + } +}; + + +/** + * Return the label + * @return {string} label + */ +links.Filter.prototype.isLoaded = function() { + return this.loaded; +}; + + +/** + * Return the loaded progress + * @return {number} percentage between 0 and 100 + */ +links.Filter.prototype.getLoadedProgress = function() { + var len = this.values.length; + + var i = 0; + while (this.dataPoints[i]) { + i++; + } + + return Math.round(i / len * 100); +}; + + +/** + * Return the label + * @return {string} label + */ +links.Filter.prototype.getLabel = function() { + return this.data.getColumnLabel(this.column); +}; + + +/** + * Return the columnIndex of the filter + * @return {number} columnIndex + */ +links.Filter.prototype.getColumn = function() { + return this.column; +}; + +/** + * Return the currently selected value. Returns undefined if there is no selection + * @return {*} value + */ +links.Filter.prototype.getSelectedValue = function() { + if (this.index === undefined) + return undefined; + + return this.values[this.index]; +}; + +/** + * Retrieve all values of the filter + * @return {Array} values + */ +links.Filter.prototype.getValues = function() { + return this.values; +}; + +/** + * Retrieve one value of the filter + * @param {number} index + * @return {*} value + */ +links.Filter.prototype.getValue = function(index) { + if (index >= this.values.length) + throw "Error: index out of range"; + + return this.values[index]; +}; + + +/** + * Retrieve the (filtered) dataPoints for the currently selected filter index + * @param {number} index (optional) + * @return {Array} dataPoints + */ +links.Filter.prototype._getDataPoints = function(index) { + if (index === undefined) + index = this.index; + + if (index === undefined) + return []; + + var dataPoints; + if (this.dataPoints[index]) { + dataPoints = this.dataPoints[index]; + } + else { + var dataView = new google.visualization.DataView(this.data); + + var f = {}; + f.column = this.column; + f.value = this.values[index]; + var filteredRows = this.data.getFilteredRows([f]); + dataView.setRows(filteredRows); + + dataPoints = this.graph._getDataPoints(dataView); + + this.dataPoints[index] = dataPoints; + } + + return dataPoints; +}; + + + +/** + * Set a callback function when the filter is fully loaded. + */ +links.Filter.prototype.setOnLoadCallback = function(callback) { + this.onLoadCallback = callback; +}; + + +/** + * Add a value to the list with available values for this filter + * No double entries will be created. + * @param {number} index + */ +links.Filter.prototype.selectValue = function(index) { + if (index >= this.values.length) + throw "Error: index out of range"; + + this.index = index; + this.value = this.values[index]; +}; + +/** + * Load all filtered rows in the background one by one + * Start this method without providing an index! + */ +links.Filter.prototype.loadInBackground = function(index) { + if (index === undefined) + index = 0; + + var frame = this.graph.frame; + + if (index < this.values.length) { + var dataPointsTemp = this._getDataPoints(index); + //this.graph.redrawInfo(); // TODO: not neat + + // create a progress box + if (frame.progress === undefined) { + frame.progress = document.createElement("DIV"); + frame.progress.style.position = "absolute"; + frame.progress.style.color = "gray"; + frame.appendChild(frame.progress); + } + var progress = this.getLoadedProgress(); + frame.progress.innerHTML = "Loading animation... " + progress + "%"; + // TODO: this is no nice solution... + frame.progress.style.bottom = links.Graph3d.px(60); // TODO: use height of slider + frame.progress.style.left = links.Graph3d.px(10); + + var me = this; + setTimeout(function() {me.loadInBackground(index+1);}, 10); + this.loaded = false; + } + else { + this.loaded = true; + + // remove the progress box + if (frame.progress !== undefined) { + frame.removeChild(frame.progress); + frame.progress = undefined; + } + + if (this.onLoadCallback) + this.onLoadCallback(); + } +}; + + + +/** + * @prototype links.StepNumber + * The class StepNumber is an iterator for numbers. You provide a start and end + * value, and a best step size. StepNumber itself rounds to fixed values and + * a finds the step that best fits the provided step. + * + * If prettyStep is true, the step size is chosen as close as possible to the + * provided step, but being a round value like 1, 2, 5, 10, 20, 50, .... + * + * Example usage: + * var step = new links.StepNumber(0, 10, 2.5, true); + * step.start(); + * while (!step.end()) { + * alert(step.getCurrent()); + * step.next(); + * } + * + * Version: 1.0 + * + * @param {number} start The start value + * @param {number} end The end value + * @param {number} step Optional. Step size. Must be a positive value. + * @param {boolean} prettyStep Optional. If true, the step size is rounded + * To a pretty step size (like 1, 2, 5, 10, 20, 50, ...) + */ +links.StepNumber = function (start, end, step, prettyStep) { + // set default values + this._start = 0; + this._end = 0; + this._step = 1; + this.prettyStep = true; + this.precision = 5; + + this._current = 0; + this.setRange(start, end, step, prettyStep); +}; + +/** + * Set a new range: start, end and step. + * + * @param {number} start The start value + * @param {number} end The end value + * @param {number} step Optional. Step size. Must be a positive value. + * @param {boolean} prettyStep Optional. If true, the step size is rounded + * To a pretty step size (like 1, 2, 5, 10, 20, 50, ...) + */ +links.StepNumber.prototype.setRange = function(start, end, step, prettyStep) { + this._start = start ? start : 0; + this._end = end ? end : 0; + + this.setStep(step, prettyStep); +}; + +/** + * Set a new step size + * @param {number} step New step size. Must be a positive value + * @param {boolean} prettyStep Optional. If true, the provided step is rounded + * to a pretty step size (like 1, 2, 5, 10, 20, 50, ...) + */ +links.StepNumber.prototype.setStep = function(step, prettyStep) { + if (step === undefined || step <= 0) + return; + + if (prettyStep !== undefined) + this.prettyStep = prettyStep; + + if (this.prettyStep === true) + this._step = links.StepNumber.calculatePrettyStep(step); + else + this._step = step; +}; + +/** + * Calculate a nice step size, closest to the desired step size. + * Returns a value in one of the ranges 1*10^n, 2*10^n, or 5*10^n, where n is an + * integer number. For example 1, 2, 5, 10, 20, 50, etc... + * @param {number} step Desired step size + * @return {number} Nice step size + */ +links.StepNumber.calculatePrettyStep = function (step) { + var log10 = function (x) {return Math.log(x) / Math.LN10;}; + + // try three steps (multiple of 1, 2, or 5 + var step1 = Math.pow(10, Math.round(log10(step))), + step2 = 2 * Math.pow(10, Math.round(log10(step / 2))), + step5 = 5 * Math.pow(10, Math.round(log10(step / 5))); + + // choose the best step (closest to minimum step) + var prettyStep = step1; + if (Math.abs(step2 - step) <= Math.abs(prettyStep - step)) prettyStep = step2; + if (Math.abs(step5 - step) <= Math.abs(prettyStep - step)) prettyStep = step5; + + // for safety + if (prettyStep <= 0) { + prettyStep = 1; + } + + return prettyStep; +}; + +/** + * returns the current value of the step + * @return {number} current value + */ +links.StepNumber.prototype.getCurrent = function () { + return parseFloat(this._current.toPrecision(this.precision)); +}; + +/** + * returns the current step size + * @return {number} current step size + */ +links.StepNumber.prototype.getStep = function () { + return this._step; +}; + +/** + * Set the current value to the largest value smaller than start, which + * is a multiple of the step size + */ +links.StepNumber.prototype.start = function() { + this._current = this._start - this._start % this._step; +}; + +/** + * Do a step, add the step size to the current value + */ +links.StepNumber.prototype.next = function () { + this._current += this._step; +}; + +/** + * Returns true whether the end is reached + * @return {boolean} True if the current value has passed the end value. + */ +links.StepNumber.prototype.end = function () { + return (this._current > this._end); +}; + + +/** + * @constructor links.Slider + * + * An html slider control with start/stop/prev/next buttons + * @param {Element} container The element where the slider will be created + * @param {Object} options Available options: + * {boolean} visible If true (default) the + * slider is visible. + */ +links.Slider = function(container, options) { + if (container === undefined) { + throw "Error: No container element defined"; + } + this.container = container; + this.visible = (options && options.visible != undefined) ? options.visible : true; + + if (this.visible) { + this.frame = document.createElement("DIV"); + //this.frame.style.backgroundColor = "#E5E5E5"; + this.frame.style.width = "100%"; + this.frame.style.position = "relative"; + this.container.appendChild(this.frame); + + this.frame.prev = document.createElement("INPUT"); + this.frame.prev.type = "BUTTON"; + this.frame.prev.value = "Prev"; + this.frame.appendChild(this.frame.prev); + + this.frame.play = document.createElement("INPUT"); + this.frame.play.type = "BUTTON"; + this.frame.play.value = "Play"; + this.frame.appendChild(this.frame.play); + + this.frame.next = document.createElement("INPUT"); + this.frame.next.type = "BUTTON"; + this.frame.next.value = "Next"; + this.frame.appendChild(this.frame.next); + + this.frame.bar = document.createElement("INPUT"); + this.frame.bar.type = "BUTTON"; + this.frame.bar.style.position = "absolute"; + this.frame.bar.style.border = "1px solid red"; + this.frame.bar.style.width = "100px"; + this.frame.bar.style.height = "6px"; + this.frame.bar.style.borderRadius = "2px"; + this.frame.bar.style.MozBorderRadius = "2px"; + this.frame.bar.style.border = "1px solid #7F7F7F"; + this.frame.bar.style.backgroundColor = "#E5E5E5"; + this.frame.appendChild(this.frame.bar); + + this.frame.slide = document.createElement("INPUT"); + this.frame.slide.type = "BUTTON"; + this.frame.slide.style.margin = "0px"; + this.frame.slide.value = " "; + this.frame.slide.style.position = "relative"; + this.frame.slide.style.left = "-100px"; + this.frame.appendChild(this.frame.slide); + + // create events + var me = this; + this.frame.slide.onmousedown = function (event) {me._onMouseDown(event);}; + this.frame.prev.onclick = function (event) {me.prev(event);}; + this.frame.play.onclick = function (event) {me.togglePlay(event);}; + this.frame.next.onclick = function (event) {me.next(event);}; + } + + this.onChangeCallback = undefined; + + this.values = []; + this.index = undefined; + + this.playTimeout = undefined; + this.playInterval = 1000; // milliseconds + this.playLoop = true; +}; + +/** + * Select the previous index + */ +links.Slider.prototype.prev = function() { + var index = this.getIndex(); + if (index > 0) { + index--; + this.setIndex(index); + } +}; + +/** + * Select the next index + */ +links.Slider.prototype.next = function() { + var index = this.getIndex(); + if (index < this.values.length - 1) { + index++; + this.setIndex(index); + } +}; + +/** + * Select the next index + */ +links.Slider.prototype.playNext = function() { + var start = new Date(); + + var index = this.getIndex(); + if (index < this.values.length - 1) { + index++; + this.setIndex(index); + } + else if (this.playLoop) { + // jump to the start + index = 0; + this.setIndex(index); + } + + var end = new Date(); + var diff = (end - start); + + // calculate how much time it to to set the index and to execute the callback + // function. + var interval = Math.max(this.playInterval - diff, 0); + // document.title = diff // TODO: cleanup + + var me = this; + this.playTimeout = setTimeout(function() {me.playNext();}, interval); +}; + +/** + * Toggle start or stop playing + */ +links.Slider.prototype.togglePlay = function() { + if (this.playTimeout === undefined) { + this.play(); + } else { + this.stop(); + } +}; + +/** + * Start playing + */ +links.Slider.prototype.play = function() { + this.playNext(); + + if (this.frame) { + this.frame.play.value = "Stop"; + } +}; + +/** + * Stop playing + */ +links.Slider.prototype.stop = function() { + clearInterval(this.playTimeout); + this.playTimeout = undefined; + + if (this.frame) { + this.frame.play.value = "Play"; + } +}; + +/** + * Set a callback function which will be triggered when the value of the + * slider bar has changed. + */ +links.Slider.prototype.setOnChangeCallback = function(callback) { + this.onChangeCallback = callback; +}; + +/** + * Set the interval for playing the list + * @param {number} interval The interval in milliseconds + */ +links.Slider.prototype.setPlayInterval = function(interval) { + this.playInterval = interval; +}; + +/** + * Retrieve the current play interval + * @return {number} interval The interval in milliseconds + */ +links.Slider.prototype.getPlayInterval = function(interval) { + return this.playInterval; +}; + +/** + * Set looping on or off + * @pararm {boolean} doLoop If true, the slider will jump to the start when + * the end is passed, and will jump to the end + * when the start is passed. + */ +links.Slider.prototype.setPlayLoop = function(doLoop) { + this.playLoop = doLoop; +}; + + +/** + * Execute the onchange callback function + */ +links.Slider.prototype.onChange = function() { + if (this.onChangeCallback !== undefined) { + this.onChangeCallback(); + } +}; + +/** + * redraw the slider on the correct place + */ +links.Slider.prototype.redraw = function() { + if (this.frame) { + // resize the bar + this.frame.bar.style.top = (this.frame.clientHeight/2 - + this.frame.bar.offsetHeight/2) + "px"; + this.frame.bar.style.width = (this.frame.clientWidth - + this.frame.prev.clientWidth - + this.frame.play.clientWidth - + this.frame.next.clientWidth - 30) + "px"; + + // position the slider button + var left = this.indexToLeft(this.index); + this.frame.slide.style.left = (left) + "px"; + } +}; + + +/** + * Set the list with values for the slider + * @param {Array} values A javascript array with values (any type) + */ +links.Slider.prototype.setValues = function(values) { + this.values = values; + + if (this.values.length > 0) + this.setIndex(0); + else + this.index = undefined; +}; + +/** + * Select a value by its index + * @param {number} index + */ +links.Slider.prototype.setIndex = function(index) { + if (index < this.values.length) { + this.index = index; + + this.redraw(); + this.onChange(); + } + else { + throw "Error: index out of range"; + } +}; + +/** + * retrieve the index of the currently selected vaue + * @return {number} index + */ +links.Slider.prototype.getIndex = function() { + return this.index; +}; + + +/** + * retrieve the currently selected value + * @return {*} value + */ +links.Slider.prototype.get = function() { + return this.values[this.index]; +}; + + +links.Slider.prototype._onMouseDown = function(event) { + // only react on left mouse button down + var leftButtonDown = event.which ? (event.which === 1) : (event.button === 1); + if (!leftButtonDown) return; + + this.startClientX = event.clientX; + this.startSlideX = parseFloat(this.frame.slide.style.left); + + this.frame.style.cursor = 'move'; + + // add event listeners to handle moving the contents + // we store the function onmousemove and onmouseup in the graph, so we can + // remove the eventlisteners lateron in the function mouseUp() + var me = this; + this.onmousemove = function (event) {me._onMouseMove(event);}; + this.onmouseup = function (event) {me._onMouseUp(event);}; + links.addEventListener(document, "mousemove", this.onmousemove); + links.addEventListener(document, "mouseup", this.onmouseup); + links.preventDefault(event); +}; + + +links.Slider.prototype.leftToIndex = function (left) { + var width = parseFloat(this.frame.bar.style.width) - + this.frame.slide.clientWidth - 10; + var x = left - 3; + + var index = Math.round(x / width * (this.values.length-1)); + if (index < 0) index = 0; + if (index > this.values.length-1) index = this.values.length-1; + + return index; +}; + +links.Slider.prototype.indexToLeft = function (index) { + var width = parseFloat(this.frame.bar.style.width) - + this.frame.slide.clientWidth - 10; + + var x = index / (this.values.length-1) * width; + var left = x + 3; + + return left; +}; + + + +links.Slider.prototype._onMouseMove = function (event) { + var diff = event.clientX - this.startClientX; + var x = this.startSlideX + diff; + + var index = this.leftToIndex(x); + + this.setIndex(index); + + links.preventDefault(); +}; + + +links.Slider.prototype._onMouseUp = function (event) { + this.frame.style.cursor = 'auto'; + + // remove event listeners + links.removeEventListener(document, "mousemove", this.onmousemove); + links.removeEventListener(document, "mouseup", this.onmouseup); + + links.preventDefault(); +}; + + + +/**--------------------------------------------------------------------------**/ + + + +/** + * Add and event listener. Works for all browsers + * @param {Element} element An html element + * @param {string} action The action, for example "click", + * without the prefix "on" + * @param {function} listener The callback function to be executed + * @param {boolean} useCapture + */ +links.addEventListener = function (element, action, listener, useCapture) { + if (element.addEventListener) { + if (useCapture === undefined) + useCapture = false; + + if (action === "mousewheel" && navigator.userAgent.indexOf("Firefox") >= 0) { + action = "DOMMouseScroll"; // For Firefox + } + + element.addEventListener(action, listener, useCapture); + } else { + element.attachEvent("on" + action, listener); // IE browsers + } +}; + +/** + * Remove an event listener from an element + * @param {Element} element An html dom element + * @param {string} action The name of the event, for example "mousedown" + * @param {function} listener The listener function + * @param {boolean} useCapture + */ +links.removeEventListener = function(element, action, listener, useCapture) { + if (element.removeEventListener) { + // non-IE browsers + if (useCapture === undefined) + useCapture = false; + + if (action === "mousewheel" && navigator.userAgent.indexOf("Firefox") >= 0) { + action = "DOMMouseScroll"; // For Firefox + } + + element.removeEventListener(action, listener, useCapture); + } else { + // IE browsers + element.detachEvent("on" + action, listener); + } +}; + +/** + * Stop event propagation + */ +links.stopPropagation = function (event) { + if (!event) + event = window.event; + + if (event.stopPropagation) { + event.stopPropagation(); // non-IE browsers + } + else { + event.cancelBubble = true; // IE browsers + } +}; + + +/** + * Cancels the event if it is cancelable, without stopping further propagation of the event. + */ +links.preventDefault = function (event) { + if (!event) + event = window.event; + + if (event.preventDefault) { + event.preventDefault(); // non-IE browsers + } + else { + event.returnValue = false; // IE browsers + } +}; + +/** + * Retrieve the absolute left value of a DOM element + * @param {Element} elem A dom element, for example a div + * @return {number} left The absolute left position of this element + * in the browser page. + */ +links.getAbsoluteLeft = function(elem) { + var left = 0; + while( elem !== null ) { + left += elem.offsetLeft; + left -= elem.scrollLeft; + elem = elem.offsetParent; + } + return left; +}; + +/** + * Retrieve the absolute top value of a DOM element + * @param {Element} elem A dom element, for example a div + * @return {number} top The absolute top position of this element + * in the browser page. + */ +links.getAbsoluteTop = function(elem) { + var top = 0; + while( elem !== null ) { + top += elem.offsetTop; + top -= elem.scrollTop; + elem = elem.offsetParent; + } + return top; +}; + +/** + * Get the horizontal mouse position from a mouse event + * @param {Event} event + * @return {number} mouse x + */ +links.getMouseX = function(event) { + if ('clientX' in event) return event.clientX; + return event.targetTouches[0] && event.targetTouches[0].clientX || 0; +}; + +/** + * Get the vertical mouse position from a mouse event + * @param {Event} event + * @return {number} mouse y + */ +links.getMouseY = function(event) { + if ('clientY' in event) return event.clientY; + return event.targetTouches[0] && event.targetTouches[0].clientY || 0; +}; + diff --git a/src/3dgraph/playground/csv2array.js b/src/3dgraph/playground/csv2array.js new file mode 100644 index 00000000..95d0c4a6 --- /dev/null +++ b/src/3dgraph/playground/csv2array.js @@ -0,0 +1,120 @@ +/** + * Convert data in CSV (comma separated value) format to a javascript array. + * + * Values are separated by a comma, or by a custom one character delimeter. + * Rows are separated by a new-line character. + * + * Leading and trailing spaces and tabs are ignored. + * Values may optionally be enclosed by double quotes. + * Values containing a special character (comma's, double-quotes, or new-lines) + * must be enclosed by double-quotes. + * Embedded double-quotes must be represented by a pair of consecutive + * double-quotes. + * + * Example usage: + * var csv = '"x", "y", "z"\n12.3, 2.3, 8.7\n4.5, 1.2, -5.6\n'; + * var array = csv2array(csv); + * + * Author: Jos de Jong, 2010 + * + * @param {string} data The data in CSV format. + * @param {string} delimeter [optional] a custom delimeter. Comma ',' by default + * The Delimeter must be a single character. + * @return {Array} array A two dimensional array containing the data + * @throw {String} error The method throws an error when there is an + * error in the provided data. + */ +function csv2array(data, delimeter) { + // Retrieve the delimeter + if (delimeter == undefined) + delimeter = ','; + if (delimeter && delimeter.length > 1) + delimeter = ','; + + // initialize variables + var newline = '\n'; + var eof = ''; + var i = 0; + var c = data.charAt(i); + var row = 0; + var col = 0; + var array = new Array(); + + while (c != eof) { + // skip whitespaces + while (c == ' ' || c == '\t' || c == '\r') { + c = data.charAt(++i); // read next char + } + + // get value + var value = ""; + if (c == '\"') { + // value enclosed by double-quotes + c = data.charAt(++i); + + do { + if (c != '\"') { + // read a regular character and go to the next character + value += c; + c = data.charAt(++i); + } + + if (c == '\"') { + // check for escaped double-quote + var cnext = data.charAt(i+1); + if (cnext == '\"') { + // this is an escaped double-quote. + // Add a double-quote to the value, and move two characters ahead. + value += '\"'; + i += 2; + c = data.charAt(i); + } + } + } + while (c != eof && c != '\"'); + + if (c == eof) { + throw "Unexpected end of data, double-quote expected"; + } + + c = data.charAt(++i); + } + else { + // value without quotes + while (c != eof && c != delimeter && c!= newline && c != ' ' && c != '\t' && c != '\r') { + value += c; + c = data.charAt(++i); + } + } + + // add the value to the array + if (array.length <= row) + array.push(new Array()); + array[row].push(value); + + // skip whitespaces + while (c == ' ' || c == '\t' || c == '\r') { + c = data.charAt(++i); + } + + // go to the next row or column + if (c == delimeter) { + // to the next column + col++; + } + else if (c == newline) { + // to the next row + col = 0; + row++; + } + else if (c != eof) { + // unexpected character + throw "Delimiter expected after character " + i; + } + + // go to the next character + c = data.charAt(++i); + } + + return array; +} diff --git a/src/3dgraph/playground/csv2datatable.html b/src/3dgraph/playground/csv2datatable.html new file mode 100644 index 00000000..35dc9bd0 --- /dev/null +++ b/src/3dgraph/playground/csv2datatable.html @@ -0,0 +1,80 @@ + + + + Convert CSV to Google Datatable + + + + + + + + + + +
+ +
+ +CSV
+ +
+
+ +
+
+ +Google DataTable
+ + + + diff --git a/src/3dgraph/playground/datasource.html b/src/3dgraph/playground/datasource.html new file mode 100644 index 00000000..efb47e1c --- /dev/null +++ b/src/3dgraph/playground/datasource.html @@ -0,0 +1,173 @@ + + + + Graph3d documentation + + + + + + + + +
+<?php
+
+/*
+This datasource returns a response in the form of a google query response
+
+USAGE
+All parameters are optional
+datasource.php?xmin=0&xmax=314&xstepnum=25&ymin=0&ymax=314&ystepnum=25
+
+DOCUMENTATION
+http://code.google.com/apis/visualization/documentation/dev/implementing_data_source.html
+
+
+EXAMPLE OF A RESPONSE FILE
+
+Note that the reqId in the response must correspond with the reqId from the
+request.
+________________________________________________________________________________
+
+google.visualization.Query.setResponse({
+  version:'0.6',
+  reqId:'0',
+  status:'ok',
+  table:{
+    cols:[
+      {id:'x',
+       label:'x',
+       type:'number'},
+      {id:'y',
+       label:'y',
+       type:'number'},
+      {id:'value',
+       label:'value',
+       type:'number'}
+    ],
+    rows:[
+      {c:[{v:0}, {v:0}, {v:10.0}]},
+      {c:[{v:1}, {v:0}, {v:12.0}]},
+      {c:[{v:2}, {v:0}, {v:13.0}]},
+      {c:[{v:0}, {v:1}, {v:11.0}]},
+      {c:[{v:1}, {v:1}, {v:14.0}]},
+      {c:[{v:2}, {v:1}, {v:11.0}]}
+    ]
+  }
+});
+________________________________________________________________________________
+
+*/
+
+
+/**
+ * A custom function
+ */
+function custom($x, $y) {
+  $d = sqrt(pow($x/100, 2) + pow($y/100, 2));
+
+  return 50 * exp(-5 * $d / 10) * sin($d*5)
+}
+
+
+
+
+// retrieve parameters
+$default_stepnum = 25;
+
+$xmin     = isset($_REQUEST['xmin'])     ? (float)$_REQUEST['xmin']   : -100;
+$xmax     = isset($_REQUEST['xmax'])     ? (float)$_REQUEST['xmax']   : 100;
+$xstepnum = isset($_REQUEST['xstepnum']) ? (int)$_REQUEST['xstepnum'] : $default_stepnum;
+
+$ymin     = isset($_REQUEST['ymin'])     ? (float)$_REQUEST['ymin']   : -100;
+$ymax     = isset($_REQUEST['ymax'])     ? (float)$_REQUEST['ymax']   : 100;
+$ystepnum = isset($_REQUEST['ystepnum']) ? (int)$_REQUEST['ystepnum'] : $default_stepnum;
+
+// in the reply we must fill in the request id that came with the request
+$reqId = getReqId();
+
+// check for a maximum number of datapoints (for safety)
+if ($xstepnum * $ystepnum > 10000) {
+  echo "google.visualization.Query.setResponse({
+    version:'0.6',
+    reqId:'$reqId',
+    status:'error',
+    errors:[{reason:'not_supported', message:'Maximum number of datapoints exceeded'}]
+  });";
+
+  exit;
+}
+
+
+// output the header part of the response
+echo "google.visualization.Query.setResponse({
+  version:'0.6',
+  reqId:'$reqId',
+  status:'ok',
+  table:{
+    cols:[
+      {id:'x',
+       label:'x',
+       type:'number'},
+      {id:'y',
+       label:'y',
+       type:'number'},
+      {id:'value',
+       label:'',
+       type:'number'}
+    ],
+    rows:[";
+
+// output the actual values
+$first = true;
+$xstep = ($xmax - $xmin) / $xstepnum;
+$ystep = ($ymax - $ymin) / $ystepnum;
+for ($x = $xmin; $x < $xmax; $x+=$xstep) {
+  for ($y = $ymin; $y < $ymax; $y+=$ystep) {
+    $value = custom($x,$y);
+
+    if (!$first) {
+      echo ",\n";
+    }
+    else {
+      echo "\n";
+    }
+    echo "      {c:[{v:$x}, {v:$y}, {v:$value}]}";
+
+    $first = false;
+  }
+}
+
+
+// output the end part of the response
+echo "
+    ]
+  }
+});
+";
+
+
+/**
+ * Retrieve the request id from the get/post data
+ * @return {number} $reqId       The request id, or 0 if not found
+ */
+function getReqId() {
+  $reqId = 0;
+
+  foreach ($_REQUEST as $req) {
+    if (substr($req, 0,6) == "reqId:") {
+      $reqId = substr($req, 6);
+    }
+  }
+
+  return $reqId;
+}
+
+
+?>
+
+
+ + + diff --git a/src/3dgraph/playground/datasource.php b/src/3dgraph/playground/datasource.php new file mode 100644 index 00000000..9c265cb9 --- /dev/null +++ b/src/3dgraph/playground/datasource.php @@ -0,0 +1,155 @@ + 10000) { + echo "google.visualization.Query.setResponse({ + version:'0.6', + reqId:'$reqId', + status:'error', + errors:[{reason:'not_supported', message:'Maximum number of datapoints exceeded'}] + });"; + + exit; +} + + +// output the header part of the response +echo "google.visualization.Query.setResponse({ + version:'0.6', + reqId:'$reqId', + status:'ok', + table:{ + cols:[ + {id:'x', + label:'x', + type:'number'}, + {id:'y', + label:'y', + type:'number'}, + {id:'value', + label:'', + type:'number'} + ], + rows:["; + +// output the actual values +$first = true; +$xstep = ($xmax - $xmin) / $xstepnum; +$ystep = ($ymax - $ymin) / $ystepnum; +for ($x = $xmin; $x < $xmax; $x+=$xstep) { + for ($y = $ymin; $y < $ymax; $y+=$ystep) { + $value = custom($x,$y); + + if (!$first) { + echo ",\n"; + } + else { + echo "\n"; + } + echo " {c:[{v:$x}, {v:$y}, {v:$value}]}"; + + $first = false; + } +} + + +// output the end part of the response +echo " + ] + } +}); +"; + + +/** + * Retrieve the request id from the get/post data + * @return {number} $reqId The request id, or 0 if not found + */ +function getReqId() { + $reqId = 0; + + foreach ($_REQUEST as $req) { + if (substr($req, 0,6) == "reqId:") { + $reqId = substr($req, 6); + } + } + + return $reqId; +} + + +?> diff --git a/src/3dgraph/playground/index.html b/src/3dgraph/playground/index.html new file mode 100644 index 00000000..d3df6e52 --- /dev/null +++ b/src/3dgraph/playground/index.html @@ -0,0 +1,247 @@ + + + + + Graph 3D - Playground + + + + + + + + + + + + + + +

Graph 3D - Playground

+ + +++ + + + + + + + +
+

Data

+

+ Graph 3D expects a data table with first three to five columns: + colums x, y, z (optional), + value, filtervalue (optional). +

+ + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Csv + +
+

+ The csv table must contain a header line with column names. +

+ +

+ Simple example + Line example + Animation example + Moving dots example + Colored dots example + Sized dots example +

+
+
+ Json + +
+

+ +

+ +

+ Simple example +

+
+
+ Javascript + +
+

+ The javascript source must create a global variable named data + which contains a google visualization DataTable. +

+ +

+ Simple example + Function example +

+
+
+ Google Spreadsheet + + +
+ Datasource + +
+ +

+ Example + (view source code) +

+
+
+ +
+
+

Graph

+

+ +

+ +
+
+

Options

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
OptionValue
width for example "500px" or "100%"
height for example "500px" or "100%"
style + +
showAnimationControls
showGrid
showPerspective
showShadow
keepAspectRatio
verticalRatio a value between 0.1 and 1.0
animationInterval in milliseconds
animationPreload
animationAutoStart
xCenter
yCenter
xMin
xMax
xStep
yMin
yMax
yStep
zMin
zMax
zStep
valueMin
valueMax
xBarWidth
yBarWidth
+ +
+ + diff --git a/src/3dgraph/playground/playground.css b/src/3dgraph/playground/playground.css new file mode 100644 index 00000000..c18ba24a --- /dev/null +++ b/src/3dgraph/playground/playground.css @@ -0,0 +1,91 @@ +body +{ + font: 13px "Lucida Grande", Tahoma, Arial, Helvetica, sans-serif; +} + +h1 +{ + font-size: 180%; + font-weight: bold; + + margin: 1em 0 1em 0; +} + +h2 +{ + font-size: 140%; + color: white; + background-color: #7F8FB1; + padding: 5px; +} + +h3 +{ + font-size: 100%; +} + +hr +{ + border: none 0; + border-top: 1px solid #7F8FB1; + height: 1px; +} + +pre.code +{ + display: block; + padding: 8px; + border: 1px dashed #ccc; +} + +table +{ + border-collapse: collapse; +} + +th, td +{ + font: 12px "Lucida Grande", Tahoma, Arial, Helvetica, sans-serif; + text-align: left; + vertical-align: top; + /*border: 1px solid #888;*/ + padding: 3px; +} + +th +{ + font-weight: bold; +} + + +textarea { + width: 500px; + height: 200px; + border: 1px solid #888; +} + +input[type=text] { + border: 1px solid #888; +} + +#datasourceText, #googlespreadsheetText { + width: 500px; + +} + +.info { + color: gray; +} + +a { + color: gray; +} +a:hover { + color: red; +} + + +#graph { + width: 100%; + height: 600px; +} diff --git a/src/3dgraph/playground/playground.js b/src/3dgraph/playground/playground.js new file mode 100644 index 00000000..1f8c0b93 --- /dev/null +++ b/src/3dgraph/playground/playground.js @@ -0,0 +1,657 @@ + +var query = null; + + +function load() { + selectDataType(); + + loadCsvExample(); + loadJsonExample(); + loadJavascriptExample(); + loadGooglespreadsheetExample(); + loadDatasourceExample(); + + draw(); +} + + + +/** + * Upate the UI based on the currently selected datatype + */ +function selectDataType() { + var datatype = getDataType(); + + document.getElementById("csv").style.overflow = "hidden"; + document.getElementById("json").style.overflow = "hidden"; + document.getElementById("javascript").style.overflow = "hidden"; + document.getElementById("googlespreadsheet").style.overflow = "hidden"; + document.getElementById("datasource").style.overflow = "hidden"; + + document.getElementById("csv").style.visibility = (datatype == "csv") ? "" : "hidden"; + document.getElementById("json").style.visibility = (datatype == "json") ? "" : "hidden"; + document.getElementById("javascript").style.visibility = (datatype == "javascript") ? "" : "hidden"; + document.getElementById("googlespreadsheet").style.visibility = (datatype == "googlespreadsheet") ? "" : "hidden"; + document.getElementById("datasource").style.visibility = (datatype == "datasource") ? "" : "hidden"; + + document.getElementById("csv").style.height = (datatype == "csv") ? "auto" : "0px"; + document.getElementById("json").style.height = (datatype == "json") ? "auto" : "0px"; + document.getElementById("javascript").style.height = (datatype == "javascript") ? "auto" : "0px"; + document.getElementById("googlespreadsheet").style.height = (datatype == "googlespreadsheet") ? "auto" : "0px"; + document.getElementById("datasource").style.height = (datatype == "datasource") ? "auto" : "0px"; +} + + +function round(value, decimals) { + return parseFloat(value.toFixed(decimals)); +} + +function loadCsvExample() { + var csv = ""; + + // headers + csv += '"x", "y", "value"\n'; + + // create some nice looking data with sin/cos + var steps = 30; + var axisMax = 314; + var axisStep = axisMax / steps; + for (var x = 0; x < axisMax; x+=axisStep) { + for (var y = 0; y < axisMax; y+=axisStep) { + var value = Math.sin(x/50) * Math.cos(y/50) * 50 + 50; + + csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(value, 2) + '\n'; + } + } + + document.getElementById("csvTextarea").innerHTML = csv; + + // also adjust some settings + document.getElementById("style").value = "surface"; + document.getElementById("verticalRatio").value = "0.5"; +} + + +function loadCsvAnimationExample() { + var csv = ""; + + // headers + csv += '"x", "y", "value", "time"\n'; + + // create some nice looking data with sin/cos + var steps = 20; + var axisMax = 314; + var tMax = 31; + var axisStep = axisMax / steps; + for (var t = 0; t < tMax; t++) { + for (var x = 0; x < axisMax; x+=axisStep) { + for (var y = 0; y < axisMax; y+=axisStep) { + var value = Math.sin(x/50 + t/10) * Math.cos(y/50 + t/10) * 50 + 50; + csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(value, 2) + ', ' + t + '\n'; + } + } + } + + document.getElementById("csvTextarea").innerHTML = csv; + + // also adjust some settings + document.getElementById("style").value = "surface"; + document.getElementById("verticalRatio").value = "0.5"; + document.getElementById("animationInterval").value = 100; + +} + + +function loadCsvLineExample() { + var csv = ""; + + // headers + csv += '"sin(t)", "cos(t)", "t"\n'; + + // create some nice looking data with sin/cos + var steps = 100; + var axisMax = 314; + var tmax = 4 * 2 * Math.PI; + var axisStep = axisMax / steps; + for (t = 0; t < tmax; t += tmax / steps) { + var r = 1; + var x = r * Math.sin(t); + var y = r * Math.cos(t); + var z = t; + csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(z, 2) + '\n'; + } + + document.getElementById("csvTextarea").innerHTML = csv; + + // also adjust some settings + document.getElementById("style").value = "line"; + document.getElementById("verticalRatio").value = "1.0"; + document.getElementById("showPerspective").checked = false; +} + +function loadCsvMovingDotsExample() { + var csv = ""; + + // headers + csv += '"x", "y", "z", "color value", "time"\n'; + + // create some shortcuts to math functions + var sin = Math.sin; + var cos = Math.cos; + var pi = Math.PI; + + // create the animation data + var tmax = 2.0 * pi; + var tstep = tmax / 75; + var dotCount = 1; // set this to 1, 2, 3, 4, ... + for (var t = 0; t < tmax; t += tstep) { + var tgroup = parseFloat(t.toFixed(2)); + var value = t; + + // a dot in the center + var x = 0; + var y = 0; + var z = 0; + csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(z, 2) + ', ' + round(value, 2)+ ', ' + round(tgroup, 2) + '\n'; + + // one or multiple dots moving around the center + for (var dot = 0; dot < dotCount; dot++) { + var tdot = t + 2*pi * dot / dotCount; + //data.addRow([sin(tdot), cos(tdot), sin(tdot), value, tgroup]); + //data.addRow([sin(tdot), -cos(tdot), sin(tdot + tmax*1/2), value, tgroup]); + + var x = sin(tdot); + var y = cos(tdot); + var z = sin(tdot); + csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(z, 2) + ', ' + round(value, 2)+ ', ' + round(tgroup, 2) + '\n'; + + var x = sin(tdot); + var y = -cos(tdot); + var z = sin(tdot + tmax*1/2); + csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(z, 2) + ', ' + round(value, 2)+ ', ' + round(tgroup, 2) + '\n'; + + } + } + + document.getElementById("csvTextarea").innerHTML = csv; + + // also adjust some settings + document.getElementById("style").value = "dot-color"; + document.getElementById("verticalRatio").value = "1.0"; + document.getElementById("animationInterval").value = "35"; + document.getElementById("animationAutoStart").checked = true; + document.getElementById("showPerspective").checked = true; +} + +function loadCsvColoredDotsExample() { + var csv = ""; + + // headers + csv += '"x", "y", "z", "distance"\n'; + + // create some shortcuts to math functions + var sqrt = Math.sqrt; + var pow = Math.pow; + var random = Math.random; + + // create the animation data + var imax = 200; + for (var i = 0; i < imax; i++) { + var x = pow(random(), 2); + var y = pow(random(), 2); + var z = pow(random(), 2); + var dist = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)); + + csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(z, 2) + ', ' + round(dist, 2)+ '\n'; + } + + document.getElementById("csvTextarea").innerHTML = csv; + + // also adjust some settings + document.getElementById("style").value = "dot-color"; + document.getElementById("verticalRatio").value = "1.0"; + document.getElementById("showPerspective").checked = true; +} + +function loadCsvSizedDotsExample() { + var csv = ""; + + // headers + csv += '"x", "y", "z", "range"\n'; + + // create some shortcuts to math functions + var sqrt = Math.sqrt; + var pow = Math.pow; + var random = Math.random; + + // create the animation data + var imax = 200; + for (var i = 0; i < imax; i++) { + var x = pow(random(), 2); + var y = pow(random(), 2); + var z = pow(random(), 2); + var dist = sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)); + var range = sqrt(2) - dist; + + csv += round(x, 2) + ', ' + round(y, 2) + ', ' + round(z, 2) + ', ' + round(range, 2)+ '\n'; + } + + document.getElementById("csvTextarea").innerHTML = csv; + + // also adjust some settings + document.getElementById("style").value = "dot-size"; + document.getElementById("verticalRatio").value = "1.0"; + document.getElementById("showPerspective").checked = true; +} + + +function loadJsonExample() { + var json = ""; + // TODO: get json working + + // headers + json += + '{\n' + + ' "cols":[\n' + + ' {"id":"x",\n' + + ' "label":"x",\n' + + ' "type":"number"},\n' + + ' {"id":"y",\n' + + ' "label":"y",\n' + + ' "type":"number"},\n' + + ' {"id":"value",\n' + + ' "label":"value",\n' + + ' "type":"number"}\n' + + ' ],\n' + + ' "rows":['; + + // create some nice looking data with sin/cos + var steps = 20; + var axisMax = 314; + var first = true; + var axisStep = axisMax / steps; + for (var x = 0; x < axisMax; x+=axisStep) { + for (var y = 0; y < axisMax; y+=axisStep) { + var value = Math.sin(x/50) * Math.cos(y/50) * 50 + 50; + if (first) { + json += '\n'; + first = false; + } + else { + json += ',\n'; + } + + json += ' {"c":[{"v":' + round(x, 2) + '}, {"v":' + round(y, 2) + '}, {"v":' + round(value, 2) + '}]}'; + } + } + + // end of the table + json += + '\n' + + ' ]\n' + + '}\n'; + + document.getElementById("jsonTextarea").innerHTML = json; + + document.getElementById("verticalRatio").value = "0.5"; +} + + +function loadJavascriptExample() { + var js = + 'data = new google.visualization.DataTable();\n' + + 'data.addColumn("number", "x");\n' + + 'data.addColumn("number", "y");\n' + + 'data.addColumn("number", "value");\n' + + '\n'; + + js += '// insert data\n'; + + var axisStep = 7; + for (var x = -100; x < 100; x += axisStep) { + for (var y = -100; y < 300; y += axisStep) { + //var value = Math.sin(x/50) * Math.cos(y/50) * 50 + 50; + + var d = Math.sqrt(Math.pow(x/100, 2) + Math.pow(y/100, 2)); + var value = 50 * Math.exp(-5 * d / 10) * Math.sin(d*5) + + js += 'data.addRow([' + round(x, 2) + ', ' + round(y,2) + ', ' + round(value, 2) + ']);\n'; + } + } + + document.getElementById("javascriptTextarea").innerHTML = js; + + document.getElementById("verticalRatio").value = "0.5"; +} + +function loadJavascriptFunctionExample() { + var js = + 'data = new google.visualization.DataTable();\n' + + 'data.addColumn("number", "x");\n' + + 'data.addColumn("number", "y");\n' + + 'data.addColumn("number", "value");\n' + + '\n' + + '// create some nice looking data with sin/cos\n' + + 'var steps = 50;\n' + + 'var axisMax = 314;\n' + + 'axisStep = axisMax / steps;\n' + + 'for (var x = 0; x < axisMax; x+=axisStep) {\n' + + ' for (var y = 0; y < axisMax; y+=axisStep) {\n' + + ' var value = Math.sin(x/50) * Math.cos(y/50) * 50 + 50;\n' + + ' data.addRow([x, y, value]);\n' + + ' }\n' + + '}'; + + document.getElementById("javascriptTextarea").innerHTML = js; + + document.getElementById("verticalRatio").value = "0.5"; +} + +function loadGooglespreadsheetExample() { + var url = + "https://spreadsheets.google.com/a/almende.org/ccc?key=tJ6gaeq2Ldy82VVMr5dPQoA&hl=en#gid=0"; + + document.getElementById("googlespreadsheetText").value = url; + + document.getElementById("verticalRatio").value = "0.5"; +} + + +function loadDatasourceExample() { + var url = "datasource.php"; + + document.getElementById("datasourceText").value = url; + + document.getElementById("verticalRatio").value = "0.5"; +} + + + +/** + * Retrieve teh currently selected datatype + * @return {string} datatype + */ +function getDataType() { + if (document.getElementById("datatypeCsv").checked) return "csv"; + if (document.getElementById("datatypeJson").checked) return "json"; + if (document.getElementById("datatypeJavascript").checked) return "javascript"; + if (document.getElementById("datatypeDatasource").checked) return "datasource"; + if (document.getElementById("datatypeGooglespreadsheet").checked) return "googlespreadsheet"; +} + + +/** + * Retrieve the datatable from the entered contents of the csv text + * @return {Google DataTable} + */ +function getDataCsv() { + var csv = document.getElementById("csvTextarea").value; + + // parse the csv content + var csvArray = csv2array(csv); + + // the first line of the csv file contains the column names + var data = new google.visualization.DataTable(); + var columnTypes = []; + var row = 0; + for (var col = 0; col < csvArray[row].length; col++) { + var label = csvArray[row][col]; + var columnType = "number"; + + if (col >= 4) { + if (csvArray.length > 1) { + var value = csvArray[1][3]; + if (value) { + columnType = typeof(value); + } + } + else { + columnType = "string"; + } + } + columnTypes[col] = columnType; + + data.addColumn(columnType, label); + } + + // read all data + var colCount = data.getNumberOfColumns(); + for (var row = 1; row < csvArray.length; row++) { + var rowData = csvArray[row]; + if (rowData.length == colCount) { + data.addRow(); + + for (var col = 0; col < csvArray[row].length; col++) { + if (columnTypes[col] == 'number') { + var value = parseFloat(csvArray[row][col]); + } + else { + var value = trim(csvArray[row][col]); + } + //alert(value) + data.setValue(row-1, col, value); + } + } + } + + return data; +} + +/** + * remove leading and trailing spaces + */ +function trim(text) { + while (text.length && text.charAt(0) == ' ') + text = text.substr(1); + + while (text.length && text.charAt(text.length-1) == ' ') + text = text.substr(0, text.length-1); + + return text; +} + +/** + * Retrieve the datatable from the entered contents of the javascript text + * @return {Google DataTable} + */ +function getDataJson() { + var json = document.getElementById("jsonTextarea").value; + var data = new google.visualization.DataTable(json); + + return data; +} + + +/** + * Retrieve the datatable from the entered contents of the javascript text + * @return {Google DataTable} + */ +function getDataJavascript() { + var js = document.getElementById("javascriptTextarea").value; + + eval(js); + + return data; +} + + +/** + * Retrieve the datatable from the entered contents of the datasource text + * @return {Google DataTable} + */ +function getDataDatasource() { + // TODO + + throw "Sorry, datasource is not yet implemented..."; +} + +/** + * Retrieve a JSON object with all options + */ +function getOptions() { + return { + width: document.getElementById("width").value, + height: document.getElementById("height").value, + style: document.getElementById("style").value, + showAnimationControls: (document.getElementById("showAnimationControls").checked != false), + showGrid: (document.getElementById("showGrid").checked != false), + showPerspective: (document.getElementById("showPerspective").checked != false), + showShadow: (document.getElementById("showShadow").checked != false), + keepAspectRatio: (document.getElementById("keepAspectRatio").checked != false), + verticalRatio: document.getElementById("verticalRatio").value, + animationInterval: document.getElementById("animationInterval").value, + animationPreload: (document.getElementById("animationPreload").checked != false), + animationAutoStart:(document.getElementById("animationAutoStart").checked != false), + + xCenter: Number(document.getElementById("xCenter").value) || undefined, + yCenter: Number(document.getElementById("yCenter").value) || undefined, + + xMin: Number(document.getElementById("xMin").value) || undefined, + xMax: Number(document.getElementById("xMax").value) || undefined, + xStep: Number(document.getElementById("xStep").value) || undefined, + yMin: Number(document.getElementById("yMin").value) || undefined, + yMax: Number(document.getElementById("yMax").value) || undefined, + yStep: Number(document.getElementById("yStep").value) || undefined, + zMin: Number(document.getElementById("zMin").value) || undefined, + zMax: Number(document.getElementById("zMax").value) || undefined, + zStep: Number(document.getElementById("zStep").value) || undefined, + + valueMin: Number(document.getElementById("valueMin").value) || undefined, + valueMax: Number(document.getElementById("valueMax").value) || undefined, + + xBarWidth: Number(document.getElementById("xBarWidth").value) || undefined, + yBarWidth: Number(document.getElementById("yBarWidth").value) || undefined + }; +} + +/** + * Redraw the graph with the entered data and options + */ +function draw() { + try { + var datatype = getDataType(); + + switch (datatype) { + case "csv": return drawCsv(); + case "json": return drawJson(); + case "javascript": return drawJavascript(); + case "googlespreadsheet": return drawGooglespreadsheet(); + case "datasource": return drawDatasource(); + default: throw "Error: no data type specified"; + } + } + catch (error) { + document.getElementById('graph').innerHTML = + "" + error + ""; + } +} + +function drawCsv() { + // Instantiate our graph object. + var graph = new links.Graph3d(document.getElementById('graph')); + + // retrieve data and options + var data = getDataCsv(); + var options = getOptions(); + + // Draw our graph with the created data and options + graph.draw(data, options); +} + +function drawJson() { + // Instantiate our graph object. + var graph = new links.Graph3d(document.getElementById('graph')); + + // retrieve data and options + var data = getDataJson(); + var options = getOptions(); + + // Draw our graph with the created data and options + graph.draw(data, options); +} + +function drawJavascript() { + // Instantiate our graph object. + var graph = new links.Graph3d(document.getElementById('graph')); + + // retrieve data and options + var data = getDataJavascript(); + var options = getOptions(); + + // Draw our graph with the created data and options + graph.draw(data, options); +} + + +function drawGooglespreadsheet() { + // Instantiate our graph object. + drawGraph = function(response) { + document.getElementById("draw").disabled = ""; + + if (response.isError()) { + error = 'Error: ' + response.getMessage(); + document.getElementById('graph').innerHTML = + "" + error + ""; ; + } + + // retrieve the data from the query response + data = response.getDataTable(); + + // specify options + options = getOptions(); + + // Instantiate our graph object. + var graph = new links.Graph3d(document.getElementById('graph')); + + // Draw our graph with the created data and options + graph.draw(data, options); + } + + url = document.getElementById("googlespreadsheetText").value; + document.getElementById("draw").disabled = "disabled"; + + // send the request + query && query.abort(); + query = new google.visualization.Query(url); + query.send(drawGraph); +} + + +function drawDatasource() { + // Instantiate our graph object. + drawGraph = function(response) { + document.getElementById("draw").disabled = ""; + + if (response.isError()) { + error = 'Error: ' + response.getMessage(); + document.getElementById('graph').innerHTML = + "" + error + ""; ; + } + + // retrieve the data from the query response + data = response.getDataTable(); + + // specify options + options = getOptions(); + + // Instantiate our graph object. + var graph = new links.Graph3d(document.getElementById('graph')); + + // Draw our graph with the created data and options + graph.draw(data, options); + }; + + url = document.getElementById("datasourceText").value; + document.getElementById("draw").disabled = "disabled"; + + // if the entered url is a google spreadsheet url, replace the part + // "/ccc?" with "/tq?" in order to retrieve a neat data query result + if (url.indexOf("/ccc?")) { + url.replace("/ccc?", "/tq?"); + } + + // send the request + query && query.abort(); + query = new google.visualization.Query(url); + query.send(drawGraph); +} diff --git a/src/3dgraph/playground/prettify/lang-apollo.js b/src/3dgraph/playground/prettify/lang-apollo.js new file mode 100644 index 00000000..bfc0014c --- /dev/null +++ b/src/3dgraph/playground/prettify/lang-apollo.js @@ -0,0 +1,2 @@ +PR.registerLangHandler(PR.createSimpleLexer([["com",/^#[^\r\n]*/,null,"#"],["pln",/^[\t\n\r \xA0]+/,null,"\t\n\r \u00a0"],["str",/^\"(?:[^\"\\]|\\[\s\S])*(?:\"|$)/,null,'"']],[["kwd",/^(?:ADS|AD|AUG|BZF|BZMF|CAE|CAF|CA|CCS|COM|CS|DAS|DCA|DCOM|DCS|DDOUBL|DIM|DOUBLE|DTCB|DTCF|DV|DXCH|EDRUPT|EXTEND|INCR|INDEX|NDX|INHINT|LXCH|MASK|MSK|MP|MSU|NOOP|OVSK|QXCH|RAND|READ|RELINT|RESUME|RETURN|ROR|RXOR|SQUARE|SU|TCR|TCAA|OVSK|TCF|TC|TS|WAND|WOR|WRITE|XCH|XLQ|XXALQ|ZL|ZQ|ADD|ADZ|SUB|SUZ|MPY|MPR|MPZ|DVP|COM|ABS|CLA|CLZ|LDQ|STO|STQ|ALS|LLS|LRS|TRA|TSQ|TMI|TOV|AXT|TIX|DLY|INP|OUT)\s/, +null],["typ",/^(?:-?GENADR|=MINUS|2BCADR|VN|BOF|MM|-?2CADR|-?[1-6]DNADR|ADRES|BBCON|[SE]?BANK\=?|BLOCK|BNKSUM|E?CADR|COUNT\*?|2?DEC\*?|-?DNCHAN|-?DNPTR|EQUALS|ERASE|MEMORY|2?OCT|REMADR|SETLOC|SUBRO|ORG|BSS|BES|SYN|EQU|DEFINE|END)\s/,null],["lit",/^\'(?:-*(?:\w|\\[\x21-\x7e])(?:[\w-]*|\\[\x21-\x7e])[=!?]?)?/],["pln",/^-*(?:[!-z_]|\\[\x21-\x7e])(?:[\w-]*|\\[\x21-\x7e])[=!?]?/i],["pun",/^[^\w\t\n\r \xA0()\"\\\';]+/]]),["apollo","agc","aea"]) \ No newline at end of file diff --git a/src/3dgraph/playground/prettify/lang-css.js b/src/3dgraph/playground/prettify/lang-css.js new file mode 100644 index 00000000..61157f38 --- /dev/null +++ b/src/3dgraph/playground/prettify/lang-css.js @@ -0,0 +1,2 @@ +PR.registerLangHandler(PR.createSimpleLexer([["pln",/^[ \t\r\n\f]+/,null," \t\r\n\u000c"]],[["str",/^\"(?:[^\n\r\f\\\"]|\\(?:\r\n?|\n|\f)|\\[\s\S])*\"/,null],["str",/^\'(?:[^\n\r\f\\\']|\\(?:\r\n?|\n|\f)|\\[\s\S])*\'/,null],["lang-css-str",/^url\(([^\)\"\']*)\)/i],["kwd",/^(?:url|rgb|\!important|@import|@page|@media|@charset|inherit)(?=[^\-\w]|$)/i,null],["lang-css-kw",/^(-?(?:[_a-z]|(?:\\[0-9a-f]+ ?))(?:[_a-z0-9\-]|\\(?:\\[0-9a-f]+ ?))*)\s*:/i],["com",/^\/\*[^*]*\*+(?:[^\/*][^*]*\*+)*\//], +["com",/^(?: + + + + + + + + + + +
+ +
+ + diff --git a/src/3dgraph/tests/test_extreme_data.html b/src/3dgraph/tests/test_extreme_data.html new file mode 100644 index 00000000..f9e55031 --- /dev/null +++ b/src/3dgraph/tests/test_extreme_data.html @@ -0,0 +1,78 @@ + + + + Graph 3D demo + + + + + + + + + + + + + + +
+ +
+ + diff --git a/src/3dgraph/tests/test_slider.html b/src/3dgraph/tests/test_slider.html new file mode 100644 index 00000000..7f7c7f01 --- /dev/null +++ b/src/3dgraph/tests/test_slider.html @@ -0,0 +1,43 @@ + + + Graph 3D demo + + + + + + + + + + + + +
+ +
+ +