|
1 |
| -import { isArray } from 'lodash'; |
2 |
| -import { Cell, DataItem, DataType, PatchContext, PatchPipeline, SimpleFieldInfo } from '../../typings'; |
3 |
| -import { detectAxesType } from '../../common/vizDataToSpec/utils'; |
| 1 | +import { isArray, isString } from 'lodash'; |
| 2 | +import { Cell, DataItem, DataType, PatchContext, PatchPipeline, ROLE, SimpleFieldInfo } from '../../typings'; |
4 | 3 | import { execPipeline } from '../../common/utils';
|
| 4 | +import { foldDatasetByYField } from '../../common/vizDataToSpec/utils'; |
| 5 | +import { FOLD_NAME, FOLD_VALUE } from '@visactor/chart-advisor'; |
5 | 6 |
|
6 | 7 | const matchFieldWithoutPunctuation = (field: string, fieldList: string[]): string | undefined => {
|
7 | 8 | //try to match the field without punctuation
|
@@ -47,6 +48,49 @@ const patchNullField: PatchPipeline = (context: PatchContext, _originalContext:
|
47 | 48 | };
|
48 | 49 | };
|
49 | 50 |
|
| 51 | +const patchField: PatchPipeline = (context: PatchContext, _originalContext: PatchContext) => { |
| 52 | + const { fieldInfo, cell } = context; |
| 53 | + const fieldNames = fieldInfo.map(field => field.fieldName); |
| 54 | + const cellNew = { ...cell }; |
| 55 | + Object.keys(cellNew).forEach(key => { |
| 56 | + const value = cellNew[key]; |
| 57 | + if (isString(value) && (value ?? '').includes(',')) { |
| 58 | + const newValue = (value as string).split(',').map(f => f.trim()); |
| 59 | + if (newValue.every(f => fieldNames.includes(f))) { |
| 60 | + cellNew[key] = newValue; |
| 61 | + } |
| 62 | + } |
| 63 | + }); |
| 64 | + return { |
| 65 | + ...context, |
| 66 | + cell: cellNew |
| 67 | + }; |
| 68 | +}; |
| 69 | + |
| 70 | +const patchColorField: PatchPipeline = (context: PatchContext, _originalContext: PatchContext) => { |
| 71 | + const { chartType, fieldInfo, cell } = context; |
| 72 | + const cellNew = { ...cell }; |
| 73 | + const { color } = cellNew; |
| 74 | + let chartTypeNew = chartType; |
| 75 | + if (color) { |
| 76 | + const colorField = fieldInfo.find(f => f.fieldName === color); |
| 77 | + if (colorField && colorField.role === ROLE.MEASURE) { |
| 78 | + cellNew.color = undefined; |
| 79 | + if (['BAR CHART', 'LINE CHART', 'DUAL AXIS CHART'].includes(chartTypeNew)) { |
| 80 | + cellNew.y = [cellNew.y, color].flat(); |
| 81 | + if (chartTypeNew === 'DUAL AXIS CHART' && cellNew.y.length > 2) { |
| 82 | + chartTypeNew = 'BAR CHART'; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return { |
| 89 | + ...context, |
| 90 | + cell: cellNew |
| 91 | + }; |
| 92 | +}; |
| 93 | + |
50 | 94 | const patchRadarChart: PatchPipeline = (context: PatchContext, _originalContext: PatchContext) => {
|
51 | 95 | const { chartType, cell } = context;
|
52 | 96 |
|
@@ -83,27 +127,22 @@ const patchBoxPlot: PatchPipeline = (context: PatchContext, _originalContext: Pa
|
83 | 127 | };
|
84 | 128 |
|
85 | 129 | const patchBarChart: PatchPipeline = (context: PatchContext, _originalContext: PatchContext) => {
|
86 |
| - const { chartType, cell } = context; |
87 |
| - let chartTypeNew = chartType; |
88 |
| - let cellNew = { ...cell }; |
89 |
| - if (chartTypeNew === 'BAR CHART') { |
90 |
| - if (isArray(cell.y) && cell.y.length === 2) { |
91 |
| - chartTypeNew = 'DUAL AXIS CHART'; |
92 |
| - } else if ((cell.y ?? '').includes(',')) { |
93 |
| - const yNew = (cell.y as string).split(','); |
94 |
| - if (yNew.length === 2) { |
95 |
| - chartTypeNew = 'DUAL AXIS CHART'; |
96 |
| - cellNew = { |
97 |
| - ...cell, |
98 |
| - y: yNew |
99 |
| - }; |
100 |
| - } |
| 130 | + const { chartType, cell, fieldInfo, dataset } = context; |
| 131 | + const chartTypeNew = chartType; |
| 132 | + const cellNew = { ...cell }; |
| 133 | + let datasetNew = dataset; |
| 134 | + if (chartTypeNew === 'BAR CHART' || chartTypeNew === 'LINE CHART') { |
| 135 | + if (isArray(cellNew.y) && cellNew.y.length > 1) { |
| 136 | + datasetNew = foldDatasetByYField(datasetNew, cellNew.y, fieldInfo); |
| 137 | + cellNew.y = FOLD_VALUE.toString(); |
| 138 | + cellNew.color = FOLD_NAME.toString(); |
101 | 139 | }
|
102 | 140 | }
|
103 | 141 | return {
|
104 | 142 | ...context,
|
105 | 143 | chartType: chartTypeNew,
|
106 |
| - cell: cellNew |
| 144 | + cell: cellNew, |
| 145 | + dataset: datasetNew |
107 | 146 | };
|
108 | 147 | };
|
109 | 148 |
|
@@ -162,6 +201,8 @@ const patchArrayField: PatchPipeline = (context: PatchContext, _originalContext:
|
162 | 201 |
|
163 | 202 | const patchPipelines: PatchPipeline[] = [
|
164 | 203 | patchNullField,
|
| 204 | + patchField, |
| 205 | + patchColorField, |
165 | 206 | patchRadarChart,
|
166 | 207 | patchBoxPlot,
|
167 | 208 | patchBarChart,
|
|
0 commit comments