Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
10 changes: 10 additions & 0 deletions .changeset/rotten-parrots-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@lg-charts/core': minor
---

Chart improvements and refactoring:
- ChartTooltip: Added `axisPointer` prop supporting 'line', 'shadow', and 'none' options
- ChartTooltip: Added `className` prop for custom styling
- Bar: `emphasis` prop now accepts 'self' and 'none' options to control hover focus behavior
Copy link
Collaborator

Choose a reason for hiding this comment

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

should update to reflect the consumer API changes

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🫑

- Axis Types: Added 'category' axis type for discrete data (e.g., bar charts) with properly aligned labels
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you add explicit details about how the discriminated union props API works for XAxis / YAxis?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fair, will do 🫑

- Bug Fix: Fixed null/undefined check in CustomTooltip to properly handle falsy values like 0 and empty strings
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would consolidate this under the ChartTooltip notes since consumers are not aware of CustomTooltip

68 changes: 68 additions & 0 deletions charts/core/src/Axis/Axis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { CartesianAxisOption } from 'echarts/types/src/coord/cartesian/AxisModel.js';

import { Theme } from '@leafygreen-ui/lib';
import {
color,
fontFamilies,
fontWeights,
InteractionState,
spacing,
Variant,
} from '@leafygreen-ui/tokens';

import { type AxisProps } from './Axis.types';

export const getAxisOptions = (
theme: Theme,
props: AxisProps,
): CartesianAxisOption => {
const { label, type } = props;

return {
type,
...(type === 'category'
? {
data: props.labels,
}
: {
min: props.min,
max: props.max,
}),
axisLine: {
show: true,
lineStyle: {
color: color[theme].border[Variant.Secondary][InteractionState.Default],
width: 1,
},
},
axisLabel: {
show: true,
fontFamily: fontFamilies.default,
fontWeight: fontWeights.medium,
fontSize: 11,
lineHeight: spacing[400],
color: color[theme].text[Variant.Secondary][InteractionState.Default],
formatter: type === 'category' ? undefined : props.formatter,
},
axisTick: {
show: false,
},
name: label,
nameLocation: 'middle',
nameTextStyle: {
fontFamily: fontFamilies.default,
fontWeight: fontWeights.medium,
fontSize: 11,
color: color[theme].text[Variant.Secondary][InteractionState.Default],
},
};
};

export const unsetAxisOptions = {
axisLine: {
show: false,
},
axisLabel: {
show: false,
},
};
74 changes: 74 additions & 0 deletions charts/core/src/Axis/Axis.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { AxisLabelValueFormatter } from '../Echart/Echart.types';

/**
* Note: choosing this axis type implicates that the charting library is free to automatically interpolate axis labels and their positions
* which might not exactly align to each provided data point.
*/
const ContinuousAxisTypes = {
Log: 'log',
Time: 'time',
Value: 'value',
} as const;

type ContinuousAxisTypes =
(typeof ContinuousAxisTypes)[keyof typeof ContinuousAxisTypes];

const DiscreteAxisTypes = {
Category: 'category',
} as const;

type DiscreteAxisTypes =
(typeof DiscreteAxisTypes)[keyof typeof DiscreteAxisTypes];

export const AxisType = {
...ContinuousAxisTypes,
...DiscreteAxisTypes,
} as const;

export type AxisType = (typeof AxisType)[keyof typeof AxisType];

interface AxisPropsBase {
/**
* Label name of the axis.
*/
label?: string;
/**
* Type of the axis.
*/
type: AxisType;
}

export interface ContinuousAxisProps extends AxisPropsBase {
type: ContinuousAxisTypes;

/**
*
* Formatter of axis label, which supports string template and callback function.
*
* ```ts
* formatter: (value, index) => `${value}GB`
* ```
*/
formatter?: AxisLabelValueFormatter | string;

/**
* Minimum value of the axis.
*/
min?: number;

/**
* Maximum value of the axis.
*/
max?: number;
}

export interface DiscreteAxisProps extends AxisPropsBase {
type: DiscreteAxisTypes;

/**
* Labels of the data points on the axis.
*/
labels?: Array<string>;
}

export type AxisProps = ContinuousAxisProps | DiscreteAxisProps;
79 changes: 79 additions & 0 deletions charts/core/src/Axis/XAxis.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { useEffect } from 'react';
import merge from 'lodash/merge';

import { useObjectDependency } from '@leafygreen-ui/hooks';
import { useDarkMode } from '@leafygreen-ui/leafygreen-provider';
import { Theme } from '@leafygreen-ui/lib';
import { spacing } from '@leafygreen-ui/tokens';

