Fix for name and value fields donut

This commit is contained in:
cmack 2020-08-07 14:28:55 +01:00
parent bba3c90cf5
commit 4dbd635985
3 changed files with 60 additions and 13 deletions

View File

@ -476,6 +476,16 @@ export default {
"yellow", "yellow",
], ],
}, },
{
label: "Name Field",
key: "nameKey",
control: Input,
},
{
label: "Value Field",
key: "valueKey",
control: Input,
},
{ {
label: "External Radius", label: "External Radius",
key: "externalRadius", key: "externalRadius",

View File

@ -287,6 +287,8 @@
"hasLastHoverSliceHighlighted": "bool", "hasLastHoverSliceHighlighted": "bool",
"hasHoverAnimation": "bool", "hasHoverAnimation": "bool",
"numberFormat": "string", "numberFormat": "string",
"nameKey": "string",
"valueKey": "string",
"isAnimated": "bool", "isAnimated": "bool",
"externalRadius": "number", "externalRadius": "number",
"internalRadius": "number", "internalRadius": "number",

View File

@ -36,6 +36,8 @@
export let width = 200 export let width = 200
export let margin = null export let margin = null
$: console.log("DATA", data)
export let centeredTextFunction = null export let centeredTextFunction = null
export let externalRadius = 25 export let externalRadius = 25
export let percentageFormat = null export let percentageFormat = null
@ -47,6 +49,8 @@
export let internalRadius = 25 export let internalRadius = 25
export let isAnimated = true export let isAnimated = true
export let radiusHoverOffset = 0 export let radiusHoverOffset = 0
export let nameKey = null
export let valueKey = null
// export let useLegend = true // export let useLegend = true
export let horizontalLegend = false export let horizontalLegend = false
export let legendWidth = null export let legendWidth = null
@ -70,13 +74,14 @@
if (chart) { if (chart) {
if (model) { if (model) {
await fetchData() await fetchData()
data = model ? $store[model] : [] data = checkAndReformatData($store[model])
} }
chart.emptyDataConfig({ chart.emptyDataConfig({
showEmptySlice: true, showEmptySlice: true,
emptySliceColor: "#F0F0F0", emptySliceColor: "#F0F0F0",
}) })
chartContainer = select(`.${chartClass}`) chartContainer = select(`.${chartClass}`)
bindChartUIProps() bindChartUIProps()
bindChartEvents() bindChartEvents()
@ -84,6 +89,37 @@
} }
}) })
function checkAndReformatData(data) {
let _data = [...data]
if (valueKey) {
_data = reformatDataKey(_data, valueKey, "quantity")
}
if (nameKey) {
_data = reformatDataKey(_data, nameKey, "name")
}
return _data.every(d => d.quantity) && _data.every(d => d.name) ? _data : []
}
function reformatDataKey(data = [], dataKey = null, formatKey = null) {
let ignoreList = ["_id", "_rev", "id"]
if (dataKey && data.every(d => d[dataKey])) {
return data.map(d => {
let obj = { ...d }
let value = obj[dataKey]
if (!ignoreList.includes(dataKey)) {
delete obj[dataKey]
}
obj[formatKey] = value
return obj
})
} else {
return data
}
}
function bindChartUIProps() { function bindChartUIProps() {
chart.percentageFormat(".0f") chart.percentageFormat(".0f")
@ -160,24 +196,23 @@
$: if (!width && chartSvg) { $: if (!width && chartSvg) {
width = chartSvg.clientWidth width = chartSvg.clientWidth
chart.width(width) chart.width(width)
debugger
chartContainer.datum(data).call(chart) chartContainer.datum(data).call(chart)
} }
// $: _data = model ? $store[model] : data
$: colorSchema = getColorSchema(color) $: colorSchema = getColorSchema(color)
</script> </script>
<div> <div>
<div bind:this={chartElement} class={chartClass} /> <div bind:this={chartElement} class={chartClass} />
<Legend {#if data.length > 0}
bind:legend={legendChart} <Legend
{colorSchema} bind:legend={legendChart}
useLegend {colorSchema}
isHorizontal={horizontalLegend} useLegend
width={legendWidth || width} isHorizontal={horizontalLegend}
height={legendHeight} width={legendWidth || width}
{chartClass} height={legendHeight}
{data} /> {chartClass}
{data} />
{/if}
</div> </div>