funky-framadate-front/src/app/poll-graphic/poll-graphic.component.ts

92 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-01-15 11:40:39 +01:00
import {Component, Inject, OnInit} from "@angular/core";
2019-11-19 14:53:53 +01:00
import {Chart} from "chart.js";
2020-01-15 11:40:39 +01:00
import {DOCUMENT} from '@angular/common';
2020-01-20 14:36:26 +01:00
import {mockGraphConfig} from "../config/mocks/mock-graph";
import {graphOptions} from "../config/graph-canevas-options";
import {ConfigService} from "../services/config.service";
2020-01-20 14:36:26 +01:00
import {mockPoll3} from "../config/mocks/mock-poll3";
2019-10-23 10:45:30 +02:00
@Component({
selector: "framadate-poll-graphic",
templateUrl: "./poll-graphic.component.html",
styleUrls: ["./poll-graphic.component.scss"]
2019-10-23 10:45:30 +02:00
})
export class PollGraphicComponent implements OnInit {
2019-11-19 14:53:53 +01:00
isColorblind: boolean = false;
pollConfigRetrieved: any = mockPoll3;
graphicConfig: any = mockGraphConfig;
2020-01-16 10:20:15 +01:00
preferred: any = "rien";
2019-11-19 10:55:56 +01:00
yesList: number[] = [];
maybeList: number[] = [];
noList: number[] = [];
nbPoll: number = 0;
dateList: string[] = [];
2019-11-19 14:53:53 +01:00
constructor(@Inject(DOCUMENT) private document: any,
private config: ConfigService) {
2019-11-19 14:53:53 +01:00
}
2019-10-23 15:39:16 +02:00
ngOnInit() {
this.formatDataAnswers(this.graphicConfig);
this.isColorblind = false;
this.pollConfigRetrieved = new Chart(this.document.getElementById("graph"), {
2019-11-19 10:55:56 +01:00
type: "horizontalBar",
data: {
labels: this.pollConfigRetrieved.choices.map(choice => choice.name),
2019-11-19 10:55:56 +01:00
datasets: [
{
type: "horizontalBar",
stack: "Yes",
backgroundColor: "#429a00",
data: this.yesList
},
{
type: "horizontalBar",
stack: "Yes",
backgroundColor: "#f5a623",
data: this.maybeList
},
{
type: "horizontalBar",
stack: "No",
backgroundColor: "#cd0000",
data: this.noList
}
]
},
options: graphOptions
2019-11-19 10:55:56 +01:00
});
}
2019-11-19 14:53:53 +01:00
toggleColorblind() {
this.isColorblind = !this.isColorblind;
}
2019-11-19 10:55:56 +01:00
formatDataAnswers(poll) {
// if (poll && poll.pollType === "date") {
this.initPollCounter();
poll.answers.forEach(response => {
switch (response.text) {
case "yes":
this.yesList[this.nbPoll - 1]++;
break;
case "maybe":
this.maybeList[this.nbPoll - 1]++;
break;
case "no":
this.noList[this.nbPoll - 1]++;
break;
}
});
// }
}
2019-11-19 10:55:56 +01:00
initPollCounter() {
this.nbPoll++;
2019-11-19 14:53:53 +01:00
this.dateList[this.nbPoll - 1] = "jeudi";
2019-11-19 10:55:56 +01:00
this.maybeList[this.nbPoll - 1] = 0;
this.yesList[this.nbPoll - 1] = 0;
this.noList[this.nbPoll - 1] = 0;
}
2019-10-23 10:45:30 +02:00
}