Improve robustness of pie and donut charts

This commit is contained in:
Andrew Kingston 2020-11-04 13:08:59 +00:00
parent e7eab46435
commit b47589cdad
3 changed files with 36 additions and 24 deletions

View File

@ -5,6 +5,7 @@
import { ApexOptionsBuilder } from "./ApexOptionsBuilder" import { ApexOptionsBuilder } from "./ApexOptionsBuilder"
import ApexChart from "./ApexChart.svelte" import ApexChart from "./ApexChart.svelte"
export let _bb
export let title export let title
export let datasource export let datasource
export let labelColumn export let labelColumn
@ -20,6 +21,7 @@
export let stacked export let stacked
export let yAxisUnits export let yAxisUnits
const store = _bb.store
let options let options
// Fetch data on mount // Fetch data on mount
@ -28,7 +30,7 @@
return return
} }
const result = (await fetchData(datasource)).slice(0, 20) const result = (await fetchData(datasource, $store)).slice(0, 20)
const data = sortBy(row => row[labelColumn])(result) const data = sortBy(row => row[labelColumn])(result)
const schema = await fetchSchema(datasource.tableId) const schema = await fetchSchema(datasource.tableId)
if (!schema || !data || !data.length) { if (!schema || !data || !data.length) {
@ -86,8 +88,6 @@
// Build chart options // Build chart options
options = builder.getOptions() options = builder.getOptions()
}) })
$: console.log(options)
</script> </script>
<ApexChart {options} /> <ApexChart {options} />

View File

@ -6,6 +6,7 @@
import ApexChart from "./ApexChart.svelte" import ApexChart from "./ApexChart.svelte"
// Common props // Common props
export let _bb
export let title export let title
export let datasource export let datasource
export let labelColumn export let labelColumn
@ -26,6 +27,7 @@
export let stacked export let stacked
export let gradient export let gradient
const store = _bb.store
let options let options
// Fetch data on mount // Fetch data on mount
@ -34,7 +36,7 @@
return return
} }
const result = (await fetchData(datasource)).slice(0, 100) const result = (await fetchData(datasource, $store)).slice(0, 100)
const data = sortBy(row => row[labelColumn])(result) const data = sortBy(row => row[labelColumn])(result)
const schema = await fetchSchema(datasource.tableId) const schema = await fetchSchema(datasource.tableId)
if (!schema || !data || !data.length) { if (!schema || !data || !data.length) {

View File

@ -1,11 +1,11 @@
<script> <script>
import { onMount } from "svelte" import { onMount } from "svelte"
import { chart } from "svelte-apexcharts" import fetchData, { fetchSchema } from "../fetchData"
import fetchData from "../fetchData" import { sortBy } from "lodash/fp"
import { isEmpty, sortBy } from "lodash/fp"
import { ApexOptionsBuilder } from "./ApexOptionsBuilder" import { ApexOptionsBuilder } from "./ApexOptionsBuilder"
import ApexChart from "./ApexChart.svelte" import ApexChart from "./ApexChart.svelte"
export let _bb
export let title export let title
export let datasource export let datasource
export let labelColumn export let labelColumn
@ -18,18 +18,32 @@
export let legend export let legend
export let donut export let donut
let data const store = _bb.store
$: options = getChartOptions(data) let options
// Fetch data on mount // Fetch data on mount
onMount(async () => { onMount(async () => {
if (!isEmpty(datasource)) { if (!datasource || !labelColumn || !valueColumn) {
const result = (await fetchData(datasource)).slice(0, 20) return
data = sortBy(row => row[labelColumn])(result) }
const result = (await fetchData(datasource, $store)).slice(0, 20)
const data = sortBy(row => row[labelColumn])(result)
const schema = await fetchSchema(datasource.tableId)
if (!schema || !data || !data.length) {
return
}
// Check columns are valid
if (datasource.type !== "view") {
if (schema[labelColumn] == null) {
return
}
if (schema[valueColumn] == null) {
return
}
} }
})
function getChartOptions(rows = []) {
// Initialise default chart // Initialise default chart
let builder = new ApexOptionsBuilder() let builder = new ApexOptionsBuilder()
.title(title) .title(title)
@ -42,19 +56,15 @@
.legendPosition("right") .legendPosition("right")
// Add data if valid datasource // Add data if valid datasource
if (rows && rows.length) { const series = data.map(row => parseFloat(row[valueColumn]))
if (!isEmpty(valueColumn) && rows[0][valueColumn] != null) {
const series = rows.map(row => parseFloat(row[valueColumn]))
builder = builder.series(series) builder = builder.series(series)
} if (data[0][labelColumn] != null) {
if (!isEmpty(rows[0][labelColumn])) { builder = builder.labels(data.map(row => row[labelColumn]))
builder = builder.labels(rows.map(row => row[labelColumn]))
}
} }
// Build chart options // Build chart options
return builder.getOptions() options = builder.getOptions()
} })
</script> </script>
<ApexChart {options} /> <ApexChart {options} />