回答編集履歴

1

TypeScript \(Angular4\) サンプル追加

2017/08/25 03:25

投稿

shimitei
shimitei

スコア799

test CHANGED
@@ -95,3 +95,101 @@
95
95
  </html>
96
96
 
97
97
  ```
98
+
99
+
100
+
101
+ TypeScriptサンプルを追加します。
102
+
103
+ Angular-Cli 1.3.2 (Angular 4.3.6) + Chart.js 2.6.0で動作しています。
104
+
105
+
106
+
107
+ ```TypeScript
108
+
109
+ import { Component, OnInit } from '@angular/core';
110
+
111
+
112
+
113
+ @Component({
114
+
115
+ selector: 'app-chart',
116
+
117
+ template: '<div><button (click)="clicked($event)">Update</button><canvas id="graph1"></canvas></div>'
118
+
119
+ })
120
+
121
+ export class ChartComponent implements OnInit {
122
+
123
+ cnt = 0;
124
+
125
+ myChart;
126
+
127
+
128
+
129
+ constructor() { }
130
+
131
+
132
+
133
+ ngOnInit() {
134
+
135
+ const element = <HTMLCanvasElement>document.getElementById('graph1');
136
+
137
+ const ctx = element.getContext('2d');
138
+
139
+ this.myChart = new Chart(ctx, {type: 'bar', data: this.chartdata([])});
140
+
141
+ }
142
+
143
+
144
+
145
+ private chartdata(newData:Array<number>) {
146
+
147
+ return {
148
+
149
+ labels: ["1月","2月","3月"],
150
+
151
+ datasets: [
152
+
153
+ {
154
+
155
+ label: "件数",
156
+
157
+ fillColor: "rgba(220,220,220,0.5)",
158
+
159
+ strokeColor: "rgba(220,220,220,0.8)",
160
+
161
+ highlightFill: "rgba(220,220,220,0.75)",
162
+
163
+ highlightStroke: "rgba(220,220,220,1)",
164
+
165
+ data: newData
166
+
167
+ }
168
+
169
+ ]
170
+
171
+ };
172
+
173
+ }
174
+
175
+
176
+
177
+ clicked(event) {
178
+
179
+ this.cnt++;
180
+
181
+ const newData = ((this.cnt & 1) === 1)
182
+
183
+ ? [10, 20, 30]
184
+
185
+ : [30, 20, 10];
186
+
187
+ this.myChart.data = this.chartdata(newData);
188
+
189
+ this.myChart.update();
190
+
191
+ }
192
+
193
+ }
194
+
195
+ ```