Guest User

visual.ts file

a guest
Mar 12th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. import "core-js/stable";
  4. import "./../style/visual.less";
  5. import powerbi from "powerbi-visuals-api";
  6. import VisualConstructorOptions = powerbi.extensibility.visual.VisualConstructorOptions;
  7. import VisualUpdateOptions = powerbi.extensibility.visual.VisualUpdateOptions;
  8. import IVisual = powerbi.extensibility.visual.IVisual;
  9. import EnumerateVisualObjectInstancesOptions = powerbi.EnumerateVisualObjectInstancesOptions;
  10. import VisualObjectInstance = powerbi.VisualObjectInstance;
  11. import DataView = powerbi.DataView;
  12. import VisualObjectInstanceEnumerationObject = powerbi.VisualObjectInstanceEnumerationObject;
  13.  
  14. import { VisualSettings } from "./settings";
  15. import * as d3 from 'd3';
  16.  
  17. interface DataPoint {
  18.     value: number;
  19.     category: string
  20. }
  21.  
  22. interface ViewModel {
  23.     dataPoints: DataPoint[];
  24.     dataMax: number
  25. }
  26.  
  27. let testData: DataPoint[] = [
  28.     {
  29.         value: 10,
  30.         category: 'a'
  31.     },
  32.     {
  33.         value: 15,
  34.         category: 'b'
  35.     },
  36.     {
  37.         value: 2,
  38.         category: 'c'
  39.     },
  40.     {
  41.         value: 25,
  42.         category: 'd'
  43.     },
  44.     {
  45.         value: 8,
  46.         category: 'e'
  47.     }
  48. ]
  49.  
  50. export class Visual implements IVisual {
  51.  
  52.     settings: VisualSettings;
  53.  
  54.     private svg: d3.Selection<SVGElement, any, any, any>;
  55.     private host: powerbi.extensibility.IVisualHost;
  56.     private barChartContainer: d3.Selection<SVGElement, any, any, any>;
  57.     private barContainer: d3.Selection<SVGElement, any, any, any>;
  58.     private xAxis: d3.Selection<SVGElement, any, any, any>;
  59.     private bars: d3.Selection<SVGElement, any, any, any>;
  60.  
  61.  
  62.     constructor(options: VisualConstructorOptions) {
  63.         debugger;
  64.         console.log('constructor called');
  65.         this.host = options.host;
  66.         let svg = this.svg = d3.select(options.element).append('svg').classed('barChart', true);
  67.         this.barContainer = svg.append('g').classed('barContainer', true);
  68.         this.xAxis = svg.append('g').classed('xAxis', true);
  69.     }
  70.  
  71.     public update(options: VisualUpdateOptions) {
  72.         debugger;
  73.         console.log('update called');
  74.         let width = options.viewport.width;
  75.         let height = options.viewport.height;
  76.  
  77.         let margin = {
  78.             top: 0,
  79.             bottom: 25,
  80.             left: 0,
  81.             right: 0
  82.         };
  83.  
  84.         let viewModel: ViewModel = {
  85.             dataPoints: testData,
  86.             dataMax: d3.max(testData.map((dataPoint) => dataPoint.value))
  87.         };
  88.  
  89.         this.svg.attr('width', width);
  90.         this.svg.attr('height', height);
  91.  
  92.         height -= margin.bottom;
  93.  
  94.         let yScale = d3.scaleLinear()
  95.             .domain([0, viewModel.dataMax])
  96.             .range([height, 0]);
  97.  
  98.         let xScale = d3.scaleBand()
  99.             .domain(viewModel.dataPoints.map(d => d.category))
  100.             .rangeRound([0, width])
  101.             .padding(0.1);
  102.        
  103.         let xAxis = d3.axisBottom(xScale);
  104.         this.xAxis.attr('transform', 'translate(0, ' + height + ')').call(xAxis);
  105.  
  106.         let bars = this.barContainer.selectAll('.bar').data(viewModel.dataPoints);
  107.  
  108.         console.log('xScale bandwidth: ', xScale.bandwidth());
  109. try {
  110.         bars.enter().append('rect').classed('bar', true);
  111.             // .attr('width', xScale.bandwidth())
  112.             // .attr('height', d => height - yScale(d.value))
  113.             // .attr('y', d => yScale(d.value))
  114.             // .attr('x', d => xScale(d.category));
  115.  
  116.         bars.transition()
  117.             .attr('width', xScale.bandwidth())
  118.             .attr('height', d => height - yScale(d.value))
  119.             .attr('y', d => yScale(d.value))
  120.             .attr('x', d => xScale(d.category));
  121. } catch (e) {
  122.     console.log(e);
  123. }
  124.         bars.exit().remove();
  125.     }
  126.  
  127.     public enumerateObjectInstances(options: EnumerateVisualObjectInstancesOptions): VisualObjectInstance[] | VisualObjectInstanceEnumerationObject {
  128.         return VisualSettings.enumerateObjectInstances(this.settings || VisualSettings.getDefault(), options);
  129.     }
  130. }
Add Comment
Please, Sign In to add comment