frequi_origin/src/components/charts/HourlyChart.vue

154 lines
3.3 KiB
Vue
Raw Normal View History

2020-08-17 19:15:15 +00:00
<template>
<v-chart
v-if="trades.length > 0"
:options="hourlyChartOptions"
autoresize
:theme="getChartTheme"
/>
2020-08-17 19:15:15 +00:00
</template>
<script lang="ts">
import { Component, Vue, Prop } from 'vue-property-decorator';
import { Getter } from 'vuex-class';
2020-08-17 19:15:15 +00:00
import ECharts from 'vue-echarts';
2020-08-17 19:15:15 +00:00
import 'echarts/lib/chart/bar';
import 'echarts/lib/chart/line';
import 'echarts/lib/component/title';
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/legend';
import 'echarts/lib/component/visualMap';
import 'echarts/lib/component/visualMapPiecewise';
2020-08-17 19:15:15 +00:00
2020-08-29 09:23:39 +00:00
import { Trade } from '@/types';
2020-08-17 19:15:15 +00:00
import { timestampHour } from '@/shared/formatters';
2020-10-06 06:00:26 +00:00
import { EChartOption } from 'echarts';
2020-08-17 19:15:15 +00:00
// Define Column labels here to avoid typos
2020-08-18 18:51:16 +00:00
const CHART_PROFIT = 'Profit %';
2020-08-17 19:15:15 +00:00
const CHART_TRADE_COUNT = 'Trade Count';
@Component({
components: {
'v-chart': ECharts,
},
})
export default class HourlyChart extends Vue {
@Prop({ required: true }) trades!: Trade[];
2020-08-17 19:15:15 +00:00
2020-08-31 15:47:26 +00:00
@Prop({ default: true, type: Boolean }) showTitle!: boolean;
@Getter getChartTheme!: string;
2020-08-17 19:15:15 +00:00
get hourlyData() {
const res = new Array(24);
for (let i = 0; i < 24; i += 1) {
res[i] = { hour: i, hourDesc: `${i}h`, profit: 0.0, count: 0.0 };
2020-08-17 19:15:15 +00:00
}
for (let i = 0, len = this.trades.length; i < len; i += 1) {
const trade = this.trades[i];
if (trade.close_timestamp) {
const hour = timestampHour(trade.close_timestamp);
2020-12-02 19:03:17 +00:00
res[hour].profit += trade.profit_ratio;
2020-08-17 19:15:15 +00:00
res[hour].count += 1;
}
}
return res;
}
2020-10-04 17:24:21 +00:00
get hourlyChartOptions(): EChartOption {
2020-08-17 19:15:15 +00:00
return {
title: {
text: 'Hourly Profit',
2020-08-31 15:47:26 +00:00
show: this.showTitle,
2020-08-17 19:15:15 +00:00
},
dataset: {
dimensions: ['hourDesc', 'profit', 'count'],
source: this.hourlyData,
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'line',
label: {
backgroundColor: '#6a7985',
},
},
},
legend: {
data: [CHART_PROFIT, CHART_TRADE_COUNT],
right: '5%',
2020-08-17 19:15:15 +00:00
},
xAxis: {
type: 'category',
},
yAxis: [
{
type: 'value',
name: CHART_PROFIT,
splitLine: {
show: false,
},
nameRotate: 90,
nameLocation: 'middle',
nameGap: 30,
},
{
type: 'value',
name: CHART_TRADE_COUNT,
nameRotate: 90,
nameLocation: 'middle',
nameGap: 30,
},
],
2020-10-04 17:24:21 +00:00
visualMap: [
{
dimension: 1,
seriesIndex: 0,
show: false,
pieces: [
{
max: -0.001,
min: -2,
color: 'red',
},
{
min: -0.01,
max: 2,
color: 'green',
},
],
},
],
2020-08-17 19:15:15 +00:00
series: [
{
type: 'line',
name: CHART_PROFIT,
animation: false,
// symbol: 'none',
2020-08-17 19:15:15 +00:00
},
{
type: 'bar',
name: CHART_TRADE_COUNT,
animation: false,
2020-10-04 17:24:21 +00:00
itemStyle: {
color: 'rgba(150,150,150,0.3)',
},
2020-08-17 19:15:15 +00:00
yAxisIndex: 1,
},
],
};
}
}
</script>
2020-08-18 05:05:40 +00:00
<style scoped>
.echarts {
width: 100%;
height: 100%;
}
</style>