2020-11-03 12:50:55 +01:00
|
|
|
<script>
|
|
|
|
import { onMount } from "svelte"
|
2020-11-04 14:08:59 +01:00
|
|
|
import fetchData, { fetchSchema } from "../fetchData"
|
2020-11-03 12:50:55 +01:00
|
|
|
import { ApexOptionsBuilder } from "./ApexOptionsBuilder"
|
2020-11-03 17:14:57 +01:00
|
|
|
import ApexChart from "./ApexChart.svelte"
|
2020-11-03 12:50:55 +01:00
|
|
|
|
2020-11-04 14:08:59 +01:00
|
|
|
export let _bb
|
2020-11-03 12:50:55 +01:00
|
|
|
export let title
|
|
|
|
export let datasource
|
|
|
|
export let labelColumn
|
|
|
|
export let valueColumn
|
|
|
|
export let height
|
|
|
|
export let width
|
|
|
|
export let color
|
|
|
|
export let dataLabels
|
|
|
|
export let animate
|
|
|
|
export let legend
|
|
|
|
export let donut
|
|
|
|
|
2020-11-04 14:08:59 +01:00
|
|
|
const store = _bb.store
|
|
|
|
let options
|
2020-11-03 12:50:55 +01:00
|
|
|
|
|
|
|
// Fetch data on mount
|
|
|
|
onMount(async () => {
|
2020-11-04 14:08:59 +01:00
|
|
|
if (!datasource || !labelColumn || !valueColumn) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-04 15:21:48 +01:00
|
|
|
// Fetch, filter and sort data
|
2020-11-04 14:08:59 +01:00
|
|
|
const schema = await fetchSchema(datasource.tableId)
|
2020-11-04 15:21:48 +01:00
|
|
|
const result = await fetchData(datasource, $store)
|
|
|
|
const data = result
|
|
|
|
.filter(row => row[labelColumn] != null && row[valueColumn] != null)
|
|
|
|
.slice(0, 20)
|
|
|
|
.sort((a, b) => (a[labelColumn] > b[labelColumn] ? 1 : -1))
|
|
|
|
if (!schema || !data.length) {
|
2020-11-04 14:08:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-03 12:50:55 +01:00
|
|
|
// Initialise default chart
|
|
|
|
let builder = new ApexOptionsBuilder()
|
|
|
|
.title(title)
|
|
|
|
.type(donut ? "donut" : "pie")
|
|
|
|
.width(width)
|
|
|
|
.height(height)
|
|
|
|
.dataLabels(dataLabels)
|
|
|
|
.animate(animate)
|
|
|
|
.legend(legend)
|
|
|
|
.legendPosition("right")
|
|
|
|
|
|
|
|
// Add data if valid datasource
|
2020-11-04 14:08:59 +01:00
|
|
|
const series = data.map(row => parseFloat(row[valueColumn]))
|
2020-11-04 15:21:48 +01:00
|
|
|
const labels = data.map(row => row[labelColumn])
|
|
|
|
builder = builder.series(series).labels(labels)
|
2020-11-03 12:50:55 +01:00
|
|
|
|
|
|
|
// Build chart options
|
2020-11-04 14:08:59 +01:00
|
|
|
options = builder.getOptions()
|
|
|
|
})
|
2020-11-03 12:50:55 +01:00
|
|
|
</script>
|
|
|
|
|
2020-11-03 17:14:57 +01:00
|
|
|
<ApexChart {options} />
|