-
Notifications
You must be signed in to change notification settings - Fork 844
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
[Elastic Charts] Added value labels styles for bar charts #4845
Changes from 14 commits
2cd5539
c09bfdf
6c4e731
e1f3f7c
9382384
56a8aab
7a3f23d
20e86ea
90166c3
69e1ab3
8d1242f
f63de66
7973a88
a5c0094
e82929f
9ea28f2
924eaf8
91e91f5
d3c215c
2833783
9a19371
467370e
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 |
---|---|---|
|
@@ -38,6 +38,8 @@ export const CategoryChart = () => { | |
const [ordered, setOrdered] = useState(true); | ||
const [formatted, setFormatted] = useState(false); | ||
const [chartType, setChartType] = useState('BarSeries'); | ||
const [valueLabels, setValueLabels] = useState(false); | ||
const [valueLabelsCenter, setValueLabelsCenter] = useState(false); | ||
|
||
const onMultiChange = (multiObject) => { | ||
const { multi, stacked } = multiObject; | ||
|
@@ -61,6 +63,14 @@ export const CategoryChart = () => { | |
setChartType(chartType); | ||
}; | ||
|
||
const onValueLabelsChange = (e) => { | ||
setValueLabels(e.target.checked); | ||
}; | ||
|
||
const onValueLabelsCenterChange = (e) => { | ||
setValueLabelsCenter(e.target.checked); | ||
}; | ||
|
||
const isDarkTheme = themeContext.theme.includes('dark'); | ||
const theme = isDarkTheme | ||
? EUI_CHARTS_THEME_DARK.theme | ||
|
@@ -70,6 +80,105 @@ export const CategoryChart = () => { | |
|
||
const DATASET = multi ? GITHUB_DATASET : SIMPLE_GITHUB_DATASET; | ||
|
||
const displayValueSettings = { | ||
showValueLabel: true, | ||
}; | ||
|
||
const customTheme = { | ||
...theme, | ||
barSeriesStyle: { | ||
displayValue: { | ||
...theme.barSeriesStyle.displayValue, | ||
offsetX: rotated ? 2 : 0, | ||
offsetY: rotated ? 0 : -2, | ||
...(valueLabelsCenter | ||
? { | ||
alignment: { | ||
vertical: 'middle', | ||
horizontal: 'center', | ||
}, | ||
} | ||
: { | ||
alignment: rotated | ||
? { | ||
vertical: 'middle', | ||
} | ||
: { | ||
horizontal: 'center', | ||
}, | ||
}), | ||
}, | ||
}, | ||
}; | ||
|
||
const defaultAlignmentToCopy = `alignment: { | ||
vertical: 'middle', | ||
horizontal: 'center', | ||
}`; | ||
|
||
const alignmentRotatedToCopy = rotated | ||
? `alignment: { | ||
vertical: 'middle', | ||
}` | ||
: `alignment: { | ||
horizontal: 'center', | ||
}`; | ||
|
||
const chartVariablesToCopy = `const theme = isDarkTheme | ||
? EUI_CHARTS_THEME_DARK.theme | ||
: EUI_CHARTS_THEME_LIGHT.theme; | ||
|
||
const customTheme = { | ||
...theme, | ||
barSeriesStyle: { | ||
displayValue: { | ||
...theme.barSeriesStyle.displayValue, | ||
offsetX: ${rotated ? '2' : '0'}, | ||
offsetY: ${rotated ? '0' : '-2'}, | ||
${valueLabelsCenter ? defaultAlignmentToCopy : alignmentRotatedToCopy}, | ||
}, | ||
};`; | ||
|
||
const chartConfigurationToCopy = `<Chart size={{height: 300}}> | ||
<Settings | ||
theme={customTheme} | ||
rotation={${rotated ? 90 : 0}} | ||
showLegend={${multi}} | ||
${multi ? 'legendPosition="right"' : ''} | ||
/> | ||
<${chartType} | ||
id="issues" | ||
name="Issues" | ||
data={${ | ||
ordered | ||
? "orderBy([{vizType: 'Data Table', count: 24, issueType: 'Bug'},{vizType: 'Heatmap',count: 12, issueType: 'Other'}], ['count'], ['desc'])" | ||
: "orderBy([{vizType: 'Data Table', count: 24, issueType: 'Bug'},{vizType: 'Heatmap',count: 12, issueType: 'Other'}], ['vizType'], ['asc'])" | ||
}} | ||
xAccessor="vizType" | ||
yAccessors={['count']} | ||
${multi ? "splitSeriesAccessors={['issueType']}" : ''} | ||
${stacked ? "stackAccessors={['issueType']}" : ''} | ||
${valueLabels ? 'displayValueSettings={{showValueLabel: true}}' : ''} | ||
/> | ||
<Axis | ||
id="bottom-axis" | ||
position={${rotated ? 'left' : 'bottom'}} | ||
showGridLines={false} | ||
/> | ||
<Axis | ||
id="left-axis" | ||
position={${rotated ? 'bottom' : 'left'}} | ||
${formatted ? 'tickFormat={d => `${round(Number(d) / 1000, 2)}k`}' : ''} | ||
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. @markov00 The one thing I noticed is that when we format the tick labels on a particular axis, it does also get applied to the 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. thank you @cchaos you are right, this is an issue in our code. I will check and fix the bug soon. 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. |
||
/> | ||
</Chart>`; | ||
|
||
const removeEmptyLines = (string) => string.replace(/(^[ \t]*\n)/gm, ''); | ||
|
||
const textToCopy = `${chartVariablesToCopy} | ||
|
||
${removeEmptyLines(chartConfigurationToCopy)} | ||
`; | ||
elizabetdev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return ( | ||
<Fragment> | ||
<EuiTitle size="xxs"> | ||
|
@@ -81,9 +190,9 @@ export const CategoryChart = () => { | |
|
||
<EuiSpacer size="s" /> | ||
|
||
<Chart size={{ height: 300 }}> | ||
<Chart size={{ height: 400 }}> | ||
<Settings | ||
theme={theme} | ||
theme={customTheme} | ||
elizabetdev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
showLegend={multi} | ||
legendPosition="right" | ||
rotation={rotated ? 90 : 0} | ||
|
@@ -100,6 +209,7 @@ export const CategoryChart = () => { | |
yAccessors={['count']} | ||
splitSeriesAccessors={multi ? ['issueType'] : undefined} | ||
stackAccessors={stacked ? ['issueType'] : undefined} | ||
displayValueSettings={valueLabels && displayValueSettings} | ||
/> | ||
<Axis | ||
id="bottom-axis" | ||
|
@@ -168,43 +278,32 @@ export const CategoryChart = () => { | |
<EuiFlexItem> | ||
<MultiChartCard onChange={onMultiChange} /> | ||
</EuiFlexItem> | ||
|
||
<EuiFlexItem> | ||
<ChartCard | ||
title="Value labels" | ||
description="Value labels can add too much detail and make categorical charts more difficult to interpret. Consider showing them only when the values are of extreme importance."> | ||
<EuiSpacer size="s" /> | ||
<EuiSwitch | ||
label="Show value labels" | ||
checked={valueLabels} | ||
onChange={onValueLabelsChange} | ||
/> | ||
<EuiSpacer size="s" /> | ||
<EuiSwitch | ||
label="Center value labels" | ||
checked={valueLabelsCenter} | ||
onChange={onValueLabelsCenterChange} | ||
disabled={!valueLabels} | ||
/> | ||
</ChartCard> | ||
</EuiFlexItem> | ||
</EuiFlexGrid> | ||
|
||
<EuiSpacer /> | ||
|
||
<div className="eui-textCenter"> | ||
<EuiCopy | ||
textToCopy={`<Chart size={{height: 300}}> | ||
<Settings | ||
theme={isDarkTheme ? EUI_CHARTS_THEME_DARK.theme : EUI_CHARTS_THEME_LIGHT.theme} | ||
rotation={${rotated ? 90 : 0}} | ||
showLegend={${multi}} | ||
${multi ? 'legendPosition="right"' : ''} | ||
/> | ||
<${chartType} | ||
id="issues" | ||
name="Issues" | ||
data={${ | ||
ordered | ||
? "orderBy([{vizType: 'Data Table', count: 24, issueType: 'Bug'},{vizType: 'Heatmap',count: 12, issueType: 'Other'}], ['count'], ['desc'])" | ||
: "orderBy([{vizType: 'Data Table', count: 24, issueType: 'Bug'},{vizType: 'Heatmap',count: 12, issueType: 'Other'}], ['vizType'], ['asc'])" | ||
}} | ||
xAccessor="vizType" | ||
yAccessors={['count']} | ||
${multi ? "splitSeriesAccessors={['issueType']}" : ''} | ||
${stacked ? "stackAccessors={['issueType']}" : ''} | ||
/> | ||
<Axis | ||
id="bottom-axis" | ||
position={${rotated ? 'left' : 'bottom'}} | ||
showGridLines={false} | ||
/> | ||
<Axis | ||
id="left-axis" | ||
position={${rotated ? 'bottom' : 'left'}} | ||
${formatted ? 'tickFormat={d => `${round(Number(d) / 1000, 2)}k`}' : ''} | ||
/> | ||
</Chart>`}> | ||
<EuiCopy textToCopy={textToCopy}> | ||
{(copy) => ( | ||
<EuiButton fill onClick={copy} iconType="copyClipboard"> | ||
Copy code of current configuration | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,9 +104,17 @@ function createTheme(colors: any, mode: string): EuiChartThemeType { | |
}, | ||
barSeriesStyle: { | ||
displayValue: { | ||
fontSize: 8, | ||
fontSize: 10, | ||
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. You can also set a min and max font size, the code automatically uses the right size depending on the available space. |
||
fontFamily: fontFamily, | ||
fill: colors.euiColorDarkShade.rgba, | ||
fill: { | ||
textInvertible: true, | ||
textContrast: true, | ||
textBorder: 0, | ||
}, | ||
alignment: { | ||
horizontal: 'center', | ||
vertical: 'middle', | ||
}, | ||
}, | ||
}, | ||
scales: { | ||
|
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.
From what I see, this is used as padding on the text. We can probably introduce a default or custom padding and we can get rid of this part
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.
elastic/elastic-charts#1375
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.
I agree 👍🏽
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.
Optional, but I'd love to see even more separation.