2020-07-20 17:25:50 +02:00
|
|
|
<script>
|
2020-07-29 17:35:44 +02:00
|
|
|
import { getColorSchema, notNull } from "./utils.js"
|
2020-08-24 17:26:00 +02:00
|
|
|
import fetchData from "../fetchData.js"
|
2020-08-04 17:21:51 +02:00
|
|
|
import Legend from "./Legend.svelte"
|
2020-07-29 17:35:44 +02:00
|
|
|
import britecharts from "britecharts"
|
|
|
|
import { onMount } from "svelte"
|
2020-08-26 18:03:30 +02:00
|
|
|
import { isEmpty } from "lodash/fp"
|
2020-07-20 17:25:50 +02:00
|
|
|
|
2020-07-29 17:35:44 +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
|
2020-07-29 17:35:44 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-07-29 17:35:44 +02:00
|
|
|
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 = []
|
2020-08-24 17:26:00 +02:00
|
|
|
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
|
2020-07-29 17:35:44 +02:00
|
|
|
export let margin = null
|
2020-07-20 17:25:50 +02:00
|
|
|
|
2020-07-29 17:35:44 +02:00
|
|
|
export let centeredTextFunction = null
|
2020-07-20 17:25:50 +02:00
|
|
|
export let externalRadius = 25
|
2020-07-29 17:35:44 +02:00
|
|
|
export let percentageFormat = null
|
2020-07-20 17:25:50 +02:00
|
|
|
export let hasFixedHighlightedSlice = false
|
|
|
|
export let hasLastHoverSliceHighlighted = false
|
2020-07-29 17:35:44 +02:00
|
|
|
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 () => {
|
2020-07-29 17:35:44 +02:00
|
|
|
if (chart) {
|
2020-08-26 18:03:30 +02:00
|
|
|
if (!isEmpty(datasource)) {
|
2020-08-24 17:26:00 +02:00
|
|
|
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
|
|
|
|
2020-07-29 17:35:44 +02:00
|
|
|
chart.emptyDataConfig({
|
|
|
|
showEmptySlice: true,
|
2020-08-07 13:57:07 +02:00
|
|
|
emptySliceColor: "#F0F0F0",
|
2020-07-29 17:35:44 +02:00
|
|
|
})
|
2020-08-07 15:28:55 +02:00
|
|
|
|
2020-07-29 17:35:44 +02:00
|
|
|
chartContainer = select(`.${chartClass}`)
|
|
|
|
bindChartUIProps()
|
|
|
|
bindChartEvents()
|
2020-08-07 13:57:07 +02:00
|
|
|
chartContainer.datum(data).call(chart)
|
2020-07-29 17:35:44 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-07 15:28:55 +02:00
|
|
|
function checkAndReformatData(data) {
|
|
|
|
let _data = [...data]
|
|
|
|
|
2020-08-26 18:03:30 +02:00
|
|
|
if (valueKey && valueKey !== "quantity") {
|
2020-08-07 15:28:55 +02:00
|
|
|
_data = reformatDataKey(_data, valueKey, "quantity")
|
|
|
|
}
|
|
|
|
|
2020-08-26 18:03:30 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:35:44 +02:00
|
|
|
function bindChartUIProps() {
|
2020-08-05 17:57:54 +02:00
|
|
|
chart.percentageFormat(".0f")
|
|
|
|
|
2020-07-29 17:35:44 +02:00
|
|
|
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`)
|
2020-07-29 17:35:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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-07-29 17:35:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>
|