-
Notifications
You must be signed in to change notification settings - Fork 120
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(bar_chart): add shadow prop for value labels #785
Changes from 6 commits
9c0d1be
f2d0f69
7051c92
8570d67
e42ad56
0499acc
14a26f2
0035949
bf9b841
5857623
86dad5d
58a5651
5777029
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ export function renderText( | |
fontSize: number; | ||
align: TextAlign; | ||
baseline: TextBaseline; | ||
shadow?: string; | ||
}, | ||
degree: number = 0, | ||
translation?: Partial<Point>, | ||
|
@@ -49,6 +50,12 @@ export function renderText( | |
if (translation?.x || translation?.y) { | ||
ctx.translate(translation?.x ?? 0, translation?.y ?? 0); | ||
} | ||
if (font.shadow) { | ||
ctx.lineWidth = 1.5; | ||
ctx.strokeStyle = font.shadow; | ||
ctx.strokeText(text, origin.x, origin.y); | ||
ctx.shadowBlur = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shadowBlur is by default 0, probably you want to disable the |
||
} | ||
ctx.fillText(text, origin.x, origin.y); | ||
}); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,9 +290,17 @@ export interface Theme { | |
export type PartialTheme = RecursivePartial<Theme>; | ||
|
||
/** @public */ | ||
export type DisplayValueStyle = TextStyle & { | ||
export type DisplayValueStyle = Omit<TextStyle, 'fill'> & { | ||
offsetX: number; | ||
offsetY: number; | ||
fill: | ||
| Color | ||
| { color: Color; borderColor?: Color } | ||
| { | ||
textInvertible: boolean; | ||
textContrast?: number | boolean; | ||
textBorder?: boolean; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probably is a breaking change that needs to be notified |
||
}; | ||
|
||
export interface PointStyle { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* 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 { boolean, color, number, select } from '@storybook/addon-knobs'; | ||
import React from 'react'; | ||
|
||
import { Axis, BarSeries, Chart, Position, ScaleType, Settings } from '../../src'; | ||
import { SeededDataGenerator } from '../../src/mocks/utils'; | ||
import { getChartRotationKnob } from '../utils/knobs'; | ||
|
||
const dataGen = new SeededDataGenerator(); | ||
function generateDataWithAdditional(num: number) { | ||
return [...dataGen.generateSimpleSeries(num), { x: num, y: 0.25, g: 0 }, { x: num + 1, y: 8, g: 0 }]; | ||
} | ||
const frozenDataSmallVolume = generateDataWithAdditional(10); | ||
const frozenDataMediumVolume = generateDataWithAdditional(50); | ||
const frozenDataHighVolume = generateDataWithAdditional(1500); | ||
|
||
const frozenData: { [key: string]: any[] } = { | ||
s: frozenDataSmallVolume, | ||
m: frozenDataMediumVolume, | ||
h: frozenDataHighVolume, | ||
}; | ||
|
||
export const Example = () => { | ||
const showValueLabel = boolean('show value label', true); | ||
const isAlternatingValueLabel = boolean('alternating value label', false); | ||
const isValueContainedInElement = boolean('contain value label within bar element', false); | ||
const hideClippedValue = boolean('hide clipped value', false); | ||
|
||
const displayValueSettings = { | ||
showValueLabel, | ||
isAlternatingValueLabel, | ||
isValueContainedInElement, | ||
hideClippedValue, | ||
}; | ||
|
||
const debug = boolean('debug', false); | ||
const useInverted = boolean('textInverted', false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was a bit surprised that the inverted colors aren't configurable in any way, was this intentional? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. There are basically three configurations available for // basic coloring
Color
|
// advanced coloring
{
color: Color;
borderColor?: Color;
borderWidth?: number;
}
|
// let the library compute it
{
textInvertible: boolean;
textContrast?: number | boolean;
textBorder?: number | boolean;
}; The only way to influence the library color pick in case of automatic color assignment is the |
||
const valueColor = color('value color', '#fff'); | ||
const borderColor = color('value border color', 'rgba(0,0,0,1)'); | ||
|
||
const theme = { | ||
barSeriesStyle: { | ||
displayValue: { | ||
fontSize: number('value font size', 10), | ||
fontFamily: "'Open Sans', Helvetica, Arial, sans-serif", | ||
fontStyle: 'normal', | ||
padding: 0, | ||
fill: useInverted | ||
? { textInverted: useInverted, textContrast: true, textBorder: true } | ||
: { color: valueColor, borderColor }, | ||
offsetX: number('offsetX', 0), | ||
offsetY: number('offsetY', 0), | ||
}, | ||
}, | ||
}; | ||
|
||
const dataSize = select( | ||
'data volume size', | ||
{ | ||
'small volume': 's', | ||
'medium volume': 'm', | ||
'high volume': 'h', | ||
}, | ||
's', | ||
); | ||
const data = frozenData[dataSize]; | ||
|
||
const isSplitSeries = boolean('split series', false); | ||
const isStackedSeries = boolean('stacked series', false); | ||
|
||
const splitSeriesAccessors = isSplitSeries ? ['g'] : undefined; | ||
const stackAccessors = isStackedSeries ? ['x'] : undefined; | ||
return ( | ||
<Chart renderer="canvas" className="story-chart"> | ||
<Settings theme={theme} debug={debug} rotation={getChartRotationKnob()} showLegend showLegendExtra /> | ||
<Axis id="bottom" position={Position.Bottom} title="Bottom axis" showOverlappingTicks /> | ||
<Axis id="left2" title="Left axis" position={Position.Left} tickFormat={(d: any) => Number(d).toFixed(2)} /> | ||
<BarSeries | ||
id="bars" | ||
displayValueSettings={displayValueSettings} | ||
xScaleType={ScaleType.Linear} | ||
yScaleType={ScaleType.Linear} | ||
xAccessor="x" | ||
yAccessors={['y']} | ||
splitSeriesAccessors={splitSeriesAccessors} | ||
stackAccessors={stackAccessors} | ||
data={data} | ||
/> | ||
<BarSeries | ||
id="bars2" | ||
displayValueSettings={displayValueSettings} | ||
xScaleType={ScaleType.Linear} | ||
yScaleType={ScaleType.Linear} | ||
xAccessor="x" | ||
yAccessors={['y']} | ||
stackAccessors={['x']} | ||
splitSeriesAccessors={['g']} | ||
data={[ | ||
{ x: 0, y: 2, g: 'a' }, | ||
{ x: 1, y: 7, g: 'a' }, | ||
{ x: 2, y: 3, g: 'a' }, | ||
{ x: 3, y: 6, g: 'a' }, | ||
{ x: 0, y: 4, g: 'b' }, | ||
{ x: 1, y: 5, g: 'b' }, | ||
{ x: 2, y: 8, g: 'b' }, | ||
{ x: 3, y: 2, g: 'b' }, | ||
]} | ||
/> | ||
</Chart> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ export { Example as withLinearXAxisNoLinearInterval } from './6_linear_no_linear | |
export { Example as withTimeXAxis } from './7_with_time_xaxis'; | ||
export { Example as withLogYAxis } from './8_with_log_yaxis'; | ||
export { Example as withStackedLogYAxis } from './9_with_stacked_log'; | ||
export { Example as withValueLabelAdvanced } from './50_label_value_advanced'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you move this right after the other |
||
|
||
export { Example as withAxisAndLegend } from './10_axis_and_legend'; | ||
export { Example as stackedWithAxisAndLegend } from './11_stacked_with_axis_and_legend'; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great to add also the border width as a customizable option. This can help create different themes and adjust the theme when consuming the chart