2020-07-26 12:54:55 +02:00
|
|
|
<script>
|
2020-08-18 16:45:44 +02:00
|
|
|
import {
|
|
|
|
getColorSchema,
|
|
|
|
getChartGradient,
|
|
|
|
notNull,
|
|
|
|
hasProp,
|
|
|
|
} from "./utils.js"
|
2020-07-28 15:19:46 +02:00
|
|
|
import britecharts from "britecharts"
|
2020-08-24 17:26:00 +02:00
|
|
|
import fetchData from "../fetchData.js"
|
2020-07-28 15:19:46 +02:00
|
|
|
import { onMount } from "svelte"
|
2020-08-26 18:03:30 +02:00
|
|
|
import { isEmpty } from "lodash/fp"
|
2020-07-26 12:54:55 +02:00
|
|
|
|
2020-07-28 15:19:46 +02:00
|
|
|
import { select } from "d3-selection"
|
|
|
|
import shortid from "shortid"
|
2020-07-26 12:54:55 +02:00
|
|
|
|
|
|
|
let tooltip
|
2020-07-28 15:19:46 +02:00
|
|
|
const _id = shortid.generate()
|
|
|
|
const chart = britecharts.bar()
|
|
|
|
const chartClass = `bar-container-${_id}`
|
|
|
|
|
|
|
|
let chartElement = null
|
|
|
|
let chartContainer = null
|
|
|
|
let tooltipContainer = null
|
2020-07-26 12:54:55 +02:00
|
|
|
|
|
|
|
export let customMouseOver = () => tooltip.show()
|
|
|
|
export let customMouseMove = (datapoint, colorMapping, x, y) =>
|
|
|
|
tooltip.update(datapoint, colorMapping, x, y)
|
|
|
|
export let customMouseOut = () => tooltip.hide()
|
|
|
|
export let customClick = null
|
|
|
|
|
2020-08-18 16:45:44 +02:00
|
|
|
let data = []
|
2020-08-26 18:03:30 +02:00
|
|
|
export let datasource = null
|
2020-07-26 12:54:55 +02:00
|
|
|
export let xAxisLabel = ""
|
|
|
|
export let yAxisLabel = ""
|
2020-08-06 18:15:57 +02:00
|
|
|
export let betweenBarsPadding = 0.1 //takes decimal values 0.1, 0.5 etc
|
2020-07-26 12:54:55 +02:00
|
|
|
export let gradient = null
|
|
|
|
export let color = "britecharts"
|
2020-08-18 16:45:44 +02:00
|
|
|
export let enableLabels = false
|
2020-07-29 17:35:44 +02:00
|
|
|
export let hasPercentage = null
|
2020-07-26 12:54:55 +02:00
|
|
|
export let hasSingleBarHighlight = true
|
2020-07-29 17:35:44 +02:00
|
|
|
export let highlightBarFunction = null
|
2020-07-26 12:54:55 +02:00
|
|
|
export let height = 200
|
|
|
|
export let width = 300
|
2020-07-29 17:35:44 +02:00
|
|
|
export let labelsMargin = null
|
2020-07-26 12:54:55 +02:00
|
|
|
export let isAnimated = true
|
|
|
|
export let isHorizontal = true
|
2020-07-29 17:35:44 +02:00
|
|
|
export let xAxisLabelOffset = null
|
|
|
|
export let yAxisLabelOffset = null
|
2020-07-26 12:54:55 +02:00
|
|
|
export let labelsNumberFormat = null
|
|
|
|
export let locale = null
|
2020-07-29 17:35:44 +02:00
|
|
|
export let valueLabel = null
|
2020-07-26 12:54:55 +02:00
|
|
|
export let nameLabel = null
|
|
|
|
export let numberFormat = null
|
2020-07-29 17:35:44 +02:00
|
|
|
export let labelsSize = null
|
|
|
|
export let xTicks = null
|
|
|
|
export let yTicks = null
|
|
|
|
export let percentageAxisToMaxRatio = null
|
2020-07-26 12:54:55 +02:00
|
|
|
|
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-26 18:03:30 +02:00
|
|
|
|
2020-08-18 16:45:44 +02:00
|
|
|
if (schemaIsValid()) {
|
|
|
|
chartContainer = select(`.${chartClass}`)
|
|
|
|
bindChartUIProps()
|
|
|
|
bindChartEvents()
|
|
|
|
chartContainer.datum(data).call(chart)
|
|
|
|
bindChartTooltip()
|
|
|
|
} else {
|
|
|
|
console.error("Bar Chart - Please provide a valid name and value label")
|
2020-08-06 18:15:57 +02:00
|
|
|
}
|
2020-07-28 15:19:46 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-18 16:45:44 +02:00
|
|
|
const schemaIsValid = () =>
|
|
|
|
(hasProp(data, "name") || hasProp(data, nameLabel)) &&
|
|
|
|
(hasProp(data, "value") || hasProp(data, valueLabel))
|
|
|
|
|
2020-07-28 15:19:46 +02:00
|
|
|
function bindChartUIProps() {
|
2020-08-18 16:45:44 +02:00
|
|
|
chart.numberFormat(".0f")
|
2020-08-06 18:15:57 +02:00
|
|
|
chart.labelsNumberFormat(".1f")
|
|
|
|
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(color)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.colorSchema(colorSchema)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(gradient)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.chartGradient(chartGradient)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(xAxisLabel)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.xAxisLabel(xAxisLabel)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(yAxisLabel)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.yAxisLabel(yAxisLabel)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(betweenBarsPadding)) {
|
2020-07-29 17:35:44 +02:00
|
|
|
chart.betweenBarsPadding(Number(betweenBarsPadding))
|
2020-07-28 15:19:46 +02:00
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(enableLabels)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.enableLabels(enableLabels)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(hasPercentage)) {
|
2020-07-29 17:35:44 +02:00
|
|
|
chart.hasPercentage(hasPercentage)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(hasSingleBarHighlight)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.hasSingleBarHighlight(hasSingleBarHighlight)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(labelsMargin)) {
|
2020-07-29 17:35:44 +02:00
|
|
|
chart.labelsMargin(labelsMargin)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(height)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.height(height)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(highlightBarFunction)) {
|
2020-07-29 17:35:44 +02:00
|
|
|
chart.highlightBarFunction(highlightBarFunction)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(width)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.width(width)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(isAnimated)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.isAnimated(isAnimated)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(isHorizontal)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.isHorizontal(isHorizontal)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(yAxisLabelOffset)) {
|
2020-08-06 18:15:57 +02:00
|
|
|
chart.yAxisLabelOffset(yAxisLabelOffset)
|
2020-07-29 17:35:44 +02:00
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(xAxisLabelOffset)) {
|
2020-07-29 17:35:44 +02:00
|
|
|
chart.xAxisLabelOffset(Number(xAxisLabelOffset))
|
2020-07-28 15:19:46 +02:00
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(labelsNumberFormat)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.labelsNumberFormat(labelsNumberFormat)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(valueLabel)) {
|
2020-07-29 17:35:44 +02:00
|
|
|
chart.valueLabel(valueLabel)
|
2020-07-28 15:19:46 +02:00
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(locale)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.locale(locale)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(nameLabel)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.nameLabel(nameLabel)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(numberFormat)) {
|
2020-07-28 15:19:46 +02:00
|
|
|
chart.numberFormat(numberFormat)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(labelsSize)) {
|
2020-07-29 17:35:44 +02:00
|
|
|
chart.labelsSize(labelsSize)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(xTicks)) {
|
2020-07-29 17:35:44 +02:00
|
|
|
chart.xTicks(xTicks)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(yTicks)) {
|
2020-07-29 17:35:44 +02:00
|
|
|
chart.yTicks(yTicks)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
if (notNull(percentageAxisToMaxRatio)) {
|
2020-07-29 17:35:44 +02:00
|
|
|
chart.percentageAxisToMaxRatio(percentageAxisToMaxRatio)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
chartContainer.datum(data).call(chart)
|
2020-07-28 15:19:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function bindChartEvents() {
|
|
|
|
if (customMouseMove) {
|
|
|
|
chart.on("customMouseMove", customMouseMove)
|
|
|
|
}
|
|
|
|
if (customMouseOut) {
|
|
|
|
chart.on("customMouseOut", customMouseOut)
|
|
|
|
}
|
|
|
|
if (customMouseOver) {
|
|
|
|
chart.on("customMouseOver", customMouseOver)
|
|
|
|
}
|
|
|
|
if (customClick) {
|
|
|
|
chart.on("customClick", customClick)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function bindChartTooltip() {
|
|
|
|
tooltip = britecharts.miniTooltip()
|
2020-08-06 18:15:57 +02:00
|
|
|
tooltip.numberFormat(".0f")
|
2020-07-28 15:19:46 +02:00
|
|
|
tooltipContainer = select(`.${chartClass} .metadata-group`)
|
|
|
|
tooltipContainer.datum([]).call(tooltip)
|
|
|
|
}
|
|
|
|
|
2020-07-26 12:54:55 +02:00
|
|
|
$: colorSchema = getColorSchema(color)
|
|
|
|
$: chartGradient = getChartGradient(gradient)
|
|
|
|
</script>
|
|
|
|
|
2020-07-28 15:19:46 +02:00
|
|
|
<div bind:this={chartElement} class={chartClass} />
|