Skip to content
Snippets Groups Projects
statistic.component.ts 3.83 KiB
Newer Older
import { Component, OnInit } from '@angular/core';
import { Chart } from 'chart.js';
import { ActivatedRoute } from '@angular/router';
import { ContentService } from '../../../services/http/content.service';
import { ContentChoice } from '../../../models/content-choice';
import { TranslateService } from '@ngx-translate/core';
import { LanguageService } from '../../../services/util/language.service';
Lukas Mauß's avatar
Lukas Mauß committed
export class AnswerList {
  label: string;
  answer: string;
Lukas Mauß's avatar
Lukas Mauß committed
  constructor(label: string, answer: string) {
    this.label = label;
    this.answer = answer;

@Component({
  selector: 'app-statistic',
  templateUrl: './statistic.component.html',
  styleUrls: ['./statistic.component.scss']
})
export class StatisticComponent implements OnInit {

Lukas Mauß's avatar
Lukas Mauß committed
  chart = Chart;
  colors: string[] = ['rgba(33,150,243, 0.8)', 'rgba(76,175,80, 0.8)', 'rgba(255,235,59, 0.8)', 'rgba(244,67,54, 0.8)',
                      'rgba(96,125,139, 0.8)', 'rgba(63,81,181, 0.8)', 'rgba(233,30,99, 0.8)', 'rgba(121,85,72, 0.8)'];
Lukas Mauß's avatar
Lukas Mauß committed
  label = 'ABCDEFGH';
  labels: string[]; // = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'];
  answers: string[];
Lukas Mauß's avatar
Lukas Mauß committed
  answerList: AnswerList[];
  contentId: string;
  subject: string;
  maxLength: number;
  showsCorrect = false;
  correctIndexes: number[];
  constructor(protected route: ActivatedRoute,
              private contentService: ContentService,
              private translateService: TranslateService,
              protected langService: LanguageService) {
              langService.langEmitter.subscribe(lang => translateService.use(lang));
  }
    this.translateService.use(localStorage.getItem('currentLang'));
    this.maxLength = innerWidth / 12;
Lukas Mauß's avatar
Lukas Mauß committed
    this.answers = new Array<string>();
    this.labels = new Array<string>();
Lukas Mauß's avatar
Lukas Mauß committed
    this.answerList = new Array<AnswerList>();
    this.correctIndexes = new Array<number>();
    this.route.params.subscribe(params => {
      this.contentId = params['contentId'];
    });
    this.contentService.getChoiceContent(this.contentId).subscribe(content => {
      this.getData(content);
  switchAnswers() {
    if (this.showsCorrect === false) {
      this.showCorrect();
    } else {
      this.showNormal();
    }
  }

  showCorrect() {
    this.showsCorrect = true;
  }

  showNormal() {
    this.showsCorrect = false;
  }

  getData(content: ContentChoice) {
    this.subject = content.subject;
    const length = content.options.length;
    for (let i = 0; i < length; i++) {
Lukas Mauß's avatar
Lukas Mauß committed
      this.answerList[i] = new AnswerList(null, null);
Lukas Mauß's avatar
Lukas Mauß committed
      this.labels[i] = this.label.charAt(i);
Lukas Mauß's avatar
Lukas Mauß committed
      this.answerList[i].label = this.labels[i];
      if (content.options[i].label.length > this.maxLength) {
        this.answerList[i].answer = content.options[i].label.substr(0, this.maxLength) + '..';
Lukas Mauß's avatar
Lukas Mauß committed
      } else {
Lukas Mauß's avatar
Lukas Mauß committed
        this.answerList[i].answer = content.options[i].label;
Lukas Mauß's avatar
Lukas Mauß committed
      }
    this.translateService.get('statistic.abstentions').subscribe(label => {
      this.labels.push(label);
    });
    this.contentService.getAnswer(content.id).subscribe(answer => {
      this.data = answer.roundStatistics[0].independentCounts;
      this.data.push(answer.roundStatistics[0].abstentionCount);
      this.chart = new Chart('chart', {
        type: 'bar',
        data: {
          labels: this.labels,
          datasets: [{
            data: this.data,
            backgroundColor: this.colors
          }]
Lukas Mauß's avatar
Lukas Mauß committed
          tooltips: {
Lukas Mauß's avatar
Lukas Mauß committed
            mode: 'index'
Lukas Mauß's avatar
Lukas Mauß committed
          maintainAspectRatio: false,
          scales: {
            yAxes: [{
              ticks: {
                beginAtZero: true,
                precision: 0
              }
            }]
          }
        }
      });