Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Add gains chart #17

Merged
merged 2 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<ClientOnly>
<Editor ref="editor" />
<button @click="backtest()">Backtest</button>
<button @click="showChart()">Show Chart</button>
<Chart v-if="chartVisable" :data="chartData" />
</ClientOnly>
</div>
</template>
Expand All @@ -22,6 +24,8 @@ const { data: stockData } = await useFetch<Ticker>(
`${config.public.apiBaseUrl}/tickers/2330.TW/historical-data?start=1980-01-01`,
);
const editor = ref<InstanceType<typeof Editor>>();
const chartVisable = ref(false);
const chartData = ref(null);

// prepare runtime
const stock = new Stock(stockData.value);
Expand Down Expand Up @@ -64,4 +68,21 @@ function backtest(): Backtest {
console.log(t);
return t;
}

function showChart() {
const result = backtest();
chartData.value = {
labels: stock.dates,
datasets: [
{
label: "Gain",
data: result.gains,
borderColor: "rgba(54, 162, 235, 1)",
backgroundColor: "rgba(54, 162, 235, 0.2)",
borderWidth: 1,
},
],
};
chartVisable.value = true;
}
</script>
45 changes: 45 additions & 0 deletions components/chart/Chart.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<template>
<canvas ref="chartCanvas"></canvas>
</template>

<script setup lang="ts">
import { Chart } from "chart.js";

const chartCanvas = ref<HTMLCanvasElement>(null);
const props = defineProps<{
data: any;
}>();
const chart = ref(null);

onMounted(() => {
chart.value = new Chart(chartCanvas.value, {
type: "line",
data: props.data,
options: {
elements: {
point: {
radius: 0,
},
},
},
});
});

watch(
() => props.data,
() => {
chart.value.destroy();
chart.value = new Chart(chartCanvas.value, {
type: "line",
data: props.data,
options: {
elements: {
point: {
radius: 0,
},
},
},
});
},
);
</script>
3 changes: 3 additions & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { defineNuxtConfig } from "nuxt";

// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
build: {
transpile: ["chart.js"],
},
runtimeConfig: {
public: {
apiBaseUrl: "",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"dependencies": {
"blockly": "^8.0.5",
"chart.js": "^3.9.1",
"indicatorts": "^1.0.10"
}
}
7 changes: 7 additions & 0 deletions plugins/chart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Chart, registerables } from "chart.js";

export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook("app:beforeMount", () => {
Chart.register(...registerables);
});
});
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.