Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/map zoom #6835

Merged
merged 14 commits into from
May 27, 2016
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 12 additions & 34 deletions src/plugins/kbn_vislib_vis_types/public/tile_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export default function TileMapVisType(Private, getAppState, courier, config) {
heatRadius: 25,
heatBlur: 15,
heatNormalizeData: true,
mapZoom: 2,
mapCenter: [15, 5],
wms: config.get('visualization:tileMap:WMSdefaults')
},
mapTypes: ['Scaled Circle Markers', 'Shaded Circle Markers', 'Shaded Geohash Grid', 'Heatmap'],
Expand All @@ -46,52 +48,28 @@ export default function TileMapVisType(Private, getAppState, courier, config) {

pushFilter(filter, false, indexPatternName);
},
mapMoveEnd: function (event) {
mapMoveEnd: function (event, uiState) {
uiState.set('mapCenter', event.center);

const agg = _.get(event, 'chart.geohashGridAgg');
if (!agg) return;

agg.params.mapZoom = event.zoom;
agg.params.mapCenter = [event.center.lat, event.center.lng];
agg.params.mapCenter = event.center;

const editableVis = agg.vis.getEditableVis();
if (!editableVis) return;


const editableAgg = editableVis.aggs.byId[agg.id];
if (editableAgg) {
editableAgg.params.mapZoom = event.zoom;
editableAgg.params.mapCenter = [event.center.lat, event.center.lng];
editableAgg.params.mapCenter = event.center;
}
},
mapZoomEnd: function (event) {
mapZoomEnd: function (event, uiState) {
const agg = _.get(event, 'chart.geohashGridAgg');
if (!agg || !agg.params.autoPrecision) return;
uiState.set('mapZoom', event.zoom);

// zoomPrecision maps event.zoom to a geohash precision value
// event.limit is the configurable max geohash precision
// default max precision is 7, configurable up to 12
const zoomPrecision = {
1: 2,
2: 2,
3: 2,
4: 3,
5: 3,
6: 4,
7: 4,
8: 5,
9: 5,
10: 6,
11: 6,
12: 7,
13: 7,
14: 8,
15: 9,
16: 10,
17: 11,
18: 12
};

const precision = config.get('visualization:tileMap:maxPrecision');
agg.params.precision = Math.min(zoomPrecision[event.zoom], precision);
if (!agg) return;
agg.params.mapZoom = event.zoom;

courier.fetch();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function visualizationLoader(savedVisualizations, Private) { // I
savedObj: savedVis,
panel: panel,
uiState: savedVis.uiStateJSON ? JSON.parse(savedVis.uiStateJSON) : {},
setUiState(uiState) { savedVis.vis.setUiState(uiState); },
Copy link
Contributor

@w33ble w33ble May 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of there being a setter here. There are other properties that are consumed off of the passed in $scope, why not do the same with the uiState as well? It'll simplify the use in panel.js as well.

editUrl: savedVisualizations.urlFor(panel.id)
};
});
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/kibana/public/dashboard/components/panel/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ uiModules
// create child ui state from the savedObj
const uiState = panelConfig.uiState || {};
$scope.uiState = $scope.parentUiState.createChild(getPanelId(panelConfig.panel), uiState, true);
if (panelConfig.uiState) {
panelConfig.uiState = $scope.uiState;
}
if (panelConfig.setUiState) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my note in panel/lib/visualization.js, I think this logic should be moved there.

panelConfig.setUiState($scope.uiState);
}

