You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.3 KiB

3 years ago
  1. export type Label = {[key: string]: number}
  2. export type User = {[key: string]: number}
  3. export class Statistics {
  4. constructor(
  5. public label: Label,
  6. public userLabel: User,
  7. public total: number,
  8. public remaining: number,
  9. public user: User
  10. ) {}
  11. static valueOf(
  12. { label, user_label, total, remaining, user }:
  13. {
  14. label: Label,
  15. user_label: User,
  16. total: number,
  17. remaining: number,
  18. user: User
  19. }
  20. ): Statistics {
  21. return new Statistics(label, user_label, total, remaining, user)
  22. }
  23. private makeData(object: Label | User, label: string) {
  24. const labels = object ? Object.keys(object) : []
  25. const counts = object ? Object.values(object) : []
  26. return {
  27. labels,
  28. datasets: [{
  29. label,
  30. backgroundColor: '#00d1b2',
  31. data: counts
  32. }]
  33. }
  34. }
  35. public labelStats(label: string) {
  36. return this.makeData(this.label, label)
  37. }
  38. public userStats(label: string) {
  39. return this.makeData(this.user, label)
  40. }
  41. public progress(labels: string[]) {
  42. const complete = this.total - this.remaining
  43. const incomplete = this.remaining
  44. return {
  45. datasets: [{
  46. data: [complete, incomplete],
  47. backgroundColor: ['#00d1b2', '#ffdd57']
  48. }],
  49. labels
  50. }
  51. }
  52. }