2020-07-26 12:54:55 +02:00
|
|
|
<script>
|
2020-08-18 16:45:44 +02:00
|
|
|
import { getColorSchema, getChartGradient, notNull, hasProp } from "./utils"
|
2020-08-24 17:26:00 +02:00
|
|
|
import fetchData from "../fetchData.js"
|
2020-07-29 17:35:44 +02:00
|
|
|
import britecharts from "britecharts"
|
|
|
|
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-29 17:35:44 +02:00
|
|
|
import { select } from "d3-selection"
|
|
|
|
import shortid from "shortid"
|
2020-07-26 12:54:55 +02:00
|
|
|
|
2020-07-29 17:35:44 +02:00
|
|
|
const _id = shortid.generate()
|
|
|
|
|
2020-08-24 17:26:00 +02:00
|
|
|
export let datasource = {}
|
2020-08-04 14:55:26 +02:00
|
|
|
|
2020-07-29 17:35:44 +02:00
|
|
|
const chart = britecharts.line()
|
|
|
|
const chartClass = `line-container-${_id}`
|
|
|
|
|
2020-08-18 16:45:44 +02:00
|
|
|
let data = { dataByTopic: [] }
|
2020-08-12 11:02:36 +02:00
|
|
|
|
2020-07-29 17:35:44 +02:00
|
|
|
let chartElement
|
|
|
|
let chartContainer
|
|
|
|
let tooltipContainer
|
|
|
|
|
2020-08-18 16:45:44 +02:00
|
|
|
let tooltip = britecharts.tooltip()
|
|
|
|
|
2020-08-17 14:59:09 +02:00
|
|
|
export let customMouseOver = () => tooltip.show()
|
|
|
|
export let customMouseMove = (
|
|
|
|
dataPoint,
|
|
|
|
topicColorMap,
|
2020-08-18 16:45:44 +02:00
|
|
|
dataPointXPosition,
|
|
|
|
yPosition
|
2020-08-17 14:59:09 +02:00
|
|
|
) => {
|
2020-08-18 16:45:44 +02:00
|
|
|
tooltip.update(dataPoint, topicColorMap, dataPointXPosition, yPosition)
|
2020-08-17 14:59:09 +02:00
|
|
|
}
|
|
|
|
export let customMouseOut = () => tooltip.hide()
|
2020-08-12 11:02:36 +02:00
|
|
|
|
2020-07-29 17:35:44 +02:00
|
|
|
export let customDataEntryClick = null
|
|
|
|
export let customTouchMove = null
|
2020-07-26 12:54:55 +02:00
|
|
|
|
|
|
|
export let color = "britecharts"
|
2020-07-29 17:35:44 +02:00
|
|
|
export let axisTimeCombinations = ""
|
2020-07-26 12:54:55 +02:00
|
|
|
export let grid = "horizontal"
|
|
|
|
export let aspectRatio = 0.5
|
|
|
|
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
|
2020-08-17 14:59:09 +02:00
|
|
|
export let dateLabel = "date"
|
2020-07-26 12:54:55 +02:00
|
|
|
export let valueLabel = null
|
|
|
|
export let xAxisLabel = ""
|
|
|
|
export let xAxisValueType = "date"
|
|
|
|
export let xAxisScale = "linear"
|
|
|
|
export let xAxisFormat = "day-month"
|
|
|
|
export let xAxisCustomFormat = "%H"
|
2020-07-29 17:35:44 +02:00
|
|
|
export let yAxisLabel = null
|
|
|
|
export let yAxisLabelPadding = null
|
|
|
|
export let lines = null //not handled by setting prop
|
|
|
|
export let tooltipThreshold = null
|
2020-08-17 14:59:09 +02:00
|
|
|
export let tooltipTitle = ""
|
2020-07-29 17:35:44 +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-17 14:59:09 +02:00
|
|
|
data = await getAndPrepareData()
|
2020-08-26 18:03:30 +02:00
|
|
|
console.log("DATA", data)
|
2020-08-18 16:45:44 +02:00
|
|
|
if (data.dataByTopic.length > 0) {
|
|
|
|
chartContainer = select(`.${chartClass}`)
|
|
|
|
bindChartUIProps()
|
|
|
|
bindChartEvents()
|
|
|
|
chartContainer.datum(data).call(chart)
|
|
|
|
bindTooltip()
|
|
|
|
} else {
|
|
|
|
console.error(
|
|
|
|
"Line Chart - Please provide valid name, value and topic labels"
|
|
|
|
)
|
|
|
|
}
|
2020-08-04 14:55:26 +02:00
|
|
|
}
|
2020-07-29 17:35:44 +02:00
|
|
|
})
|
|
|
|
|
2020-08-18 16:45:44 +02:00
|
|
|
function bindTooltip() {
|
|
|
|
tooltipContainer = select(
|
|
|
|
`.${chartClass} .metadata-group .vertical-marker-container`
|
|
|
|
)
|
|
|
|
tooltip.topicLabel("topics")
|
|
|
|
tooltipContainer.datum([]).call(tooltip)
|
|
|
|
}
|
|
|
|
|
|
|
|
const schemaIsValid = data =>
|
|
|
|
hasProp(data, valueLabel) &&
|
|
|
|
hasProp(data, dateLabel) &&
|
|
|
|
hasProp(data, topicLabel)
|
|
|
|
|
2020-08-12 11:02:36 +02:00
|
|
|
async function getAndPrepareData() {
|
2020-08-17 14:59:09 +02:00
|
|
|
let dataByTopic = []
|
|
|
|
let _data = []
|
|
|
|
|
|
|
|
if (!topicLabel) {
|
2020-08-18 16:45:44 +02:00
|
|
|
topicLabel = "topicName"
|
2020-08-17 14:59:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!valueLabel) {
|
|
|
|
valueLabel = "value"
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dateLabel) {
|
|
|
|
dateLabel = "date"
|
|
|
|
}
|
|
|
|
|
2020-08-24 17:26:00 +02:00
|
|
|
_data = await fetchData(datasource)
|
2020-08-18 16:45:44 +02:00
|
|
|
|
|
|
|
if (schemaIsValid(_data)) {
|
|
|
|
_data.forEach((data, idx, arr) => {
|
|
|
|
let topicName = data[topicLabel]
|
|
|
|
if (!dataByTopic.some(dt => dt.topicName === topicName)) {
|
|
|
|
let d = {
|
|
|
|
topicName,
|
|
|
|
topic: dataByTopic.length + 1,
|
|
|
|
dates: arr
|
|
|
|
.filter(d => d[topicLabel] === topicName)
|
|
|
|
.map(d => ({
|
|
|
|
date: new Date(d[dateLabel]),
|
|
|
|
value: d[valueLabel],
|
|
|
|
}))
|
|
|
|
.sort((a, b) => a.date - b.date),
|
|
|
|
}
|
|
|
|
dataByTopic.push(d)
|
2020-08-17 14:59:09 +02:00
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
})
|
|
|
|
}
|
2020-08-17 14:59:09 +02:00
|
|
|
|
|
|
|
return { dataByTopic }
|
2020-08-12 11:02:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function bindChartUIProps() {
|
|
|
|
chart.grid("horizontal")
|
|
|
|
chart.isAnimated(true)
|
2020-08-18 16:45:44 +02:00
|
|
|
chart.tooltipThreshold(800)
|
|
|
|
chart.aspectRatio(0.5)
|
|
|
|
chart.xAxisCustomFormat("custom")
|
2020-08-12 11:02:36 +02:00
|
|
|
|
2020-08-17 14:59:09 +02:00
|
|
|
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(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(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)
|
|
|
|
}
|
2020-08-18 16:45:44 +02:00
|
|
|
|
|
|
|
tooltip.title(tooltipTitle || "Line Tooltip")
|
2020-07-29 17:35:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function bindChartEvents() {
|
2020-08-12 11:02:36 +02:00
|
|
|
if (customMouseOver) {
|
2020-08-18 16:45:44 +02:00
|
|
|
chart.on("customMouseOver", tooltip.show)
|
2020-07-29 17:35:44 +02:00
|
|
|
}
|
|
|
|
if (customMouseMove) {
|
2020-08-18 16:45:44 +02:00
|
|
|
chart.on("customMouseMove", tooltip.update)
|
2020-07-29 17:35:44 +02:00
|
|
|
}
|
|
|
|
if (customMouseOut) {
|
2020-08-18 16:45:44 +02:00
|
|
|
chart.on("customMouseOut", tooltip.hide)
|
2020-07-29 17:35:44 +02:00
|
|
|
}
|
|
|
|
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>
|
|
|
|
|
2020-07-29 17:35:44 +02:00
|
|
|
<div bind:this={chartElement} class={chartClass} />
|