Line chart with comparison
The current period against an earlier one, each on its own date axis.
Preview
Customers vs previous period
Usage
import {
ChartsProvider,
LineChartComparison,
dimension,
measure,
results,
} from 'em-kit';
const startDate = dimension({
name: 'startDate',
title: 'Start date',
type: 'time',
granularity: 'month',
});
const customers = measure({
name: 'customers',
title: 'Customers',
inputs: {
abbreviateLargeNumber: true,
previousLineDashed: true,
previousLineColor: '#a1a1aa',
},
});
export const Example = () => (
<ChartsProvider>
<div style={{ height: 360 }}>
<LineChartComparison
title="Customers vs previous period"
xAxis={startDate}
measures={[customers]}
results={results(rows)}
resultsComparison={results(comparisonRows)}
comparisonPeriod="Previous period"
primaryDateRange={currentRange}
comparisonDateRange={previousRange}
showValueLabels={false}
/>
</div>
</ChartsProvider>
);
const currentRange = {
from: new Date('2026-03-01'),
to: new Date('2026-06-30'),
};
const previousRange = {
from: new Date('2025-11-01'),
to: new Date('2026-02-28'),
};
const rows = [
{ startDate: '2026-03-01T00:00:00.000', customers: 469 },
{ startDate: '2026-04-01T00:00:00.000', customers: 410 },
{ startDate: '2026-05-01T00:00:00.000', customers: 428 },
{ startDate: '2026-06-01T00:00:00.000', customers: 476 },
];
const comparisonRows = [
{ startDate: '2025-11-01T00:00:00.000', customers: 478 },
{ startDate: '2025-12-01T00:00:00.000', customers: 498 },
{ startDate: '2026-01-01T00:00:00.000', customers: 406 },
{ startDate: '2026-02-01T00:00:00.000', customers: 385 },
];- Both lines are solid and take the measure’s colour unless the measure says otherwise: here
previousLineDashedandpreviousLineColorset the earlier line apart. comparisonPeriodis needed: without it the earlier line is not drawn.
Install with npm install em-kit. Needs <ChartsProvider> once and a parent with a height: Quick start.
Examples
One date axis
Both lines against the current period’s dates, without the second x-axis. showComparisonAxis={false} drops the axis that carries the comparison period’s own dates.
Show full code (65 lines)
import {
ChartsProvider,
LineChartComparison,
dimension,
measure,
results,
} from 'em-kit';
const startDate = dimension({
name: 'startDate',
title: 'Start date',
type: 'time',
granularity: 'month',
});
const customers = measure({
name: 'customers',
title: 'Customers',
inputs: {
abbreviateLargeNumber: true,
previousLineDashed: true,
previousLineColor: '#a1a1aa',
},
});
export const Example = () => (
<ChartsProvider>
<div style={{ height: 360 }}>
<LineChartComparison
title="Customers vs previous period"
xAxis={startDate}
measures={[customers]}
results={results(rows)}
resultsComparison={results(comparisonRows)}
comparisonPeriod="Previous period"
primaryDateRange={currentRange}
comparisonDateRange={previousRange}
showValueLabels={false}
showComparisonAxis={false}
/>
</div>
</ChartsProvider>
);
const currentRange = {
from: new Date('2026-03-01'),
to: new Date('2026-06-30'),
};
const previousRange = {
from: new Date('2025-11-01'),
to: new Date('2026-02-28'),
};
const rows = [
{ startDate: '2026-03-01T00:00:00.000', customers: 469 },
{ startDate: '2026-04-01T00:00:00.000', customers: 410 },
{ startDate: '2026-05-01T00:00:00.000', customers: 428 },
{ startDate: '2026-06-01T00:00:00.000', customers: 476 },
];
const comparisonRows = [
{ startDate: '2025-11-01T00:00:00.000', customers: 478 },
{ startDate: '2025-12-01T00:00:00.000', customers: 498 },
{ startDate: '2026-01-01T00:00:00.000', customers: 406 },
{ startDate: '2026-02-01T00:00:00.000', customers: 385 },
];<LineChartComparison
title="Customers vs previous period"
xAxis={startDate}
measures={[customers]}
results={results(rows)}
resultsComparison={results(comparisonRows)}
comparisonPeriod="Previous period"
primaryDateRange={currentRange}
comparisonDateRange={previousRange}
showValueLabels={false}
showComparisonAxis={false}
/>Styled lines
Each period in a colour of your choice, both drawn solid. lineColor and previousLineColor are field options on the measure. Either line is solid unless lineDashed or previousLineDashed is set.
Show full code (64 lines)
import {
ChartsProvider,
LineChartComparison,
dimension,
measure,
results,
} from 'em-kit';
const startDate = dimension({
name: 'startDate',
title: 'Start date',
type: 'time',
granularity: 'month',
});
const customers = measure({
name: 'customers',
title: 'Customers',
inputs: {
abbreviateLargeNumber: true,
lineColor: '#0ea5e9',
previousLineColor: '#f59e0b',
},
});
export const Example = () => (
<ChartsProvider>
<div style={{ height: 360 }}>
<LineChartComparison
title="Customers vs previous period"
xAxis={startDate}
measures={[customers]}
results={results(rows)}
resultsComparison={results(comparisonRows)}
comparisonPeriod="Previous period"
primaryDateRange={currentRange}
comparisonDateRange={previousRange}
showValueLabels={false}
/>
</div>
</ChartsProvider>
);
const currentRange = {
from: new Date('2026-03-01'),
to: new Date('2026-06-30'),
};
const previousRange = {
from: new Date('2025-11-01'),
to: new Date('2026-02-28'),
};
const rows = [
{ startDate: '2026-03-01T00:00:00.000', customers: 469 },
{ startDate: '2026-04-01T00:00:00.000', customers: 410 },
{ startDate: '2026-05-01T00:00:00.000', customers: 428 },
{ startDate: '2026-06-01T00:00:00.000', customers: 476 },
];
const comparisonRows = [
{ startDate: '2025-11-01T00:00:00.000', customers: 478 },
{ startDate: '2025-12-01T00:00:00.000', customers: 498 },
{ startDate: '2026-01-01T00:00:00.000', customers: 406 },
{ startDate: '2026-02-01T00:00:00.000', customers: 385 },
];const customers = measure({
name: 'customers',
title: 'Customers',
inputs: {
abbreviateLargeNumber: true,
lineColor: '#0ea5e9',
previousLineColor: '#f59e0b',
},
});
<LineChartComparison
title="Customers vs previous period"
xAxis={startDate}
measures={[customers]}
results={results(rows)}
resultsComparison={results(comparisonRows)}
comparisonPeriod="Previous period"
primaryDateRange={currentRange}
comparisonDateRange={previousRange}
showValueLabels={false}
/>Props
Keyboard and screen readers: Accessibility.
| Prop | Type | Default | Description |
|---|---|---|---|
xAxisrequired | Dimension | The dimension along the x-axis. A time dimension gets gap filling and date labels. | |
measuresrequired | Measure[] | The numeric fields to plot, one series each. Their inputs set the formatting. | |
resultsrequired | DataResponse | Your query’s state: { isLoading, data, error }, where data is your rows. results(rows) builds it from an array. | |
resultsComparisonrequired | DataResponse | Rows for the comparison period, with the same keys as results. | |
comparisonDateRangerequired | TimeRange | The dates resultsComparison covers: { from: Date, to: Date }, with an optional relativeTimeString naming a preset. The comparison’s axis runs from from to to. | |
primaryDateRangerequired | TimeRange | The dates results covers: { from: Date, to: Date }, with an optional relativeTimeString naming a preset. The x-axis runs from from to to. | |
reverseXAxis | boolean | false | Reverse the order of the x-axis. |
showLegend | boolean | true | Show the legend. A chart with one series shows none; entries wrap onto more lines when they do not fit. |
showLogarithmicScale | boolean | false | Use a logarithmic value axis, for values that span several orders of magnitude. |
showTooltips | boolean | true | Show a formatted tooltip on hover. |
showValueLabels | boolean | true | Print each value on the chart. |
xAxisLabel | string | A title under the x-axis. | |
yAxisLabel | string | A title beside the y-axis. | |
yAxisRangeMax | number | Fix the highest value on the y-axis. When unset, it fits the data. | |
yAxisRangeMin | number | Fix the lowest value on the y-axis. When unset, a bar, line or area chart’s axis includes zero, and a scatter or bubble chart’s fits the data. | |
comparisonPeriod | string | Which period resultsComparison holds. One of "Previous period", "Previous week", "Previous month", "Previous quarter", "Previous year", "Same period last week", "Same period last month", "Same period last quarter", "Same period last year". Without it the comparison is not drawn. With setComparisonDateRange the comparison dates are worked out from it, and any other string gives no range. | |
showComparisonAxis | boolean | true | Show a second x-axis with the comparison period’s dates. Set it to false to draw both lines against the current dates. |
granularity | Granularity | Only sets the size of the date range a click reports (dimensionTimeRange). The axis labels and the gap filling come from the time field, dimension({ granularity }), and this prop follows the field when left out. One of "second", "minute", "hour", "day", "week", "month", "quarter", "year". | |
setGranularity | ( | Called with the granularity the reader picks in the card. The picker only shows when the time field sets showGranularityDropdown: true in its inputs. Store the value, set it as the field’s granularity and pass rows at that granularity. | |
setComparisonDateRange | ( | If set, the chart works out the comparison dates from primaryDateRange and comparisonPeriod and calls this with them, so you can query them. When it cannot work them out, it calls this with no range. | |
onLineClicked | ( | Called with the clicked point’s dimension value and date range.LineChartOptionsClickArg = { dimensionValue: string | number | undefined; dimensionTimeRange: TimeRange; measureValue?: number | undefined; measureValues?: Record<string, unknown> | undefined; } | |
componentName | string | A name for this component, included in the interaction events it dispatches on the DOM, for your analytics. | |
trackingId | string | An id included in the same events, to tell two instances apart. | |
title | string | The card’s title. A "key|fallback" string is looked up in your translations. | |
description | string | Smaller text under the title. Translates like title. | |
tooltip | string | Text for an info icon beside the title. | |
hideMenu | boolean | false | Hide the card’s header: the title, description and menu. |
menuOptions | ( | ["csv", | Which entries the card menu offers. Pass [] for none. |
A blank default means the prop is unset unless you pass it.
| Type | Definition |
|---|---|
Granularity | "second" | |
TimeRange | { from: Date |What a click’s TimeRange holds: Events. |
Field options
Set in a field’s inputs, beside the common options:
| Option | Type | Set on | Description |
|---|---|---|---|
fillUnderLine | boolean | measures | Shade the area under the line. |
lineColor | string | measures | A colour for this measure’s line, in place of the theme’s. |
previousLineColor | string | measures | A colour for the comparison period’s line. |
lineDashed | boolean | measures | Draw the current period’s line dashed. Default: false. |
previousLineDashed | boolean | measures | Draw the comparison period’s line dashed. When off, it is solid like the current line. Default: false. |
connectGaps | boolean | measures | Draw a missing value as zero, so the line carries on. When off, the line breaks at the gap. Default: false. |