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

[Elastic Charts] Added value labels styles for bar charts #4845

Merged
merged 22 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
2cd5539
Adding new theme value labels styling
elizabetdev Jun 1, 2021
c09bfdf
Merge remote-tracking branch 'upstream/master' into xy-chart-theme-va…
elizabetdev Jul 16, 2021
6c4e731
Merge remote-tracking branch 'upstream/master' into xy-chart-theme-va…
elizabetdev Jul 26, 2021
e1f3f7c
Setting `textBorder` to zero
elizabetdev Jul 26, 2021
9382384
Merge remote-tracking branch 'upstream/master' into xy-chart-theme-va…
elizabetdev Jul 26, 2021
56a8aab
Adding `showValueLabels` field in MultiChartCard
elizabetdev Jul 26, 2021
7a3f23d
Increase category chart height
elizabetdev Jul 27, 2021
20e86ea
Adding center alignment
elizabetdev Jul 27, 2021
90166c3
CL
elizabetdev Jul 27, 2021
69e1ab3
Shared comp improvement
elizabetdev Jul 27, 2021
8d1242f
Update CHANGELOG.md
elizabetdev Jul 28, 2021
f63de66
Merge remote-tracking branch 'upstream/master' into xy-chart-theme-va…
elizabetdev Jul 28, 2021
7973a88
Adding value labels card and config text to copy.
elizabetdev Jul 28, 2021
a5c0094
Merge remote-tracking branch 'upstream/master' into xy-chart-theme-va…
elizabetdev Jul 28, 2021
e82929f
Merge remote-tracking branch 'upstream/master' into xy-chart-theme-va…
elizabetdev Sep 9, 2021
9ea28f2
Adding multi and stacked centered
elizabetdev Sep 9, 2021
924eaf8
CL
elizabetdev Sep 9, 2021
91e91f5
Adding better data for multi series and removing lodash orderBy
elizabetdev Sep 9, 2021
d3c215c
Better indentation
elizabetdev Sep 9, 2021
2833783
Merge remote-tracking branch 'upstream/master' into xy-chart-theme-va…
elizabetdev Sep 9, 2021
9a19371
Custom theme only when value labels. Small styles fixes.
elizabetdev Sep 16, 2021
467370e
Merge remote-tracking branch 'upstream/master' into xy-chart-theme-va…
elizabetdev Sep 16, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Updated `EuiText`s `color` prop to accept `inherit` and custom colors. Updated the `size` prop to accept `relative` ([#4663](https://github.com/elastic/eui/pull/4663))
- Updated `EuiText`s `blockquote` font-size/line-height to match the base font-size/line-height which is the same as paragraphs ([#4663](https://github.com/elastic/eui/pull/4663))
- Added `markdownFormatProps` prop to `EuiMarkdownEditor` to extend the props passed to the rendered `EuiMarkdownFormat` ([#4663](https://github.com/elastic/eui/pull/4663))
- Updated `barSeriesStyle.displayValue` of the elastic-charts `Theme` for better default styles ([#4845](https://github.com/elastic/eui/pull/4845))
- Added optional virtualized line rendering to `EuiCodeBlock` ([#4952](https://github.com/elastic/eui/pull/4952))
- Added `current` as a `status` of `EuiHorizontalStep` ([#4911](https://github.com/elastic/eui/pull/4911))
- Exported `onChange` type for `EuiSearchBar` ([#4968](https://github.com/elastic/eui/pull/4968))
Expand Down
167 changes: 133 additions & 34 deletions src-docs/src/views/elastic_charts/category_chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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,
Copy link
Member

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree 👍🏽

Copy link
Contributor

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.

Suggested change
offsetX: rotated ? 2 : 0,
offsetY: rotated ? 0 : -2,
offsetX: rotated ? 4 : 0,
offsetY: rotated ? 0 : -4,

...(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`}' : ''}
Copy link
Contributor

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?

Screen Shot 2021-09-14 at 11 45 57 AM

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The 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">
Expand All @@ -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}
Expand All @@ -100,6 +209,7 @@ export const CategoryChart = () => {
yAccessors={['count']}
splitSeriesAccessors={multi ? ['issueType'] : undefined}
stackAccessors={stacked ? ['issueType'] : undefined}
displayValueSettings={valueLabels && displayValueSettings}
/>
<Axis
id="bottom-axis"
Expand Down Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/themes/charts/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,17 @@ function createTheme(colors: any, mode: string): EuiChartThemeType {
},
barSeriesStyle: {
displayValue: {
fontSize: 8,
fontSize: 10,
Copy link
Member

Choose a reason for hiding this comment

The 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: {
Expand Down