2020-07-30 17:39:56 +02:00
|
|
|
<script>
|
|
|
|
import { getColorSchema, getChartGradient, notNull } from "./utils"
|
2020-08-10 16:33:32 +02:00
|
|
|
import Tooltip from "./Tooltip.svelte"
|
2020-07-30 17:39:56 +02:00
|
|
|
import britecharts from "britecharts"
|
|
|
|
import { onMount } from "svelte"
|
2020-08-10 16:33:32 +02:00
|
|
|
import { select } from "d3-selection"
|
|
|
|
import shortid from "shortid"
|
2020-07-30 17:39:56 +02:00
|
|
|
/*
|
|
|
|
ISSUES
|
|
|
|
- Renders but seems to be a problem with tooltip hover
|
|
|
|
*/
|
|
|
|
|
|
|
|
const _id = shortid.generate()
|
|
|
|
|
2020-08-04 14:55:26 +02:00
|
|
|
export let _bb
|
|
|
|
export let model
|
|
|
|
|
|
|
|
let store = _bb.store
|
|
|
|
|
2020-07-30 17:39:56 +02:00
|
|
|
const chart = britecharts.groupedBar()
|
|
|
|
const chartClass = `groupedbar-container-${_id}`
|
|
|
|
const legendClass = `legend-container-${_id}`
|
|
|
|
|
|
|
|
let tooltip
|
|
|
|
let tooltipContainer
|
|
|
|
let chartElement = null
|
|
|
|
let chartContainer = null
|
|
|
|
|
|
|
|
export let customMouseOver = () => tooltip.show()
|
|
|
|
export let customMouseMove = (dataPoint, topicColorMap, dataPointXPosition) =>
|
|
|
|
tooltip.update(dataPoint, topicColorMap, dataPointXPosition)
|
|
|
|
export let customMouseOut = () => tooltip.hide()
|
|
|
|
export let customClick = null
|
|
|
|
|
2020-08-10 16:33:32 +02:00
|
|
|
let data = []
|
2020-07-30 17:39:56 +02:00
|
|
|
export let color = "britecharts"
|
|
|
|
export let height = 200
|
|
|
|
export let width = 200
|
2020-08-10 16:33:32 +02:00
|
|
|
export let margin = null
|
2020-07-30 17:39:56 +02:00
|
|
|
export let aspectRatio = null
|
|
|
|
export let grid = null
|
|
|
|
export let groupLabel = null
|
|
|
|
export let isAnimated = null
|
|
|
|
export let isHorizontal = null
|
|
|
|
export let nameLabel = null
|
|
|
|
export let valueLabel = null
|
|
|
|
export let valueLabelFormat = null
|
|
|
|
export let xTicks = null
|
|
|
|
export let yAxisLabel = null
|
|
|
|
export let yAxisLabelOffset = null
|
|
|
|
export let yTicks = null
|
|
|
|
export let yTickTextOffset = null
|
2020-08-10 16:33:32 +02:00
|
|
|
|
|
|
|
$: console.log("DATA", data)
|
|
|
|
|
|
|
|
let chartDrawn = false
|
2020-07-30 17:39:56 +02:00
|
|
|
|
2020-08-04 14:55:26 +02:00
|
|
|
onMount(async () => {
|
2020-07-30 17:39:56 +02:00
|
|
|
if (chart) {
|
2020-08-04 14:55:26 +02:00
|
|
|
if (model) {
|
|
|
|
await fetchData()
|
2020-08-10 16:33:32 +02:00
|
|
|
data = $store[model]
|
2020-08-04 14:55:26 +02:00
|
|
|
}
|
2020-07-30 17:39:56 +02:00
|
|
|
chartContainer = select(`.${chartClass}`)
|
|
|
|
bindChartUIProps()
|
2020-08-10 16:33:32 +02:00
|
|
|
bindChartEvents()
|
2020-07-30 17:39:56 +02:00
|
|
|
chartContainer.datum(data).call(chart)
|
2020-08-10 16:33:32 +02:00
|
|
|
chartDrawn = true
|
2020-07-30 17:39:56 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-04 14:55:26 +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-07-30 17:39:56 +02:00
|
|
|
function bindChartUIProps() {
|
|
|
|
if (notNull(color)) {
|
|
|
|
chart.colorSchema(colorSchema)
|
|
|
|
}
|
|
|
|
if (notNull(height)) {
|
|
|
|
chart.height(height)
|
|
|
|
}
|
|
|
|
if (notNull(width)) {
|
|
|
|
chart.width(width)
|
|
|
|
}
|
2020-08-10 16:33:32 +02:00
|
|
|
if (notNull(margin)) {
|
|
|
|
chart.margin(margin)
|
|
|
|
}
|
2020-07-30 17:39:56 +02:00
|
|
|
if (notNull(aspectRatio)) {
|
|
|
|
chart.aspectRatio(aspectRatio)
|
|
|
|
}
|
|
|
|
if (notNull(grid)) {
|
|
|
|
chart.grid(grid)
|
|
|
|
}
|
|
|
|
if (notNull(groupLabel)) {
|
|
|
|
chart.groupLabel(groupLabel)
|
|
|
|
}
|
|
|
|
if (notNull(isAnimated)) {
|
|
|
|
chart.isAnimated(isAnimated)
|
|
|
|
}
|
|
|
|
if (notNull(isHorizontal)) {
|
|
|
|
chart.isHorizontal(isHorizontal)
|
|
|
|
}
|
|
|
|
if (notNull(nameLabel)) {
|
|
|
|
chart.nameLabel(nameLabel)
|
|
|
|
}
|
|
|
|
if (notNull(valueLabel)) {
|
|
|
|
chart.valueLabel(valueLabel)
|
|
|
|
}
|
|
|
|
if (notNull(valueLabelFormat)) {
|
|
|
|
chart.valueLabelFormat(valueLabelFormat)
|
|
|
|
}
|
|
|
|
if (notNull(xTicks)) {
|
|
|
|
chart.xTicks(xTicks)
|
|
|
|
}
|
|
|
|
if (notNull(yAxisLabel)) {
|
|
|
|
chart.yAxisLabel(yAxisLabel)
|
|
|
|
}
|
|
|
|
if (notNull(yAxisLabelOffset)) {
|
|
|
|
chart.yAxisLabelOffset(yAxisLabelOffset)
|
|
|
|
}
|
|
|
|
if (notNull(yTicks)) {
|
|
|
|
chart.yTicks(yTicks)
|
|
|
|
}
|
|
|
|
if (notNull(yTickTextOffset)) {
|
|
|
|
chart.yTickTextOffset(yTickTextOffset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bindChartEvents() {
|
|
|
|
if (customClick) {
|
|
|
|
chart.on("customClick", customClick)
|
|
|
|
}
|
|
|
|
if (customMouseMove) {
|
|
|
|
chart.on("customMouseMove", customMouseMove)
|
|
|
|
}
|
|
|
|
if (customMouseOut) {
|
|
|
|
chart.on("customMouseOut", customMouseOut)
|
|
|
|
}
|
|
|
|
if (customMouseOver) {
|
|
|
|
chart.on("customMouseOver", customMouseOver)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 14:55:26 +02:00
|
|
|
$: _data = model ? $store[model] : data
|
|
|
|
|
2020-07-30 17:39:56 +02:00
|
|
|
$: colorSchema = getColorSchema(color)
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div bind:this={chartElement} class={chartClass} />
|
2020-08-10 16:33:32 +02:00
|
|
|
{#if chartDrawn}
|
2020-08-12 11:02:36 +02:00
|
|
|
<Tooltip bind:tooltip {nameLabel} {valueLabel} {chartClass} />
|
2020-07-30 17:39:56 +02:00
|
|
|
{/if}
|