Skip to content

Commit

Permalink
feat: add download chart fufllsize
Browse files Browse the repository at this point in the history
  • Loading branch information
trinhvanminh committed Aug 16, 2024
1 parent b46cfb2 commit f2a3cf7
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ <h2>Lãi suất</h2>

<div id="interest-rate-list"></div>
</section>
<div class="download-chart-container">
<canvas id="full-size-chart"></canvas>
</div>
</main>

<script type="module" src="/src/index.ts"></script>
</body>
</html>
7 changes: 7 additions & 0 deletions src/assets/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ button:hover {
margin-top: 0;
}

.download-chart-container {
width: 1000px;
position: absolute;
top: 0;
right: -1000px;
}

@media (max-width: 768px) {
body {
padding: 0;
Expand Down
19 changes: 16 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChartDataset } from "chart.js";
import chart from "./main";
import chart, { fullSizeChart } from "./main";
import {
calculateCompoundingInterest,
color,
Expand Down Expand Up @@ -128,7 +128,14 @@ addDatasetBtn.addEventListener("click", () => {
});

downloadButton.addEventListener("click", () => {
const base64Image = chart.toBase64Image("image/png", 1);
const datasets = getDatasets();
console.log("datasets", datasets);
fullSizeChart.data.datasets = datasets;
fullSizeChart.data.labels = months(lengthOfTimeInMonths);

fullSizeChart.update();

const base64Image = fullSizeChart.toBase64Image("image/png", 1);
const link = document.createElement("a");
link.href = base64Image;
link.download = "chart.png";
Expand Down Expand Up @@ -223,9 +230,15 @@ function renderInterestRateList() {

function updateChart() {
const datasets = getDatasets();
const labels = months(lengthOfTimeInMonths);

chart.data.datasets = datasets;
chart.data.labels = months(lengthOfTimeInMonths);
chart.data.labels = labels;
chart.update();

fullSizeChart.data.datasets = datasets;
fullSizeChart.data.labels = labels;
fullSizeChart.update();
}

window.addEventListener("load", () => {
Expand Down
51 changes: 51 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const ctx = document.getElementById(
"compounding-interest-chart"
) as HTMLCanvasElement;

const fullSizeCtx = document.getElementById(
"full-size-chart"
) as HTMLCanvasElement;

const getOrCreateLegendList = (_chart: Chart, id: string) => {
const legendContainer = document.getElementById(id) as HTMLDivElement;
let listContainer = legendContainer.querySelector("ul");
Expand Down Expand Up @@ -166,4 +170,51 @@ const chart = new Chart(ctx, {
},
} as ChartConfiguration);

export const fullSizeChart = new Chart(fullSizeCtx, {
type: "line",
data: {
labels: months(),
datasets: [
{
label: "Kỳ hạn n (i%)",
data: [0],
},
],
},
plugins: [htmlLegendPlugin],
options: {
maintainAspectRatio: true,
aspectRatio: 2 / 1,
scales: {
y: {
ticks: {
callback: function (value, _index, _ticks) {
return numeral(value).format("(0a)");
},
},
},
},
plugins: {
legend: {
position: "bottom",
labels: {
font: {
size: 12,
},
},
},
htmlLegend: {
containerID: "legend-container",
},
title: {
display: true,
text: "LÃI KÉP",
font: {
size: 24,
},
},
},
},
} as ChartConfiguration);

export default chart;

0 comments on commit f2a3cf7

Please sign in to comment.