-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
223 lines (199 loc) · 5.73 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import './style.css';
import {
ColumnType,
getChartContext,
ChartToTSEvent,
TSToChartEvent,
} from '@thoughtspot/ts-chart-sdk';
import _, { range } from 'lodash';
import Plotly from 'plotly.js-dist';
function getDataForColumn(column, dataArr) {
// const idx = _.findIndex(dataArr.columns, colId => column.id === colId);
// const data = _.map(dataArr.dataValue, row => row[idx]);
const idx = _.findIndex(dataArr.columns, (colId) => column.id === colId);
const data = _.map(dataArr.dataValue, (row) => row[idx]);
return data;
}
//this is to keep track of initital render
// TODO there might be a better way to do this with plotly
let plotInitial = 1;
const getDataModel = (chartModel) => {
const columns = chartModel.columns;
console.log('----------', chartModel);
const dataArr = chartModel.data[0].data;
// create projects from points & data
// const data1 = getDataForColumn(columns[1], dataArr);
// const data2 = getDataForColumn(columns[2], dataArr);
const data1 = dataArr.dataValue;
const data2 = dataArr.dataValue;
return {
data1,
data2,
};
};
async function render(ctx) {
const chartModel = ctx.getChartModel();
const measureColumns = _.filter(
chartModel.columns,
(col) => col.type === ColumnType.MEASURE
);
// const dataArr = chartModel.data?.[0]?.data ;
const dataArr = getDataModel(chartModel);
console.log('dataArr:', dataArr);
// console.log('--- Column_data:', getDataForColumn(measureColumns[0], dataArr) )
console.log('Inside render function.');
// THE CHART
var trace1 = {
y: dataArr['data2'],
name: measureColumns[0].name,
type: 'box',
};
var trace2 = {
y: dataArr['data1'],
name: measureColumns[1].name,
type: 'box',
};
var data = [trace1, trace2];
const layout1 = JSON.parse(
ctx.getChartModel()?.visualProps?.clientState || '{}'
);
if (plotInitial) {
Plotly.newPlot('app', data, layout1);
const myPlot = document.getElementById('app');
myPlot.on('plotly_relayout', (eventData, eventData2) => {
console.log('renderCount');
let localState = {};
Object.keys(eventData).forEach((key) => {
const cur = key.split('.')[0];
if (!localState[cur]) {
localState[cur] = {
range: [eventData[key]],
};
} else {
localState[cur]['range'].push(eventData[key]);
}
});
ctx.emitEvent(ChartToTSEvent.UpdateVisualProps, {
visualProps: {
...ctx.getChartModel()?.visualProps,
clientState: JSON.stringify(localState),
},
});
});
plotInitial = 0;
}
}
const renderChart = (ctx) => {
try {
ctx.emitEvent(ChartToTSEvent.RenderStart);
render(ctx);
} catch (e) {
ctx.emitEvent(ChartToTSEvent.RenderError, {
hasError: true,
error: e,
});
} finally {
ctx.emitEvent(ChartToTSEvent.RenderComplete);
}
};
const init = async () => {
const ctx = await getChartContext({
getDefaultChartConfig: (chartModel) => {
const cols = chartModel.columns;
const measureColumns = _.filter(
cols,
(col) => col.type === ColumnType.MEASURE
);
const attributeColumns = _.filter(
cols,
(col) => col.type === ColumnType.ATTRIBUTE
);
const axisConfig = {
key: 'column',
dimensions: [
{
key: 'x',
columns: [attributeColumns[0]],
},
{
key: 'y',
columns: measureColumns.slice(0, 2),
},
],
};
return [axisConfig];
},
validateConfig: (updatedConfig, chartModel) => {
if (updatedConfig.length <= 0) {
return {
isValid: false,
validationErrorMessage: ['Invalid config. no config found'],
};
}
return {
isValid: true,
};
},
getQueriesFromChartConfig: (chartConfig) => {
const queries = chartConfig.map((config) =>
_.reduce(
config.dimensions,
(acc, dimension) => ({
queryColumns: [...acc.queryColumns, ...dimension.columns],
}),
{
queryColumns: [],
}
)
);
return queries;
},
renderChart: (ctx) => renderChart(ctx),
chartConfigEditorDefinition: (currentChartConfig, ctx) => {
const { config } = currentChartConfig;
const yColumns = config?.chartConfig?.[0]?.dimensions.find(
(dimension) => dimension.key === 'y' && dimension.columns
);
const configDefinition = [
{
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++) {
configDefinition[0].columnSections.push({
key: `layers${i}`,
label: `Measures layer${i}`,
allowAttributeColumns: false,
allowMeasureColumns: true,
allowTimeSeriesColumns: false,
});
}
}
return configDefinition;
},
});
renderChart(ctx);
};
init();