Add Area chart helpers

This commit is contained in:
Matthias 2023-05-04 19:26:07 +02:00
parent 7d2be4e6df
commit 8dd846d95b
2 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,48 @@
import { PlotConfig } from '@/types';
/**
* Calculate diff over 2 dataset columns and adds it to the end of the dataset
* modifies the incomming dataset array inplace!
*/
export function calculateDiff(
columns: string[],
data: Array<number[]>,
colFrom: string,
colTo: string,
): number[][] {
const fromIdx = columns.indexOf(colFrom);
const toIdx = columns.indexOf(colTo);
columns.push(`${colFrom}-${colTo}`);
return data.map((original, idx) => {
// Prevent mutation of original data
const candle = original.slice();
const diff = idx === 0 ? 0 : candle[toIdx] - candle[fromIdx];
candle.push(diff);
return candle;
});
}
export function getDiffColumns(plotConfig: PlotConfig): string[][] {
const result: string[][] = [];
if ('main_plot' in plotConfig) {
Object.entries(plotConfig.main_plot).forEach(([key, value]) => {
if (value.fill_to) {
result.push([key, value.fill_to]);
}
});
}
if ('subplots' in plotConfig) {
Object.entries(plotConfig.subplots).forEach(([_, subplots]) => {
Object.entries(subplots).forEach(([key, value]) => {
if (value.fill_to) {
result.push([key, value.fill_to]);
}
});
});
}
console.log('getDiffColumns', result);
return result;
}
export default calculateDiff;

View File

@ -7,6 +7,7 @@ export enum ChartType {
export interface IndicatorConfig {
color?: string;
type?: ChartType;
fill_to?: string;
}
export interface PlotConfig {