WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 5614e5c

Browse files
authored
Merge pull request #5645 from ag-grid/AG-16331-axis-type
AG-16331 Change axis type to be an optional property
2 parents b014077 + abe0b58 commit 5614e5c

File tree

16 files changed

+263
-64
lines changed

16 files changed

+263
-64
lines changed

packages/ag-charts-community/src/api/preset/presetModules.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const commonSparklineOptionsDef: OptionsDefs<
7373
data: defined,
7474
styleNonce: string,
7575

76-
axis: typeUnion<AgSparklineAxisOptions>(
76+
axis: typeUnion<AgSparklineAxisOptions & { type: NonNullable<AgSparklineAxisOptions['type']> }>(
7777
{
7878
number: {
7979
...commonSparklineAxisOptionsDef,
@@ -92,7 +92,7 @@ const commonSparklineOptionsDef: OptionsDefs<
9292
},
9393
},
9494
'axis options',
95-
'category' // AG-14799 - Contrary to the AgSparklineAxisOptions interface, type is optional and defaults to 'category'.
95+
'category'
9696
),
9797
min: and(number, lessThan('max')),
9898
max: and(number, greaterThan('min')),

packages/ag-charts-community/src/api/preset/sparkline.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
AgLineSeriesOptions,
1515
AgLineSeriesTooltipRendererParams,
1616
AgSparklineAxisOptions,
17+
AgSparklineCategoryAxisOptions,
1718
AgSparklineOptions,
1819
AgSparklineTooltip,
1920
AgTooltipRendererResult,
@@ -241,7 +242,7 @@ function axisPreset(opts: AgSparklineAxisOptions | undefined): AgCartesianAxisOp
241242
if (opts == null) {
242243
return { type: 'category' };
243244
}
244-
const { reverse, paddingInner, paddingOuter } = opts;
245+
const { reverse, paddingInner, paddingOuter } = opts as AgSparklineCategoryAxisOptions;
245246
return {
246247
type: 'category',
247248
reverse,

packages/ag-charts-community/src/chart/axesOptionsDefs.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ export const discreteTimeAxisIntervalOptionsDefs: OptionsDefs<AgAxisDiscreteTime
347347

348348
export const categoryAxisOptionsDefs: OptionsDefs<AgCategoryAxisOptions> = {
349349
...cartesianAxisOptionsDefs,
350-
type: required(constant('category')),
350+
type: constant('category'),
351351
label: cartesianAxisLabelOptionsDefs,
352352
paddingInner: ratio,
353353
paddingOuter: ratio,
@@ -362,7 +362,7 @@ export const categoryAxisOptionsDefs: OptionsDefs<AgCategoryAxisOptions> = {
362362

363363
export const groupedCategoryAxisOptionsDefs: OptionsDefs<AgGroupedCategoryAxisOptions> = {
364364
...cartesianAxisOptionsDefs,
365-
type: required(constant('grouped-category')),
365+
type: constant('grouped-category'),
366366
label: cartesianAxisLabelOptionsDefs,
367367
crosshair: cartesianAxisCrosshairOptions(),
368368
bandHighlight: cartesianAxisBandHighlightOptions,
@@ -393,15 +393,15 @@ export const groupedCategoryAxisOptionsDefs: OptionsDefs<AgGroupedCategoryAxisOp
393393
export const numberAxisOptionsDefs: OptionsDefs<AgNumberAxisOptions> = {
394394
...cartesianAxisOptionsDefs,
395395
...continuousAxisOptions(number),
396-
type: required(constant('number')),
396+
type: constant('number'),
397397
label: cartesianNumericAxisLabel,
398398
crosshair: cartesianAxisCrosshairOptions(true),
399399
};
400400

401401
export const logAxisOptionsDefs: OptionsDefs<AgLogAxisOptions> = {
402402
...cartesianAxisOptionsDefs,
403403
...continuousAxisOptions(number),
404-
type: required(constant('log')),
404+
type: constant('log'),
405405
base: and(
406406
positiveNumberNonZero,
407407
attachDescription((value) => value !== 1, 'not equal to 1')
@@ -413,15 +413,15 @@ export const logAxisOptionsDefs: OptionsDefs<AgLogAxisOptions> = {
413413
export const timeAxisOptionsDefs: OptionsDefs<AgTimeAxisOptions> = {
414414
...cartesianAxisOptionsDefs,
415415
...continuousAxisOptions(or(number, date), true),
416-
type: required(constant('time')),
416+
type: constant('time'),
417417
label: cartesianTimeAxisLabel,
418418
parentLevel: cartesianTimeAxisParentLevel,
419419
crosshair: cartesianAxisCrosshairOptions(true, true),
420420
};
421421

422422
export const unitTimeAxisOptionsDefs: OptionsDefs<AgUnitTimeAxisOptions> = {
423423
...cartesianAxisOptionsDefs,
424-
type: required(constant('unit-time')),
424+
type: constant('unit-time'),
425425
unit: or(timeInterval, timeIntervalUnit),
426426
label: cartesianTimeAxisLabel,
427427
parentLevel: cartesianTimeAxisParentLevel,

packages/ag-charts-community/src/chart/axesOptionsEnterpriseDefs.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
number,
99
positiveNumber,
1010
ratio,
11-
required,
1211
union,
1312
} from 'ag-charts-core';
1413
import type {
@@ -37,7 +36,7 @@ import { numberFormatValidator, textOrSegments } from './commonOptionsDefs';
3736

3837
export const ordinalTimeAxisOptionsDefs: OptionsDefs<AgOrdinalTimeAxisOptions> = {
3938
...cartesianAxisOptionsDefs,
40-
type: required(constant('ordinal-time')),
39+
type: constant('ordinal-time'),
4140
paddingInner: ratio,
4241
paddingOuter: ratio,
4342
groupPaddingInner: ratio,
@@ -51,7 +50,7 @@ export const ordinalTimeAxisOptionsDefs: OptionsDefs<AgOrdinalTimeAxisOptions> =
5150
export const angleNumberAxisOptionsDefs: OptionsDefs<AgAngleNumberAxisOptions> = {
5251
...commonAxisOptionsDefs,
5352
...continuousAxisOptions(number),
54-
type: required(constant('angle-number')),
53+
type: constant('angle-number'),
5554
crossLines: arrayOfDefs(commonCrossLineOptionsDefs),
5655
startAngle: number,
5756
endAngle: number,
@@ -64,7 +63,7 @@ export const angleNumberAxisOptionsDefs: OptionsDefs<AgAngleNumberAxisOptions> =
6463

6564
export const angleCategoryAxisOptionsDefs: OptionsDefs<AgAngleCategoryAxisOptions> = {
6665
...commonAxisOptionsDefs,
67-
type: required(constant('angle-category')),
66+
type: constant('angle-category'),
6867
shape: union('polygon', 'circle'),
6968
crossLines: arrayOfDefs(commonCrossLineOptionsDefs),
7069
startAngle: number,
@@ -83,7 +82,7 @@ angleCategoryAxisOptionsDefs.innerRadiusRatio = ratio;
8382
export const radiusNumberAxisOptionsDefs: OptionsDefs<AgRadiusNumberAxisOptions> = {
8483
...commonAxisOptionsDefs,
8584
...continuousAxisOptions(number),
86-
type: required(constant('radius-number')),
85+
type: constant('radius-number'),
8786
shape: union('polygon', 'circle'),
8887
positionAngle: number,
8988
innerRadiusRatio: ratio,
@@ -112,7 +111,7 @@ export const radiusNumberAxisOptionsDefs: OptionsDefs<AgRadiusNumberAxisOptions>
112111

113112
export const radiusCategoryAxisOptionsDefs: OptionsDefs<AgRadiusCategoryAxisOptions> = {
114113
...commonAxisOptionsDefs,
115-
type: required(constant('radius-category')),
114+
type: constant('radius-category'),
116115
positionAngle: number,
117116
innerRadiusRatio: ratio,
118117
paddingInner: ratio,

packages/ag-charts-community/src/chart/axis/categoryAxis.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function applyIntervalOn<T extends AgCartesianChartOptions>(opts: T): T {
3333
...opts,
3434
axes:
3535
mapValues(opts.axes ?? {}, (axis) =>
36-
axesToTest.includes(axis.type)
36+
axis.type && axesToTest.includes(axis.type)
3737
? {
3838
...axis,
3939
interval: { ...(axis.interval ?? {}), placement: 'on' },
@@ -50,7 +50,7 @@ function applyIntervalBetween<T extends AgCartesianChartOptions>(opts: T): T {
5050
...opts,
5151
axes:
5252
mapValues(opts.axes ?? {}, (axis) =>
53-
axesToTest.includes(axis.type)
53+
axis.type && axesToTest.includes(axis.type)
5454
? {
5555
...axis,
5656
interval: { ...(axis.interval ?? {}), placement: 'between' },

packages/ag-charts-community/src/chart/chart.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1822,7 +1822,7 @@ export abstract class Chart extends Observable implements ModuleInstance, ChartS
18221822
const moduleContext = this.getModuleContext();
18231823

18241824
for (const [id, axisOptions] of entries(options)) {
1825-
const axis = ModuleRegistry.getAxisModule(axisOptions.type)!.create(moduleContext) as any;
1825+
const axis = ModuleRegistry.getAxisModule(axisOptions.type!)!.create(moduleContext) as any;
18261826
axis.id = id as AxisID;
18271827
this.applyAxisModules(axis, axisOptions);
18281828
jsonApply(axis, axisOptions, { skip });

packages/ag-charts-community/src/chart/crossline/crossLine.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ const INVALID_EXAMPLES: Record<string, CartesianTestCase & { warningMessages: st
204204
seriesTypes: repeat('line', 2),
205205
}),
206206
warningMessages: [
207-
"AG Charts - Option `axes[y].crossLines[0].range` cannot be set to `[null,134]`; expecting crossLine type to be 'range', a defined value array and an array of exactly 2 items, ignoring.",
207+
"AG Charts - Option `axes.y.crossLines[0].range` cannot be set to `[null,134]`; expecting crossLine type to be 'range', a defined value array and an array of exactly 2 items, ignoring.",
208208
],
209209
},
210210
INVALID_RANGE_LENGTH_CROSSLINE: {
@@ -214,7 +214,7 @@ const INVALID_EXAMPLES: Record<string, CartesianTestCase & { warningMessages: st
214214
seriesTypes: repeat('line', 2),
215215
}),
216216
warningMessages: [
217-
"AG Charts - Option `axes[y].crossLines[0].range` cannot be set to `[128,134,135]`; expecting crossLine type to be 'range', a defined value array and an array of exactly 2 items, ignoring.",
217+
"AG Charts - Option `axes.y.crossLines[0].range` cannot be set to `[128,134,135]`; expecting crossLine type to be 'range', a defined value array and an array of exactly 2 items, ignoring.",
218218
],
219219
},
220220
INVALID_RANGE_WITHOUT_TYPE_CROSSLINE: {
@@ -224,8 +224,8 @@ const INVALID_EXAMPLES: Record<string, CartesianTestCase & { warningMessages: st
224224
seriesTypes: repeat('line', 2),
225225
}),
226226
warningMessages: [
227-
"AG Charts - Option `axes[y].crossLines[0].type` is required and has not been provided; expecting a keyword such as 'line' or 'range', ignoring.",
228-
"AG Charts - Option `axes[y].crossLines[0].range` cannot be set to `[128,134]`; expecting crossLine type to be 'range', a defined value array and an array of exactly 2 items, ignoring.",
227+
"AG Charts - Option `axes.y.crossLines[0].type` is required and has not been provided; expecting a keyword such as 'line' or 'range', ignoring.",
228+
"AG Charts - Option `axes.y.crossLines[0].range` cannot be set to `[128,134]`; expecting crossLine type to be 'range', a defined value array and an array of exactly 2 items, ignoring.",
229229
],
230230
},
231231
INVALID_LINE_VALUE_CROSSLINES: {
@@ -243,7 +243,7 @@ const INVALID_EXAMPLES: Record<string, CartesianTestCase & { warningMessages: st
243243
seriesTypes: repeat('line', 2),
244244
}),
245245
warningMessages: [
246-
"AG Charts - Option `axes[y].crossLines[0].range` cannot be set to `[128,134]`; expecting crossLine type to be 'range', a defined value array and an array of exactly 2 items, ignoring.",
246+
"AG Charts - Option `axes.y.crossLines[0].range` cannot be set to `[128,134]`; expecting crossLine type to be 'range', a defined value array and an array of exactly 2 items, ignoring.",
247247
],
248248
},
249249
INVALID_LINE_WITHOUT_TYPE_CROSSLINE: {
@@ -253,8 +253,8 @@ const INVALID_EXAMPLES: Record<string, CartesianTestCase & { warningMessages: st
253253
seriesTypes: repeat('line', 2),
254254
}),
255255
warningMessages: [
256-
"AG Charts - Option `axes[y].crossLines[0].type` is required and has not been provided; expecting a keyword such as 'line' or 'range', ignoring.",
257-
"AG Charts - Option `axes[y].crossLines[0].value` cannot be set to `128`; expecting crossLine type to be 'line' and a defined value, ignoring.",
256+
"AG Charts - Option `axes.y.crossLines[0].type` is required and has not been provided; expecting a keyword such as 'line' or 'range', ignoring.",
257+
"AG Charts - Option `axes.y.crossLines[0].value` cannot be set to `128`; expecting crossLine type to be 'line' and a defined value, ignoring.",
258258
],
259259
},
260260
INVALID_LINE_WITH_RANGE_TYPE_CROSSLINE: {
@@ -264,7 +264,7 @@ const INVALID_EXAMPLES: Record<string, CartesianTestCase & { warningMessages: st
264264
seriesTypes: repeat('line', 2),
265265
}),
266266
warningMessages: [
267-
"AG Charts - Option `axes[y].crossLines[0].value` cannot be set to `128`; expecting crossLine type to be 'line' and a defined value, ignoring.",
267+
"AG Charts - Option `axes.y.crossLines[0].value` cannot be set to `128`; expecting crossLine type to be 'line' and a defined value, ignoring.",
268268
],
269269
},
270270
};

packages/ag-charts-community/src/chart/galleryExamplesUSTimezone.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
const TIME_AXIS_EXAMPLES = Object.entries(EXAMPLES)
2020
.filter(([, { options }]) => {
2121
return Object.values((options as any as AgCartesianChartOptions).axes ?? {}).some((axis) =>
22-
axis.type.endsWith('time')
22+
axis.type?.endsWith('time')
2323
);
2424
})
2525
.reduce<typeof EXAMPLES>((out, [name, testCase]) => {

packages/ag-charts-community/src/module/optionsModule.test.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,6 +2567,103 @@ describe('ChartOptions', () => {
25672567
yKeyAxis: 'y',
25682568
});
25692569
});
2570+
2571+
it('should predict missing types and positions', () => {
2572+
const options: AgCartesianChartOptions = {
2573+
series: [{ type: 'line', xKey: 'x', yKey: 'y' }],
2574+
axes: {
2575+
x: {},
2576+
y: {},
2577+
},
2578+
};
2579+
2580+
const preparedOptions = prepareOptions(options);
2581+
2582+
expect(Object.keys(preparedOptions.axes ?? {})).toHaveLength(2);
2583+
expect(preparedOptions.axes).toMatchObject({
2584+
x: { type: 'category', position: 'bottom' },
2585+
y: { type: 'number', position: 'left' },
2586+
});
2587+
});
2588+
2589+
it('should predict missing types and positions for secondary axes', () => {
2590+
const options: AgCartesianChartOptions = {
2591+
series: [
2592+
{ type: 'line', xKey: 'x', yKey: 'y' },
2593+
{ type: 'line', xKey: 'x', yKey: 'y', yKeyAxis: 'myAxis' },
2594+
],
2595+
axes: {
2596+
x: {},
2597+
y: {},
2598+
myAxis: {},
2599+
},
2600+
};
2601+
2602+
const preparedOptions = prepareOptions(options);
2603+
2604+
expect(Object.keys(preparedOptions.axes ?? {})).toHaveLength(3);
2605+
expect(preparedOptions.axes).toMatchObject({
2606+
x: { type: 'category', position: 'bottom' },
2607+
y: { type: 'number', position: 'left' },
2608+
__AXIS_ID_2: { type: 'number', position: 'left' },
2609+
});
2610+
});
2611+
2612+
it('should should predict missing types when given positions', () => {
2613+
const options: AgCartesianChartOptions = {
2614+
series: [{ type: 'line', xKey: 'x', yKey: 'y' }],
2615+
axes: {
2616+
myAxis1: { position: 'left' },
2617+
myAxis2: { position: 'bottom' },
2618+
},
2619+
};
2620+
2621+
const preparedOptions = prepareOptions(options);
2622+
2623+
expect(Object.keys(preparedOptions.axes ?? {})).toHaveLength(2);
2624+
expect(preparedOptions.axes).toMatchObject({
2625+
x: { type: 'category', position: 'bottom' },
2626+
y: { type: 'number', position: 'left' },
2627+
});
2628+
});
2629+
2630+
it('should predict missing positions for primary axes when secondary axes have positions', () => {
2631+
const options: AgCartesianChartOptions = {
2632+
series: [
2633+
{ type: 'line', xKey: 'x', yKey: 'y' },
2634+
{ type: 'line', xKey: 'x', yKey: 'y', yKeyAxis: 'myAxis' },
2635+
],
2636+
axes: {
2637+
x: {},
2638+
y: {},
2639+
myAxis: { position: 'right' },
2640+
},
2641+
};
2642+
2643+
const preparedOptions = prepareOptions(options);
2644+
2645+
expect(Object.keys(preparedOptions.axes ?? {})).toHaveLength(3);
2646+
expect(preparedOptions.axes).toMatchObject({
2647+
x: { type: 'category', position: 'bottom' },
2648+
y: { type: 'number', position: 'left' },
2649+
__AXIS_ID_2: { type: 'number', position: 'right' },
2650+
});
2651+
});
2652+
2653+
it('should discard secondary axes that are not referenced and have no position or type', () => {
2654+
const options: AgCartesianChartOptions = {
2655+
series: [{ type: 'line', xKey: 'x', yKey: 'y' }],
2656+
axes: {
2657+
myAxis1: {},
2658+
myAxis2: {},
2659+
myAxis3: {},
2660+
},
2661+
};
2662+
2663+
const preparedOptions = prepareOptions(options);
2664+
2665+
expect(Object.keys(preparedOptions.axes ?? {})).toHaveLength(2);
2666+
});
25702667
});
25712668
});
25722669
});

0 commit comments

Comments
 (0)