Daniel@0: "use strict"; Daniel@0: Daniel@0: App.module("GraphicsRenderingModule", function(GraphicsRenderingModule, App, Backbone, Marionette, $, _, Logger) { Daniel@0: Daniel@0: GraphicsRenderingModule.addInitializer(function(options){ Daniel@0: GraphicsRenderingModule.logger = Logger.get("GraphicsRenderingModule"); Daniel@0: Daniel@0: Daniel@0: GraphicsRenderingModule._formatNumberForTooltip = function(number) { Daniel@0: var nDigits = 2; Daniel@0: if (Math.abs(number) > 1000 || Math.floor(number) == number) { Daniel@0: nDigits = 0; Daniel@0: } else if (Math.abs(number) > 100) { Daniel@0: nDigits = 1; Daniel@0: } Daniel@0: return _.str.numberFormat(number, nDigits); Daniel@0: }, Daniel@0: Daniel@0: GraphicsRenderingModule.__handleVegaMouseEvent = function() { Daniel@0: Daniel@0: var pointerEventType = arguments[0].type; Daniel@0: Daniel@0: if (pointerEventType == "mouseover" || pointerEventType == "mouseout") { Daniel@0: var $vegaNode = $(arguments[0].target).closest("div"); Daniel@0: if (pointerEventType == "mouseover") { Daniel@0: $vegaNode.data("vegaHoveredItem", arguments[1]); Daniel@0: } else { Daniel@0: $vegaNode.removeData("vegaHoveredItem"); Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: var data = null; Daniel@0: if (arguments[1] && arguments[1].datum) { Daniel@0: data = arguments[1].datum; Daniel@0: } Daniel@0: if (_.isObject(data) && data.tooltip) { Daniel@0: // FIXME TMP HACK FOR GEOGRPAPHY Daniel@0: App.TooltipModule.tooltipView.update(arguments[0], _.isArray(data.tooltip) ? data.tooltip[2] : data.tooltip, data); Daniel@0: } else if (_.isArray(data)) { Daniel@0: var tooltip = data[0]; Daniel@0: if (tooltip) { Daniel@0: var labelParts = []; Daniel@0: Daniel@0: if (data[0] == "tooltip_range") { Daniel@0: labelParts.push(GraphicsRenderingModule._formatNumberForTooltip(data[2])); Daniel@0: labelParts.push(" ... "); Daniel@0: labelParts.push(GraphicsRenderingModule._formatNumberForTooltip(data[3])); Daniel@0: labelParts.push(" → "); Daniel@0: labelParts.push(GraphicsRenderingModule._formatNumberForTooltip(data[1]).replace("-", "−")); Daniel@0: } else if (data[0] == "tooltip_point") { Daniel@0: if (data[2] * 1 === data[2]) { Daniel@0: labelParts.push(GraphicsRenderingModule._formatNumberForTooltip(data[2])); Daniel@0: } else { Daniel@0: labelParts.push(data[2]); Daniel@0: } Daniel@0: labelParts.push(" → "); Daniel@0: labelParts.push(GraphicsRenderingModule._formatNumberForTooltip(data[1])); Daniel@0: } else if (data[0] == "tooltip_value") { Daniel@0: labelParts.push(GraphicsRenderingModule._formatNumberForTooltip(data[1]).replace("-", "−")); Daniel@0: } Daniel@0: Daniel@0: // prefix Daniel@0: if (data[4]) { Daniel@0: labelParts.unshift(data[4]); Daniel@0: } Daniel@0: // suffix Daniel@0: if (data[5]) { Daniel@0: labelParts.push(data[5]); Daniel@0: } Daniel@0: Daniel@0: App.TooltipModule.tooltipView.update(arguments[0], labelParts.join(""), data); Daniel@0: } Daniel@0: } else { Daniel@0: App.TooltipModule.tooltipView.update(); Daniel@0: } Daniel@0: }, Daniel@0: Daniel@0: /** Daniel@0: * Draws a chart using vega, then executes callback when ready Daniel@0: // */ Daniel@0: // GraphicsRenderingModule.vega = function ($domNode, spec, renderer, callback) { Daniel@0: // var _this = this; Daniel@0: // vg.parse.spec(spec, function(chart) { chart({ Daniel@0: // el:$domNode.get(0), Daniel@0: // renderer: renderer ? renderer : "canvas", Daniel@0: // //hover: false Daniel@0: // }) Daniel@0: // .on('mouseover', GraphicsRenderingModule.__handleVegaMouseEvent) Daniel@0: // .on('mouseout', GraphicsRenderingModule.__handleVegaMouseEvent) Daniel@0: // .on('mousemove', GraphicsRenderingModule.__handleVegaMouseEvent) Daniel@0: // .update(); Daniel@0: // }); Daniel@0: // if (_.isFunction(callback)) { Daniel@0: // var vegaIsReadyTimeout = setTimeout(function() { Daniel@0: // if (_this.vegaIsReady($domNode)) { Daniel@0: // callback($domNode); Daniel@0: // clearTimeout(vegaIsReadyTimeout); Daniel@0: // } Daniel@0: // }, 50); Daniel@0: // } Daniel@0: // }; Daniel@0: GraphicsRenderingModule.vegaIsReady = function($domNode) { Daniel@0: return $domNode.hasClass('vega'); Daniel@0: }; Daniel@0: Daniel@0: GraphicsRenderingModule.vegaAsync = function ($domNode, spec, renderer, callback) { Daniel@0: var rand = Math.random(); Daniel@0: $domNode.data("vega-async-rand", rand); Daniel@0: var $tempNode = $("
"); Daniel@0: setTimeout(function() { Daniel@0: vg.parse.spec(spec, function(chart) { Daniel@0: var vegaObj = chart({ Daniel@0: el:$tempNode.get(0), Daniel@0: renderer: App.options.vegaRenderer ? App.options.vegaRenderer : renderer, Daniel@0: //hover: false Daniel@0: }) Daniel@0: .on('mouseover', GraphicsRenderingModule.__handleVegaMouseEvent) Daniel@0: .on('mousemove', GraphicsRenderingModule.__handleVegaMouseEvent) Daniel@0: .on('mouseout', GraphicsRenderingModule.__handleVegaMouseEvent) Daniel@0: .update(); Daniel@0: var $vegaNode = $tempNode.children(0); Daniel@0: $vegaNode.data("vegaObj", vegaObj) Daniel@0: Daniel@0: //console.log("VEGA DATA", vg, vegaObj._model._data); Daniel@0: var vegaIsReadyInterval = setInterval(function() { Daniel@0: if ($domNode.data("vega-async-rand") !== rand) { Daniel@0: clearInterval(vegaIsReadyInterval); Daniel@0: return; Daniel@0: } Daniel@0: if (GraphicsRenderingModule.vegaIsReady($vegaNode)) { Daniel@0: $vegaNode.addClass("vic-vega") Daniel@0: $domNode.empty().append($vegaNode); Daniel@0: clearInterval(vegaIsReadyInterval); Daniel@0: } Daniel@0: }, 50); Daniel@0: }); Daniel@0: }, 10); Daniel@0: }; Daniel@0: Daniel@0: Daniel@0: }); Daniel@0: }, Logger);