Skip to content

Commit

Permalink
implements map mode
Browse files Browse the repository at this point in the history
closes #313
  • Loading branch information
frabarz committed Dec 4, 2018
1 parent 1be940f commit 91cc138
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 10 deletions.
6 changes: 5 additions & 1 deletion packages/vizbuilder/src/components/Sidebar/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ class Sidebar extends React.PureComponent {
const {options, query} = this.props;

return this.context.loadControl(() => {
const newState = generateBaseState(options.cubes, measure);
const newState = generateBaseState(
options.cubes,
measure,
options.geomapLevels
);
const newQuery = newState.query;
const isSameCube = newQuery.cube === query.cube;

Expand Down
3 changes: 2 additions & 1 deletion packages/vizbuilder/src/helpers/chartCriteria.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import {getTopTenByYear} from "./sorting";
import {isGeoDimension} from "./validation";

export default function chartCriteria(query, results, params) {
/** @type {Datagroup[]} */
Expand Down Expand Up @@ -44,7 +45,7 @@ export default function chartCriteria(query, results, params) {

const hasTimeLvl = timeLevelName && timeLevelMembers.length > 1;
const hasGeoLvl = [query.level, query.xlevel].some(
lvl => lvl && lvl.hierarchy.dimension.annotations.dim_type === "GEOGRAPHY"
lvl => lvl && isGeoDimension(lvl.hierarchy.dimension)
);

// Hide barcharts with more than 20 members
Expand Down
25 changes: 21 additions & 4 deletions packages/vizbuilder/src/helpers/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import {
classifyMeasures,
findByName,
getDefaultGroup,
getIncludedMembers
getIncludedMembers,
reduceLevelsFromDimension
} from "./sorting";
import {isValidDimension} from "./validation";
import {isGeoDimension, isValidDimension} from "./validation";

/**
* Prepares the array of cubes that will be used in the vizbuilder.
Expand Down Expand Up @@ -109,19 +110,35 @@ export function injectCubeInfoOnMeasure(cubes) {
* Retrieves the cube list and prepares the initial state for the first query
* @param {ExternalQueryParams} params An object with initial state parameters
*/
export function fetchCubes(params) {
export function fetchCubes(params, props) {
const isGeomapOnly =
props.visualizations.length === 1 && props.visualizations[0] === "geomap";
const geomapLevels = isGeomapOnly && Object.keys(props.topojson);

return api.cubes().then(cubes => {
// keep only cubes with geographical dimensions, whose levels are in the
// list of available geomap configurations
if (geomapLevels) {
cubes = cubes.filter(cube => {
const geoDim = cube.dimensions.find(isGeoDimension);
return geoDim && reduceLevelsFromDimension([], geoDim).some(
lvl => geomapLevels.indexOf(lvl.name) > -1
);
});
}

injectCubeInfoOnMeasure(cubes);

const {measures, measureMap} = classifyMeasures(cubes);
const measure = findByName(params.defaultMeasure, measures, true);

const newState = generateBaseState(cubes, measure);
const newState = generateBaseState(cubes, measure, geomapLevels);

const newOptions = newState.options;
newOptions.cubes = cubes;
newOptions.measures = measures;
newOptions.measureMap = measureMap;
newOptions.geomapLevels = geomapLevels;

const newQuery = newState.query;
newQuery.groups = getDefaultGroup(params.defaultGroup, newOptions.levels);
Expand Down
2 changes: 1 addition & 1 deletion packages/vizbuilder/src/helpers/permalink.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function permalinkToState(queryParams, prevState) {

const newState =
measure && measure !== prevState.query.measure
? generateBaseState(prevOptions.cubes, measure)
? generateBaseState(prevOptions.cubes, measure, prevOptions.geomapLevels)
: prevState;
const newOptions = newState.options;
const newQuery = newState.query;
Expand Down
12 changes: 10 additions & 2 deletions packages/vizbuilder/src/helpers/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {
getValidLevels,
removeDuplicateLevels
} from "./sorting";
import {isValidGrouping} from "./validation";
import {isValidGrouping, isGeoDimension} from "./validation";

/**
* Generates a partial state object, whose elements
* only depend on a measure.
* @param {Cube[]} cubes A list with all the cubes available
* @param {Measure} measure The currently selected Measure
*/
export function generateBaseState(cubes, measure) {
export function generateBaseState(cubes, measure, geomapLevels) {
const cubeName = measure.annotations._cb_name;
const cube = cubes.find(cube => cube.name === cubeName);

Expand All @@ -28,6 +28,14 @@ export function generateBaseState(cubes, measure) {
levels: getValidLevels(cube)
};

if (geomapLevels) {
options.levels = options.levels.filter(
lvl =>
isGeoDimension(lvl.hierarchy.dimension) &&
geomapLevels.indexOf(lvl.name) > -1
);
}

removeDuplicateLevels(options.levels);

return {options, query};
Expand Down
9 changes: 9 additions & 0 deletions packages/vizbuilder/src/helpers/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import yn from "yn";

import {findFirstNumber} from "./formatting";

/**
* Checks if the dimension passed as argument is a geographic-type dimension.
* @param {Dimension} dimension A mondrian-rest-client dimension object
* @returns {boolean}
*/
export function isGeoDimension(dimension) {
return dimension.annotations.dim_type === "GEOGRAPHY";
}

/**
* Checks if the dimension passed as argument is a time-type dimension.
* @param {Dimension} dimension A mondrian-rest-client dimension object
Expand Down
4 changes: 4 additions & 0 deletions packages/vizbuilder/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
& > .area-chart {
flex: 1 0 560px;
}

&.geo-only .levels-manager .grouping-items + button {
display: none;
}
}

.area-sidebar,
Expand Down
3 changes: 2 additions & 1 deletion packages/vizbuilder/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Vizbuilder extends React.PureComponent {
const defaultMeasure = props.defaultMeasure;
const defaultQuery = {defaultGroup, defaultMeasure};

let initialStatePromise = fetchCubes(defaultQuery);
let initialStatePromise = fetchCubes(defaultQuery, props);
const location = ctx.router.location;

if (props.permalink && location.search) {
Expand Down Expand Up @@ -113,6 +113,7 @@ class Vizbuilder extends React.PureComponent {
return (
<div
className={classnames("vizbuilder", {
"geo-only": Boolean(options.geomapLevels),
loading: load.inProgress
})}
>
Expand Down

0 comments on commit 91cc138

Please sign in to comment.