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

feat: gauge, goal and bullet graph (alpha) #614

Merged
merged 21 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions src/chart_types/goal_chart/layout/config/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License. */

import { Config } from '../types/config_types';
import { TAU } from '../../../partition_chart/layout/utils/math';

export const configMetadata = {
Copy link
Member

Choose a reason for hiding this comment

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

nit: We can avoid exporting this, it is not used anywhere except at the end of this file

angleStart: { dflt: Math.PI + Math.PI / 4, min: -TAU, max: TAU, type: 'number' },
angleEnd: { dflt: -Math.PI / 4, min: -TAU, max: TAU, type: 'number' },

// shape geometry
width: { dflt: 300, min: 0, max: 1024, type: 'number', reconfigurable: false },
height: { dflt: 150, min: 0, max: 1024, type: 'number', reconfigurable: false },
margin: {
type: 'group',
values: {
left: { dflt: 0, min: -0.25, max: 0.25, type: 'number' },
right: { dflt: 0, min: -0.25, max: 0.25, type: 'number' },
top: { dflt: 0, min: -0.25, max: 0.25, type: 'number' },
bottom: { dflt: 0, min: -0.25, max: 0.25, type: 'number' },
},
},

// general text config
fontFamily: {
dflt: 'Sans-Serif',
type: 'string',
},

// fill text config
minFontSize: { dflt: 8, min: 0.1, max: 8, type: 'number', reconfigurable: true },
maxFontSize: { dflt: 64, min: 0.1, max: 64, type: 'number' },

backgroundColor: { dflt: '#ffffff', type: 'color' },
sectorLineWidth: { dflt: 1, min: 0, max: 4, type: 'number' },
};

// todo switch to `io-ts` style, generic way of combining static and runtime type info
function configMap(mapper: Function, configMetadata: any): Config {
const result: Config = Object.assign(
{},
...Object.entries(configMetadata).map(([k, v]: [string, any]) => {
if (v.type === 'group') {
return { [k]: configMap(mapper, v.values) };
} else {
return { [k]: mapper(v) };
}
}),
) as Config;
return result;
}
markov00 marked this conversation as resolved.
Show resolved Hide resolved

export const config: Config = configMap((item: any) => item.dflt, configMetadata);
43 changes: 43 additions & 0 deletions src/chart_types/goal_chart/layout/types/config_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License. */

import { Pixels, SizeRatio } from '../../../partition_chart/layout/types/geometry_types';
import { FontFamily } from '../../../partition_chart/layout/types/types';
import { Color } from '../../../../utils/commons';

// todo switch to `io-ts` style, generic way of combining static and runtime type info
export interface Config {
angleStart: number;
angleEnd: number;

// shape geometry
width: number;
height: number;
markov00 marked this conversation as resolved.
Show resolved Hide resolved
margin: { left: SizeRatio; right: SizeRatio; top: SizeRatio; bottom: SizeRatio };

// general text config
fontFamily: FontFamily;

// fill text config
minFontSize: Pixels;
maxFontSize: Pixels;

// other
backgroundColor: Color;
sectorLineWidth: Pixels;
}
108 changes: 108 additions & 0 deletions src/chart_types/goal_chart/layout/types/viewmodel_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License. */

import { Config } from './config_types';

import { Pixels, PointObject } from '../../../partition_chart/layout/types/geometry_types';
import { config } from '../config/config';
import { SpecTypes } from '../../../../specs/settings';
import { BandFillColorAccessorInput, GOAL_SUBTYPES } from '../../specs/index';

interface BandViewModel {
value: number;
fillColor: string;
}

interface TickViewModel {
value: number;
text: string;
}

export interface BulletViewModel {
markov00 marked this conversation as resolved.
Show resolved Hide resolved
subtype: string;
base: number;
target: number;
actual: number;
bands: Array<BandViewModel>;
ticks: Array<TickViewModel>;
labelMajor: string;
labelMinor: string;
centralMajor: string;
centralMinor: string;
highestValue: number;
lowestValue: number;
aboveBaseCount: number;
belowBaseCount: number;
}

export type PickFunction = (x: Pixels, y: Pixels) => Array<BulletViewModel>;
markov00 marked this conversation as resolved.
Show resolved Hide resolved

export type ShapeViewModel = {
markov00 marked this conversation as resolved.
Show resolved Hide resolved
config: Config;
bulletViewModel: BulletViewModel;
chartCenter: PointObject;
pickQuads: PickFunction;
};

const commonDefaults = {
specType: SpecTypes.Series,
subtype: GOAL_SUBTYPES[0],
base: 0,
target: 100,
actual: 50,
ticks: [0, 25, 50, 75, 100],
};

