(frame: MDBFrame)
| 10 | * Handles both old format (nested visualizationConfig) and new format (separate rows) |
| 11 | */ |
| 12 | export const parseVisualizationData = (frame: MDBFrame): VisualizationConfig => { |
| 13 | // Get data source and path from schema.def |
| 14 | |
| 15 | // Find main row |
| 16 | const mainRow = frame.rows?.find(row => row.name === 'main'); |
| 17 | |
| 18 | if (!mainRow?.props) { |
| 19 | // Return default config if no main row |
| 20 | return createDefaultVisualizationConfig() |
| 21 | } |
| 22 | |
| 23 | try { |
| 24 | const parsedProps = JSON.parse(mainRow.props); |
| 25 | |
| 26 | |
| 27 | // New format - build config from separate rows |
| 28 | const titleRow = frame.rows?.find(row => row.name === 'title'); |
| 29 | const gridRow = frame.rows?.find(row => row.name === 'grid'); |
| 30 | const xAxisRow = frame.rows?.find(row => row.name === 'x-axis'); |
| 31 | const yAxisRow = frame.rows?.find(row => row.name === 'y-axis'); |
| 32 | const legendRow = frame.rows?.find(row => row.name === 'legend'); |
| 33 | const tooltipRow = frame.rows?.find(row => row.name === 'tooltip'); |
| 34 | |
| 35 | |
| 36 | const config: VisualizationConfig = { |
| 37 | id: frame.schema?.id || '', |
| 38 | name: parsedProps.name || i18n.labels.visualization, |
| 39 | chartType: parsedProps.chartType || 'bar', |
| 40 | mark: { |
| 41 | type: parsedProps.chartType === 'line' ? 'line' : |
| 42 | parsedProps.chartType === 'scatter' ? 'circle' : |
| 43 | parsedProps.chartType === 'pie' ? 'arc' : |
| 44 | parsedProps.chartType === 'area' ? 'area' : 'rect', |
| 45 | fill: parsedProps.fill, |
| 46 | stroke: parsedProps.stroke, |
| 47 | strokeWidth: parsedProps.strokeWidth, |
| 48 | interpolate: parsedProps.interpolate, |
| 49 | innerRadius: parsedProps.innerRadius, |
| 50 | ...(parsedProps.pointShow !== undefined && { |
| 51 | point: { |
| 52 | show: parsedProps.pointShow, |
| 53 | size: parsedProps.pointSize || 4 |
| 54 | } |
| 55 | }) |
| 56 | }, |
| 57 | encoding: { |
| 58 | x: parsedProps.xFields && parsedProps.xFields.length > 1 |
| 59 | ? parsedProps.xFields.map((field: string) => ({ |
| 60 | field, |
| 61 | type: parsedProps.xType || 'nominal', |
| 62 | ...(parsedProps.xAggregate && { aggregate: parsedProps.xAggregate }), |
| 63 | ...(parsedProps.xTimeUnit && { timeUnit: parsedProps.xTimeUnit }) |
| 64 | })) |
| 65 | : { |
| 66 | field: parsedProps.xField || '', |
| 67 | type: parsedProps.xType || 'nominal', |
| 68 | ...(parsedProps.xAggregate && { aggregate: parsedProps.xAggregate }), |
| 69 | ...(parsedProps.xTimeUnit && { timeUnit: parsedProps.xTimeUnit }) |
no test coverage detected