Skip to content

Commit 4c3fac5

Browse files
authored
Merge pull request #47 from ut-code/graph-layout
グラフのレイアウトを改善して、ダウンロードできるようにした
2 parents 4d1e19e + b5f93d1 commit 4c3fac5

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

desktop/frontend/src/lib/chart.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,72 @@
1-
import { type ChartConfiguration } from "chart.js";
1+
import { type ChartConfiguration, type Plugin } from "chart.js";
22

33
type Vertex = {
44
x: number;
55
y: number;
66
};
7+
78
export function setupPlot(values: number[]): ChartConfiguration {
8-
const rawData: Vertex[] = [];
9-
console.log(values);
9+
const dataPoints: Vertex[] = [];
10+
11+
const xValues = []
12+
const yValues = []
13+
1014
for (let i = 0; i < values.length; i += 2) {
11-
rawData.push({
12-
x: values[i],
13-
y: values[i + 1],
15+
const x = values[i];
16+
const y = values[i + 1];
17+
xValues.push(x)
18+
yValues.push(y)
19+
dataPoints.push({
20+
x,
21+
y,
1422
});
1523
}
24+
25+
xValues.sort()
26+
yValues.sort()
27+
const xRange = xValues[xValues.length - 1] - xValues[0]
28+
const yRange = yValues[yValues.length - 1] - yValues[0]
29+
const marginRatio = 0.1
30+
const xAxisMax = xValues[xValues.length - 1] + xRange * marginRatio
31+
const xAxisMin = xValues[0] - xRange * marginRatio
32+
const yAxisMax = yValues[yValues.length - 1] + yRange * marginRatio
33+
const yAxisMin = yValues[0] - yRange * marginRatio
1634
const data = {
1735
datasets: [
1836
{
1937
label: "Scatter Dataset",
20-
data: rawData,
38+
data: dataPoints,
2139
backgroundColor: "white",
2240
borderColor: "black",
2341
},
2442
],
2543
};
2644

45+
const plugin: Plugin = {
46+
id: 'customCanvasBackgroundColor',
47+
beforeDraw: (chart, args, options) => {
48+
const {ctx} = chart;
49+
ctx.save();
50+
ctx.globalCompositeOperation = 'destination-over';
51+
ctx.fillStyle = options.color || '#FFFFFF';
52+
ctx.fillRect(0, 0, chart.width, chart.height);
53+
ctx.restore();
54+
}
55+
};
2756
const config: ChartConfiguration = {
2857
type: "scatter",
2958
data: data,
59+
plugins: [plugin],
3060
options: {
3161
scales: {
3262
x: {
33-
type: "linear",
34-
position: "bottom",
63+
max: xAxisMax,
64+
min: xAxisMin
3565
},
66+
y: {
67+
max: yAxisMax,
68+
min: yAxisMin
69+
}
3670
},
3771
},
3872
};

desktop/frontend/src/lib/components/Chart.svelte

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,23 @@
6666
}
6767
return false;
6868
}
69+
function exportImage() {
70+
const imageData = canvasRef.toDataURL("image/png");
71+
const link = document.createElement("a");
72+
link.href = imageData;
73+
link.download = `chart-${new Date().getTime()}.png`;
74+
link.click();
75+
}
6976
</script>
7077

7178
<dialog class="modal" bind:this={dialogRef}>
72-
<div class="relative h-96 w-128">
73-
<form method="dialog">
74-
<button class="btn">Close</button>
75-
</form>
79+
<div class="modal-box">
80+
<button aria-label="export as image" class="btn btn-sm absolute top-2 right-6" onclick={exportImage}>
81+
<svg class="w-5 h-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><g id="SVGRepo_bgCarrier" stroke-width="0"></g><g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g><g id="SVGRepo_iconCarrier"> <path d="M5.25589 16C3.8899 15.0291 3 13.4422 3 11.6493C3 9.20008 4.8 6.9375 7.5 6.5C8.34694 4.48637 10.3514 3 12.6893 3C15.684 3 18.1317 5.32251 18.3 8.25C19.8893 8.94488 21 10.6503 21 12.4969C21 14.0582 20.206 15.4339 19 16.2417M12 21V11M12 21L9 18M12 21L15 18" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"></path> </g></svg>
82+
</button>
7683
<canvas bind:this={canvasRef} class="bg-white"></canvas>
7784
</div>
85+
<form method="dialog" class="modal-backdrop">
86+
<button>close</button>
87+
</form>
7888
</dialog>

0 commit comments

Comments
 (0)