import { useChartContext } from '../ChartContext';
import { type EChartOptions } from '../Echart';

import { getAxisOptions, unsetAxisOptions } from './Axis';
import { XAxisProps } from './XAxis.types';

const getXAxisOptions = (
theme: Theme,
props: XAxisProps,
): Omit<EChartOptions, 'series'> => {
const axisOptions = merge(getAxisOptions(theme, props), {
axisLabel: {
align: 'center',
margin: spacing[400],
},
nameTextStyle: {
lineHeight: spacing[400],
padding: [spacing[200], 0, 0, 0],
},
nameGap: spacing[1000],
});

return {
xAxis: axisOptions,
grid: {
bottom: props.label
? spacing[1200] // Pushes out to make room for the label
: spacing[400], // Default bottom spacing
},
};
};

/**
* React component that can render an x-axis on a parent chart.
*
* This is done by updating the parent chart's canvas configuration received via context.
*
* ```
* <Chart>
* <XAxis
* type="time",
* label="My X-Axis Data",
* formatter="{value}GB"
* />
* </Chart>
*/
export function XAxis(props: XAxisProps) {
const {
chart: { ready, updateOptions },
} = useChartContext();
const { theme } = useDarkMode();
const propsDep = useObjectDependency(props);

useEffect(() => {
if (!ready) return;

updateOptions(getXAxisOptions(theme, propsDep));

return () => {
/**
* Hides the axis when the component is unmounted.
*/
updateOptions({
xAxis: { ...unsetAxisOptions },
grid: { bottom: spacing[400] }, // Reset the grid bottom spacing
});
};
}, [ready, theme, updateOptions, propsDep]);

return null;
}
6 changes: 6 additions & 0 deletions charts/core/src/Axis/XAxis.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { AxisProps, AxisType } from './Axis.types';

export const XAxisType = AxisType;
type XAxisType = (typeof XAxisType)[keyof typeof XAxisType];

export type XAxisProps = AxisProps;
78 changes: 78 additions & 0 deletions charts/core/src/Axis/YAxis.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { useEffect } from 'react';
import merge from 'lodash/merge';

import { useObjectDependency } from '@leafygreen-ui/hooks';
import { useDarkMode } from '@leafygreen-ui/leafygreen-provider';
import { Theme } from '@leafygreen-ui/lib';
import { spacing } from '@leafygreen-ui/tokens';

import { useChartContext } from '../ChartContext';
import { type EChartOptions } from '../Echart';

import { getAxisOptions, unsetAxisOptions } from './Axis';
import { YAxisProps } from './YAxis.types';

const getYAxisOptions = (
theme: Theme,
props: YAxisProps,
): Omit<EChartOptions, 'series'> => {
const axisOptions = merge(getAxisOptions(theme, props), {
axisLabel: {
align: 'right',
margin: spacing[200],
},
nameTextStyle: {
padding: [0, 0, spacing[800], 0],
},
nameGap: spacing[900],
});

return {
yAxis: axisOptions,
grid: {
left: props.label
? spacing[1200] // Pushes out to make room for the label
: spacing[300], // Default left spacing
},
};
};

/**
* React component that can render an y-axis on a parent chart.
*
* This is done by updating the parent chart's canvas configuration received via context.
*
* ```
* <Chart>
* <YAxis
* type="value",
* label="My Y-Axis Data",
* formatter="{value}GB"
* />
* </Chart>
*/
export function YAxis(props: YAxisProps) {
const {
chart: { ready, updateOptions },
} = useChartContext();
const { theme } = useDarkMode();
const propsDep = useObjectDependency(props);

useEffect(() => {
if (!ready) return;

updateOptions(getYAxisOptions(theme, propsDep));

return () => {
/**
* Hides the axis when the component is unmounted.
*/
updateOptions({
yAxis: { ...unsetAxisOptions },
grid: { left: spacing[300] }, // Reset the grid left spacing
});
};
}, [ready, theme, updateOptions, propsDep]);

return null;
}
6 changes: 6 additions & 0 deletions charts/core/src/Axis/YAxis.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { AxisProps, AxisType } from './Axis.types';

export const YAxisType = AxisType;
type YAxisType = (typeof YAxisType)[keyof typeof YAxisType];

export type YAxisProps = AxisProps;
10 changes: 10 additions & 0 deletions charts/core/src/Axis/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type {
AxisProps,
AxisType,
ContinuousAxisProps,
DiscreteAxisProps,
} from './Axis.types';
export { XAxis } from './XAxis';
export type { XAxisProps, XAxisType } from './XAxis.types';
export { YAxis } from './YAxis';
export type { YAxisProps, YAxisType } from './YAxis.types';
Loading
Loading