1
0
Fork 0
mirror of synced 2024-09-10 06:26:02 +12:00

Datetime working now on bar charts i thjink

This commit is contained in:
Gerard Burns 2024-04-18 08:08:45 +01:00
parent 8d77bb016f
commit 257fde1481
3 changed files with 48 additions and 22 deletions

View file

@ -1655,7 +1655,7 @@
{ {
"type": "select", "type": "select",
"label": "Format", "label": "Format",
"key": "yAxisUnits", "key": "valueUnits",
"options": ["Default", "Thousands", "Millions"], "options": ["Default", "Thousands", "Millions"],
"defaultValue": "Default" "defaultValue": "Default"
}, },
@ -1813,7 +1813,7 @@
{ {
"type": "select", "type": "select",
"label": "Format", "label": "Format",
"key": "yAxisUnits", "key": "valueUnits",
"options": ["Default", "Thousands", "Millions"], "options": ["Default", "Thousands", "Millions"],
"defaultValue": "Default" "defaultValue": "Default"
}, },
@ -1966,7 +1966,7 @@
{ {
"type": "select", "type": "select",
"label": "Format", "label": "Format",
"key": "yAxisUnits", "key": "valueUnits",
"options": ["Default", "Thousands", "Millions"], "options": ["Default", "Thousands", "Millions"],
"defaultValue": "Default" "defaultValue": "Default"
}, },
@ -2410,7 +2410,7 @@
{ {
"type": "select", "type": "select",
"label": "Format", "label": "Format",
"key": "yAxisUnits", "key": "valueUnits",
"options": ["Default", "Thousands", "Millions"], "options": ["Default", "Thousands", "Millions"],
"defaultValue": "Default" "defaultValue": "Default"
}, },
@ -5341,7 +5341,7 @@
{ {
"type": "select", "type": "select",
"label": "Format", "label": "Format",
"key": "yAxisUnits", "key": "valueUnits",
"options": ["Default", "Thousands", "Millions"], "options": ["Default", "Thousands", "Millions"],
"defaultValue": "Default" "defaultValue": "Default"
}, },
@ -5436,7 +5436,7 @@
{ {
"type": "select", "type": "select",
"label": "Format", "label": "Format",
"key": "yAxisUnits", "key": "valueUnits",
"options": ["Default", "Thousands", "Millions"], "options": ["Default", "Thousands", "Millions"],
"defaultValue": "Default" "defaultValue": "Default"
}, },
@ -5485,7 +5485,7 @@
{ {
"type": "select", "type": "select",
"label": "Format", "label": "Format",
"key": "yAxisUnits", "key": "valueUnits",
"options": ["Default", "Thousands", "Millions"], "options": ["Default", "Thousands", "Millions"],
"defaultValue": "Default" "defaultValue": "Default"
}, },
@ -5567,7 +5567,7 @@
{ {
"type": "select", "type": "select",
"label": "Format", "label": "Format",
"key": "yAxisUnits", "key": "valueUnits",
"options": ["Default", "Thousands", "Millions"], "options": ["Default", "Thousands", "Millions"],
"defaultValue": "Default" "defaultValue": "Default"
}, },

View file

