pie
This commit is contained in:
parent
78bb19967f
commit
337214d48c
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
// Apex charts directly modifies the options object with default properties and internal variables. These being present could unintentionally cause issues to the provider of this prop as the changes are reflected in that component as well. To prevent any issues we clone options here to provide a buffer.
|
// Apex charts directly modifies the options object with default properties and internal variables. These being present could unintentionally cause issues to the provider of this prop as the changes are reflected in that component as well. To prevent any issues we clone options here to provide a buffer.
|
||||||
$: optionsCopy = cloneDeep(options);
|
$: optionsCopy = cloneDeep(options);
|
||||||
|
$: console.log(cloneDeep(options));
|
||||||
|
|
||||||
let chartElement;
|
let chartElement;
|
||||||
let chart;
|
let chart;
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
import ApexChart from "./ApexChart.svelte"
|
import ApexChart from "./ApexChart.svelte"
|
||||||
import { get } from "lodash";
|
import { get } from "lodash";
|
||||||
|
|
||||||
// Common props
|
|
||||||
export let title
|
export let title
|
||||||
export let dataProvider
|
export let dataProvider
|
||||||
export let labelColumn
|
export let labelColumn
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
import ApexChart from "./ApexChart.svelte"
|
import ApexChart from "./ApexChart.svelte"
|
||||||
import { get } from "lodash";
|
import { get } from "lodash";
|
||||||
|
|
||||||
// Common props
|
|
||||||
export let title
|
export let title
|
||||||
export let dataProvider
|
export let dataProvider
|
||||||
export let labelColumn
|
export let labelColumn
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<script>
|
<script>
|
||||||
import { ApexOptionsBuilder } from "./ApexOptionsBuilder"
|
|
||||||
import ApexChart from "./ApexChart.svelte"
|
import ApexChart from "./ApexChart.svelte"
|
||||||
|
import formatters from "./formatters";
|
||||||
|
|
||||||
export let title
|
export let title
|
||||||
export let dataProvider
|
export let dataProvider
|
||||||
|
@ -8,91 +8,85 @@
|
||||||
export let valueColumn
|
export let valueColumn
|
||||||
export let height
|
export let height
|
||||||
export let width
|
export let width
|
||||||
export let dataLabels
|
|
||||||
export let animate
|
export let animate
|
||||||
|
export let dataLabels
|
||||||
export let legend
|
export let legend
|
||||||
export let donut
|
|
||||||
export let palette
|
export let palette
|
||||||
export let c1, c2, c3, c4, c5
|
export let c1, c2, c3, c4, c5
|
||||||
|
|
||||||
$: options = setUpChart(
|
$: labelType = dataProvider?.schema?.[labelColumn]?.type === 'datetime' ?
|
||||||
title,
|
"datetime" : "category"
|
||||||
dataProvider,
|
$: series = getSeries(dataProvider, valueColumn)
|
||||||
labelColumn,
|
$: labels = getLabels(dataProvider, labelColumn, labelType);
|
||||||
valueColumn,
|
|
||||||
height,
|
|
||||||
width,
|
|
||||||
dataLabels,
|
|
||||||
animate,
|
|
||||||
legend,
|
|
||||||
donut,
|
|
||||||
palette,
|
|
||||||
c1 && c2 && c3 && c4 && c5 ? [c1, c2, c3, c4, c5] : null,
|
|
||||||
customColor
|
|
||||||
)
|
|
||||||
|
|
||||||
$: customColor = palette === "Custom"
|
$: options = {
|
||||||
|
series,
|
||||||
const setUpChart = (
|
labels,
|
||||||
title,
|
colors: palette === "Custom" ? [c1, c2, c3, c4, c5] : [],
|
||||||
dataProvider,
|
theme: {
|
||||||
labelColumn,
|
palette: palette === "Custom" ? null : palette
|
||||||
valueColumn,
|
},
|
||||||
height,
|
legend: {
|
||||||
width,
|
show: legend,
|
||||||
dataLabels,
|
position: "top",
|
||||||
animate,
|
horizontalAlign: "right",
|
||||||
legend,
|
showForSingleSeries: true,
|
||||||
donut,
|
showForNullSeries: true,
|
||||||
palette,
|
showForZeroSeries: true,
|
||||||
colors,
|
},
|
||||||
customColor
|
title: {
|
||||||
) => {
|
text: title,
|
||||||
if (
|
},
|
||||||
!dataProvider ||
|
dataLabels: {
|
||||||
!dataProvider.rows?.length ||
|
enabled: dataLabels
|
||||||
!labelColumn ||
|
},
|
||||||
!valueColumn
|
chart: {
|
||||||
) {
|
height: height == null || height === "" ? "auto" : height,
|
||||||
return null
|
width: width == null || width === "" ? "100%" : width,
|
||||||
|
type: "pie",
|
||||||
|
animations: {
|
||||||
|
enabled: animate
|
||||||
|
},
|
||||||
|
toolbar: {
|
||||||
|
show: false,
|
||||||
|
},
|
||||||
|
zoom: {
|
||||||
|
enabled: false,
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch, filter and sort data
|
const getSeries = (datasource, valueColumn) => {
|
||||||
const { schema, rows } = dataProvider
|
const rows = datasource.rows ?? [];
|
||||||
const data = rows
|
|
||||||
.filter(row => row[labelColumn] != null && row[valueColumn] != null)
|
return rows.map(row => {
|
||||||
.slice(0, 100)
|
const value = row?.[valueColumn]
|
||||||
if (!schema || !data.length) {
|
const numValue = parseFloat(value);
|
||||||
return null
|
if (isNaN(numValue)) {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialise default chart
|
return numValue;
|
||||||
let builder = new ApexOptionsBuilder()
|
|
||||||
.title(title)
|
|
||||||
.type(donut ? "donut" : "pie")
|
|
||||||
.width(width)
|
|
||||||
.height(height)
|
|
||||||
.dataLabels(dataLabels)
|
|
||||||
.animate(animate)
|
|
||||||
.legend(legend)
|
|
||||||
.legendPosition("right")
|
|
||||||
.palette(palette)
|
|
||||||
.colors(customColor ? colors : null)
|
|
||||||
|
|
||||||
// Add data if valid datasource
|
|
||||||
const series = data.map(row => {
|
|
||||||
if (schema[valueColumn].type === 'datetime') {
|
|
||||||
return Date.parse(row[valueColumn])
|
|
||||||
}
|
|
||||||
|
|
||||||
return parseFloat(row[valueColumn])
|
|
||||||
})
|
})
|
||||||
const labels = data.map(row => row[labelColumn])
|
|
||||||
builder = builder.series(series).labels(labels)
|
|
||||||
|
|
||||||
// Build chart options
|
|
||||||
return builder.getOptions()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getLabels = (datasource, labelColumn, labelType) => {
|
||||||
|
const rows = datasource.rows ?? [];
|
||||||
|
|
||||||
|
return rows.map(row => {
|
||||||
|
const value = row?.[labelColumn]
|
||||||
|
|
||||||
|
// If a nullish or non-scalar type, replace it with an empty string
|
||||||
|
if (!["string", "number", "boolean"].includes(typeof value)) {
|
||||||
|
return ""
|
||||||
|
} else if (labelType === "datetime") {
|
||||||
|
return formatters["Datetime"](value)
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
})
|
||||||
|
}
|
||||||
|
$: console.log("opt", options);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<ApexChart {options} />
|
<ApexChart {options} />
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
const formatters = {
|
||||||
|
["Default"]: val => val,
|
||||||
|
["Thousands"]: val => `${Math.round(val / 1000)}K`,
|
||||||
|
["Millions"]: val => `${Math.round(val / 1000000)}M`,
|
||||||
|
["Datetime"]: val => (new Date(val)).toLocaleString()
|
||||||
|
}
|
||||||
|
|
||||||
|
export default formatters
|
Loading…
Reference in New Issue