123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074 |
- /*
- IBM Confidential
- OCO Source Materials
- IBM Cognos Products: irot
- (C) Copyright IBM Corp. 2003, 2018
- The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what has been deposited with the U.S. Copyright Office.
- */
- define([], function(G_Rave2, G_Rave2Library) {
- var G_Rave2Adapter = {};
- G_Rave2Adapter.Initialize = function( v_sAdapterId, v_sAdapterLocation, v_oAdapterSettings )
- {
- if (!this.m_oDeferredInitialize)
- {
- this.m_oDeferredInitialize = Q.defer();
-
- this.M_oAdapterSettings = v_oAdapterSettings;
-
- // if has not been initialized
- if (!this.m_oBundles)
- {
- this.m_oBundles = {};
- this.m_oVisDataDefinitions = {};
- this.m_oRaveConfigs = {};
- var v_sAdapterLocationPath = v_sAdapterLocation.substr(0, v_sAdapterLocation.lastIndexOf("/"));
- // sad assumption here is bundles are deployed as siblings to adapters...
- var v_sBundleLocation = v_sAdapterLocationPath + "/../../bundles";
- var v_oConfig = {
- paths :
- {
- 'rave' : v_sAdapterLocationPath + '/rave/rave.min',
- 'rave-library' : v_sAdapterLocationPath + '/rave-library/rave-library.min',
- 'rave-legends' : v_sAdapterLocationPath + '/rave-legends/rave-legends.min',
- 'rave-layouts' : v_sAdapterLocationPath + '/rave-layouts/rave-layouts.min',
- 'rave-utilities' : v_sAdapterLocationPath + '/rave-utilities/rave-utilities.min',
- 'rave-navigation' : v_sAdapterLocationPath + '/rave-navigation/rave-navigation.min',
- 'rave-accessibility' : v_sAdapterLocationPath + '/rave-accessibility/rave-accessibility.min',
- 'mapboxgl' : v_sBundleLocation + '/rave-library-tiledmapV2/mapbox-gl',
- 'turf' : v_sBundleLocation + '/rave-library-tiledmap/turf.min'
- }
- };
- require(v_oConfig, ["rave", "rave-library", "rave-accessibility"], function(v_oRave, v_oRaveLibrary) {
- this.G_Rave2 = v_oRave;
- this.G_Rave2Library = v_oRaveLibrary;
- this.m_oDeferredInitialize.resolve(true);
- }.bind(this));
- }
- }
- return this.m_oDeferredInitialize.promise;
- };
- G_Rave2Adapter.Destroy = function()
- {
- this.m_bDestroyed = true;
- };
- G_Rave2Adapter.f_getChartTypeInfo = function(v_sChartTypeId)
- {
- var v_aChartTypeParts = v_sChartTypeId.split(",");
- return {
- bundleId : v_aChartTypeParts[0],
- configId : v_aChartTypeParts[1]
- };
- };
- G_Rave2Adapter.LoadVisChartType = function( v_sChartTypeId, v_sBundleLocation, v_fnOnVisChartTypeLoaded )
- {
- var v_oChartTypeInfo = this.f_getChartTypeInfo(v_sChartTypeId);
- var v_sBundleId = v_oChartTypeInfo.bundleId;
- if ( this.m_oBundles[v_sBundleId] )
- {
- v_fnOnVisChartTypeLoaded(v_sChartTypeId);
- return;
- }
- requirejs( [v_sBundleLocation], this.f_onVisBundleLoaded.bind( this, v_sBundleId, v_sChartTypeId, v_fnOnVisChartTypeLoaded ) );
- };
- G_Rave2Adapter.f_onVisBundleLoaded = function( v_sBundleId, v_sChartTypeId, v_fnOnVisChartTypeLoaded, v_oBundle )
- {
- if ( this.m_bDestroyed )
- {
- return;
- }
- this.m_oBundles[v_sBundleId] = v_oBundle;
- v_fnOnVisChartTypeLoaded( v_sChartTypeId );
- };
- G_Rave2Adapter.CreateVis = function( v_sChartTypeId )
- {
- var v_oChartTypeInfo = this.f_getChartTypeInfo(v_sChartTypeId);
- return new C_Rave2Viz( v_sChartTypeId, v_oChartTypeInfo, this.m_oBundles[v_oChartTypeInfo.bundleId] );
- };
- G_Rave2Adapter.f_getRaveConfig = function(v_sChartTypeId)
- {
- if (!this.m_oRaveConfigs[v_sChartTypeId])
- {
- var v_oChartTypeInfo = this.f_getChartTypeInfo(v_sChartTypeId);
- var v_aConfigs = this.m_oBundles[v_oChartTypeInfo.bundleId].configurations();
-
- for (var i = 0; i < v_aConfigs.length; ++i)
- {
- var v_oConfig = v_aConfigs[i];
- this.m_oRaveConfigs[v_oChartTypeInfo.bundleId + "," + v_oConfig.id()] = v_oConfig;
- }
- }
-
- return this.m_oRaveConfigs[v_sChartTypeId];
- };
- G_Rave2Adapter.GetVisDataDefinition = function( v_sChartTypeId )
- {
- var v_oDef = this.m_oVisDataDefinitions[v_sChartTypeId];
- if ( !v_oDef )
- {
- var v_oRaveConfig = this.f_getRaveConfig(v_sChartTypeId);
- v_oDef = { dataSets : [] };
-
- // only use the first data model, the configurations for reporting should ensure it is tabular
- var v_oDataModel = v_oRaveConfig.info().dataModels()[0];
-
- var v_aDataSets = v_oDataModel.dataSets();
-
- for ( var v_iDataSet = 0; v_iDataSet < v_aDataSets.length; v_iDataSet++ )
- {
- var v_oDataSet = v_aDataSets[v_iDataSet];
- var v_oDataDef = { id : v_oDataSet.id(), slots : [] };
- v_oDef.dataSets.push( v_oDataDef );
- var v_aDataSlots = v_oDataSet.dataSlots();
- for ( var v_iSlot = 0; v_iSlot < v_aDataSlots.length; v_iSlot++ )
- {
- var v_oDataSlot = v_aDataSlots[v_iSlot];
- var v_aDataTypes = v_oDataSlot.dataTypes();
- var v_sCardinality = v_oDataSlot.cardinality();
- var v_bOptional = (v_sCardinality == "0..1");
-
- var v_aTags = v_oDataSlot.tags();
- var v_sHandling = null;
- for ( var v_iTag = 0; v_iTag < v_aTags.length; v_iTag++ )
- {
- if ( v_aTags[v_iTag].substr(0, "handling:".length) == "handling:" )
- {
- v_sHandling = v_aTags[v_iTag].substr("handling:".length, v_aTags[v_iTag].length);
- break;
- }
- }
-
- // ignore slots that do not meet the conditions below (similar rules are implemented in VGS)
- if (v_aDataTypes.indexOf("string") != -1 || v_aTags.indexOf("ca.reporting.categorical") != -1)
- {
- v_oDataDef.slots.push({
- id : v_oDataSlot.id(),
- type : "category",
- optional : v_bOptional,
- handling : v_sHandling
- });
- }
- else if (v_aDataTypes.indexOf("numeric") != -1)
- {
- v_oDataDef.slots.push({
- id : v_oDataSlot.id(),
- type : "value",
- optional : v_bOptional,
- handling : v_sHandling
- });
- }
- }
- }
-
- v_oDef.properties = {};
- var v_aProps = v_oRaveConfig.info().properties();
-
- for (var i = 0; i < v_aProps.length; ++i)
- {
- var v_oProp = v_aProps[i];
- v_oDef.properties[v_oProp.id()] = {
- type : v_oProp.type()
- };
- }
- this.m_oVisDataDefinitions[v_sChartTypeId] = v_oDef;
- }
-
- return v_oDef;
- };
- //=======================================
- var C_Rave2Viz = function( v_sChartTypeId, v_oChartTypeInfo, v_oBundle )
- {
- this.m_sChartTypeId = v_sChartTypeId;
- this.m_oChartTypeInfo = v_oChartTypeInfo;
- this.m_oBundle = v_oBundle;
- this.m_sErrorMessage = "";
- this.m_aData = [];
- this.m_oOverridenPropertyDefaults = {};
- this.m_bMouseMoveSet = false;
- };
- C_Rave2Viz.prototype.Initialize = function( v_elContainerDiv, v_oVisSettings, v_fnOnInitializeComplete )
- {
- this.m_elContainerDiv = v_elContainerDiv;
- this.m_elContainerDiv.style.overflow = "hidden";
- this.m_elVisContainer = this.m_elContainerDiv.appendChild( this.m_elContainerDiv.ownerDocument.createElementNS( "http://www.w3.org/2000/svg", "svg" ) );
- this.m_elVisContainer.setAttribute( "height", v_oVisSettings.properties.height || "500" );
- this.m_elVisContainer.setAttribute( "width", v_oVisSettings.properties.width || "500" );
- this.m_oVis = this.m_oBundle.create(this.m_oChartTypeInfo.configId);
-
- var v_oDisconnectActions = G_Rave2Adapter.M_oAdapterSettings.useRave2Interactions ?
- {
- "select" : true,
- "deselect" : true,
- "deselectAll" : true,
- "toggleSelect" : true
- } : null;
- // disconnect defined actions
- var v_aActions = this.m_oVis.info().actions();
-
- for (var i = 0; i < v_aActions.length; ++i)
- {
- var v_sAction = v_aActions[i].id();
- var v_oAction = this.m_oVis.action(v_sAction);
-
- if (!v_oDisconnectActions || v_oDisconnectActions[v_sAction])
- {
- v_oAction.autoBind(false);
- }
- }
-
- this.m_oVis.node( G_Rave2Adapter.G_Rave2.select( this.m_elVisContainer ) );
- this.m_oDataModel = this.m_oVis.createDataModel( this.m_oVis.info().dataModels()[0].id() );
- this.m_oVisDataDefinition = G_Rave2Adapter.GetVisDataDefinition( this.m_sChartTypeId );
- this.ApplySettings( v_oVisSettings );
-
- this.m_bSupportsSelectionActions = Boolean(this.m_oVis.action("deselectAll") && this.m_oVis.action("toggleSelect"));
- this.m_bSupportsHoverActions = Boolean(this.m_oVis.action("unhighlight") && this.m_oVis.action("highlight"));
- if ( v_oVisSettings.callbacks && v_oVisSettings.callbacks["customPolygonError"] )
- {
- this.m_oVis.on( "mappingError", function(v_oCustomPolygonError){
- if ( !this.m_bDestroyed )
- {
- v_oVisSettings.callbacks["customPolygonError"](v_oCustomPolygonError);
- }
- }.bind(this));
- }
- v_fnOnInitializeComplete();
- };
- C_Rave2Viz.prototype.Destroy = function()
- {
- this.m_bDestroyed = true;
- if ( this.m_elVisContainer )
- {
- this.m_elVisContainer.parentNode.removeChild( this.m_elVisContainer );
- this.m_elVisContainer = null;
- }
- this.m_oVis = null;
- };
- C_Rave2Viz.prototype.SetHoverChangeListener = function(v_fnHoverCallback)
- {
- var v_oLocateAction = this.m_oVis.action("locate");
-
- if (v_oLocateAction)
- {
- // only maps support hover locator and require hooking a Rave specific move method after the chart is drawn
- this.m_fnHoverCallbackForLocator = v_fnHoverCallback;
- this.m_elContainerDiv.ondblclick = this.F_CancelBubble;
- }
- else
- {
- this.m_elVisContainer.onmousemove = this.F_Debounce(this.f_onMouseMoveHover.bind(this, v_fnHoverCallback), 200);
- }
- };
- // copy of U_DOM.F_CancelBubble
- C_Rave2Viz.prototype.F_CancelBubble = function( e )
- {
- e.stopPropagation();
- };
- // copy of U_JsUtils.F_Debounce
- C_Rave2Viz.prototype.F_Debounce = function(v_fn, v_iInterval)
- {
- var m_bTimeoutActive = false;
- var m_context = null;
- var m_args = null;
- var m_bDoCall = false;
-
- return function()
- {
- if (m_bTimeoutActive)
- {
- m_context = this;
- m_args = arguments;
- m_bDoCall = true;
- }
- else
- {
- // call immediately
- v_fn.apply(this, arguments);
- m_bTimeoutActive = true;
- setTimeout(function() {
- if (m_bDoCall)
- {
- v_fn.apply(m_context, m_args);
- m_context = m_args = null;
- m_bDoCall = false;
- }
- m_bTimeoutActive = false;
- }, v_iInterval);
- }
- };
- };
- C_Rave2Viz.prototype.Draw = function( v_fnOnDrawComplete )
- {
- this.m_aSelectedSceneItems = null;
-
- if ( this.m_sErrorMessage )
- {
- this.DrawBlank();
- v_fnOnDrawComplete( { error : this.m_sErrorMessage } );
- return;
- }
-
- this.m_oVis.render( function() {
- if (this.m_fnHoverCallbackForLocator && !this.m_bMouseMoveSet)
- {
- this.m_oVis.on("mousemove", this.F_Debounce(this.f_onMouseMoveHoverForLocator.bind(this, this.m_fnHoverCallbackForLocator), 200));
- this.m_bMouseMoveSet = true;
- }
-
- v_fnOnDrawComplete();
- }.bind(this));
- };
- C_Rave2Viz.prototype.DrawHighlights = function(v_aSelectionVisElements, v_aHoverVisElements)
- {
- if (!this.m_bSupportsSelectionActions && !this.m_bSupportsHoverActions)
- {
- return;
- }
-
- var v_oVisNode = this.m_oVis.node();
- var v_aAllElements = v_oVisNode.selectAll(".element-shape");
-
- if (this.m_bSupportsSelectionActions)
- {
- var v_aSelectedSceneItems = (v_aSelectionVisElements && v_aSelectionVisElements.length) ? this.f_createSceneItemsFromVisualElements(v_aSelectionVisElements) : [];
-
- if (!this.m_aSelectedSceneItems || !this.f_isEqualSceneItems(this.m_aSelectedSceneItems, v_aSelectedSceneItems))
- {
- this.m_aSelectedSceneItems = v_aSelectedSceneItems;
-
- this.m_oVis.action("deselectAll")(v_aAllElements);
-
- if (v_aSelectedSceneItems.length)
- {
- this.m_oVis.action("toggleSelect")(v_oVisNode.selectAll(function() { return v_aSelectedSceneItems; }), v_aAllElements);
- }
- }
- }
-
- if (this.m_bSupportsHoverActions)
- {
- // do hover effects
- this.m_oVis.action("unhighlight")(v_aAllElements);
-
- var v_aHoverSceneItems = (v_aHoverVisElements && v_aHoverVisElements.length) ? this.f_createSceneItemsFromVisualElements(v_aHoverVisElements) : null;
-
- if (v_aHoverSceneItems)
- {
- // convert array to a selector and then highlight it
- this.m_oVis.action("highlight")(this.m_oVis.node().selectAll(function() { return v_aHoverSceneItems; }));
- }
- }
- };
- /*
- * SetSelection(idxDataSet, v_aRows)
- * RegisterSelectionListener
- * if adapter supports SetSelection, do not hookup mouse or touch events
- * drawSelections does SetSelection if exists
- */
- C_Rave2Viz.prototype.SetSelections = function( v_idxDataSet, v_aRows )
- {
- var v_aVisElements = this.GetVisElementsForRows(v_idxDataSet, v_aRows);
- var v_aSelectedVisElements = [];
- for (var i = 0; i < v_aVisElements.length; ++i)
- {
- var v_oVisElement = v_aVisElements[i];
- if (v_oVisElement.GetType() == "intersection")
- {
- v_aSelectedVisElements.push(v_oVisElement);
- }
- }
- this.DrawHighlights(v_aSelectedVisElements, null);
- };
- C_Rave2Viz.prototype.DrawBlank = function()
- {
- this.m_aSelectedSceneItems = null;
- };
- C_Rave2Viz.prototype.f_categoryValueAccessor = function( v_aDataRows, v_iField, v_aCategoryValues, v_oDatum )
- {
- return v_aCategoryValues[v_aDataRows[v_oDatum.r][v_iField]];
- };
- C_Rave2Viz.prototype.f_valueAccessor = function( v_aDataRows, v_iField, v_oDatum )
- {
- return v_aDataRows[v_oDatum.r][v_iField];
- };
- C_Rave2Viz.prototype.f_createRaveDatumArray = function( v_idxDataSet, v_iNbRows )
- {
- var v_aRowNumbers = [];
-
- for (var i = 0; i < v_iNbRows; ++i)
- {
- v_aRowNumbers[i] = {
- d: v_idxDataSet,
- r: i
- };
- }
-
- return v_aRowNumbers;
- };
- C_Rave2Viz.prototype.SetData = function( v_idxDataSet, v_oDataSetChanges )
- {
- this.m_aData[v_idxDataSet] = v_oDataSetChanges; // this is only used for debug info
-
- var v_oDataSetDef = this.m_oVisDataDefinition.dataSets[v_idxDataSet];
- var v_oDataSet = this.m_oDataModel.dataset( v_oDataSetDef.id );
-
- var v_aFields = v_oDataSetChanges.fields;
- var v_oFieldLookup = {};
- var v_oFieldIdx = {};
-
- for (var i = 0; i < v_aFields.length; ++i)
- {
- var v_oField = v_aFields[i];
- v_oFieldLookup[v_oField.id] = v_oField;
- v_oFieldIdx[v_oField.id] = i;
- }
-
- v_oDataSet.data(this.f_createRaveDatumArray(v_idxDataSet, v_oDataSetChanges.rows.length));
- var oRave1SlotTypeToRave2SlotType =
- {
- "category" : "string",
- "value" : "numeric"
- };
-
- var v_aSlots = v_oDataSetDef.slots;
-
- for ( var v_iSlot = 0; v_iSlot < v_aSlots.length; v_iSlot++ )
- {
- var v_oSlot = v_aSlots[v_iSlot];
- var v_sSlotId = v_oSlot.id;
- var v_oField = v_oFieldLookup[v_sSlotId];
-
- var v_oRaveSlot = v_oDataSet.slot( v_sSlotId );
- v_oRaveSlot.clear();
-
- if (v_oField)
- {
- var v_oRaveSlotEntry = v_oRaveSlot.push();
- v_oRaveSlotEntry.type(oRave1SlotTypeToRave2SlotType[v_oSlot.type]);
- v_oRaveSlotEntry.label(v_oField.label);
-
- if (v_oField.categories)
- {
- v_oRaveSlotEntry.accessor(this.f_categoryValueAccessor.bind(this, v_oDataSetChanges.rows, v_oFieldIdx[v_sSlotId], v_oField.categories));
- v_oRaveSlotEntry.domain(v_oField.categories);
- }
- else
- {
- v_oRaveSlotEntry.accessor(this.f_valueAccessor.bind(this, v_oDataSetChanges.rows, v_oFieldIdx[v_sSlotId]));
- var v_fMin = v_oField.min;
- var v_fMax = v_oField.max;
- if ( ( v_fMin !== undefined ) && ( v_fMax !== undefined ) )
- {
- v_oRaveSlotEntry.domain( [v_fMin, v_fMax] );
- }
- }
- }
- }
- };
- C_Rave2Viz.prototype.SetDataByAccessors = function( v_idxDataSet, v_iNbRows, v_oSlotDataMap )
- {
- // this is only used for debug info
- this.m_aData[v_idxDataSet] = { m_oSlotDataMap : v_oSlotDataMap, m_iNbRows : v_iNbRows };
-
- var v_oDataSetDef = this.m_oVisDataDefinition.dataSets[v_idxDataSet];
- var v_oDataSet = this.m_oDataModel.dataset( v_oDataSetDef.id );
- v_oDataSet.data(this.f_createRaveDatumArray(v_idxDataSet, v_iNbRows));
- var v_aSlots = v_oDataSetDef.slots;
-
- for ( var v_iSlot = 0; v_iSlot < v_aSlots.length; v_iSlot++ )
- {
- var v_oSlot = v_aSlots[v_iSlot];
- var v_sSlotId = v_oSlot.id;
-
- var v_oRaveSlot = v_oDataSet.slot( v_sSlotId );
- v_oRaveSlot.clear();
-
- var v_oSlotData = v_oSlotDataMap[v_sSlotId];
-
- if (v_oSlotData)
- {
- var v_oRaveSlotEntry = v_oRaveSlot.push();
- v_oRaveSlotEntry.type(v_oSlotData.dataType);
- v_oRaveSlotEntry.label(v_oSlotData.label);
- v_oRaveSlotEntry.accessor(this.f_rowAccessor.bind(this, v_oSlotData.accessor));
- v_oRaveSlotEntry.domain(v_oSlotData.domain);
- }
- }
- };
- C_Rave2Viz.prototype.f_rowAccessor = function( v_fnAccessor, v_oDatum )
- {
- return v_fnAccessor(v_oDatum.r);
- };
- C_Rave2Viz.prototype.f_createVisElementForSceneItem = function( v_oSceneItem )
- {
- var v_oRaveDatum = (v_oSceneItem && v_oSceneItem.rave_hasData && v_oSceneItem.rave_hasData()) ? v_oSceneItem.rave_getData() : null;
- if (v_oRaveDatum && v_oRaveDatum.originalData && v_oRaveDatum.originalData())
- {
- return new C_Rave2VizElement( v_oSceneItem );
- }
- return null;
- };
- C_Rave2Viz.prototype.f_createVisElementsForSceneItemNodeList = function( v_nlSceneItems )
- {
- var a = [];
- var v_iLength = v_nlSceneItems.length;
- for ( var i = 0; i < v_iLength; i++ )
- {
- var v_oVisElement = this.f_createVisElementForSceneItem(v_nlSceneItems.item(i));
-
- if (v_oVisElement)
- {
- a.push( v_oVisElement );
- }
- }
-
- return a;
- };
- C_Rave2Viz.prototype.GetVisElementsAtPoint = function( x, y )
- {
- // rave_intersectionList does not exist in Firefox and does not work correct in Safari or Chrome. Seems to only work in IE
- var v_oSceneItem = this.f_createVisElementForSceneItem(document.elementFromPoint(x,y));
- return v_oSceneItem ? [ v_oSceneItem ] : null;
- };
- C_Rave2Viz.prototype.GetVisElementsAtTouchPoint = function( x, y )
- {
- // Rave2 does not provide provide any polygon hit testing like we have in Rave1 to do fuzzy clicking support on iPad (the ability to click close to an item for selection)
- // Instead we will just do point testing
- return this.GetVisElementsAtPoint(x,y);
- };
- C_Rave2Viz.prototype.GetVisElementsForRows = function( v_idxDataSet, v_aRows )
- {
- return this.f_createVisElementsForSceneItemsSelector(this.m_oVis.node().selectAll(".element-shape").filter(function(d) {
- var datum = d && d.originalData && d.originalData();
- return datum && v_aRows.indexOf(datum.r) != -1;
- }));
- };
- C_Rave2Viz.prototype.SetValueSlotFormatter = function( v_fnValueFormatter )
- {
- this.m_oVis.setCustomFormatter( "all", function(value, index, groupIndex, info, doAbbreviatedFormatting) {
- if (doAbbreviatedFormatting)
- {
- // use what Rave already has
- return null;
- }
-
- // NOTE: this inside of this method refers to the SVG element that the formatting is being done for
- if (info.entries().length > 0)
- {
- var v_oEntry = info.entries()[0];
- var v_oSlotDescriptor = v_oEntry.dataSlot().descriptor();
-
- // if it is a value slot (not categorical)
- if (v_oSlotDescriptor.dataTypes().indexOf("string") == -1)
- {
- // undefined by default
- var v_iRow;
-
- // this is the SVG element that the formatting is being done for
- var v_oData = this.rave_getData();
-
- if (v_oData && v_oData.originalData)
- {
- var v_oDatum = v_oData.originalData();
- v_iRow = v_oDatum.r;
- }
-
- return v_fnValueFormatter( value, v_iRow, v_oSlotDescriptor.id(), v_oEntry.dataSet().descriptor().id() );
- }
- }
-
- return value;
- });
- };
- C_Rave2Viz.prototype.GetCurrentVisualizationState = function()
- {
- var v_oVisState = {
- width: this.m_oVisSettings.properties.height || "500",
- height: this.m_oVisSettings.properties.width || "500",
- locale: 'en',
- bundle: this.m_oChartTypeInfo.bundleId,
- configuration : this.m_oChartTypeInfo.configId,
- data: {
- dataModelId: this.m_oVis.info().dataModels()[0].id(),
- dataSets : {},
- },
- properties : {},
- actions : {}
- };
-
- var v_aActions = this.m_oVis.info().actions();
-
- for (var i = 0; i < v_aActions.length; ++i)
- {
- v_oVisState.actions[v_aActions[i].id()] = {
- autoBind : false
- };
- }
-
- var v_oRavePalettes = {};
- for ( var s in this.m_oVisSettings.properties )
- {
- var v_oPropDef = this.m_oVisDataDefinition.properties[s];
- if (v_oPropDef)
- {
- if (v_oPropDef.type == "palette")
- {
- var v_oPalette = this.m_oVisSettings.properties[s];
- var v_sPaletteId = "ca.report." + v_oPalette.id;
-
- if (!v_oRavePalettes[v_sPaletteId])
- {
- var v_oRavePalette = this.f_createRavePalette(v_sPaletteId, v_oPalette);
- v_oRavePalettes[v_sPaletteId] = v_oRavePalette;
-
- // The viz state spec only supports one custom palette. Last one wins
- v_oVisState.customPalette = v_oRavePalette;
- }
-
- this.m_oVis.property( s, v_sPaletteId );
- }
- else
- {
- v_oVisState.properties[s] = this.m_oVisSettings.properties[s];
- }
- }
- }
- for ( var i = 0; i < this.m_aData.length; i++ )
- {
- var v_oDataSetIn = this.m_aData[i];
- var v_oDataSetDef = this.m_oVisDataDefinition.dataSets[i];
- var v_oDataSetOut = v_oVisState.data.dataSets[v_oDataSetDef.id] = {
- rows: [],
- accessors : {}
- };
-
- if (v_oDataSetIn.m_oSlotDataMap)
- {
- this.f_createVisDefDataFromAccessors(v_oDataSetIn, v_oDataSetOut);
- }
- else
- {
- this.f_createVisDefDataFromData(v_oDataSetIn, v_oDataSetOut);
- }
-
- }
-
- var v_sJSON = JSON.stringify(v_oVisState, null, "\t");
- return "var s = " + v_sJSON.replace(/"(function[^"]*)"/g, "$1") + "; s;";
- };
- C_Rave2Viz.prototype.f_createVisDefDataFromAccessors = function(v_oDataSetIn, v_oDataSetOut)
- {
- for (var v_sSlotId in v_oDataSetIn.m_oSlotDataMap)
- {
- var v_oSlotData = v_oDataSetIn.m_oSlotDataMap[v_sSlotId];
- v_oDataSetOut.accessors[v_sSlotId] = {
- type : v_oSlotData.dataType,
- accessor : "function(d) { return d['" + v_sSlotId + "']; }",
- domain : v_oSlotData.domain,
- label : v_oSlotData.label
- };
- }
-
- for (var v_iRow = 0; v_iRow < v_oDataSetIn.m_iNbRows; ++v_iRow)
- {
- var v_oRowOut = {};
- v_oDataSetOut.rows.push(v_oRowOut);
- for (var v_sSlotId in v_oDataSetIn.m_oSlotDataMap)
- {
- var v_oSlotData = v_oDataSetIn.m_oSlotDataMap[v_sSlotId];
- v_oRowOut[v_sSlotId] = v_oSlotData.accessor(v_iRow);
- }
- }
- };
- C_Rave2Viz.prototype.f_createVisDefDataFromData = function(v_oDataSetIn, v_oDataSetOut)
- {
- var v_aFields = v_oDataSetIn.fields;
- var v_iNbRows = v_oDataSetIn.rows.length;
-
- for (var ir = 0; ir < v_iNbRows; ++ir)
- {
- var v_aRowIn = v_oDataSetIn.rows[ir];
- var v_oRowOut = {};
- v_oDataSetOut.rows.push(v_oRowOut);
-
- for (var ic = 0; ic < v_aFields.length; ++ic)
- {
- var v_oField = v_aFields[ic];
- v_oRowOut[v_oField.id] = v_oField.categories ?
- v_oField.categories[v_aRowIn[ic]] :
- v_aRowIn[ic];
- }
- }
-
- for (var ic = 0; ic < v_aFields.length; ++ic)
- {
- var v_oField = v_aFields[ic];
-
- if (v_oField.categories)
- {
- v_oDataSetOut.accessors[v_oField.id] = {
- type : "string",
- accessor : "function(d) { return d['" + v_oField.id + "']; }",
- domain : v_oField.categories,
- label : v_oField.label
- };
- }
- else
- {
- var v_oAccessor = v_oDataSetOut.accessors[v_oField.id] = {
- type : "numeric",
- accessor : "function(d) { return +d['" + v_oField.id + "']; }",
- label : v_oField.label
- };
-
- var v_fMin = v_oField.min;
- var v_fMax = v_oField.max;
- if ( ( v_fMin !== undefined ) && ( v_fMax !== undefined ) )
- {
- v_oAccessor.domain = [v_fMin, v_fMax];
- }
- }
- }
- };
- C_Rave2Viz.prototype.SetAccessibilityDescription = function( v_sDescription )
- {
- this.m_oVis.node().extension( "raveAccessibility" ).description( v_sDescription );
- };
- C_Rave2Viz.prototype.ApplySettings = function( v_oVisSettings )
- {
- this.m_elVisContainer.setAttribute( "height", v_oVisSettings.properties.height || "500" );
- this.m_elVisContainer.setAttribute( "width", v_oVisSettings.properties.width || "500" );
- // restore default properties if they are not being explicitly set
- for ( var s in this.m_oOverridenPropertyDefaults )
- {
- if (v_oVisSettings.properties[s] === undefined)
- {
- this.m_oVis.property( s, this.m_oOverridenPropertyDefaults[s] );
- this.m_oOverridenPropertyDefaults[s] = undefined;
- }
- }
-
- this.m_oVisSettings = v_oVisSettings;
-
- for ( var s in v_oVisSettings.properties )
- {
- var v_oPropDef = this.m_oVisDataDefinition.properties[s];
- if (v_oPropDef)
- {
- var v_sPropValue;
-
- if (v_oPropDef.type == "palette")
- {
- var v_oPalette = v_oVisSettings.properties[s];
- var v_oRavePalettes = G_Rave2Adapter.G_Rave2Library.palettes;
- var v_sPaletteId = "ca.report." + v_oPalette.id;
-
- if (!v_oRavePalettes.getPalette(v_sPaletteId))
- {
- var v_oRavePalette = this.f_createRavePalette(v_sPaletteId, v_oPalette);
- v_oRavePalettes.addPalette(v_oRavePalette);
- }
-
- v_sPropValue = v_sPaletteId;
- }
- else
- {
- v_sPropValue = v_oVisSettings.properties[s];
- }
-
- if (this.m_oOverridenPropertyDefaults[s] === undefined)
- {
- this.m_oOverridenPropertyDefaults[s] = this.m_oVis.property(s);
- }
- this.m_oVis.property( s, v_sPropValue );
- }
- }
- };
- C_Rave2Viz.prototype.f_createRavePalette = function( v_sPaletteId, v_oPalette )
- {
- var v_oRavePalette = {
- id : v_sPaletteId,
- fillType : v_oPalette.fillType,
- fills : v_oPalette.fills
- };
-
- if (v_oPalette.valueType)
- {
- v_oRavePalette.valueType = v_oPalette.valueType;
- }
- return v_oRavePalette;
- };
- C_Rave2Viz.prototype.f_createVisElementsForSceneItemsSelector = function( v_oSceneItemsSelector )
- {
- var a = [];
-
- v_oSceneItemsSelector.each(function(v_oRaveDataum) {
- if (v_oRaveDataum && v_oRaveDataum.originalData && v_oRaveDataum.originalData())
- {
- a.push( new C_Rave2VizElement( this ) );
- }
- });
- return a;
- };
- C_Rave2Viz.prototype.f_createSceneItemsFromVisualElements = function(v_aVisElements)
- {
- var a = [];
- var v_iLength = v_aVisElements.length;
- for ( var i = 0; i < v_iLength; i++ )
- {
- a.push(v_aVisElements[i].m_oSceneItem);
- }
- return a;
- };
- C_Rave2Viz.prototype.IsEqualVisElement = function( v_oVisElement1, v_oVisElement2 )
- {
- if (v_oVisElement1 && v_oVisElement2)
- {
- return (v_oVisElement1.m_oSceneItem === v_oVisElement2.m_oSceneItem);
- }
- // return true if both are null
- return (v_oVisElement1 === v_oVisElement2);
- };
- C_Rave2Viz.prototype.f_isEqualSceneItems = function( v_aSceneItems1, v_aSceneItems2 )
- {
- if (v_aSceneItems1.length != v_aSceneItems2.length)
- {
- return false;
- }
-
- for (var i = 0; i < v_aSceneItems1.length; ++i)
- {
- if (v_aSceneItems1[i] !== v_aSceneItems2[i])
- {
- return false;
- }
- }
-
- return true;
- };
- C_Rave2Viz.prototype.f_onMouseMoveHover = function(v_fnHoverCallback, e)
- {
- var v_oVisElement = this.f_createVisElementForSceneItem(document.elementFromPoint(e.clientX, e.clientY));
-
- if (v_oVisElement && v_oVisElement.GetType() != "intersection")
- {
- v_oVisElement = null;
- }
-
- if ( this.IsEqualVisElement(this.m_oHoverVisElement, v_oVisElement))
- {
- return;
- }
-
- this.m_oHoverVisElement = v_oVisElement;
- v_fnHoverCallback(v_oVisElement, e);
- };
- C_Rave2Viz.prototype.f_onMouseMoveHoverForLocator = function(v_fnHoverCallback, e)
- {
- var v_oVisElement;
-
- var v_oHighlights = { items: [] };
- if (e.items.length > 0)
- {
- // assumes item at 0 is the one on top
- v_oVisElement = new C_Rave2MapElement(e.items[0].datum);
- v_oHighlights.items.push(e.items[0]);
- }
- this.m_oVis.action( "highlight" )( v_oHighlights );
-
- if (C_Rave2MapElement.F_IsEqualVisElement(this.m_oHoverVisElement, v_oVisElement))
- {
- return;
- }
-
- this.m_oHoverVisElement = v_oVisElement;
- v_fnHoverCallback(v_oVisElement, e.originalEvent);
- };
- //==================================================================================
- var C_Rave2MapElement = function( v_oDatum )
- {
- this.m_oDatum = v_oDatum;
- };
- C_Rave2MapElement.F_IsEqualVisElement = function( v_oVisElement1, v_oVisElement2 )
- {
- if (v_oVisElement1 && v_oVisElement2)
- {
- return (v_oVisElement1.m_oDatum === v_oVisElement2.m_oDatum);
- }
- // return true if both are null
- return (v_oVisElement1 === v_oVisElement2);
- };
- C_Rave2MapElement.prototype.GetType = function()
- {
- return "intersection";
- };
- C_Rave2MapElement.prototype.GetDataSetIdx = function()
- {
- return this.m_oDatum.d;
- };
- C_Rave2MapElement.prototype.GetRows = function()
- {
- // TODO: This is just one row. Are there potentially more? How to I get them?
- // return this.m_oSceneItem.getRowsWithoutAugmentedRows();
- return [this.m_oDatum.r];
- };
- C_Rave2MapElement.prototype.GetFields = function()
- {
- };
- C_Rave2MapElement.prototype.GetColor = function()
- {
- return {
- r : 255,
- g : 255,
- b : 255
- };
- };
- //==================================================================================
- var C_Rave2VizElement = function( v_oSceneItem )
- {
- this.m_oSceneItem = v_oSceneItem;
- var v_oData = v_oSceneItem.rave_getData().originalData();
- // on some charts (e.g. area) you can get back multiple dataums, use the first one
- this.m_aDataums = v_oData.length ? v_oData : [ v_oData ];
- };
- C_Rave2VizElement.prototype.GetType = function()
- {
- if (!this.m_sType)
- {
- this.m_sType = ( this.m_oSceneItem.rave_containsClass("element-shape") ) ? "intersection" : "category";
- }
-
- return this.m_sType;
- };
- C_Rave2VizElement.prototype.GetDataSetIdx = function()
- {
- // assume viz element only represents data from one data set
- return this.m_aDataums[0].d;
- };
- C_Rave2VizElement.prototype.GetRows = function()
- {
- if (!this.m_aRows)
- {
- this.m_aRows = this.m_aDataums.map(function(v_oDataum) { return v_oDataum.r; });
- }
-
- return this.m_aRows;
- };
- C_Rave2VizElement.prototype.GetFields = function()
- {
- };
- C_Rave2VizElement.prototype.GetColor = function()
- {
- var v_sColor = this.m_oSceneItem.style && this.m_oSceneItem.style.fill;
-
- if (v_sColor)
- {
- if (v_sColor.match(/#([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})/))
- {
- return {
- r : parseInt(RegExp.$1, 16),
- g : parseInt(RegExp.$2, 16),
- b : parseInt(RegExp.$3, 16)
- };
- }
- else if (v_sColor.match(/\s*rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/i))
- {
- return {
- r : parseInt(RegExp.$1),
- g : parseInt(RegExp.$2),
- b : parseInt(RegExp.$3)
- };
-
- }
- }
- };
- return G_Rave2Adapter;
- });
|