diff --git a/packages/osd-charts/integration/tests/__image_snapshots__/all-test-ts-baseline-visual-tests-for-all-stories-scales-x-scale-fallback-visually-looks-correct-1-snap.png b/packages/osd-charts/integration/tests/__image_snapshots__/all-test-ts-baseline-visual-tests-for-all-stories-scales-x-scale-fallback-visually-looks-correct-1-snap.png new file mode 100644 index 000000000000..4334b70a0ef9 Binary files /dev/null and b/packages/osd-charts/integration/tests/__image_snapshots__/all-test-ts-baseline-visual-tests-for-all-stories-scales-x-scale-fallback-visually-looks-correct-1-snap.png differ diff --git a/packages/osd-charts/src/chart_types/xy_chart/domains/x_domain.test.ts b/packages/osd-charts/src/chart_types/xy_chart/domains/x_domain.test.ts index f3ee4c5d0a7a..5650296ee1ea 100644 --- a/packages/osd-charts/src/chart_types/xy_chart/domains/x_domain.test.ts +++ b/packages/osd-charts/src/chart_types/xy_chart/domains/x_domain.test.ts @@ -470,9 +470,12 @@ describe('X Domain', () => { ], }; const specDataSeries = [ds1, ds2]; + const customDomain = { + min: 0, + }; const { xValues } = getSplittedSeries(specDataSeries); - const mergedDomain = mergeXDomain( + const getResult = () => mergeXDomain( [ { seriesType: SeriesTypes.Bar, @@ -480,14 +483,21 @@ describe('X Domain', () => { }, { seriesType: SeriesTypes.Bar, - xScaleType: ScaleType.Ordinal, + xScaleType: ScaleType.Linear, }, ], xValues, + customDomain, + ScaleType.Ordinal, ); + + expect(getResult).not.toThrow(); + + const mergedDomain = getResult(); expect(mergedDomain.domain).toEqual([0, 'a', 2, 5, 7]); expect(mergedDomain.scaleType).toEqual(ScaleType.Ordinal); }); + test('Should merge multi bar/line ordinal series correctly', () => { const ds1: BasicSeriesSpec = { chartType: ChartTypes.XYAxis, diff --git a/packages/osd-charts/src/chart_types/xy_chart/domains/x_domain.ts b/packages/osd-charts/src/chart_types/xy_chart/domains/x_domain.ts index 98d66e536ad2..72373bae72da 100644 --- a/packages/osd-charts/src/chart_types/xy_chart/domains/x_domain.ts +++ b/packages/osd-charts/src/chart_types/xy_chart/domains/x_domain.ts @@ -60,7 +60,14 @@ export function mergeXDomain( if (Array.isArray(customXDomain)) { seriesXComputedDomains = customXDomain; } else { - throw new TypeError('xDomain for ordinal scale should be an array of values, not a DomainRange object'); + if (fallbackScale === ScaleType.Ordinal) { + Logger.warn(`xDomain ignored for fallback ordinal scale. Options to resolve: + +1) Correct data to match ${mainXScaleType.scaleType} scale type (see previous warning) +2) Change xScaleType to ordinal and set xDomain to Domain array`); + } else { + throw new TypeError('xDomain for ordinal scale should be an array of values, not a DomainRange object.'); + } } } } else { diff --git a/packages/osd-charts/stories/scales/6_x_scale_fallback.tsx b/packages/osd-charts/stories/scales/6_x_scale_fallback.tsx new file mode 100644 index 000000000000..5bec96e611e6 --- /dev/null +++ b/packages/osd-charts/stories/scales/6_x_scale_fallback.tsx @@ -0,0 +1,55 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { boolean } from '@storybook/addon-knobs'; +import React from 'react'; + +import { Axis, Chart, BarSeries, Position, ScaleType, Settings } from '../../src'; + +export const Example = () => { + const includeString = boolean('include string is x data', true); + return ( + + + + + + + ); +}; + +Example.story = { + parameters: { + info: { + text: 'Using string values with a `Linear` scale will attempt to fallback to an `Ordinal` scale. Notice how the custom `xDomain` is ignored when the scale falls back to `Ordinal`.', + }, + }, +}; diff --git a/packages/osd-charts/stories/scales/scales.stories.tsx b/packages/osd-charts/stories/scales/scales.stories.tsx index 522543225974..5a6fe64b56da 100644 --- a/packages/osd-charts/stories/scales/scales.stories.tsx +++ b/packages/osd-charts/stories/scales/scales.stories.tsx @@ -31,3 +31,4 @@ export { Example as tooltipInLocalTimezone } from './2_local_tooltip'; export { Example as tooltipInUTC } from './3_utc_tooltip'; export { Example as specifiedTimezone } from './4_specified_timezone'; export { Example as removeDuplicateAxis } from './5_remove_duplicates'; +export { Example as xScaleFallback } from './6_x_scale_fallback';