2020-07-26 12:54:55 +02:00
|
|
|
<script>
|
2020-07-28 15:19:46 +02:00
|
|
|
import { getColorSchema, getChartGradient } from "./Chart.svelte"
|
|
|
|
import britecharts from "britecharts"
|
|
|
|
import { onMount } from "svelte"
|
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
|
|
|
/*
|
|
|
|
ISSUES:
|
|
|
|
nameLabel, valueLabel - what are these? Seem to break whenever passed a string or number. What type?
|
|
|
|
https://github.com/britecharts/britecharts/blob/a2c45ab023112b36e14f47c278e3a1e1c05f8383/src/charts/bar.js#L145
|
|
|
|
*/
|
|
|
|
|
|
|
|
let tooltip
|
2020-07-28 15:19:46 +02:00
|
|
|
const _id = shortid.generate()
|
|
|
|
const chart = britecharts.bar()
|
|
|
|
const chartClass = `bar-container-${_id}`
|
|
|
|
const legendClass = `legend-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
|
|
|
|
|
|
|
|
export let data = []
|
|
|
|
export let xAxisLabel = ""
|
|
|
|
export let yAxisLabel = ""
|
|
|
|
export let betweenBarsPadding = 5
|
|
|
|
export let gradient = null
|
|
|
|
export let color = "britecharts"
|
|
|
|
export let enableLabels = true
|
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
|
|
|
|
|
|
|
export let useLegend = true
|
|
|
|
|
2020-07-28 15:19:46 +02:00
|
|
|
onMount(() => {
|
|
|
|
if (chartElement) {
|
|
|
|
chartContainer = select(`.${chartClass}`)
|
|
|
|
bindChartUIProps()
|
|
|
|
bindChartEvents()
|
|
|
|
chartContainer.datum(data).call(chart)
|
2020-07-29 17:35:44 +02:00
|
|
|
bindChartTooltip()
|
2020-07-28 15:19:46 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function bindChartUIProps() {
|
|
|
|
if (color) {
|
|
|
|
chart.colorSchema(colorSchema)
|
|
|
|
}
|
|
|
|
if (gradient) {
|
|
|
|
chart.chartGradient(chartGradient)
|
|
|
|
}
|
|
|
|
if (xAxisLabel) {
|
|
|
|
chart.xAxisLabel(xAxisLabel)
|
|
|
|
}
|
|
|
|
if (yAxisLabel) {
|
|
|
|
chart.yAxisLabel(yAxisLabel)
|
|
|
|
}
|
|
|
|
if (betweenBarsPadding) {
|
2020-07-29 17:35:44 +02:00
|
|
|
chart.betweenBarsPadding(Number(betweenBarsPadding))
|
2020-07-28 15:19:46 +02:00
|
|
|
}
|
|
|
|
if (enableLabels) {
|
|
|
|
chart.enableLabels(enableLabels)
|
|
|
|
}
|
2020-07-29 17:35:44 +02:00
|
|
|
if (hasPercentage) {
|
|
|
|
chart.hasPercentage(hasPercentage)
|
|
|
|
}
|
2020-07-28 15:19:46 +02:00
|
|
|
if (hasSingleBarHighlight) {
|
|
|
|
chart.hasSingleBarHighlight(hasSingleBarHighlight)
|
|
|
|
}
|
2020-07-29 17:35:44 +02:00
|
|
|
if (labelsMargin) {
|
|
|
|
chart.labelsMargin(labelsMargin)
|
|
|
|
}
|
2020-07-28 15:19:46 +02:00
|
|
|
if (height) {
|
|
|
|
chart.height(height)
|
|
|
|
}
|
2020-07-29 17:35:44 +02:00
|
|
|
if (highlightBarFunction) {
|
|
|
|
chart.highlightBarFunction(highlightBarFunction)
|
|
|
|
}
|
2020-07-28 15:19:46 +02:00
|
|
|
if (width) {
|
|
|
|
chart.width(width)
|
|
|
|
}
|
|
|
|
if (isAnimated) {
|
|
|
|
chart.isAnimated(isAnimated)
|
|
|
|
}
|
|
|
|
if (isHorizontal) {
|
|
|
|
chart.isHorizontal(isHorizontal)
|
|
|
|
}
|
2020-07-29 17:35:44 +02:00
|
|
|
if (yAxisLabelOffset) {
|
|
|
|
chart.yAxisLabelOffset(Number(yAxisLabelOffset))
|
|
|
|
}
|
|
|
|
if (xAxisLabelOffset) {
|
|
|
|
chart.xAxisLabelOffset(Number(xAxisLabelOffset))
|
2020-07-28 15:19:46 +02:00
|
|
|
}
|
|
|
|
if (labelsNumberFormat) {
|
|
|
|
chart.labelsNumberFormat(labelsNumberFormat)
|
|
|
|
}
|
2020-07-29 17:35:44 +02:00
|
|
|
if (valueLabel) {
|
|
|
|
chart.valueLabel(valueLabel)
|
2020-07-28 15:19:46 +02:00
|
|
|
}
|
|
|
|
if (locale) {
|
|
|
|
chart.locale(locale)
|
|
|
|
}
|
|
|
|
if (nameLabel) {
|
|
|
|
chart.nameLabel(nameLabel)
|
|
|
|
}
|
|
|
|
if (numberFormat) {
|
|
|
|
chart.numberFormat(numberFormat)
|
|
|
|
}
|
2020-07-29 17:35:44 +02:00
|
|
|
if (labelsSize) {
|
|
|
|
chart.labelsSize(labelsSize)
|
|
|
|
}
|
|
|
|
if (xTicks) {
|
|
|
|
chart.xTicks(xTicks)
|
|
|
|
}
|
|
|
|
if (yTicks) {
|
|
|
|
chart.yTicks(yTicks)
|
|
|
|
}
|
|
|
|
if (percentageAxisToMaxRatio) {
|
|
|
|
chart.percentageAxisToMaxRatio(percentageAxisToMaxRatio)
|
|
|
|
}
|
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()
|
|
|
|
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} />
|
|
|
|
{#if useLegend}
|
|
|
|
<div class={legendClass} />
|
|
|
|
{/if}
|