export const defaultGoalSpec = {
markov00 marked this conversation as resolved.
Show resolved Hide resolved
...commonDefaults,
bands: [50, 75, 100],
bandFillColor: ({ value, base, highestValue, lowestValue }: BandFillColorAccessorInput) => {
const aboveBase = value > base;
const ratio = aboveBase
? (value - base) / (Math.max(base, highestValue) - base)
: (value - base) / (Math.min(base, lowestValue) - base);
const level = Math.round(255 * ratio);
return aboveBase ? `rgb(0, ${level}, 0)` : `rgb( ${level}, 0, 0)`;
},
tickValueFormatter: ({ value }: BandFillColorAccessorInput) => String(value),
labelMajor: ({ base }: BandFillColorAccessorInput) => String(base),
labelMinor: ({}: BandFillColorAccessorInput) => 'unit',
centralMajor: ({ base }: BandFillColorAccessorInput) => String(base),
centralMinor: ({ target }: BandFillColorAccessorInput) => String(target),
};

export const nullGoalViewModel = {
markov00 marked this conversation as resolved.
Show resolved Hide resolved
...commonDefaults,
bands: [],
ticks: [],
labelMajor: '',
labelMinor: '',
centralMajor: '',
centralMinor: '',
highestValue: 100,
lowestValue: 0,
aboveBaseCount: 3,
markov00 marked this conversation as resolved.
Show resolved Hide resolved
belowBaseCount: 0,
};

export const nullShapeViewModel = (specifiedConfig?: Config, chartCenter?: PointObject): ShapeViewModel => ({
markov00 marked this conversation as resolved.
Show resolved Hide resolved
config: specifiedConfig || config,
bulletViewModel: nullGoalViewModel,
chartCenter: chartCenter || { x: 0, y: 0 },
pickQuads: () => [],
});
106 changes: 106 additions & 0 deletions src/chart_types/goal_chart/layout/viewmodel/viewmodel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License. */

import { TextMeasure } from '../../../partition_chart/layout/types/types';
import { Config } from '../types/config_types';
import { BulletViewModel, PickFunction, ShapeViewModel } from '../types/viewmodel_types';
import { GoalSpec } from '../../specs/index';

export function shapeViewModel(textMeasure: TextMeasure, spec: GoalSpec, config: Config): ShapeViewModel {
Copy link
Member

Choose a reason for hiding this comment

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

nit: textMeasure is not used in this function, we can remove it completely

const { width, height, margin } = config;

const innerWidth = width * (1 - Math.min(1, margin.left + margin.right));
const innerHeight = height * (1 - Math.min(1, margin.top + margin.bottom));

const chartCenter = {
x: width * margin.left + innerWidth / 2,
y: height * margin.top + innerHeight / 2,
};

const pickQuads: PickFunction = (x, y) => {
return -innerWidth / 2 <= x && x <= innerWidth / 2 && -innerHeight / 2 <= y && y <= innerHeight / 2
? [bulletViewModel]
: [];
};

const {
subtype,
base,
target,
actual,
bands,
ticks,
bandFillColor,
tickValueFormatter,
labelMajor,
labelMinor,
centralMajor,
centralMinor,
} = spec;

const [lowestValue, highestValue] = [base, target, actual, ...bands, ...ticks].reduce(
([min, max], value) => [Math.min(min, value), Math.max(max, value)],
[Infinity, -Infinity],
);

const aboveBaseCount = bands.filter((b: number) => b > base).length;
const belowBaseCount = bands.filter((b: number) => b <= base).length;

const callbackArgs = {
base,
target,
actual,
highestValue,
lowestValue,
aboveBaseCount,
belowBaseCount,
};

const bulletViewModel: BulletViewModel = {
subtype,
base,
target,
actual,
bands: bands.map((value: number, index: number) => ({
value,
fillColor: bandFillColor({ value, index, ...callbackArgs }),
})),
ticks: ticks.map((value: number, index: number) => ({
value,
text: tickValueFormatter({ value, index, ...callbackArgs }),
})),
labelMajor: typeof labelMajor === 'string' ? labelMajor : labelMajor({ value: NaN, index: 0, ...callbackArgs }),
labelMinor: typeof labelMinor === 'string' ? labelMinor : labelMinor({ value: NaN, index: 0, ...callbackArgs }),
centralMajor:
typeof centralMajor === 'string' ? centralMajor : centralMajor({ value: NaN, index: 0, ...callbackArgs }),
centralMinor:
typeof centralMinor === 'string' ? centralMinor : centralMinor({ value: NaN, index: 0, ...callbackArgs }),
highestValue,
lowestValue,
aboveBaseCount,
belowBaseCount,
};

// combined viewModel
return {
config,
chartCenter,
bulletViewModel,
pickQuads,
};
}
9 changes: 9 additions & 0 deletions src/chart_types/goal_chart/renderer/_index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.echCanvasRenderer {
position: absolute;
top: 0;
left: 0;
padding: 0;
margin: 0;
border: 0;
background: transparent;
}
markov00 marked this conversation as resolved.
Show resolved Hide resolved
Loading