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

87 lines
1.9 KiB
Svelte
Raw Normal View History

2020-08-04 17:21:51 +02:00
<script>
import britecharts from "britecharts"
import { notNull } from "./utils"
import { select } from "d3-selection"
import { onMount } from "svelte"
export let useLegend = true
export let data = []
export let width = null
export let height = null
export let colorSchema = null
export let highlight = null
export let highlightByEntryId = null
export let isHorizontal = false
export let margin = null
export let marginRatio = null
export let markerSize = null
export let numberFormat = null
export let unit = null
2020-08-05 15:19:56 +02:00
export let legend = britecharts.legend() //exported it can be bound to
2020-08-04 17:21:51 +02:00
let legendContainer = null
let legendElement = null
2020-08-05 17:57:54 +02:00
$: {
if (legendElement) {
legendContainer = select(legendElement)
legend.numberFormat(".0f")
2020-08-04 17:21:51 +02:00
2020-08-05 17:57:54 +02:00
if (width) {
legend.width(width)
}
2020-08-04 17:21:51 +02:00
2020-08-05 17:57:54 +02:00
if (notNull(height)) {
legend.height(height)
}
2020-08-04 17:21:51 +02:00
2020-08-05 17:57:54 +02:00
if (notNull(colorSchema)) {
legend.colorSchema(colorSchema)
}
2020-08-04 17:21:51 +02:00
2020-08-05 17:57:54 +02:00
if (notNull(highlight)) {
legend.highlight(highlight)
}
2020-08-04 17:21:51 +02:00
2020-08-05 17:57:54 +02:00
if (notNull(highlightByEntryId)) {
legend.highlightByEntryId(highlightByEntryId)
}
2020-08-04 17:21:51 +02:00
2020-08-05 18:05:06 +02:00
if (notNull(isHorizontal)) {
legend.isHorizontal(isHorizontal)
}
2020-08-05 17:57:54 +02:00
if (notNull(margin)) {
legend.margin(margin)
}
2020-08-04 17:21:51 +02:00
2020-08-05 17:57:54 +02:00
if (notNull(marginRatio)) {
legend.marginRatio(marginRatio)
}
2020-08-04 17:21:51 +02:00
2020-08-05 17:57:54 +02:00
if (notNull(markerSize)) {
legend.markerSize(markerSize)
}
2020-08-04 17:21:51 +02:00
2020-08-05 17:57:54 +02:00
if (notNull(numberFormat)) {
legend.numberFormat(numberFormat)
}
2020-08-04 17:21:51 +02:00
2020-08-05 17:57:54 +02:00
if (notNull(unit)) {
legend.unit(unit)
}
2020-08-10 11:32:46 +02:00
2020-08-05 17:57:54 +02:00
legendContainer.datum(data).call(legend)
2020-08-04 17:21:51 +02:00
}
}
const legendClass = `legend-container`
</script>
{#if useLegend}
2020-08-10 11:32:46 +02:00
<div
bind:this={legendElement}
style={`width: ${width}px`}
class={legendClass} />
2020-08-04 17:21:51 +02:00
{/if}