frequi_origin/src/components/charts/DailyChart.vue

171 lines
3.7 KiB
Vue
Raw Normal View History

<template>
2021-05-24 14:24:46 +00:00
<v-chart v-if="dailyStats.data" :option="dailyChartOptions" :theme="getChartTheme" autoresize />
</template>
<script lang="ts">
2021-12-25 12:45:26 +00:00
import { useGetters } from 'vuex-composition-helpers';
import { ref, defineComponent, computed, ComputedRef } from '@vue/composition-api';
import ECharts from 'vue-echarts';
2021-12-25 12:45:26 +00:00
// import { EChartsOption } from 'echarts';
2020-10-04 17:24:21 +00:00
2021-05-24 15:45:20 +00:00
import { use } from 'echarts/core';
import { CanvasRenderer } from 'echarts/renderers';
import { LineChart, BarChart } from 'echarts/charts';
2021-08-08 17:53:05 +00:00
import {
DatasetComponent,
GridComponent,
LegendComponent,
TitleComponent,
TooltipComponent,
VisualMapComponent,
} from 'echarts/components';
2020-08-29 09:23:39 +00:00
import { DailyReturnValue } from '@/types';
2021-08-08 17:53:05 +00:00
use([
BarChart,
LineChart,
CanvasRenderer,
GridComponent,
DatasetComponent,
LegendComponent,
TitleComponent,
TooltipComponent,
VisualMapComponent,
]);
2021-05-24 15:45:20 +00:00
// Define Column labels here to avoid typos
const CHART_ABS_PROFIT = 'Absolute profit';
const CHART_TRADE_COUNT = 'Trade Count';
2021-12-25 12:45:26 +00:00
export default defineComponent({
components: {
'v-chart': ECharts,
},
2021-12-25 12:45:26 +00:00
props: {
dailyStats: {
type: Object as () => DailyReturnValue,
required: true,
},
showTitle: {
type: Boolean,
default: true,
},
},
2021-12-25 12:45:26 +00:00
setup(props) {
const { getChartTheme } = useGetters(['getChartTheme']);
const absoluteMin: ComputedRef<number> = computed(() =>
props.dailyStats.data.reduce(
2020-08-18 18:51:16 +00:00
(min, p) => (p.abs_profit < min ? p.abs_profit : min),
2021-12-25 12:45:26 +00:00
props.dailyStats.data[0]?.abs_profit,
2020-08-18 18:51:16 +00:00
),
);
2021-12-25 12:45:26 +00:00
const absoluteMax: ComputedRef<number> = computed(() =>
props.dailyStats.data.reduce(
2020-08-18 18:51:16 +00:00
(max, p) => (p.abs_profit > max ? p.abs_profit : max),
2021-12-25 12:45:26 +00:00
props.dailyStats.data[0]?.abs_profit,
2020-08-18 18:51:16 +00:00
),
);
2021-12-25 12:45:26 +00:00
// : Ref<EChartsOption>
const dailyChartOptions = ref({
title: {
text: 'Daily profit',
2021-12-25 12:45:26 +00:00
show: props.showTitle,
},
2021-05-25 20:06:43 +00:00
backgroundColor: 'rgba(0, 0, 0, 0)',
dataset: {
dimensions: ['date', 'abs_profit', 'trade_count'],
2021-12-25 12:45:26 +00:00
source: props.dailyStats.data,
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'line',
label: {
backgroundColor: '#6a7985',
},
},
},
legend: {
data: [CHART_ABS_PROFIT, CHART_TRADE_COUNT],
right: '5%',
},
2021-12-25 12:45:26 +00:00
xAxis: [
{
type: 'category',
inverse: true,
},
],
2020-10-04 17:24:21 +00:00
visualMap: [
{
dimension: 1,
seriesIndex: 0,
show: false,
pieces: [
{
max: 0.0,
2021-12-25 12:45:26 +00:00
min: absoluteMin.value,
2020-10-04 17:24:21 +00:00
color: 'red',
},
{
min: 0.0,
2021-12-25 12:45:26 +00:00
max: absoluteMax.value,
2020-10-04 17:24:21 +00:00
color: 'green',
},
],
},
],
yAxis: [
{
type: 'value',
name: CHART_ABS_PROFIT,
splitLine: {
show: false,
},
nameRotate: 90,
nameLocation: 'middle',
nameGap: 40,
},
{
type: 'value',
name: CHART_TRADE_COUNT,
nameRotate: 90,
nameLocation: 'middle',
nameGap: 30,
},
],
series: [
{
type: 'line',
name: CHART_ABS_PROFIT,
2020-10-04 17:24:21 +00:00
// Color is induced by visualMap
},
{
type: 'bar',
name: CHART_TRADE_COUNT,
2020-10-04 17:24:21 +00:00
itemStyle: {
color: 'rgba(150,150,150,0.3)',
},
yAxisIndex: 1,
},
],
2021-12-25 12:45:26 +00:00
});
return {
dailyChartOptions,
getChartTheme,
};
2021-12-25 12:45:26 +00:00
},
});
</script>
2020-08-18 05:05:40 +00:00
<style lang="scss" scoped>
.echarts {
width: 100%;
height: 100%;
2021-05-26 19:00:39 +00:00
min-height: 240px;
2020-08-18 05:05:40 +00:00
}
</style>