2020-11-04 16:21:11 +01:00
|
|
|
<script>
|
2020-11-18 22:06:12 +01:00
|
|
|
import { getContext, onMount } from "svelte"
|
2020-11-04 16:21:11 +01:00
|
|
|
import { ApexOptionsBuilder } from "./ApexOptionsBuilder"
|
|
|
|
import ApexChart from "./ApexChart.svelte"
|
2020-11-04 16:56:57 +01:00
|
|
|
import { isEmpty } from "lodash/fp"
|
2020-11-04 16:21:11 +01:00
|
|
|
|
2020-11-20 10:50:10 +01:00
|
|
|
const { API } = getContext("sdk")
|
2020-11-19 14:52:11 +01:00
|
|
|
const dataContext = getContext("data")
|
2020-11-18 22:06:12 +01:00
|
|
|
|
2020-11-04 16:21:11 +01:00
|
|
|
export let title
|
|
|
|
export let datasource
|
|
|
|
export let dateColumn
|
|
|
|
export let openColumn
|
|
|
|
export let highColumn
|
|
|
|
export let lowColumn
|
|
|
|
export let closeColumn
|
|
|
|
export let xAxisLabel
|
|
|
|
export let yAxisLabel
|
|
|
|
export let height
|
|
|
|
export let width
|
|
|
|
export let animate
|
|
|
|
export let yAxisUnits
|
|
|
|
|
|
|
|
let options
|
|
|
|
|
|
|
|
// Fetch data on mount
|
|
|
|
onMount(async () => {
|
|
|
|
const allCols = [dateColumn, openColumn, highColumn, lowColumn, closeColumn]
|
2020-11-04 16:56:57 +01:00
|
|
|
if (isEmpty(datasource) || allCols.find(x => x == null)) {
|
2020-11-04 16:43:43 +01:00
|
|
|
options = false
|
2020-11-04 16:21:11 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch, filter and sort data
|
2020-11-18 22:06:12 +01:00
|
|
|
const schema = (await API.fetchTableDefinition(datasource.tableId)).schema
|
2020-11-19 14:52:11 +01:00
|
|
|
const result = await API.fetchDatasource(datasource, $dataContext)
|
2020-11-04 16:21:11 +01:00
|
|
|
const reducer = row => (valid, column) => valid && row[column] != null
|
|
|
|
const hasAllColumns = row => allCols.reduce(reducer(row), true)
|
|
|
|
const data = result
|
|
|
|
.filter(row => hasAllColumns(row))
|
|
|
|
.slice(0, 100)
|
|
|
|
.sort((a, b) => (a[dateColumn] > b[dateColumn] ? 1 : -1))
|
|
|
|
if (!schema || !data.length) {
|
2020-11-04 16:43:43 +01:00
|
|
|
options = false
|
2020-11-04 16:21:11 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise default chart
|
|
|
|
let builder = new ApexOptionsBuilder()
|
|
|
|
.type("candlestick")
|
|
|
|
.title(title)
|
|
|
|
.width(width)
|
|
|
|
.height(height)
|
|
|
|
.xLabel(xAxisLabel)
|
|
|
|
.yLabel(yAxisLabel)
|
|
|
|
.animate(animate)
|
|
|
|
.yUnits(yAxisUnits)
|
|
|
|
.yTooltip(true)
|
|
|
|
.xType("datetime")
|
|
|
|
|
|
|
|
// Add data
|
2020-11-05 14:27:29 +01:00
|
|
|
const parseDate = d => (isNaN(d) ? Date.parse(d).valueOf() : parseInt(d))
|
2020-11-04 16:21:11 +01:00
|
|
|
const chartData = data.map(row => ({
|
2020-11-05 14:27:29 +01:00
|
|
|
x: parseDate(row[dateColumn]),
|
2020-11-04 16:21:11 +01:00
|
|
|
y: [row[openColumn], row[highColumn], row[lowColumn], row[closeColumn]],
|
|
|
|
}))
|
|
|
|
builder = builder.series([{ data: chartData }])
|
|
|
|
|
|
|
|
// Build chart options
|
|
|
|
options = builder.getOptions()
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
2020-11-18 22:06:12 +01:00
|
|
|
<ApexChart {options} {styles} />
|