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

160 lines
4.6 KiB
TypeScript
Raw Normal View History

import { Component, OnInit } from "@angular/core";
import { Chart } from "chart.js";
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 {
isColorblind: boolean;
lineChart: Chart;
pollData: any;
2019-11-19 10:55:56 +01:00
yesList: number[] = [];
maybeList: number[] = [];
noList: number[] = [];
nbPoll: number = 0;
dateList: string[] = [];
constructor() {}
2019-10-23 15:39:16 +02:00
ngOnInit() {
2019-11-19 10:55:56 +01:00
var toto = {
step: 0,
stepMax: 3,
pollType: "special dates",
title: "",
description: "",
myName: "",
visibility: "link_only",
// date specific poll
allowSeveralHours: "true",
dateLgfgfgfgist: ["jeudi", "vendredi", "samedi"], // sets of days as strings
timeList: ["08:00", "08:30", "09:00"], // ranges of time expressed as strings
answers: [
{
id: 0,
text: "no"
},
{
id: 1,
text: "yes"
},
{
id: 2,
text: "maybe"
},
{
id: 3,
text: "maybe"
},
{
id: 4,
text: "maybe"
},
{
id: 5,
text: "maybe"
},
{
id: 6,
text: "maybe"
},
{
id: 7,
text: "maybe"
},
{
id: 8,
text: "maybe"
}
]
};
2019-10-23 15:39:16 +02:00
2019-11-19 10:55:56 +01:00
this.formatDataAnswers(toto);
2019-10-23 10:45:30 +02:00
this.isColorblind = false;
2019-11-18 10:19:28 +01:00
this.pollData = new Chart(document.getElementById("graph"), {
2019-11-19 10:55:56 +01:00
type: "horizontalBar",
data: {
2019-11-19 10:55:56 +01:00
labels: ["jeudi"],
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: {
2019-11-19 10:55:56 +01:00
legend: { display: false },
scales: {
xAxes: [
{
gridLines: { drawBorder: false, display: false },
display: false,
stacked: true,
ticks: {
beginAtZero: true,
maxRotation: 0,
minRotation: 0
}
}
],
yAxes: [
{
gridLines: { drawBorder: true, display: false },
display: true,
stacked: true
}
]
}
}
2019-11-19 10:55:56 +01:00
});
}
setColorblind() {
this.isColorblind = !this.isColorblind;
}
2019-11-19 10:55:56 +01:00
formatDataAnswers(poll) {
if (poll && poll.pollType === "special dates") {
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++;
this.dateList[this.nbPoll -1] = "jeudi";
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
}