Skip to content

Commit

Permalink
solid area color and add interative selection
Browse files Browse the repository at this point in the history
Signed-off-by: Anan Zhuang <[email protected]>
  • Loading branch information
ananzh committed Jul 20, 2024
1 parent ed827b5 commit 3229fac
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { buildEncoding } from './components/encoding';
import { buildMark } from './components/mark';
import { buildTooltip } from './components/tooltip';
import { buildLegend } from './components/legend';
import { buildSelection } from './components/selection';
import { StyleState } from '../../application/utils/state_management';
import { VegaLiteSpec, AxisFormats } from './utils/types';

Expand Down Expand Up @@ -43,11 +44,12 @@ export const buildVegaSpecViaVegaLite = (
};

// Build the base Vega-Lite specification
const baseSpec: VegaSpec = {
const baseSpec: VegaLiteSpec = {

Check warning on line 47 in src/plugins/vis_builder/public/visualizations/vega/build_spec_vega_lite.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vega/build_spec_vega_lite.ts#L47

Added line #L47 was not covered by tests
$schema: 'https://vega.github.io/schema/vega-lite/v5.json',
data: { values: transformedData },
mark: buildMark(type),
encoding: buildEncoding(dimensions, formats),
selection: buildSelection(),
};

// Handle special case for line charts with dot size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,32 @@
*/

import { mapFieldTypeToVegaType } from '../utils/helpers';
import { AxisFormats } from '../utils/types';
import { AxisFormat, AxisFormats } from '../utils/types';

interface EncodingChannel {
field: string;
type: string;
interface BaseEncodingChannel {
field?: string;
type?: string;
}

interface AxisEncodingChannel extends BaseEncodingChannel {
axis?: { title: string };
}

interface ColorEncodingChannel extends BaseEncodingChannel {
legend?: { title: string | null };
}

interface OpacityEncodingChannel extends BaseEncodingChannel {
condition: { selection: string; value: number };
value: number;
}

interface VegaEncoding {
[key: string]: EncodingChannel;
x?: AxisEncodingChannel;
y?: AxisEncodingChannel;
color?: ColorEncodingChannel;
opacity?: OpacityEncodingChannel;
[key: string]: BaseEncodingChannel | undefined;
}

interface VegaScale {
Expand All @@ -25,7 +40,7 @@ interface VegaScale {
field: string;
filter?: string;
};
range: string;
range?: string;
padding?: number;
nice?: boolean;
zero?: boolean;
Expand All @@ -41,11 +56,9 @@ interface VegaScale {
*/
export const buildEncoding = (
dimensions: any,
formats: any,
formats: AxisFormats,
isVega: boolean = false
): VegaEncoding | VegaScale[] => {
const { xAxisFormat, xAxisLabel, yAxisFormat, yAxisLabel, zAxisFormat } = formats;

if (isVega) {
return buildVegaScales(dimensions, formats);

Check warning on line 63 in src/plugins/vis_builder/public/visualizations/vega/components/encoding.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vega/components/encoding.ts#L63

Added line #L63 was not covered by tests
}
Expand All @@ -57,10 +70,10 @@ export const buildEncoding = (
* Builds encoding configuration for Vega-Lite specifications.
*
* @param {any} dimensions - The dimensions of the data.
* @param {any} formats - The formatting information for axes.
* @param {AxisFormats} formats - The formatting information for axes.
* @returns {VegaEncoding} The Vega-Lite encoding configuration.
*/
const buildVegaLiteEncoding = (dimensions: any, formats: any): VegaEncoding => {
const buildVegaLiteEncoding = (dimensions: any, formats: AxisFormats): VegaEncoding => {
const { xAxisFormat, xAxisLabel, yAxisFormat, yAxisLabel, zAxisFormat } = formats;
const encoding: VegaEncoding = {};

Check warning on line 78 in src/plugins/vis_builder/public/visualizations/vega/components/encoding.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vega/components/encoding.ts#L77-L78

Added lines #L77 - L78 were not covered by tests

Expand All @@ -77,6 +90,12 @@ const buildVegaLiteEncoding = (dimensions: any, formats: any): VegaEncoding => {
encoding.color = buildColorEncoding('series', mapFieldTypeToVegaType(zAxisFormat?.id || ''));
}

// Always add opacity encoding
encoding.opacity = {

Check warning on line 94 in src/plugins/vis_builder/public/visualizations/vega/components/encoding.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vega/components/encoding.ts#L94

Added line #L94 was not covered by tests
condition: { selection: 'legend_selection', value: 1 },
value: 0.2,
};

return encoding;

Check warning on line 99 in src/plugins/vis_builder/public/visualizations/vega/components/encoding.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vega/components/encoding.ts#L99

Added line #L99 was not covered by tests
};

Expand Down Expand Up @@ -129,13 +148,13 @@ const buildVegaScales = (dimensions: any, formats: any): VegaScale[] => {
const buildAxisEncoding = (
field: string,
dimension: any[] | undefined,
axisFormat: AxisFormat,
axisLabel: string
axisFormat?: AxisFormat,
axisLabel?: string
): EncodingChannel => {
return {

Check warning on line 154 in src/plugins/vis_builder/public/visualizations/vega/components/encoding.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vega/components/encoding.ts#L154

Added line #L154 was not covered by tests
field,
type: dimension ? mapFieldTypeToVegaType(axisFormat.id) : 'ordinal',
axis: { title: axisLabel },
type: dimension ? mapFieldTypeToVegaType(axisFormat?.id) : 'ordinal',
axis: { title: axisLabel ? axisLabel : '' },
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,28 @@

import { mapChartTypeToVegaType } from '../utils/helpers';

type VegaMarkType = 'line' | 'rect' | 'area' | 'symbol' | 'bar' | 'point' | 'circle' | 'square';
type VegaMarkType =
| 'line'
| 'rect'
| 'area'
| 'symbol'
| 'bar'
| 'point'
| 'circle'
| 'square'
| 'group';

interface VegaMark {
type: VegaMarkType;
from?: { data: string };
from?: {
data?: string;
facet?: {
name?: string;
data?: string;
groupby?: string;
filter?: string;
};
};
encode?: {
enter?: Record<string, any>;
update?: Record<string, any>;
Expand Down Expand Up @@ -66,16 +83,19 @@ export const buildMark = (
* @returns {VegaLiteMark} The Vega-Lite mark configuration.
*/
const buildMarkForVegaLite = (vegaType: VegaMarkType): VegaLiteMark => {
const baseMark = {

Check warning on line 86 in src/plugins/vis_builder/public/visualizations/vega/components/mark.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vega/components/mark.ts#L86

Added line #L86 was not covered by tests
opacity: { condition: { selection: 'hover', value: 1 }, value: 0.3 },
};
switch (vegaType) {
case 'line':
return { type: 'line', point: true };
return { ...baseMark, type: 'line', point: true };

Check warning on line 91 in src/plugins/vis_builder/public/visualizations/vega/components/mark.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vega/components/mark.ts#L91

Added line #L91 was not covered by tests
case 'area':
return { type: 'area', line: true };
return { ...baseMark, type: 'area', line: true, opacity: 1, fillOpacity: 1 };

Check warning on line 93 in src/plugins/vis_builder/public/visualizations/vega/components/mark.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vega/components/mark.ts#L93

Added line #L93 was not covered by tests
case 'rect':
case 'bar':
return { type: 'bar' };
return { ...baseMark, type: 'bar' };

Check warning on line 96 in src/plugins/vis_builder/public/visualizations/vega/components/mark.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vega/components/mark.ts#L96

Added line #L96 was not covered by tests
default:
return { type: vegaType };
return { ...baseMark, type: vegaType };

Check warning on line 98 in src/plugins/vis_builder/public/visualizations/vega/components/mark.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vega/components/mark.ts#L98

Added line #L98 was not covered by tests
}
};

Expand Down Expand Up @@ -184,7 +204,9 @@ const buildMarkForArea = (): VegaMark[] => [
y: { scale: 'yscale', field: 'y' },
y2: { scale: 'yscale', value: 0 },
fill: { scale: 'color', field: 'series' },
fillOpacity: { value: 0.7 },
fillOpacity: { value: 1 },
stroke: { scale: 'color', field: 'series' },
strokeOpacity: { value: 1 },
},
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

// Define types for selection configurations
interface SelectionConfig {
type: 'single' | 'multi';
fields: string[];
bind: 'legend' | { [key: string]: any };
}

interface VegaLiteSelection {
[key: string]: SelectionConfig;
}

/**
* Builds a selection configuration for Vega-Lite specifications.
* This function creates a multi-selection bound to the legend,
* allowing for interactive filtering of data based on legend items.
*
* @returns {VegaLiteSelection} The selection configuration object.
*/
export const buildSelection = (): VegaLiteSelection => {
return {

Check warning on line 25 in src/plugins/vis_builder/public/visualizations/vega/components/selection.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vega/components/selection.ts#L25

Added line #L25 was not covered by tests
legend_selection: {
type: 'multi', // Allows multiple items to be selected
fields: ['series'], // The field to be used for selection (typically the series field)
bind: 'legend', // Binds the selection to the chart's legend
},
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { AxisFormats } from './types';
* @param {Array} group - The group containing axis information
*/
const setAxisProperties = (converted: any, group: any[]): void => {
const axes: Array<keyof AxisFormats> = ['xAxis', 'yAxis', 'zAxis'];
const axes = ['xAxis', 'yAxis', 'zAxis'];
const properties = ['Format', 'Label'];

Check warning on line 19 in src/plugins/vis_builder/public/visualizations/vega/utils/helpers.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vega/utils/helpers.ts#L18-L19

Added lines #L18 - L19 were not covered by tests

axes.forEach((axis) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export interface VegaLiteSpec {
legend?: any;
[key: string]: any;
};
selection?: {
legend_selection?: {
type: string;
fields: string[];
bind: string;
};
};
}

// Define a more general VegaSpec interface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,6 @@ export const toExpression = async (
const params = getPipelineParams();
const dimensions = await buildVislibDimensions(vis, params);
const valueAxes = getValueAxes(dimensions.y);
const seriesParams = [
{
"show": true,
"type": "area",
"mode": "stacked",
"data": {
"label": "Count",
"id": "1"
},
"drawLinesBetweenPoints": true,
"lineWidth": 2,
"showCircles": true,
"interpolate": "linear",
"valueAxis": "ValueAxis-1"
}
]

// TODO: what do we want to put in this "vis config"?
const visConfig = {
Expand All @@ -57,11 +41,8 @@ export const toExpression = async (
addTooltip,
dimensions,
valueAxes,
seriesParams
};



if (useVega === true) {
const rawDataFn = buildExpressionFunction('rawData', {});
const dataExpression = buildExpression([...expressionFns, rawDataFn]).toString();

Check warning on line 48 in src/plugins/vis_builder/public/visualizations/vislib/area/to_expression.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/vis_builder/public/visualizations/vislib/area/to_expression.ts#L47-L48

Added lines #L47 - L48 were not covered by tests
Expand Down

0 comments on commit 3229fac

Please sign in to comment.