2020-07-30 17:39:56 +02:00
|
|
|
<script>
|
2020-08-18 16:45:44 +02:00
|
|
|
import { getColorSchema, getChartGradient, notNull, hasProp } from "./utils"
|
2020-08-10 16:33:32 +02:00
|
|
|
import Tooltip from "./Tooltip.svelte"
|
2020-08-24 17:26:00 +02:00
|
|
|
import fetchData from "../fetchData.js"
|
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-08-26 18:03:30 +02:00
|
|
|
import { isEmpty } from "lodash/fp"
|
2020-07-30 17:39:56 +02:00
|
|
|
|
|
|
|
const _id = shortid.generate()
|
|
|
|
|
|
|
|
const chart = britecharts.groupedBar()
|
|
|
|
const chartClass = `groupedbar-container-${_id}`
|
|
|
|
const legendClass = `legend-container-${_id}`
|
|
|
|
|
2020-08-18 16:45:44 +02:00
|
|
|
let tooltip = britecharts.tooltip()
|
2020-07-30 17:39:56 +02:00
|
|
|
let tooltipContainer
|
|
|
|
let chartElement = null
|
|
|
|
let chartContainer = null
|
|
|
|
|
|
|
|
export let customClick = null
|
|
|
|
|
2020-08-10 16:33:32 +02:00
|
|
|
let data = []
|
2020-08-26 18:03:30 +02:00
|
|
|
export let datasource = {}
|
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-18 16:45:44 +02:00
|
|
|
export let tooltipTitle = ""
|
2020-08-10 16:33:32 +02:00
|
|
|
|
2020-08-18 16:45:44 +02:00
|
|
|
const schemaIsValid = () =>
|
|
|
|
(hasProp(data, "name") || hasProp(data, nameLabel)) &&
|
|
|
|
(hasProp(data, "group") || hasProp(data, groupLabel)) &&
|
|
|
|
(hasProp(data, "value") || hasProp(data, valueLabel))
|
|
|
|
|
2020-08-04 14:55:26 +02:00
|
|
|
onMount(async () => {
|
2020-08-26 18:03:30 +02:00
|
|
|
if (!isEmpty(datasource)) {
|
2020-08-24 17:26:00 +02:00
|
|
|
data = await fetchData(datasource)
|
2020-08-18 16:45:44 +02:00
|
|
|
if (schemaIsValid()) {
|
|
|
|
chartContainer = select(`.${chartClass}`)
|
|
|
|
bindChartUIProps()
|
|
|
|
bindChartEvents()
|
|
|
|
chartContainer.datum(data).call(chart)
|
|
|
|
bindTooltip()
|
|
|
|
} else {
|
|
|
|
console.error(
|
|
|
|
"Grouped bar - Please provide valid name, value and group labels"
|
|
|
|
)
|
2020-08-04 14:55:26 +02:00
|
|
|
}
|
2020-07-30 17:39:56 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-18 16:45:44 +02:00
|
|
|
function bindTooltip() {
|
|
|
|
tooltipContainer = select(`.${chartClass} .metadata-group`)
|
|
|
|
tooltip.topicLabel("values")
|
|
|
|
tooltip.shouldShowDateInTitle(false)
|
|
|
|
tooltipContainer.datum([]).call(tooltip)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2020-08-28 15:47:03 +02:00
|
|
|
if (notNull(tooltipTitle)) {
|
|
|
|
tooltip.title(tooltipTitle)
|
|
|
|
} else if (datasource.label) {
|
|
|
|
tooltip.title(datasource.label)
|
|
|
|
}
|
2020-07-30 17:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function bindChartEvents() {
|
|
|
|
if (customClick) {
|
|
|
|
chart.on("customClick", customClick)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
chart.on("customMouseMove", tooltip.update)
|
|
|
|
chart.on("customMouseOut", tooltip.hide)
|
|
|
|
chart.on("customMouseOver", tooltip.show)
|
2020-07-30 17:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$: colorSchema = getColorSchema(color)
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div bind:this={chartElement} class={chartClass} />
|