Daniel@0
|
1 "use strict";
|
Daniel@0
|
2
|
Daniel@0
|
3 App.module("GraphicsRenderingModule", function(GraphicsRenderingModule, App, Backbone, Marionette, $, _, Logger) {
|
Daniel@0
|
4
|
Daniel@0
|
5 GraphicsRenderingModule.addInitializer(function(options){
|
Daniel@0
|
6 GraphicsRenderingModule.logger = Logger.get("GraphicsRenderingModule");
|
Daniel@0
|
7
|
Daniel@0
|
8
|
Daniel@0
|
9 GraphicsRenderingModule._formatNumberForTooltip = function(number) {
|
Daniel@0
|
10 var nDigits = 2;
|
Daniel@0
|
11 if (Math.abs(number) > 1000 || Math.floor(number) == number) {
|
Daniel@0
|
12 nDigits = 0;
|
Daniel@0
|
13 } else if (Math.abs(number) > 100) {
|
Daniel@0
|
14 nDigits = 1;
|
Daniel@0
|
15 }
|
Daniel@0
|
16 return _.str.numberFormat(number, nDigits);
|
Daniel@0
|
17 },
|
Daniel@0
|
18
|
Daniel@0
|
19 GraphicsRenderingModule.__handleVegaMouseEvent = function() {
|
Daniel@0
|
20
|
Daniel@0
|
21 var pointerEventType = arguments[0].type;
|
Daniel@0
|
22
|
Daniel@0
|
23 if (pointerEventType == "mouseover" || pointerEventType == "mouseout") {
|
Daniel@0
|
24 var $vegaNode = $(arguments[0].target).closest("div");
|
Daniel@0
|
25 if (pointerEventType == "mouseover") {
|
Daniel@0
|
26 $vegaNode.data("vegaHoveredItem", arguments[1]);
|
Daniel@0
|
27 } else {
|
Daniel@0
|
28 $vegaNode.removeData("vegaHoveredItem");
|
Daniel@0
|
29 }
|
Daniel@0
|
30 }
|
Daniel@0
|
31
|
Daniel@0
|
32 var data = null;
|
Daniel@0
|
33 if (arguments[1] && arguments[1].datum) {
|
Daniel@0
|
34 data = arguments[1].datum;
|
Daniel@0
|
35 }
|
Daniel@0
|
36 if (_.isObject(data) && data.tooltip) {
|
Daniel@0
|
37 // FIXME TMP HACK FOR GEOGRPAPHY
|
Daniel@0
|
38 App.TooltipModule.tooltipView.update(arguments[0], _.isArray(data.tooltip) ? data.tooltip[2] : data.tooltip, data);
|
Daniel@0
|
39 } else if (_.isArray(data)) {
|
Daniel@0
|
40 var tooltip = data[0];
|
Daniel@0
|
41 if (tooltip) {
|
Daniel@0
|
42 var labelParts = [];
|
Daniel@0
|
43
|
Daniel@0
|
44 if (data[0] == "tooltip_range") {
|
Daniel@0
|
45 labelParts.push(GraphicsRenderingModule._formatNumberForTooltip(data[2]));
|
Daniel@0
|
46 labelParts.push(" ... ");
|
Daniel@0
|
47 labelParts.push(GraphicsRenderingModule._formatNumberForTooltip(data[3]));
|
Daniel@0
|
48 labelParts.push(" → ");
|
Daniel@0
|
49 labelParts.push(GraphicsRenderingModule._formatNumberForTooltip(data[1]).replace("-", "−"));
|
Daniel@0
|
50 } else if (data[0] == "tooltip_point") {
|
Daniel@0
|
51 if (data[2] * 1 === data[2]) {
|
Daniel@0
|
52 labelParts.push(GraphicsRenderingModule._formatNumberForTooltip(data[2]));
|
Daniel@0
|
53 } else {
|
Daniel@0
|
54 labelParts.push(data[2]);
|
Daniel@0
|
55 }
|
Daniel@0
|
56 labelParts.push(" → ");
|
Daniel@0
|
57 labelParts.push(GraphicsRenderingModule._formatNumberForTooltip(data[1]));
|
Daniel@0
|
58 } else if (data[0] == "tooltip_value") {
|
Daniel@0
|
59 labelParts.push(GraphicsRenderingModule._formatNumberForTooltip(data[1]).replace("-", "−"));
|
Daniel@0
|
60 }
|
Daniel@0
|
61
|
Daniel@0
|
62 // prefix
|
Daniel@0
|
63 if (data[4]) {
|
Daniel@0
|
64 labelParts.unshift(data[4]);
|
Daniel@0
|
65 }
|
Daniel@0
|
66 // suffix
|
Daniel@0
|
67 if (data[5]) {
|
Daniel@0
|
68 labelParts.push(data[5]);
|
Daniel@0
|
69 }
|
Daniel@0
|
70
|
Daniel@0
|
71 App.TooltipModule.tooltipView.update(arguments[0], labelParts.join(""), data);
|
Daniel@0
|
72 }
|
Daniel@0
|
73 } else {
|
Daniel@0
|
74 App.TooltipModule.tooltipView.update();
|
Daniel@0
|
75 }
|
Daniel@0
|
76 },
|
Daniel@0
|
77
|
Daniel@0
|
78 /**
|
Daniel@0
|
79 * Draws a chart using vega, then executes callback when ready
|
Daniel@0
|
80 // */
|
Daniel@0
|
81 // GraphicsRenderingModule.vega = function ($domNode, spec, renderer, callback) {
|
Daniel@0
|
82 // var _this = this;
|
Daniel@0
|
83 // vg.parse.spec(spec, function(chart) { chart({
|
Daniel@0
|
84 // el:$domNode.get(0),
|
Daniel@0
|
85 // renderer: renderer ? renderer : "canvas",
|
Daniel@0
|
86 // //hover: false
|
Daniel@0
|
87 // })
|
Daniel@0
|
88 // .on('mouseover', GraphicsRenderingModule.__handleVegaMouseEvent)
|
Daniel@0
|
89 // .on('mouseout', GraphicsRenderingModule.__handleVegaMouseEvent)
|
Daniel@0
|
90 // .on('mousemove', GraphicsRenderingModule.__handleVegaMouseEvent)
|
Daniel@0
|
91 // .update();
|
Daniel@0
|
92 // });
|
Daniel@0
|
93 // if (_.isFunction(callback)) {
|
Daniel@0
|
94 // var vegaIsReadyTimeout = setTimeout(function() {
|
Daniel@0
|
95 // if (_this.vegaIsReady($domNode)) {
|
Daniel@0
|
96 // callback($domNode);
|
Daniel@0
|
97 // clearTimeout(vegaIsReadyTimeout);
|
Daniel@0
|
98 // }
|
Daniel@0
|
99 // }, 50);
|
Daniel@0
|
100 // }
|
Daniel@0
|
101 // };
|
Daniel@0
|
102 GraphicsRenderingModule.vegaIsReady = function($domNode) {
|
Daniel@0
|
103 return $domNode.hasClass('vega');
|
Daniel@0
|
104 };
|
Daniel@0
|
105
|
Daniel@0
|
106 GraphicsRenderingModule.vegaAsync = function ($domNode, spec, renderer, callback) {
|
Daniel@0
|
107 var rand = Math.random();
|
Daniel@0
|
108 $domNode.data("vega-async-rand", rand);
|
Daniel@0
|
109 var $tempNode = $("<div/>");
|
Daniel@0
|
110 setTimeout(function() {
|
Daniel@0
|
111 vg.parse.spec(spec, function(chart) {
|
Daniel@0
|
112 var vegaObj = chart({
|
Daniel@0
|
113 el:$tempNode.get(0),
|
Daniel@0
|
114 renderer: App.options.vegaRenderer ? App.options.vegaRenderer : renderer,
|
Daniel@0
|
115 //hover: false
|
Daniel@0
|
116 })
|
Daniel@0
|
117 .on('mouseover', GraphicsRenderingModule.__handleVegaMouseEvent)
|
Daniel@0
|
118 .on('mousemove', GraphicsRenderingModule.__handleVegaMouseEvent)
|
Daniel@0
|
119 .on('mouseout', GraphicsRenderingModule.__handleVegaMouseEvent)
|
Daniel@0
|
120 .update();
|
Daniel@0
|
121 var $vegaNode = $tempNode.children(0);
|
Daniel@0
|
122 $vegaNode.data("vegaObj", vegaObj)
|
Daniel@0
|
123
|
Daniel@0
|
124 //console.log("VEGA DATA", vg, vegaObj._model._data);
|
Daniel@0
|
125 var vegaIsReadyInterval = setInterval(function() {
|
Daniel@0
|
126 if ($domNode.data("vega-async-rand") !== rand) {
|
Daniel@0
|
127 clearInterval(vegaIsReadyInterval);
|
Daniel@0
|
128 return;
|
Daniel@0
|
129 }
|
Daniel@0
|
130 if (GraphicsRenderingModule.vegaIsReady($vegaNode)) {
|
Daniel@0
|
131 $vegaNode.addClass("vic-vega")
|
Daniel@0
|
132 $domNode.empty().append($vegaNode);
|
Daniel@0
|
133 clearInterval(vegaIsReadyInterval);
|
Daniel@0
|
134 }
|
Daniel@0
|
135 }, 50);
|
Daniel@0
|
136 });
|
Daniel@0
|
137 }, 10);
|
Daniel@0
|
138 };
|
Daniel@0
|
139
|
Daniel@0
|
140
|
Daniel@0
|
141 });
|
Daniel@0
|
142 }, Logger);
|