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(goal): auto generated linear ticks #1637

Merged
merged 16 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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.
8 changes: 8 additions & 0 deletions integration/tests/goal_stories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ describe('Goal stories', () => {
);
});

describe('auto ticks', () => {
it.each<boolean>([true, false])('reverse %p', async (reverse) => {
await common.expectChartAtUrlToMatchScreenshot(
`http://localhost:9001/?path=/story/goal-alpha--auto-linear-ticks&knob-reverse=${reverse}`,
);
});
});

describe('sagitta shifted goal charts', () => {
it.each<[title: string, startAngle: number, endAngle: number]>([
// top openings
Expand Down
4 changes: 3 additions & 1 deletion packages/charts/api/charts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,8 @@ export interface GoalSpec extends Spec {
// (undocumented)
labelMinor: string | GoalLabelAccessor;
// (undocumented)
nice: boolean;
// (undocumented)
specType: typeof SpecType.Series;
// Warning: (ae-forgotten-export) The symbol "GoalSubtype" needs to be exported by the entry point index.d.ts
//
Expand All @@ -1001,7 +1003,7 @@ export interface GoalSpec extends Spec {
// (undocumented)
target?: number;
// (undocumented)
ticks: number[];
ticks?: number[];
// (undocumented)
tickValueFormatter: GoalLabelAccessor;
// (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type ShapeViewModel = {
const commonDefaults = {
base: 0,
actual: 50,
ticks: [0, 25, 50, 75, 100],
nice: false,
};

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import { ScaleContinuous } from '../../../../scales';
import { Dimensions } from '../../../../utils/dimensions';
import { Theme } from '../../../../utils/themes/theme';
import { GoalSpec } from '../../specs';
Expand All @@ -28,7 +29,6 @@ export function shapeViewModel(spec: GoalSpec, theme: Theme, chartDimensions: Di
target,
actual,
bands,
ticks,
bandFillColor,
tickValueFormatter,
labelMajor,
Expand All @@ -38,11 +38,24 @@ export function shapeViewModel(spec: GoalSpec, theme: Theme, chartDimensions: Di
bandLabels,
angleStart,
angleEnd,
nice,
} = spec;
const [lowestValue, highestValue] = [base, ...(target ? [target] : []), actual, ...bands, ...ticks].reduce(
([min, max], value) => [Math.min(min, value), Math.max(max, value)],
[Infinity, -Infinity],
);
const [lowestValue, highestValue] = [
nickofthyme marked this conversation as resolved.
Show resolved Hide resolved
base,
...(target ? [target] : []),
actual,
...bands,
...(spec.ticks ?? []),
].reduce(([min, max], value) => [Math.min(min, value), Math.max(max, value)], [Infinity, -Infinity]);
markov00 marked this conversation as resolved.
Show resolved Hide resolved

const ticks =
spec.ticks ??
new ScaleContinuous({
markov00 marked this conversation as resolved.
Show resolved Hide resolved
type: 'linear', // TODO allow other scale types
domain: [lowestValue, highestValue],
range: [0, chartDimensions.width], // TODO apply range based on chart rotation
markov00 marked this conversation as resolved.
Show resolved Hide resolved
nice,
markov00 marked this conversation as resolved.
Show resolved Hide resolved
}).ticks();

const aboveBaseCount = bands.filter((b: number) => b > base).length;
const belowBaseCount = bands.filter((b: number) => b <= base).length;
Expand Down
3 changes: 2 additions & 1 deletion packages/charts/src/chart_types/goal_chart/specs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export interface GoalSpec extends Spec {
target?: number;
actual: number;
bands: number[];
ticks: number[];
ticks?: number[];
nice: boolean;
bandFillColor: BandFillColorAccessor;
tickValueFormatter: GoalLabelAccessor;
labelMajor: string | GoalLabelAccessor;
Expand Down
56 changes: 56 additions & 0 deletions storybook/stories/goal/26_auto_linear_ticks.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { boolean } from '@storybook/addon-knobs';
import React from 'react';

import { Chart, Goal, Settings } from '@elastic/charts';
import { BandFillColorAccessorInput } from '@elastic/charts/src/chart_types/goal_chart/specs';
import { GoalSubtype } from '@elastic/charts/src/chart_types/goal_chart/specs/constants';

import { Color } from '../../../packages/charts/src/common/colors';
import { useBaseTheme } from '../../use_base_theme';

const subtype = GoalSubtype.Goal;

const colorMap: { [k: number]: Color } = {
200: '#fc8d62',
250: 'lightgrey',
300: '#66c2a5',
};

const bandFillColor = (x: number): Color => colorMap[x];

export const Example = () => {
const reverse = boolean('reverse', false);
return (
<Chart>
<Settings baseTheme={useBaseTheme()} />
<Goal
id="spec"
subtype={subtype}
markov00 marked this conversation as resolved.
Show resolved Hide resolved
base={0}
target={260}
actual={280}
bands={[200, 250, 300]}
angleStart={reverse ? -Math.PI / 4 : Math.PI + Math.PI / 4}
angleEnd={reverse ? Math.PI + Math.PI / 4 : -Math.PI / 4}
tickValueFormatter={({ value }: BandFillColorAccessorInput) => String(value)}
bandFillColor={({ value }: BandFillColorAccessorInput) => bandFillColor(value)}
labelMajor=""
labelMinor=""
centralMajor="280 MB/s"
markov00 marked this conversation as resolved.
Show resolved Hide resolved
centralMinor=""
/>
</Chart>
);
};

Example.parameters = {
markdown: `Leaving \`ticks\` as undefined will automatically create a linear ticks array given the domain`,
};
1 change: 1 addition & 0 deletions storybook/stories/goal/goal.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ export { Example as horizontalPlusMinus } from './22_horizontal_plusminus.story'
export { Example as verticalPlusMinus } from './23_vertical_plusminus.story';
export { Example as goalPlusMinus } from './24_goal_plusminus.story';
export { Example as goalSemantics } from './25_goal_semantic.story';
export { Example as autoLinearTicks } from './26_auto_linear_ticks.story';