Guest User

Untitled

a guest
Dec 12th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. import { Component, Input, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
  2. import { StateService } from '../../../shared/services/state.service';
  3. import { State } from '../../../shared/interfaces';
  4. import { Chart } from 'chart.js';
  5.  
  6. @Component({
  7. selector: 'app-state',
  8. templateUrl: './state.component.html',
  9. styleUrls: ['./state.component.css']
  10. })
  11. export class StateComponent implements AfterViewInit {
  12.  
  13. @Input('reports_id') reports_id: string
  14. @ViewChild('state') stateRef: ElementRef
  15.  
  16. constructor(
  17. private stateService: StateService
  18. ) { }
  19.  
  20.  
  21. ngAfterViewInit() {
  22. const stateConfig: any = {
  23. label: 'Density',
  24. backgroundColor: '#009688',
  25. borderColor: '#00796b'
  26. }
  27.  
  28. const visconsityConfig: any = {
  29. label: 'Visconsity',
  30. backgroundColor: '#009688',
  31. borderColor: '#00796b'
  32. }
  33.  
  34.  
  35. this.stateService.fetch(this.reports_id).subscribe((data: State[]) => {
  36. stateConfig.data = data.map(item => item.density)
  37. visconsityConfig.data = data.map(item => item.visconsity)
  38. stateConfig.labels = data.map(item => item.state_time)
  39. const stateCtx = this.stateRef.nativeElement.getContext('2d')
  40. new Chart(stateCtx, createChartConfig(stateConfig, visconsityConfig))
  41. }
  42. )
  43. }
  44. }
  45.  
  46. function createChartConfig({labels, data, label, borderColor, backgroundColor}, {}) {
  47. return {
  48. type: 'bar',
  49. options: {
  50. scales: {
  51. xAxes: [{
  52. stacked: true
  53. }],
  54. yAxes: [{
  55. stacked: true
  56. }]
  57. }},
  58. data: {
  59. labels,
  60. datasets: [
  61. {
  62. label, data,
  63. backgroundColor: backgroundColor,
  64. borderColor: borderColor,
  65. borderWidth: 1
  66. },
  67. {
  68. label, data,
  69. backgroundColor: backgroundColor,
  70. borderColor: borderColor,
  71. borderWidth: 1
  72. }
  73. ],
  74. }
  75. }
  76. }
Add Comment
Please, Sign In to add comment