$scope.filter = function (field, value, operator) {
const index = $scope.savedObj.searchSource.get('index').id;
Expand Down
1 change: 0 additions & 1 deletion src/plugins/kibana/public/visualize/editor/agg.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ uiModules
template: aggTemplate,
require: 'form',
link: function ($scope, $el, attrs, kbnForm) {
$scope.$bind('outputAgg', 'outputVis.aggs.byId[agg.id]', $scope);
$scope.editorOpen = !!$scope.agg.brandNew;

$scope.$watch('editorOpen', function (open) {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/kibana/public/visualize/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ uiModules
$scope.editableVis = editableVis;
$scope.state = $state;
$scope.uiState = $state.makeStateful('uiState');
vis.setUiState($scope.uiState);
$scope.timefilter = timefilter;
$scope.opts = _.pick($scope, 'doSave', 'savedVis', 'shareData', 'timefilter');

Expand Down
1 change: 0 additions & 1 deletion src/plugins/kibana/public/visualize/editor/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ uiModules
controllerAs: 'sidebar',
controller: function ($scope) {
$scope.$bind('vis', 'editableVis');
$scope.$bind('outputVis', 'vis');
$scope.$watch('vis.type', (visType) => {
if (visType) {
this.showData = visType.schemas.buckets || visType.schemas.metrics;
Expand Down
41 changes: 32 additions & 9 deletions src/ui/public/agg_types/buckets/geo_hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ export default function GeoHashAggDefinition(Private, config) {
let BucketAggType = Private(AggTypesBucketsBucketAggTypeProvider);
let defaultPrecision = 2;

// zoomPrecision maps event.zoom to a geohash precision value
// event.limit is the configurable max geohash precision
// default max precision is 7, configurable up to 12
const zoomPrecision = {
1: 2,
2: 2,
3: 2,
4: 3,
5: 3,
6: 4,
7: 4,
8: 5,
9: 5,
10: 6,
11: 6,
12: 7,
13: 7,
14: 8,
15: 9,
16: 10,
17: 11,
18: 12
};

function getPrecision(precision) {
let maxPrecision = _.parseInt(config.get('visualization:tileMap:maxPrecision'));

Expand Down Expand Up @@ -45,19 +69,18 @@ export default function GeoHashAggDefinition(Private, config) {
},
{
name: 'precision',
default: defaultPrecision,
editor: precisionTemplate,
deserialize: getPrecision,
controller: function ($scope) {
$scope.$watchMulti([
'agg.params.autoPrecision',
'outputAgg.params.precision'
], function (cur, prev) {
if (cur[1]) $scope.agg.params.precision = cur[1];
});
},
deserialize: getPrecision,
write: function (aggConfig, output) {
output.params.precision = getPrecision(aggConfig.params.precision);
let currZoom = null;
if (aggConfig.params.mapZoom || aggConfig.vis.hasUiState()) { // First iteration
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of all of this aggconfig.params business, this IMO be removable

currZoom = aggConfig.vis.hasUiState() ? aggConfig.vis.uiStateVal('mapZoom') : aggConfig.params.mapZoom;
}
currZoom = currZoom || aggConfig.vis.params.mapZoom;
const autoPrecisionVal = zoomPrecision[currZoom];
output.params.precision = aggConfig.params.autoPrecision ? autoPrecisionVal : getPrecision(aggConfig.params.precision);
}
}
]
Expand Down
22 changes: 21 additions & 1 deletion src/ui/public/vis/vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function VisFactory(Notifier, Private) {
location: 'Vis'
});

function Vis(indexPattern, state) {
function Vis(indexPattern, state, uiState) {
state = state || {};

if (_.isString(state)) {
Expand All @@ -24,6 +24,7 @@ export default function VisFactory(Notifier, Private) {

// http://aphyr.com/data/posts/317/state.gif
this.setState(state);
this.__uiState = uiState;
}

Vis.convertOldState = function (type, oldState) {
Expand Down Expand Up @@ -125,5 +126,24 @@ export default function VisFactory(Notifier, Private) {
});
};

Vis.prototype.hasUiState = function () {
return !!this.__uiState;
};
Vis.prototype.setUiState = function (uiState) {
this.__uiState = uiState;
};
Vis.prototype.getUiState = function () {
return this.__uiState;
};
Vis.prototype.uiStateVal = function(key, val) {
if (this.hasUiState()) {
if (_.isUndefined(val)) {
return this.__uiState.get(key);
Copy link
Contributor

@w33ble w33ble May 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You never check that this.__uiState is actually a PersistedState, so you can pass in anything, including a raw object, and this will fail, as there is no native get method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I will change.

}
return this.__uiState.set(key, val);
}
return val;
};

return Vis;
};
4 changes: 2 additions & 2 deletions src/ui/public/vislib/lib/handler/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function HandlerBaseClass(Private) {
return new Handler(vis, opts);
}

this.data = opts.data || new Data(vis.data, vis._attr, vis.uiState);
this.data = opts.data || new Data(vis.data, vis._attr, vis.getUiState());
this.vis = vis;
this.el = vis.el;
this.ChartClass = vis.ChartClass;
Expand Down Expand Up @@ -55,7 +55,7 @@ export default function HandlerBaseClass(Private) {
this.getProxyHandler = _.memoize(function (event) {
let self = this;
return function (e) {
self.vis.emit(event, e);
self.vis.emit(event, e, vis.uiState);
};
});
}
Expand Down
9 changes: 4 additions & 5 deletions src/ui/public/vislib/visualizations/_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export default function MapFactory(Private) {
this._valueFormatter = params.valueFormatter || _.identity;
this._tooltipFormatter = params.tooltipFormatter || _.identity;
this._geoJson = _.get(this._chartData, 'geoJson');
this._mapZoom = params.zoom || defaultMapZoom;
this._mapCenter = params.center || defaultMapCenter;
this._attr = params.attr || {};

let mapOptions = {
Expand Down Expand Up @@ -211,7 +213,8 @@ export default function MapFactory(Private) {
this.map.on('moveend', function setZoomCenter(ev) {
if (!self.map) return;
// update internal center and zoom references
self._mapCenter = self.map.getCenter();
const uglyCenter = self.map.getCenter();
self._mapCenter = [uglyCenter.lat, uglyCenter.lng];
self._mapZoom = self.map.getZoom();
self._addMarkers();

Expand Down Expand Up @@ -264,10 +267,6 @@ export default function MapFactory(Private) {
TileMapMap.prototype._createMap = function (mapOptions) {
if (this.map) this.destroy();

// get center and zoom from mapdata, or use defaults
this._mapCenter = _.get(this._geoJson, 'properties.center') || defaultMapCenter;
this._mapZoom = _.get(this._geoJson, 'properties.zoom') || defaultMapZoom;

// add map tiles layer, using the mapTiles object settings
if (this._attr.wms && this._attr.wms.enabled) {
this._tileLayer = L.tileLayer.wms(this._attr.wms.url, this._attr.wms.options);
Expand Down
17 changes: 13 additions & 4 deletions src/ui/public/vislib/visualizations/tile_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,20 @@ export default function TileMapFactory(Private) {
* @param selection {Object} d3 selection
*/
TileMap.prototype._appendMap = function (selection) {
let container = $(selection).addClass('tilemap');
const container = $(selection).addClass('tilemap');
// Lol couldn't think of another way to access this object...
const uiStateParams = {
mapCenter: this.handler.vis.uiState.get('mapCenter'),
mapZoom: this.handler.vis.uiState.get('mapZoom')
};

const params = _.assign({}, this._chartData.geohashGridAgg.vis.params, uiStateParams);

// this.handler.vis.uiState

let map = new TileMapMap(container, this._chartData, {
// center: this._attr.mapCenter,
// zoom: this._attr.mapZoom,
const map = new TileMapMap(container, this._chartData, {
center: params.mapCenter,
zoom: params.mapZoom,
events: this.events,
markerType: this._attr.mapType,
tooltipFormatter: this.tooltipFormatter,
Expand Down