budibase/packages/standard-components/src/Chart/Bar.svelte

188 lines
5.2 KiB
Svelte
Raw Normal View History

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"
import fetchData from "../fetchData.js"
2020-07-28 15:19:46 +02:00
import { onMount } from "svelte"
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 = []
export let datasource = null
2020-07-26 12:54:55 +02:00
export let xAxisLabel = ""
export let yAxisLabel = ""
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
export let hasPercentage = null
2020-07-26 12:54:55 +02:00
export let hasSingleBarHighlight = true
export let highlightBarFunction = null
2020-07-26 12:54:55 +02:00
export let height = 200
export let width = 300
export let labelsMargin = null
2020-07-26 12:54:55 +02:00
export let isAnimated = true
export let isHorizontal = true
export let xAxisLabelOffset = null
export let yAxisLabelOffset = null
2020-07-26 12:54:55 +02:00
export let labelsNumberFormat = null
export let locale = null
export let valueLabel = null
2020-07-26 12:54:55 +02:00
export let nameLabel = null
export let numberFormat = null
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 () => {
if (!isEmpty(datasource)) {
data = await fetchData(datasource)
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-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")
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)) {
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)) {
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)) {
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)) {
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)) {
chart.yAxisLabelOffset(yAxisLabelOffset)
}
2020-08-18 16:45:44 +02:00
if (notNull(xAxisLabelOffset)) {
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)) {
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)) {
chart.labelsSize(labelsSize)
}
2020-08-18 16:45:44 +02:00
if (notNull(xTicks)) {
chart.xTicks(xTicks)
}
2020-08-18 16:45:44 +02:00
if (notNull(yTicks)) {
chart.yTicks(yTicks)
}
2020-08-18 16:45:44 +02:00
if (notNull(percentageAxisToMaxRatio)) {
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()
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} />