Skip to content

Commit ff23cf2

Browse files
fix(charts): resolve threshold axis by index before id, matching ECharts
1 parent a2bbba7 commit ff23cf2

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

‎apps/sim/lib/charts/annotations.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,29 @@ describe('chart annotations', () => {
166166
expect(labels.markLine).toMatchObject({ data: [{ xAxis: 5, label: { position: 'start' } }] })
167167
})
168168

169+
it('prefers the axis index over the axis id, as ECharts does', () => {
170+
const option = applyChartAnnotations(
171+
{
172+
xAxis: { type: 'time' },
173+
yAxis: [
174+
{ id: 'stage', type: 'category' },
175+
{ id: 7, type: 'value' },
176+
],
177+
series: [{ type: 'line', yAxisIndex: 1, yAxisId: 'stage' }],
178+
},
179+
{ thresholds: [{ value: 5 }] },
180+
palette
181+
)
182+
expect((option.series as Series[])[0].markLine).toMatchObject({ data: [{ yAxis: 5 }] })
183+
expect(() =>
184+
applyChartAnnotations(
185+
{ ...option, series: [{ type: 'line', yAxisId: '7' }] },
186+
{ thresholds: [{ value: 5 }] },
187+
palette
188+
)
189+
).not.toThrow()
190+
})
191+
169192
it('rejects a first series that references an axis the chart does not define', () => {
170193
for (const reference of [{ yAxisIndex: 1 }, { yAxisIndex: -1 }, { yAxisIndex: 0.5 }])
171194
expect(() =>

‎apps/sim/lib/charts/annotations.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,19 @@ function firstSeries(option: Record<string, unknown>): Record<string, unknown> {
3131
return toRecord(Array.isArray(option.series) ? option.series[0] : option.series)
3232
}
3333

34-
/** The axis the first series is plotted on, resolved through its `*AxisId` or `*AxisIndex`. */
34+
/**
35+
* The axis the first series is plotted on. Like ECharts, `*AxisIndex` wins over `*AxisId`, and ids
36+
* match across string and number.
37+
*/
3538
function seriesAxis(
3639
option: Record<string, unknown>,
3740
key: 'xAxis' | 'yAxis'
3841
): Record<string, unknown> {
3942
const axes = Array.isArray(option[key]) ? (option[key] as unknown[]) : [option[key]]
4043
const series = firstSeries(option)
4144
const id = series[`${key}Id`]
42-
if (id !== undefined) {
43-
const axis = axes.find((candidate) => toRecord(candidate).id === id)
45+
if (series[`${key}Index`] === undefined && id !== undefined) {
46+
const axis = axes.find((candidate) => String(toRecord(candidate).id) === String(id))
4447
if (axis === undefined) throw new Error(`The first series references a missing ${key} "${id}"`)
4548
return toRecord(axis)
4649
}

0 commit comments

Comments
 (0)