2020-11-03 10:43:45 +01:00
|
|
|
<script>
|
|
|
|
import { onMount } from "svelte"
|
2020-11-04 13:43:56 +01:00
|
|
|
import fetchData, { fetchSchema } from "../fetchData"
|
|
|
|
import { sortBy } from "lodash/fp"
|
2020-11-03 10:43:45 +01:00
|
|
|
import { ApexOptionsBuilder } from "./ApexOptionsBuilder"
|
2020-11-03 17:14:57 +01:00
|
|
|
import ApexChart from "./ApexChart.svelte"
|
2020-11-03 10:43:45 +01:00
|
|
|
|
2020-11-03 15:32:21 +01:00
|
|
|
// Common props
|
2020-11-04 14:08:59 +01:00
|
|
|
export let _bb
|
2020-11-03 10:43:45 +01:00
|
|
|
export let title
|
|
|
|
export let datasource
|
|
|
|
export let labelColumn
|
|
|
|
export let valueColumns
|
|
|
|
export let xAxisLabel
|
|
|
|
export let yAxisLabel
|
|
|
|
export let height
|
|
|
|
export let width
|
|
|
|
export let color
|
|
|
|
export let animate
|
|
|
|
export let dataLabels
|
|
|
|
export let curve
|
|
|
|
export let legend
|
2020-11-03 15:32:21 +01:00
|
|
|
export let yAxisUnits
|
|
|
|
|
|
|
|
// Area specific props
|
|
|
|
export let area
|
|
|
|
export let stacked
|
|
|
|
export let gradient
|
2020-11-03 10:43:45 +01:00
|
|
|
|
2020-11-04 14:08:59 +01:00
|
|
|
const store = _bb.store
|
2020-11-04 13:43:56 +01:00
|
|
|
let options
|
2020-11-03 10:43:45 +01:00
|
|
|
|
|
|
|
// Fetch data on mount
|
|
|
|
onMount(async () => {
|
2020-11-04 13:43:56 +01:00
|
|
|
if (!datasource || !labelColumn || !valueColumns || !valueColumns.length) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-04 14:08:59 +01:00
|
|
|
const result = (await fetchData(datasource, $store)).slice(0, 100)
|
2020-11-04 13:43:56 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
for (let i = 0; i < valueColumns.length; i++) {
|
|
|
|
if (schema[valueColumns[i]] == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-11-03 10:43:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise default chart
|
|
|
|
let builder = new ApexOptionsBuilder()
|
|
|
|
.title(title)
|
2020-11-03 15:32:21 +01:00
|
|
|
.type(area ? "area" : "line")
|
2020-11-03 10:43:45 +01:00
|
|
|
.width(width)
|
|
|
|
.height(height)
|
|
|
|
.xLabel(xAxisLabel)
|
|
|
|
.yLabel(yAxisLabel)
|
|
|
|
.dataLabels(dataLabels)
|
|
|
|
.animate(animate)
|
|
|
|
.curve(curve.toLowerCase())
|
2020-11-03 15:32:21 +01:00
|
|
|
.gradient(gradient)
|
|
|
|
.stacked(stacked)
|
2020-11-03 10:43:45 +01:00
|
|
|
.legend(legend)
|
2020-11-03 15:32:21 +01:00
|
|
|
.yUnits(yAxisUnits)
|
2020-11-03 10:43:45 +01:00
|
|
|
|
2020-11-04 13:43:56 +01:00
|
|
|
// Add data
|
|
|
|
let useDates = false
|
|
|
|
if (datasource.type !== "view" && schema[labelColumn]) {
|
|
|
|
const labelFieldType = schema[labelColumn].type
|
|
|
|
builder = builder.xType(labelFieldType)
|
|
|
|
useDates = labelFieldType === "datetime"
|
|
|
|
}
|
|
|
|
const series = valueColumns.map(column => ({
|
|
|
|
name: column,
|
|
|
|
data: data.map(row => {
|
|
|
|
if (!useDates) {
|
|
|
|
return row[column]
|
|
|
|
} else {
|
|
|
|
return [row[labelColumn], row[column]]
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
}))
|
|
|
|
builder = builder.series(series)
|
|
|
|
if (!useDates && schema[labelColumn]) {
|
|
|
|
builder = builder.categories(data.map(row => row[labelColumn]))
|
2020-11-03 10:43:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build chart options
|
2020-11-04 13:43:56 +01:00
|
|
|
options = builder.getOptions()
|
|
|
|
})
|
2020-11-03 10:43:45 +01:00
|
|
|
</script>
|
|
|
|
|
2020-11-03 17:14:57 +01:00
|
|
|
<ApexChart {options} />
|