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-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-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
|
|
|
|
export let model
|
|
|
|
|
|
|
|
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-04 11:02:58 +02:00
|
|
|
export let data = model ? $store[model] : []
|
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-05 17:57:54 +02:00
|
|
|
export let useLegend = true
|
|
|
|
export let horizontalLegend = false
|
|
|
|
export let legendWidth = null
|
|
|
|
export let legendHeight = null
|
2020-07-26 12:54:55 +02:00
|
|
|
|
2020-08-04 11:02:58 +02:00
|
|
|
async function fetchData() {
|
|
|
|
const FETCH_RECORDS_URL = `/api/views/all_${model}`
|
|
|
|
const response = await _bb.api.get(FETCH_RECORDS_URL)
|
|
|
|
if (response.status === 200) {
|
|
|
|
const json = await response.json()
|
|
|
|
store.update(state => {
|
|
|
|
state[model] = json
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
throw new Error("Failed to fetch records.", response)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 14:55:26 +02:00
|
|
|
onMount(async () => {
|
2020-07-29 17:35:44 +02:00
|
|
|
if (chart) {
|
2020-08-05 15:19:56 +02:00
|
|
|
if (model) {
|
|
|
|
await fetchData()
|
|
|
|
}
|
2020-08-04 14:55:26 +02:00
|
|
|
|
2020-07-29 17:35:44 +02:00
|
|
|
chart.emptyDataConfig({
|
|
|
|
showEmptySlice: true,
|
|
|
|
emptySliceColor: "#000000",
|
|
|
|
})
|
|
|
|
chartContainer = select(`.${chartClass}`)
|
|
|
|
bindChartUIProps()
|
|
|
|
bindChartEvents()
|
2020-08-04 14:55:26 +02:00
|
|
|
chartContainer.datum(_data).call(chart)
|
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-05 17:57:54 +02:00
|
|
|
chartContainer.datum(_data).call(chart)
|
|
|
|
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)
|
|
|
|
chartContainer.datum(_data).call(chart)
|
|
|
|
}
|
|
|
|
|
2020-08-04 17:21:51 +02:00
|
|
|
$: _data = model ? $store[model] : data
|
2020-08-05 15:19:56 +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} />
|
|
|
|
{#if useLegend}
|
|
|
|
<Legend
|
|
|
|
bind:legend={legendChart}
|
|
|
|
{colorSchema}
|
|
|
|
useLegend
|
|
|
|
isHorizontal={horizontalLegend}
|
|
|
|
width={legendWidth || width}
|
|
|
|
height={legendHeight}
|
|
|
|
{chartClass}
|
|
|
|
data={_data} />
|
|
|
|
{/if}
|
|
|
|
</div>
|