回答編集履歴

1

canvasのサイズ固定にしておく

2019/11/28 16:14

投稿

rururu3
rururu3

スコア5545

test CHANGED
@@ -20,19 +20,53 @@
20
20
 
21
21
  <script src="https://d3js.org/d3.v4.min.js"></script>
22
22
 
23
-
24
-
25
23
  </head>
26
24
 
27
25
  <body>
28
26
 
27
+ <div class="chart-container" style="position: relative; height:400px; width:400px">
28
+
29
- <canvas id="myChart" width="400" height="400"></canvas>
29
+ <canvas id="myChart"></canvas>
30
+
31
+ </div>
30
32
 
31
33
  <input type="file" id="input" onchange="handleFiles(this.files)">
32
34
 
33
35
 
34
36
 
35
37
  <script type="text/javascript">
38
+
39
+ // 先にチャート作成しておく
40
+
41
+ var ctx = document.getElementById("myChart");
42
+
43
+ var myChart = new Chart(ctx,{
44
+
45
+ type:"scatter",
46
+
47
+ data: {
48
+
49
+ datasets:[{
50
+
51
+ label:"実験",
52
+
53
+ data: [],
54
+
55
+ }]
56
+
57
+ },
58
+
59
+ options: {
60
+
61
+ responsive: true,
62
+
63
+ maintainAspectRatio: false
64
+
65
+ }
66
+
67
+ });
68
+
69
+
36
70
 
37
71
  function handleFiles(files) {
38
72
 
@@ -56,25 +90,17 @@
56
90
 
57
91
  });
58
92
 
59
- var ctx = document.getElementById("myChart");
93
+
60
94
 
61
- var myChart = new Chart(ctx,{
95
+ // データを入れ直す
62
96
 
63
- type:"scatter",
97
+ myChart.data.datasets[0].data = list;
64
98
 
65
- data: {
99
+
66
100
 
67
- datasets:[{
101
+ // グラフ更新
68
102
 
69
- label:"実験",
103
+ myChart.update();
70
-
71
- data: list,
72
-
73
- }]
74
-
75
- }
76
-
77
- })
78
104
 
79
105
  }
80
106