(rows, cols)
| 10 | } |
| 11 | |
| 12 | export const getPlotConfig = (rows, cols) => { |
| 13 | let data = [] |
| 14 | let layout = {} |
| 15 | |
| 16 | if (rows.length == 0 || cols.length == 0) { |
| 17 | return {} |
| 18 | } else if (rows.length >= 0 && cols.length == 2) { |
| 19 | // 2 cols, N rows ==> Bar chart |
| 20 | // Col 0 is X axis, Col 1 is Y axis |
| 21 | // Example query: "Top 5 cities in CA with the highest crime and what is the total crime in each of those cities" |
| 22 | |
| 23 | data = [ |
| 24 | { |
| 25 | x: rows.map(x => '\b' + x[0]), // convert to string. otherwise plotly treats 941002 as 94.1k |
| 26 | y: rows.map(x => x[1]), |
| 27 | type: 'bar', |
| 28 | marker: { color: '#006AF9' } |
| 29 | } |
| 30 | ]; |
| 31 | |
| 32 | layout = { |
| 33 | xaxis: {title: cols[0]}, |
| 34 | yaxis: {title: cols[1]}, |
| 35 | } |
| 36 | |
| 37 | } else if (rows.length == 1 && cols.length >= 1) { |
| 38 | // N cols, 1 row ==> Bar chart |
| 39 | // columns is X axis, row 1 is Y axis |
| 40 | // Example query: "What is the distribution of different categories of crimes in Dallas, TX" |
| 41 | |
| 42 | data = [ |
| 43 | { |
| 44 | x: isGeoColumn(cols[0]) ? cols.slice(1) : cols, |
| 45 | y: isGeoColumn(cols[0]) ? rows[0].slice(1) : rows[0], |
| 46 | type: 'bar', |
| 47 | marker: { color: '#006AF9' } |
| 48 | } |
| 49 | ]; |
| 50 | |
| 51 | } else { |
| 52 | // N cols, N rows ==> Stacked chart. |
| 53 | // column 0 is X axis, column 1 to N is Y axis |
| 54 | // Example query: "What is the percentage population of asian, black and hispanic people in all zipcodes in san francisco" |
| 55 | |
| 56 | for (let i = 1; i < cols.length; i++) { |
| 57 | |
| 58 | // if the column is not a number, don't plot it |
| 59 | if (typeof rows[0][i] !== 'number') { |
| 60 | continue |
| 61 | } |
| 62 | |
| 63 | data.push({ |
| 64 | x: rows.map(x => '\b' + x[0]), // convert to string. otherwise plotly treats 941002 as 94.1k |
| 65 | y: rows.map(x => x[i]), |
| 66 | name: cols[i], |
| 67 | type: 'bar' |
| 68 | }) |
| 69 | } |
no test coverage detected