( prev: ChartValue, newResult: DatabaseResultSet, sql: string, theme: string )
| 10 | import { generateGradientColors } from "../chart/echart-options-builder"; |
| 11 | |
| 12 | export function createAutoBoardChartValue( |
| 13 | prev: ChartValue, |
| 14 | newResult: DatabaseResultSet, |
| 15 | sql: string, |
| 16 | theme: string |
| 17 | ): ChartValue { |
| 18 | return produce(prev, (draft) => { |
| 19 | draft.params.layers[0].sql = sql; |
| 20 | if (newResult.headers?.length > 0) { |
| 21 | const columns = newResult.headers.map((header) => header.name); |
| 22 | |
| 23 | if (newResult.rows.length > 0) { |
| 24 | const xAxisKeys: string[] = []; |
| 25 | const yAxisKeys: string[] = []; |
| 26 | const firstRecord = newResult.rows[0]; |
| 27 | const columnType = getColumnType(firstRecord); |
| 28 | |
| 29 | // remove the previous xAxisKey and yAxisKeys |
| 30 | |
| 31 | // study each column value and try to guess the type |
| 32 | if (columnType) { |
| 33 | for (const column of columns) { |
| 34 | if (columnType[column] === "number") { |
| 35 | yAxisKeys.push(column); |
| 36 | } else { |
| 37 | xAxisKeys.push(column); |
| 38 | } |
| 39 | } |
| 40 | if (xAxisKeys.length === 0 && yAxisKeys.length > 1) { |
| 41 | xAxisKeys.push(yAxisKeys.shift() as string); |
| 42 | } |
| 43 | } else { |
| 44 | // if there is no column type, we will just use the first column as xAxisKey |
| 45 | for (let i = 0; i < newResult.headers.length; i++) { |
| 46 | if (i === 0) { |
| 47 | xAxisKeys.push(newResult.headers[i].name); |
| 48 | } else { |
| 49 | yAxisKeys.push(newResult.headers[i].name); |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if (draft.params.options.xAxisKey === "") { |
| 55 | draft.params.options.xAxisKey = |
| 56 | xAxisKeys.length > 0 ? xAxisKeys[0] : ""; |
| 57 | } |
| 58 | |
| 59 | if (draft.params.options.yAxisKeys.length === 0) { |
| 60 | draft.params.options.yAxisKeys = yAxisKeys; |
| 61 | } |
| 62 | |
| 63 | const suggestedChartTypes = suggestChartType( |
| 64 | xAxisKeys, |
| 65 | yAxisKeys, |
| 66 | newResult.rows |
| 67 | ); |
| 68 | |
| 69 | draft.suggestedChartType = suggestedChartTypes; |
no test coverage detected