Add horizontal bar chart setting

This commit is contained in:
Andrew Kingston 2021-09-09 19:28:37 +01:00
parent b8f000092a
commit ad3cb0a2c4
4 changed files with 57 additions and 7 deletions

View File

@ -1230,6 +1230,12 @@
"key": "stacked", "key": "stacked",
"defaultValue": false "defaultValue": false
}, },
{
"type": "boolean",
"label": "Horizontal",
"key": "horizontal",
"defaultValue": false
},
{ {
"type": "boolean", "type": "boolean",
"label": "Data Labels", "label": "Data Labels",

View File

@ -1,6 +1,6 @@
export class ApexOptionsBuilder { export class ApexOptionsBuilder {
formatters = { formatters = {
["Default"]: val => Math.round(val * 100) / 100, ["Default"]: val => (isNaN(val) ? val : Math.round(val * 100) / 100),
["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`,
} }
@ -22,6 +22,11 @@ export class ApexOptionsBuilder {
enabled: false, enabled: false,
}, },
}, },
xaxis: {
labels: {
formatter: this.formatters.Default,
},
},
yaxis: { yaxis: {
labels: { labels: {
formatter: this.formatters.Default, formatter: this.formatters.Default,
@ -77,10 +82,14 @@ export class ApexOptionsBuilder {
return this.setOption(["yaxis", "title", "text"], label) return this.setOption(["yaxis", "title", "text"], label)
} }
categories(categories) { xCategories(categories) {
return this.setOption(["xaxis", "categories"], categories) return this.setOption(["xaxis", "categories"], categories)
} }
yCategories(categories) {
return this.setOption(["yaxis", "categories"], categories)
}
series(series) { series(series) {
return this.setOption(["series"], series) return this.setOption(["series"], series)
} }
@ -130,6 +139,13 @@ export class ApexOptionsBuilder {
return this.setOption(["labels"], labels) return this.setOption(["labels"], labels)
} }
xUnits(units) {
return this.setOption(
["xaxis", "labels", "formatter"],
this.formatters[units || "Default"]
)
}
yUnits(units) { yUnits(units) {
return this.setOption( return this.setOption(
["yaxis", "labels", "formatter"], ["yaxis", "labels", "formatter"],
@ -137,10 +153,24 @@ export class ApexOptionsBuilder {
) )
} }
clearXFormatter() {
delete this.options.xaxis.labels
return this
}
clearYFormatter() {
delete this.options.yaxis.labels
return this
}
xType(type) { xType(type) {
return this.setOption(["xaxis", "type"], type) return this.setOption(["xaxis", "type"], type)
} }
yType(type) {
return this.setOption(["yaxis", "type"], type)
}
yTooltip(yTooltip) { yTooltip(yTooltip) {
return this.setOption(["yaxis", "tooltip", "enabled"], yTooltip) return this.setOption(["yaxis", "tooltip", "enabled"], yTooltip)
} }

View File

@ -16,6 +16,7 @@
export let stacked export let stacked
export let yAxisUnits export let yAxisUnits
export let palette export let palette
export let horizontal
$: options = setUpChart(dataProvider) $: options = setUpChart(dataProvider)
@ -25,7 +26,7 @@
return null return null
} }
// Fatch data // Fetch data
const { schema, rows } = provider const { schema, rows } = provider
const reducer = row => (valid, column) => valid && row[column] != null const reducer = row => (valid, column) => valid && row[column] != null
const hasAllColumns = row => allCols.reduce(reducer(row), true) const hasAllColumns = row => allCols.reduce(reducer(row), true)
@ -46,14 +47,18 @@
.animate(animate) .animate(animate)
.legend(legend) .legend(legend)
.stacked(stacked) .stacked(stacked)
.yUnits(yAxisUnits)
.palette(palette) .palette(palette)
.horizontal(horizontal)
// Add data // Add data
let useDates = false let useDates = false
if (schema[labelColumn]) { if (schema[labelColumn]) {
const labelFieldType = schema[labelColumn].type const labelFieldType = schema[labelColumn].type
builder = builder.xType(labelFieldType) if (horizontal) {
builder = builder.yType(labelFieldType).xUnits(yAxisUnits)
} else {
builder = builder.xType(labelFieldType).yUnits(yAxisUnits)
}
useDates = labelFieldType === "datetime" useDates = labelFieldType === "datetime"
} }
const series = valueColumns.map(column => ({ const series = valueColumns.map(column => ({
@ -68,7 +73,14 @@
})) }))
builder = builder.series(series) builder = builder.series(series)
if (!useDates) { if (!useDates) {
builder = builder.categories(data.map(row => row[labelColumn])) builder = builder.xCategories(data.map(row => row[labelColumn]))
} else {
// Horizontal dates don't work anyway, but this is the correct logic
if (horizontal) {
builder = builder.clearYFormatter()
} else {
builder = builder.clearXFormatter()
}
} }
// Build chart options // Build chart options

View File

@ -77,7 +77,9 @@
})) }))
builder = builder.series(series) builder = builder.series(series)
if (!useDates) { if (!useDates) {
builder = builder.categories(data.map(row => row[labelColumn])) builder = builder.xCategories(data.map(row => row[labelColumn]))
} else {
builder = builder.clearXFormatter()
} }
// Build chart options // Build chart options