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

240 lines
6.3 KiB
Svelte
Raw Normal View History

2020-07-26 12:54:55 +02:00
<script>
import { getColorSchema, getChartGradient, notNull } from "./utils"
2020-08-12 11:02:36 +02:00
import Tooltip from "./Tooltip.svelte"
import britecharts from "britecharts"
import { onMount } from "svelte"
2020-07-26 12:54:55 +02:00
import { select } from "d3-selection"
import shortid from "shortid"
2020-07-26 12:54:55 +02:00
const _id = shortid.generate()
2020-08-04 14:55:26 +02:00
export let _bb
export let model
let store = _bb.store
const chart = britecharts.line()
const chartClass = `line-container-${_id}`
const legendClass = `legend-container-${_id}`
2020-08-12 11:02:36 +02:00
const testData = {
data: [
{
topicName: "Oakland",
name: 2,
date: "2017-01-16T16:00:00-08:00",
value: 3,
},
{
topicName: "Oakland",
name: 2,
date: "2017-01-17T16:00:00-08:00",
value: 7,
},
{
topicName: "Oakland",
name: 2,
date: "2017-01-18T16:00:00-08:00",
value: 5,
},
{
topicName: "Oakland",
name: 2,
date: "2017-01-19T16:00:00-08:00",
value: 6,
},
{
topicName: "Oakland",
name: 2,
date: "2017-01-20T16:00:00-08:00",
value: 1,
},
],
}
let data = testData
let chartElement
let chartContainer
2020-07-26 12:54:55 +02:00
let tooltip
let tooltipContainer
2020-08-12 11:02:36 +02:00
export let customMouseOver = null //() => tooltip.show()
export let customMouseMove = null //(dataPoint, topicColorMap, dataPointXPosition) =>
//tooltip.update(dataPoint, topicColorMap, dataPointXPosition)
export let customMouseOut = null //() => tooltip.hide()
export let customDataEntryClick = null
export let customTouchMove = null
2020-07-26 12:54:55 +02:00
export let color = "britecharts"
export let axisTimeCombinations = ""
2020-07-26 12:54:55 +02:00
export let grid = "horizontal"
export let aspectRatio = 0.5
export let dateLabel = "date"
export let width = null
export let height = null
export let isAnimated = true
export let lineCurve = "linear" //see api for possible opts
2020-08-12 11:02:36 +02:00
export let lineGradient = null
2020-07-26 12:54:55 +02:00
export let locale = "en-GB"
export let numberFormat = ""
export let shouldShowAllDataPoints = true
export let topicLabel = null
export let valueLabel = null
export let xAxisLabel = ""
export let xAxisValueType = "date"
export let xAxisScale = "linear"
export let xAxisFormat = "day-month"
export let xAxisCustomFormat = "%H"
export let yAxisLabel = null
export let yAxisLabelPadding = null
export let lines = null //not handled by setting prop
export let tooltipThreshold = null
2020-08-12 11:02:36 +02:00
let chartDrawn = false
2020-08-04 14:55:26 +02:00
onMount(async () => {
if (chart) {
2020-08-12 11:02:36 +02:00
// data = await getAndPrepareData()
2020-08-04 14:55:26 +02:00
chartContainer = select(`.${chartClass}`)
bindChartUIProps()
bindChartEvents()
2020-08-12 11:02:36 +02:00
chartContainer.datum(data).call(chart)
chartDrawn = true
2020-08-04 14:55:26 +02:00
}
})
2020-08-04 14:55:26 +02:00
async function fetchData() {
const FETCH_RECORDS_URL = `/api/views/all_${model}`
const response = await _bb.api.get(FETCH_RECORDS_URL)
if (response.status === 200) {
const json = await response.json()
store.update(state => {
state[model] = json
return state
})
} else {
throw new Error("Failed to fetch records.", response)
}
}
2020-08-12 11:02:36 +02:00
async function getAndPrepareData() {
let data = []
if (model) {
await fetchData()
data = $store[model]
}
2020-08-12 11:02:36 +02:00
return { data }
}
$: console.log("DATA", data)
function bindChartUIProps() {
chart.grid("horizontal")
chart.dateLabel("date")
chart.aspectRatio(0.5)
chart.isAnimated(true)
// if (notNull(color)) {
// //chart.colorSchema(colorSchema)
// }
// if (notNull(lineGradient)) {
// chart.lineGradient(chartGradient)
// }
// if (notNull(axisTimeCombinations)) {
// chart.axisTimeCombinations(axisTimeCombinations)
// }
// if (notNull(grid)) {
// chart.grid(grid)
// }
// if (notNull(aspectRatio)) {
// chart.aspectRatio(aspectRatio)
// }
// if (notNull(dateLabel)) {
// chart.dateLabel(dateLabel)
// }
// if (notNull(width)) {
// chart.width(width)
// }
// if (notNull(height)) {
// chart.height(height)
// }
// if (notNull(isAnimated)) {
// chart.isAnimated(isAnimated)
// }
// if (notNull(lineCurve)) {
// chart.lineCurve(lineCurve)
// }
// if (notNull(locale)) {
// chart.locale(locale)
// }
// if (notNull(numberFormat)) {
// chart.numberFormat(numberFormat)
// }
// if (notNull(shouldShowAllDataPoints)) {
// chart.shouldShowAllDataPoints(shouldShowAllDataPoints)
// }
// if (notNull(topicLabel)) {
// chart.topicLabel(topicLabel)
// }
// if (notNull(valueLabel)) {
// chart.valueLabel(valueLabel)
// }
// if (notNull(xAxisLabel)) {
// chart.xAxisLabel(xAxisLabel)
// }
// if (notNull(xAxisValueType)) {
// chart.xAxisValueType(xAxisValueType)
// }
// if (notNull(xAxisScale)) {
// chart.xAxisScale(xAxisScale)
// }
// if (notNull(xAxisFormat)) {
// chart.xAxisFormat(xAxisFormat)
// }
// if (notNull(xAxisCustomFormat)) {
// chart.xAxisCustomFormat(xAxisCustomFormat)
// }
// if (notNull(yAxisLabel)) {
// chart.yAxisLabel(yAxisLabel)
// }
// if (notNull(yAxisLabelPadding)) {
// chart.yAxisLabelPadding(yAxisLabelPadding)
// }
// if (notNull(tooltipThreshold)) {
// chart.tooltipThreshold(tooltipThreshold)
// }
// if (notNull(lines)) {
// chart.lines(lines)
// }
}
function bindChartEvents() {
2020-08-12 11:02:36 +02:00
if (customMouseOver) {
chart.on("customMouseOver", customMouseOver)
}
if (customMouseMove) {
chart.on("customMouseMove", customMouseMove)
}
if (customMouseOut) {
chart.on("customMouseOut", customMouseOut)
}
if (customDataEntryClick) {
chart.on("customDataEntryClick", customDataEntryClick)
}
if (customTouchMove) {
chart.on("customTouchMove", customTouchMove)
}
}
2020-07-26 12:54:55 +02:00
$: colorSchema = getColorSchema(color)
2020-08-12 11:02:36 +02:00
$: chartGradient = getChartGradient(lineGradient)
2020-07-26 12:54:55 +02:00
</script>
<div bind:this={chartElement} class={chartClass} />
2020-08-12 11:02:36 +02:00
<!-- {#if chartDrawn}
<Tooltip bind:tooltip {valueLabel} {chartClass} />
{/if} -->