1
0
mirror of https://github.com/lana-k/sqliteviz.git synced 2025-12-08 11:08:55 +08:00

add chart module (refactoring)

This commit is contained in:
lana-k
2021-02-12 18:57:18 +01:00
parent 9a7dca3482
commit 7e26446573
6 changed files with 70 additions and 38 deletions

34
src/chart.js Normal file
View File

@@ -0,0 +1,34 @@
import dereference from 'react-chart-editor/lib/lib/dereference'
export function getDataSourcesFromSqlResult (sqlResult) {
if (!sqlResult) {
return {}
}
const dataSorces = {}
const matrix = sqlResult.values
const [row] = matrix
const transposedMatrix = row.map((value, column) => matrix.map(row => row[column]))
sqlResult.columns.forEach((column, index) => {
dataSorces[column] = transposedMatrix[index]
})
return dataSorces
}
export function getOptionsFromDataSources (dataSources) {
return Object.keys(dataSources).map(name => ({
value: name,
label: name
}))
}
export function getChartStateForSave (state, dataSources) {
// we don't need to save the data, only settings
// so we modify state.data using dereference
const stateCopy = JSON.parse(JSON.stringify(state))
const emptySources = {}
for (const key in dataSources) {
emptySources[key] = []
}
dereference(stateCopy.data, emptySources)
return stateCopy
}