Skip to content

Commit

Permalink
[feat,LineChart][s]: support for multiple series
Browse files Browse the repository at this point in the history
  • Loading branch information
olayway authored Oct 23, 2024
1 parent f86f054 commit 63d9e3b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-ways-refuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@portaljs/components': minor
---

Support for plotting multiple series in LineChart component.
28 changes: 26 additions & 2 deletions packages/components/src/components/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ export type LineChartProps = {
xAxis: string;
xAxisType?: AxisType;
xAxisTimeUnit?: TimeUnit;
yAxis: string;
yAxis: string | string[];
yAxisType?: AxisType;
fullWidth?: boolean;
symbol?: string;
};

export function LineChart({
Expand All @@ -26,13 +27,15 @@ export function LineChart({
xAxisTimeUnit = 'year', // TODO: defaults to undefined would probably work better... keeping it as it's for compatibility purposes
yAxis,
yAxisType = 'quantitative',
symbol,
}: LineChartProps) {
const url = data.url;
const values = data.values;
const [isLoading, setIsLoading] = useState<boolean>(false);

// By default, assumes data is an Array...
const [specData, setSpecData] = useState<any>({ name: 'table' });
const isMultiYAxis = Array.isArray(yAxis);

const spec = {
$schema: 'https://vega.github.io/schema/vega-lite/v5.json',
Expand All @@ -46,6 +49,11 @@ export function LineChart({
tooltip: true,
},
data: specData,
...(isMultiYAxis
? {
transform: [{ fold: yAxis, as: ['key', 'value'] }],
}
: {}),
selection: {
grid: {
type: 'interval',
Expand All @@ -59,9 +67,25 @@ export function LineChart({
type: xAxisType,
},
y: {
field: yAxis,
field: isMultiYAxis ? 'value' : yAxis,
type: yAxisType,
},
...(symbol
? {
color: {
field: symbol,
type: 'nominal',
},
}
: {}),
...(isMultiYAxis
? {
color: {
field: 'key',
type: 'nominal',
},
}
: {}),
},
} as any;

Expand Down
51 changes: 50 additions & 1 deletion packages/components/stories/LineChart.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ Must be an object with one of the following properties: `url` or `values` \n\n \
},
yAxis: {
description:
'Name of the column header or object property that represents the Y-axis on the data.',
'Name of the column headers or object properties that represent the Y-axis on the data.',
},
yAxisType: {
description: 'Type of the Y-axis',
},
symbol: {
description:
'Name of the column header or object property that represents a series for multiple series.',
},
},
};

Expand All @@ -60,6 +64,51 @@ export const FromDataPoints: Story = {
},
};

export const MultiSeries: Story = {
name: 'Line chart with multiple series (specifying symbol)',
args: {
data: {
values: [
{ year: '1850', value: -0.41765878, z: 'A' },
{ year: '1851', value: -0.2333498, z: 'A' },
{ year: '1852', value: -0.22939907, z: 'A' },
{ year: '1853', value: -0.27035445, z: 'A' },
{ year: '1854', value: -0.29163003, z: 'A' },
{ year: '1850', value: -0.42993882, z: 'B' },
{ year: '1851', value: -0.30365549, z: 'B' },
{ year: '1852', value: -0.27905189, z: 'B' },
{ year: '1853', value: -0.22939704, z: 'B' },
{ year: '1854', value: -0.25688013, z: 'B' },
{ year: '1850', value: -0.4757164, z: 'C' },
{ year: '1851', value: -0.41971018, z: 'C' },
{ year: '1852', value: -0.40724799, z: 'C' },
{ year: '1853', value: -0.45049156, z: 'C' },
{ year: '1854', value: -0.41896583, z: 'C' },
],
},
xAxis: 'year',
yAxis: 'value',
symbol: 'z',
},
};

export const MultiColumns: Story = {
name: 'Line chart with multiple series (with multiple columns)',
args: {
data: {
values: [
{ year: '1850', A: -0.41765878, B: -0.42993882, C: -0.4757164 },
{ year: '1851', A: -0.2333498, B: -0.30365549, C: -0.41971018 },
{ year: '1852', A: -0.22939907, B: -0.27905189, C: -0.40724799 },
{ year: '1853', A: -0.27035445, B: -0.22939704, C: -0.45049156 },
{ year: '1854', A: -0.29163003, B: -0.25688013, C: -0.41896583 },
],
},
xAxis: 'year',
yAxis: ['A', 'B', 'C'],
},
};

export const FromURL: Story = {
name: 'Line chart from URL',
args: {
Expand Down

0 comments on commit 63d9e3b

Please sign in to comment.