@ -8,6 +8,9 @@
const component = getContext("component") const component = getContext("component")
export let options export let options
// 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 this here to provide a buffer.
$: optionsCopy = cloneDeep(options);
/* /*
export let invalid = false export let invalid = false
@ -27,8 +30,8 @@
return parsedValue return parsedValue
} }
const parseOptions = (options) => { const parseOptions = (optionsCopy) => {
const parsedOptions = { series: [], ...cloneDeep(options)} const parsedOptions = { series: [], ...cloneDeep(optionsCopy)}
// Object form of series, used by most charts // Object form of series, used by most charts
if (parsedOptions.series.some(entry => Array.isArray(entry?.data))) { if (parsedOptions.series.some(entry => Array.isArray(entry?.data))) {
@ -41,7 +44,7 @@
return parsedOptions; return parsedOptions;
} }
$: parsedOptions = parseOptions(options); $: parsedOptions = parseOptions(optionsCopy);
*/ */
let chartElement; let chartElement;
@ -58,7 +61,7 @@
const renderChart = async (newChartElement) => { const renderChart = async (newChartElement) => {
try { try {
await chart?.destroy() await chart?.destroy()
chart = new ApexCharts(newChartElement, options) chart = new ApexCharts(newChartElement, optionsCopy)
await chart.render() await chart.render()
} catch(e) { } catch(e) {
//console.log(e) //console.log(e)
@ -69,17 +72,17 @@
return true return true
} }
$: noData = options == null || options?.series?.length === 0 $: noData = optionsCopy == null || optionsCopy?.series?.length === 0
$: hide = noData || !seriesValid $: hide = noData || !seriesValid
// Call render chart upon changes to hide, as apex charts has issues with rendering upon changes automatically // Call render chart upon changes to hide, as apex charts has issues with rendering upon changes automatically
// if the chart is hidden. // if the chart is hidden.
$: renderChart(chartElement, hide) $: renderChart(chartElement, hide)
$: updateChart(options) $: updateChart(optionsCopy)
$: seriesValid = isSeriesValid(options?.series || []) $: seriesValid = isSeriesValid(optionsCopy?.series || [])
</script> </script>
{#key options?.customColor} {#key optionsCopy?.customColor}
<div class:hide use:styleable={$component.styles} bind:this={chartElement} /> <div class:hide use:styleable={$component.styles} bind:this={chartElement} />
{#if $builderStore.inBuilder && noData } {#if $builderStore.inBuilder && noData }
<div class="component-placeholder" use:styleable={{ ...$component.styles, normal: {}, custom: null, empty: true }}> <div class="component-placeholder" use:styleable={{ ...$component.styles, normal: {}, custom: null, empty: true }}>

View file

@ -16,7 +16,7 @@
export let animate export let animate
export let legend export let legend
export let stacked export let stacked
export let yAxisUnits export let valueUnits
export let palette export let palette
export let c1, c2, c3, c4, c5 export let c1, c2, c3, c4, c5
export let horizontal export let horizontal
@ -25,13 +25,17 @@
["Default"]: val => val, ["Default"]: val => val,
["Thousands"]: val => `${Math.round(val / 1000)}K`, ["Thousands"]: val => `${Math.round(val / 1000)}K`,
["Millions"]: val => `${Math.round(val / 1000000)}M`, ["Millions"]: val => `${Math.round(val / 1000000)}M`,
["Datetime"]: val => (new Date(val)).toLocaleString()
} }
$: series = getSeries(dataProvider, valueColumns) $: series = getSeries(dataProvider, valueColumns)
$: categories = getCategories(dataProvider, labelColumn); $: categories = getCategories(dataProvider, labelColumn);
$: dataType = dataProvider?.schema?.[labelColumn]?.type === 'datetime' ? $: labelType = dataProvider?.schema?.[labelColumn]?.type === 'datetime' ?
"datetime" : "category" "datetime" : "category"
$: formatter = getFormatter(labelType, valueUnits)
$: xAxisFormatter = getFormatter(labelType, valueUnits, horizontal, "x")
$: yAxisFormatter = getFormatter(labelType, valueUnits, horizontal, "y")
$: options = { $: options = {
series, series,
@ -68,19 +72,29 @@
enabled: false, enabled: false,
}, },
}, },
plotOptions: {
bar: {
horizontal
}
},
// We can just always provide the categories to the xaxis and horizontal mode automatically handles "tranposing" the categories to the yaxis, but certain things like labels need to be manually put on a certain axis based on the selected mode. Titles do not need to be handled this way, they are exposed to the user as "X axis" and Y Axis" so flipping them would be confusing.
xaxis: { xaxis: {
type: dataType, type: labelType,
categories, categories,
labels: {
formatter: xAxisFormatter
},
title: { title: {
text: xAxisLabel text: xAxisLabel
} }
}, },
// bar charts in horizontal mode don't have a default setting for parsing the labels of dates, and will just spit out the unix epoch value. It also doesn't seem to respect any date based formatting properties passed in. So we'll just manualy format the labels, the chart still sorts the dates correctly in any case
yaxis: { yaxis: {
labels: {
formatter: yAxisFormatter
},
title: { title: {
text: yAxisLabel text: yAxisLabel
},
labels: {
formatter: formatters[yAxisUnits]
} }
} }
} }
@ -111,6 +125,15 @@
}) })
} }
const getFormatter = (labelType, valueUnits, horizontal, axis) => {
const isLabelAxis = (axis === "y" && horizontal) || axis === "x" && !horizontal
if (labelType === "datetime" && isLabelAxis) {
return formatters["Datetime"]
}
return formatters[valueUnits]
}
$: console.log("opt", options); $: console.log("opt", options);
</script> </script>