Extract area chart generation to separate file

This commit is contained in:
Matthias 2023-05-04 19:36:06 +02:00
parent b88ee2413f
commit 6c80c0d741
2 changed files with 44 additions and 27 deletions

View File

@ -14,14 +14,14 @@ import {
IndicatorConfig, IndicatorConfig,
ChartType, ChartType,
} from '@/types'; } from '@/types';
import { generateCandleSeries } from '@/shared/charts/candleChartSeries'; import { generateCandleSeries, generateAreaCandleSeries } from '@/shared/charts/candleChartSeries';
import heikinashi from '@/shared/charts/heikinashi'; import heikinashi from '@/shared/charts/heikinashi';
import { getTradeEntries } from '@/shared/charts/tradeChartData'; import { getTradeEntries } from '@/shared/charts/tradeChartData';
import ECharts from 'vue-echarts'; import ECharts from 'vue-echarts';
import { format } from 'date-fns-tz'; import { format } from 'date-fns-tz';
import { use } from 'echarts/core'; import { use } from 'echarts/core';
import { EChartsOption, LineSeriesOption, ScatterSeriesOption } from 'echarts'; import { EChartsOption, ScatterSeriesOption } from 'echarts';
import { CanvasRenderer } from 'echarts/renderers'; import { CanvasRenderer } from 'echarts/renderers';
import { CandlestickChart, LineChart, BarChart, ScatterChart } from 'echarts/charts'; import { CandlestickChart, LineChart, BarChart, ScatterChart } from 'echarts/charts';
import { import {
@ -178,7 +178,6 @@ function updateChart(initial = false) {
// Enhance dataset with diff columns for area plots // Enhance dataset with diff columns for area plots
dataset = calculateDiff(columns, dataset, colFrom, colTo); dataset = calculateDiff(columns, dataset, colFrom, colTo);
}); });
console.log(columns, dataset[dataset.length - 1]);
// Add new rows to end to allow slight "scroll past" // Add new rows to end to allow slight "scroll past"
const newArray = Array(dataset.length > 0 ? dataset[dataset.length - 2].length : 0); const newArray = Array(dataset.length > 0 ? dataset[dataset.length - 2].length : 0);
newArray[colDate] = dataset[dataset.length - 1][colDate] + props.dataset.timeframe_ms * 3; newArray[colDate] = dataset[dataset.length - 1][colDate] + props.dataset.timeframe_ms * 3;
@ -343,31 +342,10 @@ function updateChart(initial = false) {
const fillColKey = `${key}-${value.fill_to}`; const fillColKey = `${key}-${value.fill_to}`;
const fillCol = columns.findIndex((el) => el === fillColKey); const fillCol = columns.findIndex((el) => el === fillColKey);
const fillValue: IndicatorConfig = { const fillValue: IndicatorConfig = {
// color: value.color; color: value.color,
type: ChartType.line, type: ChartType.line,
}; };
const areaSeries = generateCandleSeries( const areaSeries = generateAreaCandleSeries(colDate, fillCol, key, fillValue, 0);
colDate,
fillCol,
fillColKey,
fillValue,
) as LineSeriesOption;
const areaOptions: LineSeriesOption = {
stack: key,
lineStyle: {
opacity: 0,
},
showSymbol: false,
areaStyle: {
color: value.color,
opacity: 0.2,
},
tooltip: {
show: false, // hide value on tooltip
},
};
Object.assign(areaSeries, areaOptions);
chartOptions.value.series[chartOptions.value.series.length - 1]['stack'] = key; chartOptions.value.series[chartOptions.value.series.length - 1]['stack'] = key;
chartOptions.value?.series.push(areaSeries); chartOptions.value?.series.push(areaSeries);

View File

@ -1,4 +1,4 @@
import { IndicatorConfig } from '@/types'; import { ChartType, IndicatorConfig } from '@/types';
import { BarSeriesOption, LineSeriesOption, ScatterSeriesOption } from 'echarts'; import { BarSeriesOption, LineSeriesOption, ScatterSeriesOption } from 'echarts';
import randomColor from '../randomColor'; import randomColor from '../randomColor';
@ -27,3 +27,42 @@ export function generateCandleSeries(
}; };
return sp; return sp;
} }
export function generateAreaCandleSeries(
colDate: number,
fillCol: number,
key: string,
value: IndicatorConfig,
axis = 0,
): SupportedSeriesTypes {
const fillValue: IndicatorConfig = {
// color: value.color;
type: ChartType.line,
};
const areaSeries = generateCandleSeries(
colDate,
fillCol,
key,
fillValue,
axis,
) as LineSeriesOption;
const areaOptions: LineSeriesOption = {
stack: key,
lineStyle: {
opacity: 0,
},
showSymbol: false,
areaStyle: {
color: value.color,
opacity: 0.1,
},
tooltip: {
show: false, // hide value on tooltip
},
};
Object.assign(areaSeries, areaOptions);
return areaSeries;
}