Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. import { Component, OnInit } from "@angular/core";
  2. import { groupBy, values, maxBy } from "lodash";
  3. import { TopicsService } from "../../services/topics.service";
  4. import { Article, Keyword, RootObject, Topic } from "../../models/TopicModel";
  5. import {
  6. ChartDataSets,
  7. ChartOptions,
  8. ChartType,
  9. RadialChartOptions
  10. } from "chart.js";
  11. import { Label } from "ng2-charts";
  12. import { plainToClass } from "class-transformer";
  13. import { ActivatedRoute, Params } from "@angular/router";
  14.  
  15. @Component({
  16. selector: "app-show",
  17. templateUrl: "./show.component.html",
  18. styleUrls: ["./show.component.scss"]
  19. })
  20. export class ShowComponent implements OnInit {
  21. displayedColumns: string[] = ["date", "grouping"];
  22. radarChartOptions: RadialChartOptions = { responsive: true };
  23. radarChartLabels: Label[] = [];
  24. radarChartData: ChartDataSets[] = [];
  25. radarChartDataValues: number[] = [];
  26. radarChartType: ChartType = "radar";
  27.  
  28. barChartOptions: ChartOptions = { responsive: true };
  29. barChartType: ChartType = "bar";
  30. barChartLegend = true;
  31. barChartPlugins = [];
  32.  
  33. scatterChartOptions: ChartOptions = {
  34. responsive: true,
  35. legend: { display: false }
  36. };
  37. scatterChartType: ChartType = "scatter";
  38. scatterChartDataDates: number[] = [];
  39. scatterChartData: ChartDataSets[] = [{ radius: 3, pointStyle: "cross" }];
  40. scatterChartLabels: string[] = [];
  41.  
  42. showClicked = 0;
  43. topic: Topic;
  44. keyword: Keyword;
  45. article: Article;
  46.  
  47. rootObject;
  48. topics;
  49. topicsObject;
  50. status = "Show data (it takes a second...)";
  51. topicId;
  52. dateCounter: number[];
  53. constructor(
  54. private topicsService: TopicsService,
  55. private activatedRoute: ActivatedRoute
  56. ) {}
  57. ngOnInit() {
  58. this.topicId = this.activatedRoute.snapshot.paramMap.get("id");
  59. }
  60.  
  61. getTopics() {
  62. this.status = "Processing...";
  63. this.topicsService.getTopics(this.topicId).subscribe(response => {
  64. this.topicsObject = response;
  65. this.rootObject = plainToClass(RootObject, this.topicsObject);
  66. this.topics = this.rootObject.topics;
  67. this.status = "Complete!";
  68. console.log(this.topics);
  69. });
  70. }
  71. logTopics() {
  72. this.getTopics();
  73. }
  74.  
  75. printTopics(id: number) {
  76. this.radarChartLabels = [];
  77. this.radarChartData = [];
  78. this.radarChartDataValues = [];
  79. this.scatterChartData = [];
  80. this.scatterChartLabels = [];
  81. this.topic = this.topics.find(x => x.id === id);
  82. for (this.keyword of this.topic.keywords) {
  83. this.radarChartLabels.push(this.keyword.word);
  84. this.radarChartDataValues.push(this.keyword.score);
  85. }
  86.  
  87. const years = [
  88. ...new Set(
  89. this.topic.articles.map(a => new Date(a.date).getFullYear())
  90. )
  91. ];
  92.  
  93. for (const year of years) {
  94. const articles = this.topic.articles.filter(
  95. a => new Date(a.date).getFullYear() == year
  96. );
  97. const articlesByTitle = groupBy(articles, "title");
  98.  
  99. const mostPopularArticleTitle = maxBy(
  100. values(articlesByTitle),
  101. x => x.length
  102. )[0].title;
  103.  
  104. this.scatterChartLabels.push(mostPopularArticleTitle); // Bug: https://github.com/valor-software/ng2-charts/issues/1126
  105. this.scatterChartData.push({
  106. data: [
  107. {
  108. x: year,
  109. y: articlesByTitle[mostPopularArticleTitle].length
  110. }
  111. ],
  112. label: mostPopularArticleTitle
  113. });
  114. }
  115.  
  116. this.radarChartData.push({
  117. data: this.radarChartDataValues,
  118. label: "Keywords"
  119. });
  120.  
  121. this.showClicked = 1;
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement