refactor of bar done i think

This commit is contained in:
Gerard Burns 2024-04-18 06:51:54 +01:00
parent 1a1a417d94
commit 8d77bb016f
4 changed files with 166 additions and 197 deletions

View File

@ -159,7 +159,7 @@
box-sizing: border-box; box-sizing: border-box;
border: 1px solid var(--grey-4); border: 1px solid var(--grey-4);
opacity: 0; opacity: 0;
pointer-events: none; overflow: hidden;
transition: width 300ms ease-in, height 300ms ease-in, top 300ms ease-in, left 300ms ease-in; transition: width 300ms ease-in, height 300ms ease-in, top 300ms ease-in, left 300ms ease-in;
} }

View File

@ -1684,19 +1684,19 @@
"type": "select", "type": "select",
"label": "Colors", "label": "Colors",
"key": "palette", "key": "palette",
"defaultValue": "Palette 1", "defaultValue": "palette1",
"options": [ "options": [
"Custom", { "label": "Custom", "value": "Custom" },
"Palette 1", { "label": "Palette 1", "value": "palette1" },
"Palette 2", { "label": "Palette 2", "value": "palette2" },
"Palette 3", { "label": "Palette 3", "value": "palette3" },
"Palette 4", { "label": "Palette 4", "value": "palette4" },
"Palette 5", { "label": "Palette 5", "value": "palette5" },
"Palette 6", { "label": "Palette 6", "value": "palette6" },
"Palette 7", { "label": "Palette 7", "value": "palette7" },
"Palette 8", { "label": "Palette 8", "value": "palette8" },
"Palette 9", { "label": "Palette 9", "value": "palette9" },
"Palette 10" { "label": "Palette 10", "value": "palette10" }
] ]
}, },
{ {
@ -1844,17 +1844,17 @@
"key": "palette", "key": "palette",
"defaultValue": "Palette 1", "defaultValue": "Palette 1",
"options": [ "options": [
"Custom", { "label": "Custom", "value": "Custom" },
"Palette 1", { "label": "Palette 1", "value": "palette1" },
"Palette 2", { "label": "Palette 2", "value": "palette2" },
"Palette 3", { "label": "Palette 3", "value": "palette3" },
"Palette 4", { "label": "Palette 4", "value": "palette4" },
"Palette 5", { "label": "Palette 5", "value": "palette5" },
"Palette 6", { "label": "Palette 6", "value": "palette6" },
"Palette 7", { "label": "Palette 7", "value": "palette7" },
"Palette 8", { "label": "Palette 8", "value": "palette8" },
"Palette 9", { "label": "Palette 9", "value": "palette9" },
"Palette 10" { "label": "Palette 10", "value": "palette10" }
] ]
}, },
{ {
@ -1995,19 +1995,19 @@
"type": "select", "type": "select",
"label": "Colors", "label": "Colors",
"key": "palette", "key": "palette",
"defaultValue": "Palette 1", "defaultValue": "palette1",
"options": [ "options": [
"Custom", { "label": "Custom", "value": "Custom" },
"Palette 1", { "label": "Palette 1", "value": "palette1" },
"Palette 2", { "label": "Palette 2", "value": "palette2" },
"Palette 3", { "label": "Palette 3", "value": "palette3" },
"Palette 4", { "label": "Palette 4", "value": "palette4" },
"Palette 5", { "label": "Palette 5", "value": "palette5" },
"Palette 6", { "label": "Palette 6", "value": "palette6" },
"Palette 7", { "label": "Palette 7", "value": "palette7" },
"Palette 8", { "label": "Palette 8", "value": "palette8" },
"Palette 9", { "label": "Palette 9", "value": "palette9" },
"Palette 10" { "label": "Palette 10", "value": "palette10" }
] ]
}, },
{ {
@ -2143,19 +2143,19 @@
"type": "select", "type": "select",
"label": "Colors", "label": "Colors",
"key": "palette", "key": "palette",
"defaultValue": "Palette 1", "defaultValue": "palette1",
"options": [ "options": [
"Custom", { "label": "Custom", "value": "Custom" },
"Palette 1", { "label": "Palette 1", "value": "palette1" },
"Palette 2", { "label": "Palette 2", "value": "palette2" },
"Palette 3", { "label": "Palette 3", "value": "palette3" },
"Palette 4", { "label": "Palette 4", "value": "palette4" },
"Palette 5", { "label": "Palette 5", "value": "palette5" },
"Palette 6", { "label": "Palette 6", "value": "palette6" },
"Palette 7", { "label": "Palette 7", "value": "palette7" },
"Palette 8", { "label": "Palette 8", "value": "palette8" },
"Palette 9", { "label": "Palette 9", "value": "palette9" },
"Palette 10" { "label": "Palette 10", "value": "palette10" }
] ]
}, },
{ {
@ -2272,19 +2272,19 @@
"type": "select", "type": "select",
"label": "Colors", "label": "Colors",
"key": "palette", "key": "palette",
"defaultValue": "Palette 1", "defaultValue": "palette1",
"options": [ "options": [
"Custom", { "label": "Custom", "value": "Custom" },
"Palette 1", { "label": "Palette 1", "value": "palette1" },
"Palette 2", { "label": "Palette 2", "value": "palette2" },
"Palette 3", { "label": "Palette 3", "value": "palette3" },
"Palette 4", { "label": "Palette 4", "value": "palette4" },
"Palette 5", { "label": "Palette 5", "value": "palette5" },
"Palette 6", { "label": "Palette 6", "value": "palette6" },
"Palette 7", { "label": "Palette 7", "value": "palette7" },
"Palette 8", { "label": "Palette 8", "value": "palette8" },
"Palette 9", { "label": "Palette 9", "value": "palette9" },
"Palette 10" { "label": "Palette 10", "value": "palette10" }
] ]
}, },
{ {
@ -2510,19 +2510,19 @@
"type": "select", "type": "select",
"label": "Colors", "label": "Colors",
"key": "palette", "key": "palette",
"defaultValue": "Palette 1", "defaultValue": "palette1",
"options": [ "options": [
"Custom", { "label": "Custom", "value": "Custom" },
"Palette 1", { "label": "Palette 1", "value": "palette1" },
"Palette 2", { "label": "Palette 2", "value": "palette2" },
"Palette 3", { "label": "Palette 3", "value": "palette3" },
"Palette 4", { "label": "Palette 4", "value": "palette4" },
"Palette 5", { "label": "Palette 5", "value": "palette5" },
"Palette 6", { "label": "Palette 6", "value": "palette6" },
"Palette 7", { "label": "Palette 7", "value": "palette7" },
"Palette 8", { "label": "Palette 8", "value": "palette8" },
"Palette 9", { "label": "Palette 9", "value": "palette9" },
"Palette 10" { "label": "Palette 10", "value": "palette10" }
] ]
}, },
{ {
@ -5187,19 +5187,19 @@
"type": "select", "type": "select",
"label": "Colors", "label": "Colors",
"key": "palette", "key": "palette",
"defaultValue": "Palette 1", "defaultValue": "palette1",
"options": [ "options": [
"Custom", { "label": "Custom", "value": "Custom" },
"Palette 1", { "label": "Palette 1", "value": "palette1" },
"Palette 2", { "label": "Palette 2", "value": "palette2" },
"Palette 3", { "label": "Palette 3", "value": "palette3" },
"Palette 4", { "label": "Palette 4", "value": "palette4" },
"Palette 5", { "label": "Palette 5", "value": "palette5" },
"Palette 6", { "label": "Palette 6", "value": "palette6" },
"Palette 7", { "label": "Palette 7", "value": "palette7" },
"Palette 8", { "label": "Palette 8", "value": "palette8" },
"Palette 9", { "label": "Palette 9", "value": "palette9" },
"Palette 10" { "label": "Palette 10", "value": "palette10" }
] ]
}, },
{ {

View File

@ -8,9 +8,6 @@
const component = getContext("component") const component = getContext("component")
export let options export let options
$: {
console.log(options);
}
/* /*
export let invalid = false export let invalid = false

View File

@ -3,10 +3,11 @@
import ApexChart from "./ApexChart.svelte" import ApexChart from "./ApexChart.svelte"
import { get } from "lodash"; import { get } from "lodash";
export let title
export let dataProvider export let dataProvider
export let labelColumn export let labelColumn
export let valueColumns export let valueColumns
export let title
export let xAxisLabel export let xAxisLabel
export let yAxisLabel export let yAxisLabel
export let height export let height
@ -20,126 +21,97 @@
export let c1, c2, c3, c4, c5 export let c1, c2, c3, c4, c5
export let horizontal export let horizontal
$: options = setUpChart( const formatters = {
title, ["Default"]: val => val,
dataProvider, ["Thousands"]: val => `${Math.round(val / 1000)}K`,
labelColumn, ["Millions"]: val => `${Math.round(val / 1000000)}M`,
valueColumns, }
xAxisLabel,
yAxisLabel, $: series = getSeries(dataProvider, valueColumns)
height, $: categories = getCategories(dataProvider, labelColumn);
width,
dataLabels, $: dataType = dataProvider?.schema?.[labelColumn]?.type === 'datetime' ?
animate, "datetime" : "category"
legend,
$: options = {
series,
colors: palette === "Custom" ? [c1, c2, c3, c4, c5] : [],
theme: {
palette: palette === "Custom" ? null : palette
},
legend: {
show: legend,
position: "top",
horizontalAlign: "right",
showForSingleSeries: true,
showForNullSeries: true,
showForZeroSeries: true,
},
title: {
text: title,
},
dataLabels: {
enabled: dataLabels
},
chart: {
height: height == null || height === "" ? "auto" : height,
width: width == null || width === "" ? "100%" : width,
type: "bar",
stacked, stacked,
yAxisUnits, animations: {
palette, enabled: animate
horizontal, },
c1 && c2 && c3 && c4 && c5 ? [c1, c2, c3, c4, c5] : null, toolbar: {
customColor show: false,
) },
zoom: {
$: customColor = palette === "Custom" enabled: false,
},
const setUpChart = ( },
title, xaxis: {
dataProvider, type: dataType,
labelColumn, categories,
valueColumns, title: {
xAxisLabel, text: xAxisLabel
yAxisLabel,
height,
width,
dataLabels,
animate,
legend,
stacked,
yAxisUnits,
palette,
horizontal,
colors,
customColor
) => {
const allCols = [labelColumn, ...(valueColumns || [null])]
if (
!dataProvider ||
!dataProvider.rows?.length ||
allCols.find(x => x == null)
) {
return null
} }
},
// Fetch data yaxis: {
const { schema, rows } = dataProvider title: {
text: yAxisLabel
const data = rows.slice(0, 100) },
if (!schema || !data.length) { labels: {
return null formatter: formatters[yAxisUnits]
}
// Initialise default chart
let builder = new ApexOptionsBuilder()
.type("bar")
.title(title)
.width(width)
.height(height)
.xLabel(xAxisLabel)
.yLabel(yAxisLabel)
.dataLabels(dataLabels)
.animate(animate)
.legend(legend)
.stacked(stacked)
.palette(palette)
.horizontal(horizontal)
.colors(customColor ? colors : null)
// Add data
if (schema[labelColumn]) {
const labelFieldType = schema[labelColumn].type
if (horizontal) {
builder = builder.xUnits(yAxisUnits)
} else {
builder = builder.yUnits(yAxisUnits)
} }
} }
const series = (valueColumns ?? []).map(column => ({ }
const getSeries = (datasource, valueColumns = []) => {
const rows = datasource.rows ?? [];
return valueColumns.map(column => ({
name: column, name: column,
data: data.map(row => { data: rows.map(row => {
const value = get(row, column); return row?.[column]
}),
if (schema?.[column]?.type === 'datetime') { }))
return Date.parse(value)
} }
if (Array.isArray(value)) { const getCategories = (datasource, labelColumn) => {
return null; const rows = datasource.rows ?? [];
}
if (Number.isNaN(parseInt(value, 10))) { return rows.map(row => {
return null; 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 ""
} }
return value; return value;
}), })
}))
builder = builder.series(series)
builder = builder.xCategories(data.map(row => {
const value = row[labelColumn]
if (schema[labelColumn]?.type === 'datetime') {
const dateString = (new Date(Date.parse(value))).toLocaleDateString()
console.log(value)
console.log(dateString)
console.log()
return dateString
} }
return value ?? "" $: console.log("opt", options);
}))
// Build chart options
return builder.getOptions()
}
</script> </script>
<ApexChart {options} /> <ApexChart {options} />