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

Bar chart to skip x-axis labels like Linear charts #1777

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type {Story} from '@storybook/react';

export {META as default} from './meta';

import type {BarChartProps} from '../../../components';

import {MONTHLY_REPORT_DATA, Template} from './data';

export const MonthlyReport: Story<BarChartProps> = Template.bind({});

MonthlyReport.args = {
data: MONTHLY_REPORT_DATA,
onError: (a, b) => {
console.log({a, b});
},
};
23 changes: 23 additions & 0 deletions packages/polaris-viz/src/components/BarChart/stories/data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@ export const Template: Story<BarChartProps> = (args: BarChartProps) => {
return <BarChart {...args} />;
};

const generateDataSeries = (length = 30) =>
Array(length)
.fill(0)
.map((value: number, index) => {
const date = new Date();
date.setDate(date.getDate() + value + index);

return {
key: date.toLocaleDateString(undefined, {
month: 'short',
day: 'numeric',
}),
value: Math.floor(Math.random() * 1000),
};
});

export const MONTHLY_REPORT_DATA: DataSeries[] = [
{
name: 'Current month',
data: generateDataSeries(),
},
];

export const DEFAULT_DATA: DataSeries[] = [
{
name: 'Breakfast',
Expand Down
31 changes: 25 additions & 6 deletions packages/polaris-viz/src/components/VerticalBarChart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,6 @@ export function Chart({
})
: null;

const reducedLabelIndexes = useReducedLabelIndexes({
dataLength: data[0] ? data[0].data.length : 0,
});

const {min, max} = getStackedMinMax({
stackedValues,
data,
Expand Down Expand Up @@ -194,6 +190,26 @@ export function Chart({
yAxisWidth: yAxisLabelWidth,
});

const longestLabelWidth = useMemo(() => {
const longest = Math.max(
...formattedLabels.map((formattedLabel) =>
estimateStringWidth(formattedLabel, characterWidths),
),
);

return longest;
}, [characterWidths, formattedLabels]);

const numberOfLabelsThatFit = Math.floor(drawableWidth / longestLabelWidth);
const skipEveryNthLabel = Math.ceil(
formattedLabels.length / numberOfLabelsThatFit,
);

const reducedLabelIndexes = useReducedLabelIndexes({
dataLength: data[0] ? data[0].data.length : 0,
skipEveryNthLabel,
});

const annotationsDrawableHeight =
chartYPosition + drawableHeight + ANNOTATIONS_LABELS_OFFSET;

Expand Down Expand Up @@ -229,6 +245,9 @@ export function Chart({
annotationsLookupTable,
);

const scaleStep = xScale.step();
const scaleStepHalf = xScale.step() / 2;
const labelWidth = xScale.bandwidth() + scaleStep;
const xAxisLabelHalf = xScale.bandwidth() / 2;

return (
Expand All @@ -244,10 +263,10 @@ export function Chart({
<XAxis
allowLineWrap={xAxisOptions.allowLineWrap}
labels={formattedLabels}
labelWidth={xScale.bandwidth()}
labelWidth={labelWidth}
onHeightChange={setXAxisHeight}
reducedLabelIndexes={reducedLabelIndexes}
x={xAxisBounds.x}
x={xAxisBounds.x - scaleStepHalf}
xScale={xScale}
y={xAxisBounds.y}
/>
Expand Down
Loading