Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.46 KB | None | 0 0
  1. import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
  2. import { ProfileService } from '../profile.service';
  3. import { IResponseUser, Profile, ILanguage } from '../../interfaces';
  4. import { ActivatedRoute, Router } from '@angular/router';
  5. import { UserService } from '../../shared/services/user.service';
  6. import { SharedService } from '../../shared/shared.service';
  7. import { ICity } from '../../interfaces/ICity';
  8. import { ICompetence } from '../../interfaces/ICompetence';
  9. import { Chart } from 'chart.js';
  10. import { BaseChartDirective } from 'ng2-charts/ng2-charts';
  11.  
  12. import * as moment from 'moment';
  13.  
  14. @Component({
  15. selector: 'app-account',
  16. templateUrl: './account.component.html',
  17. styleUrls: ['./account.component.scss']
  18. })
  19.  
  20. export class AccountComponent implements OnInit {
  21.  
  22. @ViewChild(BaseChartDirective) chart: BaseChartDirective;
  23.  
  24. @ViewChild('visitsChart')
  25. visitsChart: BaseChartDirective;
  26.  
  27. @ViewChild('detailsChart')
  28. detailsChart: BaseChartDirective;
  29.  
  30. dataRangemodel: any = {
  31. beginDate: { year: 2018, month: 10, day: 9 },
  32. endDate: { year: 2018, month: 10, day: 19 }
  33. };
  34.  
  35. currentSpec: string;
  36. public cities: ICity[];
  37. public profile: Profile;
  38. public languges: ILanguage[];
  39. public competencies: ICompetence[];
  40. private currentCity: string;
  41.  
  42. public chartColors: any[] = [{}];
  43.  
  44. public chartOptions = {
  45. responsive: true,
  46. maintainAspectRatio: false,
  47. hover: {
  48. mode: 'index'
  49. },
  50. scales: {
  51. yAxes: [{
  52. gridLines: {
  53. display: true,
  54. color: '#e6e6ed',
  55. drawBorder: true,
  56. tickMarkLength: 0,
  57. zeroLineColor: '#d5d5de'
  58. },
  59. ticks: {
  60. suggestedMin: 5,
  61. stepSize: 5,
  62. suggestedMax: 10,
  63. padding: 9,
  64. fontSize: 11,
  65. fontFamily: 'Montserrat',
  66. fontColor: '#000000'
  67. }
  68. }],
  69. xAxes: [{
  70. gridLines: {
  71. display: true,
  72. color: '#e6e6ed',
  73. drawBorder: false,
  74. tickMarkLength: 0,
  75. zeroLineColor: '#d5d5de',
  76. },
  77. ticks: {
  78. suggestedMin: 5,
  79. stepSize: 5,
  80. padding: 9,
  81. fontSize: 11,
  82. fontFamily: 'Montserrat',
  83. fontColor: '#000000',
  84. }
  85. }],
  86. },
  87. tooltips: {
  88. enabled: false,
  89. position: 'nearest',
  90. mode: 'index',
  91. intersect: false,
  92. custom: ((that: any) => {
  93. return function(tooltip) {
  94. that.customTooltips(tooltip, that, this);
  95. };
  96. })(this)
  97. },
  98. };
  99.  
  100. public visitsStatisticsChartLabels = ['', '19', '20', '21', '22', '23', '24'];
  101. public detailViewChartLabels = ['', '19', '20', '21', '22', '23', '24'];
  102.  
  103. public chartConfig = [
  104. {
  105. chartName: 'visitsChart',
  106. datasets: [
  107. {
  108. id: 0,
  109. hidden: false,
  110. name: 'user',
  111. point: 'circleGold',
  112. data: [0, 17, 17, 19, 16, 19, 42],
  113. borderColor: '#C68A54',
  114. label: 'ProfileViews',
  115. navTitle: 'My Page Views',
  116. tipTitle: 'Profile Views',
  117. iconClass: 'chart-icon--circle-gold',
  118. hiddenIconClass: 'chart-icon--circle-hidden',
  119. fill: false,
  120. pointRadius: 2,
  121. borderWidth: 2,
  122. },
  123. {
  124. id: 1,
  125. hidden: false,
  126. name: 'category',
  127. point: 'diamondGray',
  128. data: [0, 30, 22, 37, 20, 31, 49],
  129. borderColor: '#e6e6ed',
  130. label: 'CategoryViews',
  131. navTitle: 'Category Visits',
  132. tipTitle: 'Category Views',
  133. iconClass: 'chart-icon--diamond-gray',
  134. hiddenIconClass: 'chart-icon--diamond-hidden',
  135. fill: false,
  136. pointRadius: 2,
  137. borderWidth: 2,
  138. borderDash: [10, 5]
  139. },
  140. ],
  141. templateFunction: 'visitTemplate'
  142. },
  143. {
  144. chartName: 'detailsChart',
  145. datasets: [
  146. {
  147. id: 3,
  148. hidden: false,
  149. name: 'user',
  150. point: 'circleGray',
  151. data: [30, 40, 42, 50, 41, 49, 80],
  152. borderColor: '#e6e6ed',
  153. label: 'TotalProfileViews',
  154. navTitle: 'My page view',
  155. tipTitle: 'Total Profile Views',
  156. iconClass: 'chart-icon--circle-gray',
  157. hiddenIconClass: 'chart-icon--circle-hidden',
  158. fill: false,
  159. pointRadius: 2,
  160. borderWidth: 2,
  161. borderDash: [10, 5]
  162. },
  163. {
  164. id: 4,
  165. hidden: false,
  166. name: 'map',
  167. point: 'diamondBlue',
  168. data: [28, 10, 22, 40, 23, 39, 39],
  169. borderColor: '#8BBEFF',
  170. label: 'MapView',
  171. navTitle: 'Map checks',
  172. tipTitle: 'Map',
  173. iconClass: 'chart-icon--diamond-blue',
  174. hiddenIconClass: 'chart-icon--diamond-hidden',
  175. fill: false,
  176. pointRadius: 2,
  177. borderWidth: 2,
  178. },
  179. {
  180. id: 5,
  181. hidden: false,
  182. name: 'email',
  183. point: 'diamondRed',
  184. data: [0, 11, 9, 17, 10, 13, 20],
  185. borderColor: '#DF4F4F',
  186. label: 'EmailView',
  187. navTitle: 'Email checks',
  188. tipTitle: 'Email',
  189. iconClass: 'chart-icon--diamond-red',
  190. hiddenIconClass: 'chart-icon--diamond-hidden',
  191. fill: false,
  192. pointRadius: 2,
  193. borderWidth: 2,
  194. },
  195. {
  196. id: 6,
  197. hidden: false,
  198. name: 'phone',
  199. point: 'diamondGreen',
  200. data: [0, 0, 18, 0, 19, 28, 8],
  201. borderColor: '#8CCE7C',
  202. label: 'PhoneView',
  203. navTitle: 'Phone checks',
  204. tipTitle: 'Phone',
  205. iconClass: 'chart-icon--diamond-green',
  206. hiddenIconClass: 'chart-icon--diamond-hidden',
  207. fill: false,
  208. pointRadius: 2,
  209. borderWidth: 2,
  210. }
  211. ],
  212. templateFunction: 'detailsTemplate'
  213. }
  214. ];
  215.  
  216. public pointImageSources = {
  217. circleGray: '/assets/img/chart-icons/circle-gray.svg',
  218. circleGold: '/assets/img/chart-icons/circle-gold.svg',
  219. diamondGray: '/assets/img/chart-icons/diamond-gray.svg',
  220. diamondBlue: '/assets/img/chart-icons/diamond-blue.svg',
  221. diamondGreen: '/assets/img/chart-icons/diamond-green.svg',
  222. diamondRed: '/assets/img/chart-icons/diamond-red.svg'
  223. };
  224.  
  225. public customTooltips (tooltip, that, chart) {
  226. const chartName = chart._chart.canvas.id;
  227. const config = that.chartConfig.find(chartCfg => chartCfg.chartName === chartName);
  228. const positionY = chart._chart.canvas.getBoundingClientRect().y + window.pageYOffset;
  229. const positionX = chart._chart.canvas.getBoundingClientRect().x;
  230. const domTooltip = document.getElementById('tooltip');
  231.  
  232. if (!tooltip || !tooltip.opacity) {
  233. domTooltip.style.opacity = '0';
  234. return;
  235. }
  236.  
  237. if (tooltip.dataPoints.length > 0) {
  238. tooltip.dataPoints.forEach((dataPoint, i) => {
  239. const content = that[config.templateFunction](that, config, dataPoint);
  240.  
  241. domTooltip.innerHTML = content;
  242. domTooltip.style.opacity = '1';
  243.  
  244. domTooltip.style.top = positionY + dataPoint.y + 'px';
  245. domTooltip.style.left = positionX + dataPoint.x + 'px';
  246. });
  247. }
  248. }
  249.  
  250. public visitTemplate(that, config, dataPoint) {
  251. return config.datasets.filter(item => !item.hidden).map(item => {
  252. return `
  253. <div class='tooltip__row'>
  254. <span class='chart-icon ${ item.iconClass }'></span>
  255. <span class='tooltip__text'>${ item.tipTitle }: ${ item.data[dataPoint.index] }</span>
  256. </div>
  257. `;
  258. }).join('');
  259. }
  260.  
  261. public detailsTemplate(that, config, dataPoint) {
  262. const items = config.datasets.filter(item => !item.hidden).map(item => {
  263. return `
  264. <div class='tooltip__row'>
  265. <span class='chart-icon ${ item.iconClass }'></span>
  266. <span class='tooltip__text'>${ item.tipTitle }: ${ item.data[dataPoint.index] }</span>
  267. </div>
  268. `;
  269. });
  270.  
  271. items.splice(1, 0, '<div class="tooltip__row-splitter"></div>');
  272.  
  273. return items.join('');
  274. }
  275.  
  276. constructor(
  277. private _profileService: ProfileService,
  278. private _router: Router,
  279. private _route: ActivatedRoute,
  280. private _userService: UserService,
  281. private _sharedService: SharedService) { }
  282.  
  283. ngOnInit() {
  284. this.getLanguages();
  285. this.getProfileData();
  286. this.getCities();
  287. this.getAllSpecialization();
  288. this.getLawyerStatistics();
  289.  
  290. Chart.pluginService.register({
  291. afterUpdate: (chart) => {
  292. const chartName = chart.canvas.id;
  293. const config = this.chartConfig.find(chartCfg => chartCfg.chartName === chartName);
  294.  
  295. if (config) {
  296. chart.config.data.datasets.map(dataset => {
  297. dataset._meta[chart.id].data.forEach(dataItem => {
  298. const pointImg = new Image();
  299. pointImg.src = this.pointImageSources[dataset.point];
  300. dataItem._model.pointStyle = pointImg;
  301. });
  302. });
  303. }
  304. }
  305. });
  306. }
  307.  
  308. private _isCitySpecId(profile: Profile) {
  309. return !this.profile.city_id === true || !this.profile.competencies_id == true;
  310. }
  311.  
  312. public toggleActiveChartLine() {
  313. const el = event.currentTarget as HTMLElement;
  314. const datasetId = +el.getAttribute('data-dataset-id');
  315. const chartName = el.getAttribute('data-chart');
  316.  
  317. const chart = this[chartName] as BaseChartDirective;
  318.  
  319. const chartIndex = this.chartConfig.findIndex(config => config.chartName === chartName);
  320. const datasetIndex = this.chartConfig[chartIndex].datasets.findIndex(dataset => dataset.id === datasetId);
  321.  
  322.  
  323. const isHidden = this.chartConfig[chartIndex].datasets[datasetIndex].hidden;
  324.  
  325. // deny hide last active line in chart
  326. if (this.chartConfig[chartIndex].datasets.filter(item => !item.hidden).length === 1 && isHidden === false) {
  327. return;
  328. }
  329.  
  330. this.chartConfig[chartIndex].datasets[datasetIndex].hidden = !isHidden;
  331.  
  332. const chartObj = chart as any;
  333. chartObj.refresh();
  334. }
  335.  
  336. public mapStatistics(statistics) {
  337.  
  338. const datasets = [
  339. {
  340. statisticName:'user',
  341. chartName: 'detailsChart',
  342. data: []
  343. },
  344. {
  345. statisticName:'phone',
  346. chartName: 'detailsChart',
  347. data: []
  348. },
  349. {
  350. statisticName:'email',
  351. chartName: 'detailsChart',
  352. data: []
  353. },
  354. {
  355. statisticName:'map',
  356. chartName: 'detailsChart',
  357. data: []
  358. },
  359. {
  360. statisticName:'user',
  361. chartName: 'visitsChart',
  362. data: []
  363. },
  364. {
  365. statisticName:'category',
  366. chartName: 'visitsChart',
  367. data: []
  368. }
  369. ];
  370.  
  371. const dateAxis = this.parseDateAxis(statistics.visits_statistics);
  372.  
  373. const detailViews = this.parseChartData([
  374. 'email',
  375. 'phone',
  376. 'map',
  377. 'user'
  378. ], statistics.detail_views)
  379.  
  380. const visitsStatistics = this.parseChartData([
  381. 'category',
  382. 'user'
  383. ], statistics.visits_statistics)
  384.  
  385. const parsedStatistics = {...detailViews, ...visitsStatistics};
  386.  
  387. datasets.forEach(dataSet => {
  388. dataSet.data = parsedStatistics[dataSet.statisticName];
  389. })
  390.  
  391. this.setDateAxisForCharts(dateAxis)
  392. this.setValueAxisForCharts(datasets);
  393. }
  394.  
  395. public parseChartData(keys, data) {
  396. let parsedData = {}
  397.  
  398. keys.forEach(key => parsedData[key] = [])
  399.  
  400. data.forEach(dataItem => {
  401. keys.forEach(key => {
  402. const findItem = dataItem.data.find(statisticItem => statisticItem.type === key);
  403. if (findItem) {
  404. parsedData[key].push(findItem.count)
  405. } else {
  406. parsedData[key].push(0)
  407. }
  408. });
  409. });
  410.  
  411. return parsedData;
  412. }
  413.  
  414. public parseDateAxis(data) {
  415. return data.map(dataItem => moment(dataItem._id.day).format("D MMM"));
  416. }
  417.  
  418. public setDateAxisForCharts(date) {
  419. this.visitsStatisticsChartLabels = date;
  420. this.detailViewChartLabels = date;
  421. }
  422.  
  423. public setValueAxisForCharts(datasets) {
  424. datasets.forEach(dataSet => {
  425. const index = this.chartConfig.findIndex(chart => chart.chartName === dataSet.chartName);
  426. const datasetIndex = this.chartConfig[index].datasets.findIndex(dataset => dataset.name === dataSet.statisticName);
  427.  
  428. this.chartConfig[index].datasets[datasetIndex].data = dataSet.data;
  429. });
  430. }
  431.  
  432. getProfileData() {
  433. const id = this._profileService.currentUserData._id;
  434. this._profileService.getCurrentProfile(id).subscribe((res: IResponseUser) => {
  435. this.profile = res.response;
  436.  
  437. this.profile.languages = this.profile.languages.map(userLang => {
  438. return this.languges.find(lang => userLang == lang.id);
  439. });
  440.  
  441. this._profileService.currentUserData = this.profile;
  442.  
  443. // if not key city_id & competency_id redirect to profile/spec
  444. if (this._isCitySpecId(this.profile)) {
  445. this._router.navigate(['/profile/spec']);
  446. }
  447. }, err => {
  448. console.log(err);
  449. });
  450. }
  451.  
  452. goToPayment() {
  453. this._router.navigate(['profile/payment']);
  454. }
  455.  
  456. logOut() {
  457. this._userService.logout().subscribe(res => {
  458. localStorage.removeItem('user');
  459. this._router.navigate(['/']);
  460. }, err => {
  461. console.log(err);
  462. });
  463. }
  464.  
  465. private getCities() {
  466. this._sharedService.getCities()
  467. .subscribe((cities: ICity[]) => {
  468. this.cities = cities;
  469. this._sharedService.cities = this.cities;
  470. }, err => {
  471. console.error(err);
  472. });
  473. }
  474.  
  475. private getLawyerStatistics() {
  476. const params = {
  477. user_id: this._profileService.currentUserData._id,
  478. start_date: moment(Date.now()).subtract(7,'d').format("Y-M-D"),
  479. end_date: moment(Date.now()).format("Y-M-D"),
  480. filter: 'period'
  481. };
  482. this._sharedService.getLawyerClickStatistics(params)
  483. .subscribe((res: any) => {
  484. this.mapStatistics(res);
  485. }, err => {
  486. console.log(err);
  487. });
  488. }
  489.  
  490. public getAllSpecialization() {
  491. this._sharedService.getAllCompetition().subscribe((res: any) => {
  492. this.competencies = res;
  493. this._sharedService.competencies = this.competencies;
  494. }, err => {
  495. console.error(err);
  496. });
  497. }
  498.  
  499. getLanguages() {
  500. this._profileService.getLanguages().subscribe((languages: ILanguage[]) => {
  501. this.languges = languages;
  502. });
  503. }
  504. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement