budibase/packages/standard-components/src/Chart/Donut.svelte

214 lines
5.6 KiB
Svelte
Raw Normal View History

2020-07-20 17:25:50 +02:00
<script>
import { getColorSchema, notNull } from "./utils.js"
import fetchData from "../fetchData.js"
2020-08-04 17:21:51 +02:00
import Legend from "./Legend.svelte"
import britecharts from "britecharts"
import { onMount } from "svelte"
import { isEmpty } from "lodash/fp"
2020-07-20 17:25:50 +02:00
import { select } from "d3-selection"
import shortid from "shortid"
const _id = shortid.generate()
const chart = britecharts.donut()
const chartClass = `donut-container-${_id}`
const legendClass = `legend-container-${_id}`
2020-08-05 15:19:56 +02:00
let legendChart
let chartElement = null
let chartContainer = null
2020-08-05 17:57:54 +02:00
let chartSvgWidth = 0
let chartSvg = null
2020-08-04 11:02:58 +02:00
export let _bb
let store = _bb.store
export let customMouseMove = null
export let customClick = null
export let orderingFunction = null
2020-07-20 17:25:50 +02:00
2020-08-07 13:57:07 +02:00
let data = []
export let datasource = {}
2020-07-26 12:54:55 +02:00
export let color = "britecharts"
2020-07-20 17:25:50 +02:00
export let height = 200
export let width = 200
export let margin = null
2020-07-20 17:25:50 +02:00
export let centeredTextFunction = null
2020-07-20 17:25:50 +02:00
export let externalRadius = 25
export let percentageFormat = null
2020-07-20 17:25:50 +02:00
export let hasFixedHighlightedSlice = false
export let hasLastHoverSliceHighlighted = false
export let hasHoverAnimation = true
export let highlightSliceById = null
export let numberFormat = null
2020-07-20 17:25:50 +02:00
export let internalRadius = 25
export let isAnimated = true
export let radiusHoverOffset = 0
2020-08-07 15:28:55 +02:00
export let nameKey = null
export let valueKey = null
2020-08-07 13:57:07 +02:00
// export let useLegend = true
2020-08-05 17:57:54 +02:00
export let horizontalLegend = false
export let legendWidth = null
export let legendHeight = null
2020-07-26 12:54:55 +02:00
2020-08-04 14:55:26 +02:00
onMount(async () => {
if (chart) {
if (!isEmpty(datasource)) {
let _data = await fetchData(datasource)
data = checkAndReformatData(_data)
2020-08-07 15:46:00 +02:00
if (data.length === 0) {
console.error(
"Donut - please provide a valid name and value field for the chart"
)
}
2020-08-05 15:19:56 +02:00
}
2020-08-04 14:55:26 +02:00
chart.emptyDataConfig({
showEmptySlice: true,
2020-08-07 13:57:07 +02:00
emptySliceColor: "#F0F0F0",
})
2020-08-07 15:28:55 +02:00
chartContainer = select(`.${chartClass}`)
bindChartUIProps()
bindChartEvents()
2020-08-07 13:57:07 +02:00
chartContainer.datum(data).call(chart)
}
})
2020-08-07 15:28:55 +02:00
function checkAndReformatData(data) {
let _data = [...data]
if (valueKey && valueKey !== "quantity") {
2020-08-07 15:28:55 +02:00
_data = reformatDataKey(_data, valueKey, "quantity")
}
if (nameKey && nameKey !== "name") {
2020-08-07 15:28:55 +02:00
_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 => {
2020-08-07 15:46:00 +02:00
let clonedRecord = { ...d }
if (clonedRecord[formatKey]) {
delete clonedRecord[formatKey]
}
let value = clonedRecord[dataKey]
2020-08-07 15:28:55 +02:00
if (!ignoreList.includes(dataKey)) {
2020-08-07 15:46:00 +02:00
delete clonedRecord[dataKey]
2020-08-07 15:28:55 +02:00
}
2020-08-07 15:46:00 +02:00
clonedRecord[formatKey] = value
return clonedRecord
2020-08-07 15:28:55 +02:00
})
} else {
return data
}
}
function bindChartUIProps() {
2020-08-05 17:57:54 +02:00
chart.percentageFormat(".0f")
if (notNull(color)) {
chart.colorSchema(getColorSchema(color))
}
if (notNull(height)) {
chart.height(height)
}
if (notNull(width)) {
chart.width(width)
}
if (notNull(margin)) {
chart.margin(margin)
}
if (notNull(centeredTextFunction)) {
chart.centeredTextFunction(centeredTextFunction)
}
if (notNull(externalRadius)) {
chart.externalRadius(externalRadius)
}
if (notNull(percentageFormat)) {
chart.percentageFormat(percentageFormat)
}
if (notNull(hasFixedHighlightedSlice)) {
chart.hasFixedHighlightedSlice(hasFixedHighlightedSlice)
}
if (notNull(hasLastHoverSliceHighlighted)) {
chart.hasLastHoverSliceHighlighted(hasLastHoverSliceHighlighted)
}
if (notNull(hasHoverAnimation)) {
chart.hasHoverAnimation(hasHoverAnimation)
}
if (notNull(highlightSliceById)) {
chart.highlightSliceById(highlightSliceById)
}
if (notNull(numberFormat)) {
chart.numberFormat(numberFormat)
}
if (notNull(internalRadius)) {
chart.internalRadius(internalRadius)
}
if (notNull(isAnimated)) {
chart.isAnimated(isAnimated)
}
if (notNull(radiusHoverOffset)) {
chart.radiusHoverOffset(radiusHoverOffset)
}
if (notNull(orderingFunction)) {
chart.orderingFunction(orderingFunction)
}
2020-08-07 13:57:07 +02:00
chartContainer.datum(data).call(chart)
2020-08-05 17:57:54 +02:00
chartSvg = document.querySelector(`.${chartClass} .britechart`)
}
function bindChartEvents() {
if (customClick) {
chart.on("customClick", customClick)
}
if (customMouseMove) {
chart.on("customMouseMove", customMouseMove)
}
2020-08-05 15:19:56 +02:00
if (legendChart) {
chart.on("customMouseOut", function() {
legendChart.clearHighlight()
})
chart.on("customMouseOver", function(data) {
legendChart.highlight(data.data.id)
})
}
}
2020-08-05 17:57:54 +02:00
$: if (!width && chartSvg) {
width = chartSvg.clientWidth
chart.width(width)
2020-08-07 13:57:07 +02:00
chartContainer.datum(data).call(chart)
2020-08-05 17:57:54 +02:00
}
2020-07-26 12:54:55 +02:00
$: colorSchema = getColorSchema(color)
2020-07-20 17:25:50 +02:00
</script>
2020-08-05 17:57:54 +02:00
<div>
<div bind:this={chartElement} class={chartClass} />
2020-08-07 15:28:55 +02:00
{#if data.length > 0}
<Legend
bind:legend={legendChart}
{colorSchema}
useLegend
isHorizontal={horizontalLegend}
width={legendWidth || width}
height={legendHeight}
{chartClass}
{data} />
{/if}
2020-08-05 17:57:54 +02:00
</div>