Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic config + visual props POC #26

Closed
wants to merge 12 commits into from
134 changes: 96 additions & 38 deletions example/custom-bar-chart/custom-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Copyright: ThoughtSpot Inc. 2023
*/

import { VisualPropEditorDefinition } from '@thoughtspot/ts-chart-sdk';
import {
ChartColumn,
ChartConfig,
Expand All @@ -21,7 +22,9 @@ import {
Query,
VisualProps,
} from '@thoughtspot/ts-chart-sdk';
import { ChartConfigEditorDefinition } from '@thoughtspot/ts-chart-sdk/src';
import Chart from 'chart.js/auto';
import { toDimension } from 'chart.js/dist/helpers/helpers.core';
import ChartDataLabels from 'chartjs-plugin-datalabels';
import _ from 'lodash';

Expand Down Expand Up @@ -151,6 +154,11 @@ function render(ctx: CustomChartContext) {
visualPropKeyMap[2],
false,
);
const labelColor = _.get(
chartModel.visualProps,
visualPropKeyMap[1],
'blue',
);
if (!dataModel) {
return;
}
Expand All @@ -174,11 +182,8 @@ function render(ctx: CustomChartContext) {
plugins: {
// Change options for ALL labels of THIS CHART
datalabels: {
display: allowLabels ? 'auto' : false,
formatter: value => numberFormatter(value),
color: 'blue',
textStrokeColor: 'white',
textStrokeWidth: 5,
display: allowLabels,
color: labelColor,
labels: {
title: {
font: {
Expand Down Expand Up @@ -312,34 +317,70 @@ const renderChart = async (ctx: CustomChartContext): Promise<void> => {
return queries;
},
renderChart: ctx => renderChart(ctx),
chartConfigEditorDefinition: [
{
key: 'column',
label: 'Custom Column',
descriptionText:
'X Axis can only have attributes, Y Axis can only have measures, Color can only have attributes. ' +
'Should have just 1 column in Y axis with colors columns.',
columnSections: [
{
key: 'x',
label: 'Custom X Axis',
allowAttributeColumns: true,
allowMeasureColumns: false,
allowTimeSeriesColumns: true,
maxColumnCount: 1,
},
{
key: 'y',
label: 'Custom Y Axis',
chartConfigEditorDefinition: (
chartModel: ChartModel,
updatedChartConfig: ChartConfig[],
updatedVisualPropEditorDefinition:
| VisualPropEditorDefinition
| undefined,
): ChartConfigEditorDefinition[] => {
let yColumns;
if(updatedChartConfig.length === 0) {
yColumns = chartModel?.config?.chartConfig?.[0]?.dimensions.find(
dimension => dimension.key === 'y' && dimension.columns,
);
}
else {
yColumns = updatedChartConfig?.[0]?.dimensions.find(
dimension => dimension.key === 'y' && dimension.columns,
);
}

const config = [
{
key: 'column',
label: 'Custom Column',
descriptionText:
'X Axis can only have attributes, Y Axis can only have measures, Color can only have attributes. ' +
'Should have just 1 column in Y axis with colors columns.',
columnSections: [
{
key: 'x',
label: 'Custom X Axis',
allowAttributeColumns: true,
allowMeasureColumns: false,
allowTimeSeriesColumns: true,
maxColumnCount: 1,
},
{
key: 'y',
label: 'Custom Y Axis',
allowAttributeColumns: false,
allowMeasureColumns: true,
allowTimeSeriesColumns: false,
},
],
},
];
if (yColumns?.columns.length) {
for (let i = 0; i < yColumns.columns.length; i++) {
config[0].columnSections.push({
key: 'layers',
label: 'Measures layer',
allowAttributeColumns: false,
allowMeasureColumns: true,
allowTimeSeriesColumns: false,
},
],
},
],
visualPropEditorDefinition: {
elements: [
});
}
}
return config;
},
visualPropEditorDefinition: (
chartModel: ChartModel,
visualProps: VisualProps,
chartConfig: ChartConfigEditorDefinition[] | undefined,
): VisualPropEditorDefinition => {
const elements = [
{
key: 'color',
type: 'radio',
Expand All @@ -352,13 +393,6 @@ const renderChart = async (ctx: CustomChartContext): Promise<void> => {
key: 'accordion',
label: 'Accordion',
children: [
{
key: 'Color2',
type: 'radio',
defaultValue: 'blue',
values: ['blue', 'white', 'red'],
label: 'Color2',
},
{
key: 'datalabels',
type: 'toggle',
Expand All @@ -367,7 +401,31 @@ const renderChart = async (ctx: CustomChartContext): Promise<void> => {
},
],
},
],
];
if(visualProps.length !== 0) {
if (visualProps?.accordion?.datalabels) {
elements[1].children?.push({
key: 'Color2',
type: 'radio',
defaultValue: 'blue',
values: ['blue', 'white', 'red'],
label: 'Color2',
});
}
}
else {
if (chartModel.visualProps?.accordion?.datalabels) {
elements[1].children?.push({
key: 'Color2',
type: 'radio',
defaultValue: 'blue',
values: ['blue', 'white', 'red'],
label: 'Color2',
});
}
}

return { elements };
},
});

Expand Down
49 changes: 44 additions & 5 deletions src/main/custom-chart-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@
*
* @version SDK: 0.1 | ThoughtSpot:
*/
chartConfigEditorDefinition?: ChartConfigEditorDefinition[];
chartConfigEditorDefinition?: (
chartModel: ChartModel,
updatedChartConfig: ChartConfig[],
visualPropEditorDefinition: VisualPropEditorDefinition | undefined,
) => ChartConfigEditorDefinition[];

/**
* Definition to help edit/customize the visual properties from chart settings editor
Expand All @@ -117,7 +121,11 @@
* @returns {@link VisualPropEditorDefinition}
* @version SDK: 0.1 | ThoughtSpot:
*/
visualPropEditorDefinition?: VisualPropEditorDefinition;
visualPropEditorDefinition?: (
chartModel: ChartModel,
updatedVisualProps: VisualProps,
chartConfigEditorDefinition: ChartConfigEditorDefinition[] | undefined,
) => VisualPropEditorDefinition;
};

/**
Expand All @@ -126,7 +134,7 @@
const DEFAULT_CHART_CONTEXT_PROPS: Partial<CustomChartContextProps> = {
validateConfig: () => ({ isValid: true }),
validateVisualProps: () => ({ isValid: true }),
chartConfigEditorDefinition: undefined,
chartConfigEditorDefinition: () => undefined,
};

export class CustomChartContext {
Expand Down Expand Up @@ -241,7 +249,7 @@
* @version SDK: 0.1 | ThoughtSpot:
*/
public initialize = (): Promise<void> => {
console.log('Chart Context: initialization start');

Check warning on line 252 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
return this.hasInitializedPromise;
};

Expand Down Expand Up @@ -273,7 +281,7 @@
*/
public off<T extends keyof TSToChartEventsPayloadMap>(eventType: T): void {
if (_.isNil(this.eventListeners[eventType])) {
console.log('No event listener found to remove');

Check warning on line 284 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
this.eventListeners[eventType] = [];
return;
}
Expand Down Expand Up @@ -430,7 +438,7 @@
...eventPayload: ChartToTSEventsPayloadMap[T]
): Promise<any> {
if (!isInitialized) {
console.log(

Check warning on line 441 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
'Chart Context: not initialized the context, something went wrong',
);
return Promise.reject(new Error('Context not initialized'));
Expand All @@ -453,7 +461,7 @@
*/
private registerEventProcessor = () => {
if (isInitialized) {
console.error(

Check warning on line 464 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
'The context is already initialized. you cannot have multiple contexts',
);
throw new Error(ErrorType.MultipleContextsNotSupported);
Expand All @@ -473,7 +481,7 @@
return;
}

console.log(

Check warning on line 484 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
'Chart Context: message received:',
event.data.eventType,
event.data,
Expand Down Expand Up @@ -532,6 +540,18 @@
payload.visualProps,
this.chartModel,
);
if (validationResponse.isValid) {

Check failure on line 543 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
const visualPropEditorDefinition =

Check failure on line 544 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
this.chartContextProps.visualPropEditorDefinition(

Check failure on line 545 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
this.chartModel,

Check failure on line 546 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
payload.visualProps,

Check failure on line 547 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
payload.chartConfigEditorDefinition,

Check failure on line 548 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
);

Check failure on line 549 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can also change the configEditorDefinition

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return {

Check failure on line 550 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
...validationResponse,

Check failure on line 551 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
visualPropEditorDefinition,

Check failure on line 552 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Delete `····`
};
}
return validationResponse;
}
// this will never be true
Expand All @@ -553,6 +573,25 @@
payload.chartConfig,
this.chartModel,
);
if (validationResponse.isValid) {
const chartConfigEditorDefinition =
this.chartContextProps.chartConfigEditorDefinition(
this.chartModel,
payload.chartConfig,
payload.visualPropEditorDefinition,
);
const visualPropEditorDefinition =
this.chartContextProps.visualPropEditorDefinition(
this.chartModel,
payload.visualPropEditorDefinition,
payload.chartConfig,
);
return {
...validationResponse,
chartConfigEditorDefinition,
visualPropEditorDefinition,
};
}
return validationResponse;
}
// this will never be true
Expand Down Expand Up @@ -615,7 +654,7 @@
isValid: true,
};
} catch (error: unknown) {
console.log(

Check warning on line 657 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
'ContextMenuCustomAction: payload recieved:',
payload,
'CustomActionCallbackStore:',
Expand Down Expand Up @@ -658,7 +697,7 @@
isValid: true,
};
} catch (error: unknown) {
console.log(

Check warning on line 700 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
'AxisMenuCustomAction: payload recieved:',
payload,
'CustomActionCallbackStore:',
Expand Down Expand Up @@ -780,9 +819,9 @@
isConfigValid: isValid,
defaultChartConfig,
chartConfigEditorDefinition:
this.chartContextProps.chartConfigEditorDefinition,
this.chartContextProps.chartConfigEditorDefinition(this.chartModel, defaultChartConfig),
visualPropEditorDefinition:
this.chartContextProps.visualPropEditorDefinition,
this.chartContextProps.visualPropEditorDefinition(this.chartModel, defaultChartConfig),
};
};

Expand Down Expand Up @@ -810,7 +849,7 @@
};
}

console.log(

Check warning on line 852 in src/main/custom-chart-context.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected console statement
'ChartContext: Response:',
event.data.eventType,
response,
Expand Down
4 changes: 4 additions & 0 deletions src/types/common.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

import { ChartColumn } from './answer-column.types';
import { ChartConfigEditorDefinition } from './configurator.types';
import { VisualPropEditorDefinition } from './visual-prop.types';

/**
* List of Columns for a dimension in the Custom Chart Config.
Expand Down Expand Up @@ -144,6 +146,8 @@ export interface ChartModel {
export type ValidationResponse = {
isValid: boolean;
validationErrorMessage?: string[];
chartConfigEditorDefinition?: ChartConfigEditorDefinition[],
visualPropEditorDefinition?: VisualPropEditorDefinition,
};

/**
Expand Down
2 changes: 2 additions & 0 deletions src/types/ts-to-chart-event.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ export interface VisualPropsValidateEventPayload {
* @version SDK: 0.1 | ThoughtSpot:
*/
visualProps: VisualProps;
chartConfigEditorDefinition: ChartConfigEditorDefinition[] | undefined;
}

/**
Expand All @@ -279,6 +280,7 @@ export interface ChartConfigValidateEventPayload {
* @version SDK: 0.1 | ThoughtSpot:
*/
chartConfig: ChartConfig[];
visualPropEditorDefinition: VisualPropEditorDefinition | undefined;
}

/**
Expand Down
Loading