mirror of
https://github.com/lana-k/sqliteviz.git
synced 2025-12-06 18:18:53 +08:00
add chart module (refactoring)
This commit is contained in:
34
src/chart.js
Normal file
34
src/chart.js
Normal 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
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@
|
|||||||
:dataSourceOptions="dataSourceOptions"
|
:dataSourceOptions="dataSourceOptions"
|
||||||
:plotly="plotly"
|
:plotly="plotly"
|
||||||
@onUpdate="update"
|
@onUpdate="update"
|
||||||
|
@onRender="go"
|
||||||
:useResizeHandler="true"
|
:useResizeHandler="true"
|
||||||
:debug="true"
|
:debug="true"
|
||||||
:advancedTraceTypeSelector="true"
|
:advancedTraceTypeSelector="true"
|
||||||
@@ -26,6 +27,11 @@ import plotly from 'plotly.js/dist/plotly'
|
|||||||
import 'react-chart-editor/lib/react-chart-editor.min.css'
|
import 'react-chart-editor/lib/react-chart-editor.min.css'
|
||||||
|
|
||||||
import PlotlyEditor from 'react-chart-editor'
|
import PlotlyEditor from 'react-chart-editor'
|
||||||
|
import {
|
||||||
|
getOptionsFromDataSources,
|
||||||
|
getDataSourcesFromSqlResult,
|
||||||
|
getChartStateForSave
|
||||||
|
} from '@/chart'
|
||||||
import dereference from 'react-chart-editor/lib/lib/dereference'
|
import dereference from 'react-chart-editor/lib/lib/dereference'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -46,23 +52,10 @@ export default {
|
|||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
dataSources () {
|
dataSources () {
|
||||||
if (!this.sqlResult) {
|
return getDataSourcesFromSqlResult(this.sqlResult)
|
||||||
return {}
|
|
||||||
}
|
|
||||||
const dataSorces = {}
|
|
||||||
const matrix = this.sqlResult.values
|
|
||||||
const [row] = matrix
|
|
||||||
const transposedMatrix = row.map((value, column) => matrix.map(row => row[column]))
|
|
||||||
this.sqlResult.columns.forEach((column, index) => {
|
|
||||||
dataSorces[column] = transposedMatrix[index]
|
|
||||||
})
|
|
||||||
return dataSorces
|
|
||||||
},
|
},
|
||||||
dataSourceOptions () {
|
dataSourceOptions () {
|
||||||
return Object.keys(this.dataSources).map(name => ({
|
return getOptionsFromDataSources(this.dataSources)
|
||||||
value: name,
|
|
||||||
label: name
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
watch: {
|
watch: {
|
||||||
@@ -73,20 +66,15 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
go (data, layout, frames) {
|
||||||
|
// TODO: check changes and enable Save button if needed
|
||||||
|
},
|
||||||
update (data, layout, frames) {
|
update (data, layout, frames) {
|
||||||
this.state = { data, layout, frames }
|
this.state = { data, layout, frames }
|
||||||
this.$emit('update')
|
this.$emit('update')
|
||||||
},
|
},
|
||||||
getChartStateForSave () {
|
getChartStateForSave () {
|
||||||
// we don't need to save the data, only settings
|
return getChartStateForSave(this.state, this.dataSources)
|
||||||
// so we modify state.data using dereference
|
|
||||||
const stateCopy = JSON.parse(JSON.stringify(this.state))
|
|
||||||
const emptySources = {}
|
|
||||||
for (const key in this.dataSources) {
|
|
||||||
emptySources[key] = []
|
|
||||||
}
|
|
||||||
dereference(stateCopy.data, emptySources)
|
|
||||||
return stateCopy
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,7 +77,8 @@ export default {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
const tabIndex = this.currentQuery.tabIndex
|
const tabIndex = this.currentQuery.tabIndex
|
||||||
return this.$store.state.tabs[tabIndex].isUnsaved
|
const tab = this.$store.state.tabs[tabIndex]
|
||||||
|
return tab && tab.isUnsaved
|
||||||
},
|
},
|
||||||
isPredefined () {
|
isPredefined () {
|
||||||
if (this.currentQuery) {
|
if (this.currentQuery) {
|
||||||
|
|||||||
@@ -139,9 +139,6 @@ export default {
|
|||||||
// 40 - height of table header
|
// 40 - height of table header
|
||||||
const freeSpace = bottomPane.offsetHeight - 88 - 42 - 30 - 5 - 40
|
const freeSpace = bottomPane.offsetHeight - 88 - 42 - 30 - 5 - 40
|
||||||
this.tableViewHeight = freeSpace - (freeSpace % 40)
|
this.tableViewHeight = freeSpace - (freeSpace % 40)
|
||||||
},
|
|
||||||
getChartStateForSave () {
|
|
||||||
return this.$refs.chart.getChartStateForSave()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export default {
|
|||||||
const value = {
|
const value = {
|
||||||
id: queryTab.isPredefined ? nanoid() : queryTab.id,
|
id: queryTab.isPredefined ? nanoid() : queryTab.id,
|
||||||
query: queryTab.query,
|
query: queryTab.query,
|
||||||
chart: queryTab.getChartStateForSave(),
|
chart: queryTab.$refs.chart.getChartStateForSave(),
|
||||||
name: newName || queryTab.initName
|
name: newName || queryTab.initName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -187,15 +187,19 @@ describe('storedQueries.js', () => {
|
|||||||
query: 'select * from foo',
|
query: 'select * from foo',
|
||||||
chart: [],
|
chart: [],
|
||||||
initName: null,
|
initName: null,
|
||||||
|
$refs: {
|
||||||
|
chart: {
|
||||||
getChartStateForSave () {
|
getChartStateForSave () {
|
||||||
return []
|
return ['chart']
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const value = storedQueries.save(tab, 'foo')
|
const value = storedQueries.save(tab, 'foo')
|
||||||
expect(value.id).to.equal(tab.id)
|
expect(value.id).to.equal(tab.id)
|
||||||
expect(value.name).to.equal('foo')
|
expect(value.name).to.equal('foo')
|
||||||
expect(value.query).to.equal(tab.query)
|
expect(value.query).to.equal(tab.query)
|
||||||
expect(value.chart).to.eql(tab.getChartStateForSave())
|
expect(value.chart).to.eql(['chart'])
|
||||||
expect(value).to.have.property('createdAt').which.within(now, nowPlusMinute)
|
expect(value).to.have.property('createdAt').which.within(now, nowPlusMinute)
|
||||||
const queries = storedQueries.getStoredQueries()
|
const queries = storedQueries.getStoredQueries()
|
||||||
expect(JSON.stringify(queries)).to.equal(JSON.stringify([value]))
|
expect(JSON.stringify(queries)).to.equal(JSON.stringify([value]))
|
||||||
@@ -207,8 +211,12 @@ describe('storedQueries.js', () => {
|
|||||||
query: 'select * from foo',
|
query: 'select * from foo',
|
||||||
chart: [],
|
chart: [],
|
||||||
initName: null,
|
initName: null,
|
||||||
|
$refs: {
|
||||||
|
chart: {
|
||||||
getChartStateForSave () {
|
getChartStateForSave () {
|
||||||
return []
|
return ['chart']
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,7 +231,7 @@ describe('storedQueries.js', () => {
|
|||||||
expect(second.id).to.equal(first.id)
|
expect(second.id).to.equal(first.id)
|
||||||
expect(second.name).to.equal(first.name)
|
expect(second.name).to.equal(first.name)
|
||||||
expect(second.query).to.equal(tab.query)
|
expect(second.query).to.equal(tab.query)
|
||||||
expect(second.chart).to.eql(first.chart)
|
expect(second.chart).to.eql(['chart'])
|
||||||
expect(new Date(second.createdAt).getTime()).to.equal(first.createdAt.getTime())
|
expect(new Date(second.createdAt).getTime()).to.equal(first.createdAt.getTime())
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -235,8 +243,12 @@ describe('storedQueries.js', () => {
|
|||||||
query: 'select * from foo',
|
query: 'select * from foo',
|
||||||
chart: [],
|
chart: [],
|
||||||
initName: 'foo predefined',
|
initName: 'foo predefined',
|
||||||
|
$refs: {
|
||||||
|
chart: {
|
||||||
getChartStateForSave () {
|
getChartStateForSave () {
|
||||||
return []
|
return ['chart']
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
isPredefined: true
|
isPredefined: true
|
||||||
}
|
}
|
||||||
@@ -247,7 +259,7 @@ describe('storedQueries.js', () => {
|
|||||||
expect(queries[0]).to.have.property('id').which.not.equal(tab.id)
|
expect(queries[0]).to.have.property('id').which.not.equal(tab.id)
|
||||||
expect(queries[0].name).to.equal('foo')
|
expect(queries[0].name).to.equal('foo')
|
||||||
expect(queries[0].query).to.equal(tab.query)
|
expect(queries[0].query).to.equal(tab.query)
|
||||||
expect(queries[0].chart).to.eql(tab.getChartStateForSave())
|
expect(queries[0].chart).to.eql(['chart'])
|
||||||
expect(new Date(queries[0].createdAt)).to.be.within(now, nowPlusMinute)
|
expect(new Date(queries[0].createdAt)).to.be.within(now, nowPlusMinute)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user