forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Anan Zhuang <[email protected]>
- Loading branch information
Showing
3 changed files
with
125 additions
and
18 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
src/plugins/vis_builder/public/visualizations/vega/components/axes.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { buildAxes } from './axes'; | ||
|
||
describe('axes.ts', () => { | ||
describe('buildAxes', () => { | ||
it('should return correct axis configurations for date x-axis', () => { | ||
const dimensions = { | ||
x: { format: { id: 'date' } }, | ||
y: [{ label: 'Y Axis' }], | ||
}; | ||
const formats = { | ||
xAxisLabel: 'X Axis', | ||
yAxisLabel: 'Custom Y Axis', | ||
}; | ||
|
||
const result = buildAxes(dimensions, formats); | ||
|
||
expect(result).toHaveLength(2); | ||
expect(result[0]).toEqual({ | ||
orient: 'bottom', | ||
scale: 'x', | ||
labelAngle: -90, | ||
labelAlign: 'right', | ||
labelBaseline: 'middle', | ||
title: 'X Axis', | ||
format: '%Y-%m-%d %H:%M', | ||
}); | ||
expect(result[1]).toEqual({ | ||
orient: 'left', | ||
scale: 'y', | ||
title: 'Custom Y Axis', | ||
}); | ||
}); | ||
|
||
it('should not add format when x is not date', () => { | ||
const dimensions = { | ||
x: { format: { id: 'number' } }, | ||
y: [{ label: 'Y Axis' }], | ||
}; | ||
const result = buildAxes(dimensions, 'X', 'Y'); | ||
|
||
expect(result[0]).not.toHaveProperty('format'); | ||
}); | ||
|
||
it('should use default labels when not provided', () => { | ||
const dimensions = { | ||
x: {}, | ||
y: [{ label: 'Default Y' }], | ||
}; | ||
const result = buildAxes(dimensions, '', ''); | ||
|
||
expect(result[0].title).toBe('_all'); | ||
expect(result[1].title).toBe('Default Y'); | ||
}); | ||
}); | ||
}); |
51 changes: 51 additions & 0 deletions
51
src/plugins/vis_builder/public/visualizations/vega/components/axes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { AxisFormats } from '../utils/types'; | ||
|
||
export interface AxisConfig { | ||
orient?: string; | ||
scale?: string; | ||
labelAngle?: number; | ||
labelAlign?: string; | ||
labelBaseline?: string; | ||
title: any; | ||
format?: string; // property for date format | ||
} | ||
|
||
/** | ||
* Builds the axes configuration for a chart. | ||
* | ||
* Note: This axis configuration is currently tailored for specific use cases. | ||
* In the future, we plan to expand and generalize this function to accommodate | ||
* a wider range of chart types and axis configurations. | ||
* @param {any} dimensions - The dimensions of the data. | ||
* @param {AxisFormats} formats - The formatting information for axes. | ||
*/ | ||
|
||
export const buildAxes = (dimensions: any, formats: AxisFormats): AxisConfig[] => { | ||
const { xAxisLabel, yAxisLabel } = formats; | ||
const xAxis: AxisConfig = { | ||
orient: 'bottom', | ||
scale: 'x', | ||
labelAngle: -90, | ||
labelAlign: 'right', | ||
labelBaseline: 'middle', | ||
title: xAxisLabel || '_all', | ||
}; | ||
|
||
// Add date format if x dimension is a date type | ||
if (dimensions.x && dimensions.x.format && dimensions.x.format.id === 'date') { | ||
xAxis.format = '%Y-%m-%d %H:%M'; | ||
} | ||
|
||
const yAxis: AxisConfig = { | ||
orient: 'left', | ||
scale: 'y', | ||
title: yAxisLabel ? yAxisLabel : dimensions.y && dimensions.y[0] ? dimensions.y[0].label : '', | ||
}; | ||
|
||
return [xAxis, yAxis]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters