-
Notifications
You must be signed in to change notification settings - Fork 122
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 10 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 |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* 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 borderSize = number('value border width', 1.5); | ||
|
||
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: borderSize } | ||
: { color: valueColor, borderColor, borderWidth: borderSize }, | ||
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> | ||
); | ||
}; |
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