Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { AfterViewInit, Component, ElementRef, ViewChild } from '@angular/core';
  2. import { BarChartModel, BarChartSize, ExampleData } from 'shared/components/graph/bar-chart/bar-chart.model';
  3. import * as d3 from 'd3';
  4.  
  5. @Component({
  6.     selector: 'app-graph-bar-chart',
  7.     templateUrl: 'bar-chart.component.html',
  8.     styleUrls: ['bar-chart.component.css']
  9. })
  10. export class BarChartComponent implements AfterViewInit{
  11.     private _data: BarChartModel = ExampleData;
  12.     private _size: BarChartSize = {
  13.         height: 0,
  14.         width: 0,
  15.         top: 20,
  16.         left: 50,
  17.         right: 20,
  18.         bottom: 30
  19.     };
  20.  
  21.     private _svg;
  22.     private _width;
  23.     private _height;
  24.     private _scaleLinear;
  25.     private _scaleBand;
  26.     private _g;
  27.  
  28.     @ViewChild('container') container;
  29.  
  30.     constructor(private _elementRef: ElementRef) {}
  31.  
  32.     ngAfterViewInit() {
  33.         this._size.width = this._elementRef.nativeElement.offsetWidth;
  34.         this._size.height = 500;
  35.         this._svg = d3.select(this.container.nativeElement);
  36.         this._svg.attr('width', this._size.width);
  37.         this._svg.attr('height', this._size.height);
  38.         this._g = this._svg.append("g").attr('transform', `translate(${this._size.left}, ${this._size.top})`);
  39.  
  40.         this.getWidthAndHeight();
  41.         this.setScalers();
  42.         this.updateGraph();
  43.     }
  44.  
  45.     private getWidthAndHeight(): void {
  46.         this._width = +this._svg.attr('width') - this._size.left - this._size.right;
  47.         this._height = +this._svg.attr('height') - this._size.top - this._size.bottom;
  48.     }
  49.  
  50.     private setScalers(): void {
  51.         this._scaleBand = d3.scaleBand()
  52.             .rangeRound([0, this._width])
  53.             .padding(0.1);
  54.  
  55.         this._scaleBand.domain(this._data.items.map(function (d) {
  56.             return d.xItem;
  57.         }));
  58.  
  59.         this._scaleLinear = d3.scaleLinear()
  60.             .rangeRound([this._height, 0]);
  61.  
  62.         this._scaleLinear.domain([0, d3.max(this._data.items, function (d) {
  63.             return Number(d.yItem);
  64.         })]);
  65.     }
  66.  
  67.     private updateGraph(): void {
  68.         this._svg.select('g').remove();
  69.         const g = this._svg.append('g')
  70.             .attr('transform', `translate(${this._size.left}, ${this._size.top})`);
  71.  
  72.         g.append('g')
  73.             .attr('transform', `translate(0, ${this._height})`)
  74.             .call(d3.axisBottom(this._scaleBand));
  75.  
  76.         g.append('g')
  77.             .call(d3.axisLeft(this._scaleLinear))
  78.             .append('text')
  79.             .attr('fill', '#000')
  80.             .attr('transform', 'rotate(-90)')
  81.             .attr('y', 6)
  82.             .attr('dy', '0.71em')
  83.             .attr('text-anchor', 'end')
  84.             .text(this._data.yLabel);
  85.  
  86.         g.selectAll('.bar')
  87.             .data(this._data.items)
  88.             .enter()
  89.             .append('rect')
  90.             .attr('class', 'bar')
  91.             .attr('x', item => {
  92.                 return this._scaleBand(item.xItem);
  93.             })
  94.             .attr('y', item => {
  95.                 return this._scaleLinear(Number(item.yItem));
  96.             })
  97.             .attr('width', this._scaleBand.bandwidth())
  98.             .attr('height', item => {
  99.                 return this._height - this._scaleLinear(Number(item.yItem));
  100.             });
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement