aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/app/_elements/_charts/pie-chart/pie-chart.component.ts
blob: cba5252ace6fe589b1366c82e4c89e82cc3a73c8 (plain) (blame)
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
import { AfterViewInit, Component, ElementRef, Input, OnInit, ViewChild } from '@angular/core';
import {Chart} from 'chart.js';

@Component({
  selector: 'app-pie-chart',
  templateUrl: './pie-chart.component.html',
  styleUrls: ['./pie-chart.component.css']
})
export class PieChartComponent implements AfterViewInit {

  @Input()width?: number;
  @Input()height?: number;
  @Input()uniqueValues?: string[] = [];
  @Input()uniqueValuesPercent?: number[] = [];

  updatePieChart(uniqueValues: string[], uniqueValuesPercent: number[]){
    //console.log(this.uniqueValues, this.uniqueValuesPercent);
    this.pieChartData.datasets =  [{
        label: "%",
        backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850", "#000000"],
        data: uniqueValuesPercent,
      }];
      this.pieChartData.labels = uniqueValues
      console.log(this.uniqueValues, this.uniqueValuesPercent);
      this.myChart?.update() 
    };
  
  @ViewChild('piechart') chartRef!: ElementRef;
  constructor() { }

  pieChartData = {
    datasets: [{
      label: "Population (millions)",
      backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850"],
      data: [2478,5267,734,784,433]
    }], labels : [""]
}

  myChart?: Chart;
  ngAfterViewInit(): void {
    let rem = 100;
    const percents : number[] = []
    this.uniqueValuesPercent?.forEach(percent => {
      rem-=percent*100;
      percents.push(percent*100)
      
    })
    const data = {
      datasets: [{
        label: "%",
        backgroundColor: ["#3e95cd", "#8e5ea2","#3cba9f","#e8c3b9","#c45850", "#000000"],
        data: [...percents, rem]
      }], labels : [...this.uniqueValues!,"Ostalo"]
    }
    
    const myChart = new Chart(this.chartRef.nativeElement, {
    type: 'pie',
    data: data     
  ,
    options: {
      /*title: {
        display: true,
        text: 'Predicted world population (millions) in 2050'
      }*/
      plugins:{   
        legend: {
          display: false
                },
             },
             layout: {
              padding: 15
            }
    }
  });}

}