-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[VisBuilder] Add Capability to generate dynamic vega
In this PR, we add the capability for Visbuilder to generate dynamic Vega and Vega-Lite specifications based on user settings and aggregation configurations. * developed functions buildVegaSpecViaVega and buildVegaSpecViaVegaLite that can create either Vega or Vega-Lite specifications depending on the complexity of the visualization. * added VegaSpec and VegaLiteSpec interfaces to provide better type checking * broken down the specification building into smaller, reusable components (like buildEncoding, buildMark, buildLegend, buildTooltip) to make the code more maintainable and easier to extend. * added flattenDataHandler to prepare and transform data for use in Vega visualizations Issue Resolve #7067 Signed-off-by: Anan Zhuang <[email protected]>
- Loading branch information
Showing
26 changed files
with
1,216 additions
and
46 deletions.
There are no files selected for viewing
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
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
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,6 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export const VISBUILDER_ENABLE_VEGA_SETTING = 'visbuilder:enableVega'; |
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
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
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
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
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
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
126 changes: 126 additions & 0 deletions
126
src/plugins/vis_builder/public/visualizations/vega/build_spec_vega.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,126 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { buildEncoding } from './components/encoding'; | ||
import { buildMark } from './components/mark'; | ||
import { buildLegend } from './components/legend'; | ||
import { VegaSpec, AxisFormats } from './utils/types'; | ||
import { StyleState } from '../../application/utils/state_management'; | ||
|
||
/** | ||
* Builds a Vega specification based on the provided data, visual configuration, and style. | ||
* | ||
* @param {object} data - The data object containing series and axis information. | ||
* @param {any} visConfig - The visual configuration settings. | ||
* @param {StyleState} style - The style configuration for the visualization. | ||
* @returns {VegaSpec} The complete Vega specification. | ||
*/ | ||
export const buildVegaSpecViaVega = (data: any, visConfig: any, style: StyleState): VegaSpec => { | ||
const { dimensions, addLegend, legendPosition } = visConfig; | ||
const { type } = style; | ||
const { | ||
xAxisFormat, | ||
xAxisLabel, | ||
yAxisFormat, | ||
yAxisLabel, | ||
zAxisFormat, | ||
series: transformedData, | ||
} = data; | ||
|
||
const formats: AxisFormats = { | ||
xAxisFormat, | ||
xAxisLabel, | ||
yAxisFormat, | ||
yAxisLabel, | ||
zAxisFormat, | ||
}; | ||
|
||
const spec: VegaSpec = { | ||
$schema: 'https://vega.github.io/schema/vega/v5.json', | ||
padding: 5, | ||
data: [ | ||
{ | ||
name: 'source', | ||
values: transformedData, | ||
}, | ||
{ | ||
name: 'splits', | ||
source: 'source', | ||
transform: [ | ||
{ | ||
type: 'aggregate', | ||
groupby: ['split'], | ||
}, | ||
], | ||
}, | ||
], | ||
signals: [ | ||
{ name: 'splitCount', update: 'length(data("splits"))' }, | ||
{ name: 'chartWidth', update: 'width / splitCount - 10' }, | ||
], | ||
scales: [ | ||
{ | ||
name: 'splitScale', | ||
type: 'band', | ||
domain: { data: 'splits', field: 'split' }, | ||
range: 'width', | ||
padding: 0.1, | ||
}, | ||
{ | ||
name: 'color', | ||
type: 'ordinal', | ||
domain: { data: 'source', field: 'series' }, | ||
range: 'category', | ||
}, | ||
], | ||
layout: { | ||
columns: { signal: 'splitCount' }, | ||
padding: { row: 40, column: 20 }, | ||
}, | ||
marks: [ | ||
{ | ||
type: 'group', | ||
from: { data: 'splits' }, | ||
encode: { | ||
enter: { | ||
width: { signal: 'chartWidth' }, | ||
height: { signal: 'height' }, | ||
stroke: { value: '#ccc' }, | ||
strokeWidth: { value: 1 }, | ||
}, | ||
}, | ||
signals: [{ name: 'width', update: 'chartWidth' }], | ||
scales: buildEncoding(dimensions, formats, true), | ||
axes: [ | ||
{ | ||
orient: 'bottom', | ||
scale: 'xscale', | ||
zindex: 1, | ||
labelAngle: -90, | ||
labelAlign: 'right', | ||
labelBaseline: 'middle', | ||
}, | ||
{ orient: 'left', scale: 'yscale', zindex: 1 }, | ||
], | ||
title: { | ||
text: { signal: 'parent.split' }, | ||
anchor: 'middle', | ||
offset: 10, | ||
limit: { signal: 'chartWidth' }, | ||
wrap: true, | ||
align: 'center', | ||
}, | ||
marks: buildMark(type, true), | ||
}, | ||
], | ||
}; | ||
|
||
// Add legend if specified | ||
if (addLegend) { | ||
spec.legends = [buildLegend(legendPosition, true)]; | ||
} | ||
|
||
return spec; | ||
}; |
Oops, something went wrong.