-
Notifications
You must be signed in to change notification settings - Fork 841
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 all 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,7 +38,7 @@ export const CategoryChart = () => { | |
const [ordered, setOrdered] = useState(true); | ||
const [formatted, setFormatted] = useState(false); | ||
const [chartType, setChartType] = useState('BarSeries'); | ||
|
||
const [valueLabels, setValueLabels] = useState(false); | ||
const onMultiChange = (multiObject) => { | ||
const { multi, stacked } = multiObject; | ||
setMulti(multi); | ||
|
@@ -61,6 +61,10 @@ export const CategoryChart = () => { | |
setChartType(chartType); | ||
}; | ||
|
||
const onValueLabelsChange = (e) => { | ||
setValueLabels(e.target.checked); | ||
}; | ||
|
||
const isDarkTheme = themeContext.theme.includes('dark'); | ||
const theme = isDarkTheme | ||
? EUI_CHARTS_THEME_DARK.theme | ||
|
@@ -70,6 +74,115 @@ 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 ? 4 : 0, | ||
offsetY: rotated ? 0 : -4, | ||
...(multi && stacked | ||
? { | ||
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 chartVariablesForValueLabels = `const theme = isDarkTheme | ||
? EUI_CHARTS_THEME_DARK.theme | ||
: EUI_CHARTS_THEME_LIGHT.theme; | ||
|
||
const customTheme = { | ||
...theme, | ||
barSeriesStyle: { | ||
displayValue: { | ||
...theme.barSeriesStyle.displayValue, | ||
offsetX: ${rotated ? '4' : '0'}, | ||
offsetY: ${rotated ? '0' : '-4'}, | ||
${multi && stacked ? defaultAlignmentToCopy : alignmentRotatedToCopy}, | ||
}, | ||
}, | ||
};`; | ||
|
||
const multiConfigData = `[{ vizType: "Data Table", count: 6, issueType: "Bug" }, | ||
{ vizType: "Data Table", count: 24, issueType: "Other" }, | ||
{ vizType: "Heatmap", count: 12, issueType: "Bug" }, | ||
{ vizType: "Heatmap", count: 20, issueType: "Other" }] | ||
`; | ||
|
||
const singleConfigData = `[{vizType: 'Data Table', count: 24, issueType: 'Bug'}, | ||
{vizType: 'Heatmap',count: 12, issueType: 'Other'}] | ||
`; | ||
|
||
const chartConfigurationToCopy = `<Chart size={{height: 300}}> | ||
<Settings | ||
theme={${valueLabels ? 'customTheme' : 'theme'}} | ||
rotation={${rotated ? 90 : 0}} | ||
showLegend={${multi}} | ||
${multi ? 'legendPosition="right"' : ''} | ||
/> | ||
<${chartType} | ||
id="issues" | ||
name="Issues" | ||
data={ | ||
${multi ? multiConfigData : singleConfigData} | ||
} | ||
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`}' : ''} | ||
/> | ||
</Chart>`; | ||
|
||
const removeEmptyLines = (string) => string.replace(/(^[ \t]*\n)/gm, ''); | ||
|
||
const textToCopy = valueLabels | ||
? `${chartVariablesForValueLabels} | ||
|
||
${removeEmptyLines(chartConfigurationToCopy)}` | ||
: `${removeEmptyLines(chartConfigurationToCopy)}`; | ||
|
||
return ( | ||
<Fragment> | ||
<EuiTitle size="xxs"> | ||
|
@@ -81,9 +194,9 @@ export const CategoryChart = () => { | |
|
||
<EuiSpacer size="s" /> | ||
|
||
<Chart size={{ height: 300 }}> | ||
<Chart size={{ height: 400 }}> | ||
<Settings | ||
theme={theme} | ||
theme={customTheme} | ||
miukimiu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
showLegend={multi} | ||
legendPosition="right" | ||
rotation={rotated ? 90 : 0} | ||
|
@@ -100,6 +213,7 @@ export const CategoryChart = () => { | |
yAccessors={['count']} | ||
splitSeriesAccessors={multi ? ['issueType'] : undefined} | ||
stackAccessors={stacked ? ['issueType'] : undefined} | ||
displayValueSettings={valueLabels && displayValueSettings} | ||
/> | ||
<Axis | ||
id="bottom-axis" | ||
|
@@ -170,44 +284,26 @@ 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." | ||
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. suggestion: usually the reason why you want to show numbers on a chart is that you want to clearly present the exact number (all digits/decimal digits are important). If that is the case, a suggestion could be to include, on the side, a small table that definitely shows clearly all the numbers and allow the user to sort them by value or categories. |
||
> | ||
<EuiSpacer size="s" /> | ||
<EuiSwitch | ||
label="Show value labels" | ||
checked={valueLabels} | ||
onChange={onValueLabelsChange} | ||
/> | ||
</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 |
---|---|---|
|
@@ -100,9 +100,17 @@ function createTheme(colors: any): 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.euiTextSubduedColor.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.
@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
showValueLabel
setting, except when the chart gets rotated. Is this a known issue in elastic-charts?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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
elastic/elastic-charts#1387