-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.component.ts
493 lines (305 loc) · 12.5 KB
/
app.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import { Component, ViewChild ,ElementRef,OnInit, Inject,OnChanges } from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {ChartsModule, Color} from 'ng2-charts';
import{EnergyService} from '../app/services/wind.service';
import{IEnergy} from './Energy';
import 'chart.js/src/chart';
declare var options:any;
declare var Chart :any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers: [EnergyService]
})
export class AppComponent implements OnInit,OnChanges {
//@ViewChild('layout') canvasRef;
// @ViewChild("layout") layout: ElementRef;
@ViewChild('mycanvas')canvas:ElementRef;
public options:any;
public errorMsg :string ="10" ;
public numbers :number[] =[];
public errornumber:number;
// texts:string="'/n</br>'fjgidfjgidgh" ;
public numberss :IEnergy[];dataType:any[];
//public doughnutChartData:number[] ;
constructor(private _energyService:EnergyService){}
ngOnChanges(){
}
ngOnInit(){
console.log("ngInit hooked");
// var x= this._energyService.getEnergy().subscribe(
// (numberss)=> console.log(numberss=this.numberss),
// err=> this.errorMsg =<any>err);
// for (let i = 0; i < this.numberss.length; i++) {
// console.log(this.numberss);
// this.numbers= this.numberss[i].EnergyValue[0];
// }
// let ctx = this.canvas.nativeElement.getContext("2d");
// console.log(ctx);
// let me = this;
// this.options = {
// circumference: Math.PI,
// rotation : Math.PI,
// animation:{ onComplete: function() {
// me.doit(ctx);
// }}
// }
}
ngAfterViewInit() {
this._energyService.getEnergy().subscribe((response) => {this.numberss = response;
this.errornumber = response[0].EnergyValue;
// this.errornumber = 10;
this.doughnutChartOptions ={elements: {
center: {
// text: this.errornumber + "%" +"Energy produced by the Wind",
text:this.errornumber +"% <br>Energy produced <br> by Wind Today" ,
//fontColor: '#fff',
//fontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
}
}
};
console.log("hooked");
console.log(response[0].EnergyValue);
for(let item of response){
this.numbers.push(item.EnergyValue);
}
}),(err)=> {this.errorMsg =<any>err};
this.ng2(this.errornumber);
// Chart.pluginService.register({
// afterDraw: function (chart) {
// if (chart.config.options.elements.center) {
// var helpers = Chart.helpers;
// var centerX = (chart.chartArea.left + chart.chartArea.right) / 2;
// var centerY = (chart.chartArea.top + chart.chartArea.bottom) / 2;
// var ctx = chart.chart.ctx;
// ctx.save();
// var fontSize = helpers.getValueOrDefault(chart.config.options.elements.center.fontSize, Chart.defaults.global.defaultFontSize);
// var fontStyle = helpers.getValueOrDefault(chart.config.options.elements.center.fontStyle, Chart.defaults.global.defaultFontStyle);
// var fontFamily = helpers.getValueOrDefault(chart.config.options.elements.center.fontFamily, Chart.defaults.global.defaultFontFamily);
// var font = helpers.fontString(fontSize, fontStyle, fontFamily);
// ctx.font = font;
// ctx.fillStyle = helpers.getValueOrDefault(chart.config.options.elements.center.fontColor, Chart.defaults.global.defaultFontColor);
// ctx.textAlign = 'center';
// ctx.textBaseline = 'middle';
// ctx.fillText(chart.config.options.elements.center.text, centerX, centerY);
// ctx.restore();
// }
// },
// })
Chart.pluginService.register({
beforeDraw: function (chart) {
if (chart.config.options.elements.center) {
//Get ctx from string
var ctx = chart.chart.ctx;
//Get options from the center object in options
var centerConfig = chart.config.options.elements.center;
// var fontStyle = centerConfig.fontStyle || 'Arial';
var fontStyle = 'bold italic Arial';
var txt = centerConfig.text;
var color = centerConfig.color || '#000';
var sidePadding = centerConfig.sidePadding || 20;
var sidePaddingCalculated = (sidePadding/100) * (chart.innerRadius * 2)
// ctx.font = "22px" + fontStyle;
//Get the width of the string and also the width of the element minus 10 to give it 5px side padding
var stringWidth = ctx.measureText(txt).width;
var elementWidth = (chart.innerRadius * 2) - sidePaddingCalculated;
// Find out how much the font can grow in width.
var widthRatio = elementWidth / stringWidth;
var newFontSize = Math.floor(30 * widthRatio);
var elementHeight = (chart.innerRadius * 2);
// Pick a new font size so it will not be larger than the height of label.
var fontSizeToUse = Math.min(newFontSize, elementHeight);
//Set font settings to draw it correctly.
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
var centerX = ((chart.chartArea.left + chart.chartArea.right) / 2);
var centerY = ((chart.chartArea.top + chart.chartArea.bottom) / 2);
// ctx.font = fontSizeToUse+"px" + fontStyle;
//ctx.font ="10px italic Arial";
// console.log(ctx.font);
ctx.fillStyle = color;
// var img = new Image();
// // img.src = 'https://developers.google.com/maps/images/lhimages/api/icon_streetviewimageapi.svg';
// // var fillPattern = ctx.createPattern(img, 'repeat');
// var words = txt.split(' ');
// var line = '';
// for(var n = 0; n < words.length; n++) {
// var testLine = line + words[n] + ' ';
// var metrics = stringWidth;
// var testWidth = metrics.width;
// if (testWidth > stringWidth && n > 0) {
// ctx.fillText(line, centerX, centerY);
// line = words[n] + ' ';
// centerY += elementHeight;
// }
// else {
// line = testLine;
// }
// }
// console.log(centerX);
// if(centerX<376){
// var array = txt.split("<br>");
// for (var i = 0; i < array.length; i++) {
// ctx.fillText(array[i], centerX,centerY);
// centerY += 23;
// }
// }
// else{
// var array = txt.split("<br>");
// for (var i = 0; i < array.length; i++) {
// ctx.fillText(array[i], centerX,centerY);
// centerY += 43;
// }
// }
// }
// }
//ctx.fillText(txt, centerX, centerY);
// ctx.font = "200"+"px" + "bold Arial";
var array = txt.split("<br>");
for (var i = 0; i < array.length; i++) {
// console.log(array);
if(i===0)
{
if(centerX<376)
{
var centerXX = ((chart.chartArea.left + chart.chartArea.right) / 2);
var centerYY = ((chart.chartArea.top + chart.chartArea.bottom) / 2.3);
ctx.font ="30pt Arial";
ctx.fillText(array[0],centerXX,centerYY);
centerY += 23;
}
else
{
var centerXX = ((chart.chartArea.left + chart.chartArea.right) / 2);
var centerYY = ((chart.chartArea.top + chart.chartArea.bottom) / 2.3);
ctx.font ="30pt Arial";
ctx.fillText(array[0],centerXX,centerYY);
centerYY += 53;
}
}
else if(i!=0){
if(centerX<376)
{
alert("x");
// var centerXXX = ((chart.chartArea.left + chart.chartArea.right) / 2);
// var centerYYY = ((chart.chartArea.top + chart.chartArea.bottom) / 1.7);
ctx.font = "10pt bold arial",
ctx.fillText(array[i], centerX,centerY);
centerY += 20;
}
else{
console.log(centerX);
ctx.font = "20pt bold arial",
ctx.fillText(array[i],centerX,centerY);
centerY += 60;
}
}
// ctx.font ="20px italic Arial";
// var centerX = ((chart.chartArea.left + chart.chartArea.right) / 2);
// var centerY = ((chart.chartArea.top + chart.chartArea.bottom) / 2.1);
//ctx.fillText(array[i], centerX,centerY);
}
//wrapText(ctx, txt, centerX, centerY, widthRatio, elementHeight);
// console.log(txt);
// var lines = txt.split("\n");
// for (var i = 0; i < lines.length; ++i) {
// }
// var text = txt;
// // text.replace('E', '<br/><br/>');
// txt= text.replace(/\n/g, " " ).split( " " ) ;
// console.log(txt);
// ctx.fillText(txt, centerX, centerY);
//Draw text in center
//ctx.fillText(txt, centerX, centerY);
}
}
});
// let context = canvas.getContext('2d');
// let can = this.layout.nativeElement;
// let ctx = can.getContext('2d');
// ctx.fillStyle = "blue";
// ctx.font = "20pt Verdana";
// ctx.textAlign = "center";
// ctx.textBaseline = "middle";
// let step = 0;
// let steps = can.height + 50;
}
// let canvas = this.layout.nativeElement;
// // let context = canvas.getContext('2d');
// // var x = canvas.width / 2;
// // var y = canvas.height / 2;
// let context: CanvasRenderingContext2D = this.layout.nativeElement.getContext("2d");
// var x = canvas.width / 2;
// var y = canvas.height / 2;
// context.font = '90pt Calibri';
// context.textAlign = 'center';
// context.fillStyle = 'blue';
// context.fillText('Hello Worldetrereygerhg!', x, y);
// Method 2
// end of AfterViewInit
// doit(ctx) {
// alert("triggered");
// var width = this.canvas.nativeElement.clientWidth,
// height = this.canvas.nativeElement.clientHeight;
// var fontSize = (height / 250).toFixed(2);
// ctx.font = fontSize + "em Verdana";
// ctx.textBaseline = "middle";
// ctx.fillStyle = "blue";
// var text = "Pass Rate 82%",
// textX = Math.round((width - ctx.measureText(text).width) / 2),
// textY = height -10;
// ctx.fillText(text, textX, textY);
// ctx.restore();
// }
public doughnutChartOptions: any = {
cutoutPercentage:65,
responsive: true,
animaiton: {
animateRotate: true,
animateScale: true,
animationDuration: 111,
rotation:1000,
duration:8000
},
elements: {
center: {
// text: this.errornumber + "%" +"Energy produced by the Wind",
text:this.errornumber +"% <br>Energy produced <br> by Wind Today" ,
//fontColor: '#fff',
//fontFamily: "'Helvetica Neue', 'Helvetica', 'Arial', sans-serif",
fontFamily:"'Arial'",
//fontSize: ,
fontStyle: 'Bold Italic'
}
}
};
public pieChartLabels:string[] = ['Wind Energy', 'Total Energy', 'Nuclear Energy'];
public pieChartData:number[] =[300, 500, 10];
public pieChartType:string = 'pie';
public chartClicked(e:any):void {
}
public chartHovered(e:any):void {
}
ng2(numberss){
// widget 2
alert("2");
}
public doughnutChartLabels:string[] = ['Wind Energy'];
public doughnutChartData:number[] = this.numbers;
//public doughnutChartData:number[] ;
public doughnutChartType:string = 'doughnut';
public doughnutChartColors: any[] = [
{
backgroundColor: ["rgb(242, 163, 106)", "rgba(255,255,255,.3)"],
//borderColor: 'black'
}
];
// events
public chartClicked1(e:any):void {
console.log(e);
}
public chartHovered1(e:any):void {
console.log(e);
}
}