candlestick

This commit is contained in:
Gerard Burns 2024-04-18 14:22:36 +01:00
parent 6176381f5b
commit 415b109f23
6 changed files with 77 additions and 80 deletions

View File

@ -99,8 +99,8 @@
} }
} }
const getSeries = (datasource, valueColumns = []) => { const getSeries = (dataProvider, valueColumns = []) => {
const rows = datasource.rows ?? []; const rows = dataProvider.rows ?? [];
return valueColumns.map(column => ({ return valueColumns.map(column => ({
name: column, name: column,
@ -110,8 +110,8 @@
})) }))
} }
const getCategories = (datasource, labelColumn) => { const getCategories = (dataProvider, labelColumn) => {
const rows = datasource.rows ?? []; const rows = dataProvider.rows ?? [];
return rows.map(row => { return rows.map(row => {
const value = row?.[labelColumn] const value = row?.[labelColumn]

View File

@ -98,8 +98,8 @@
} }
} }
const getSeries = (datasource, valueColumns = []) => { const getSeries = (dataProvider, valueColumns = []) => {
const rows = datasource.rows ?? []; const rows = dataProvider.rows ?? [];
return valueColumns.map(column => ({ return valueColumns.map(column => ({
name: column, name: column,
@ -109,8 +109,8 @@
})) }))
} }
const getCategories = (datasource, labelColumn) => { const getCategories = (dataProvider, labelColumn) => {
const rows = datasource.rows ?? []; const rows = dataProvider.rows ?? [];
return rows.map(row => { return rows.map(row => {
const value = row?.[labelColumn] const value = row?.[labelColumn]

View File

@ -1,6 +1,7 @@
<script> <script>
import { ApexOptionsBuilder } from "./ApexOptionsBuilder" import { ApexOptionsBuilder } from "./ApexOptionsBuilder"
import ApexChart from "./ApexChart.svelte" import ApexChart from "./ApexChart.svelte"
import formatters from './formatters';
export let title export let title
export let dataProvider export let dataProvider
@ -14,82 +15,78 @@
export let height export let height
export let width export let width
export let animate export let animate
export let yAxisUnits export let valueUnits
$: options = setUpChart( $: series = getSeries(
title,
dataProvider, dataProvider,
dateColumn, dateColumn,
openColumn, openColumn,
highColumn, highColumn,
lowColumn, lowColumn,
closeColumn, closeColumn
xAxisLabel,
yAxisLabel,
height,
width,
animate,
yAxisUnits
) )
const setUpChart = ( $: options = {
title, series,
title: {
text: title,
},
chart: {
height: height == null || height === "" ? "auto" : height,
width: width == null || width === "" ? "100%" : width,
type: "candlestick",
animations: {
enabled: animate
},
toolbar: {
show: false,
},
zoom: {
enabled: false,
},
},
xaxis: {
tooltip: {
formatter: formatters["Datetime"]
},
type: "datetime",
title: {
text: xAxisLabel
}
},
yaxis: {
title: {
text: yAxisLabel
}
}
}
const getSeries = (
dataProvider, dataProvider,
dateColumn, dateColumn,
openColumn, openColumn,
highColumn, highColumn,
lowColumn, lowColumn,
closeColumn, closeColumn
xAxisLabel,
yAxisLabel,
height,
width,
animate,
yAxisUnits
) => { ) => {
const allCols = [dateColumn, openColumn, highColumn, lowColumn, closeColumn] const rows = dataProvider.rows ?? [];
if (
!dataProvider ||
!dataProvider.rows?.length ||
allCols.find(x => x == null)
) {
return null
}
// Fetch data return [{
const { schema, rows } = dataProvider data: rows.map(row => {
const reducer = row => (valid, column) => valid && row[column] != null const open = parseFloat(row[openColumn])
const hasAllColumns = row => allCols.reduce(reducer(row), true) const high = parseFloat(row[highColumn])
const data = rows.filter(row => hasAllColumns(row)) const low = parseFloat(row[lowColumn])
if (!schema || !data.length) { const close = parseFloat(row[closeColumn])
return null
}
// Initialise default chart return [
let builder = new ApexOptionsBuilder() Date.parse(row[dateColumn]),
.type("candlestick") isNaN(open) ? 0 : open,
.title(title) isNaN(high) ? 0 : high,
.width(width) isNaN(low) ? 0 : low,
.height(height) isNaN(close) ? 0 : close,
.xLabel(xAxisLabel) ]
.yLabel(yAxisLabel) })
.animate(animate) }]
.yUnits(yAxisUnits)
.yTooltip(true)
//.xType("datetime")
.candleStick()
// Add data
//const parseDate = d => d
const parseDate = d => (isNaN(d) ? Date.parse(d).valueOf() : parseInt(d))
const chartData = data.map(row => ([
parseDate(row[dateColumn]),
row[openColumn], row[highColumn], row[lowColumn], row[closeColumn]
]))
builder = builder.series([{ data: chartData }])
// Build chart options
return builder.getOptions()
} }
</script> </script>

View File

@ -56,8 +56,8 @@
}, },
} }
const getSeries = (datasource, valueColumn) => { const getSeries = (dataProvider, valueColumn) => {
const rows = datasource.rows ?? []; const rows = dataProvider.rows ?? [];
return rows.map(row => { return rows.map(row => {
const value = row?.[valueColumn] const value = row?.[valueColumn]
@ -70,8 +70,8 @@
}) })
} }
const getLabels = (datasource, labelColumn, labelType) => { const getLabels = (dataProvider, labelColumn, labelType) => {
const rows = datasource.rows ?? []; const rows = dataProvider.rows ?? [];
return rows.map(row => { return rows.map(row => {
const value = row?.[labelColumn] const value = row?.[labelColumn]

View File

@ -91,8 +91,8 @@
} }
} }
const getSeries = (datasource, valueColumns = []) => { const getSeries = (dataProvider, valueColumns = []) => {
const rows = datasource.rows ?? []; const rows = dataProvider.rows ?? [];
return valueColumns.map(column => ({ return valueColumns.map(column => ({
name: column, name: column,
@ -102,8 +102,8 @@
})) }))
} }
const getCategories = (datasource, labelColumn) => { const getCategories = (dataProvider, labelColumn) => {
const rows = datasource.rows ?? []; const rows = dataProvider.rows ?? [];
return rows.map(row => { return rows.map(row => {
const value = row?.[labelColumn] const value = row?.[labelColumn]

View File

@ -56,8 +56,8 @@
}, },
} }
const getSeries = (datasource, valueColumn) => { const getSeries = (dataProvider, valueColumn) => {
const rows = datasource.rows ?? []; const rows = dataProvider.rows ?? [];
return rows.map(row => { return rows.map(row => {
const value = row?.[valueColumn] const value = row?.[valueColumn]
@ -70,8 +70,8 @@
}) })
} }
const getLabels = (datasource, labelColumn, labelType) => { const getLabels = (dataProvider, labelColumn, labelType) => {
const rows = datasource.rows ?? []; const rows = dataProvider.rows ?? [];
return rows.map(row => { return rows.map(row => {
const value = row?.[labelColumn] const value = row?.[labelColumn]