Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Power BI Visual CLI
  3.  *
  4.  *  Copyright (c) Microsoft Corporation
  5.  *  All rights reserved.
  6.  *  MIT License
  7.  *
  8.  *  Permission is hereby granted, free of charge, to any person obtaining a copy
  9.  *  of this software and associated documentation files (the ""Software""), to deal
  10.  *  in the Software without restriction, including without limitation the rights
  11.  *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12.  *  copies of the Software, and to permit persons to whom the Software is
  13.  *  furnished to do so, subject to the following conditions:
  14.  *
  15.  *  The above copyright notice and this permission notice shall be included in
  16.  *  all copies or substantial portions of the Software.
  17.  *
  18.  *  THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19.  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20.  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21.  *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22.  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23.  *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24.  *  THE SOFTWARE.
  25.  */
  26.  
  27. module powerbi.extensibility.visual {
  28.     'use strict';
  29.  
  30.     interface DataPoint {
  31.         category: string;
  32.         value: number;
  33.     };
  34.  
  35.     interface ViewModel{
  36.         dataPoints: DataPoint[];
  37.         maxValue: number;
  38.  
  39.     };
  40.    
  41.     export class Visual implements IVisual {
  42.        
  43.         private host: IVisualHost;
  44.         private svg: d3.Selection<SVGElement>;
  45.         private barGroup: d3.Selection<SVGElement>;
  46.  
  47.         private settings: VisualSettings;
  48.  
  49.         xAxisPadding: number = 0.1;
  50.  
  51.         constructor(options: VisualConstructorOptions) {
  52.             this.host = options.host;
  53.             this.svg = d3.select(options.element)
  54.                 .append("svg")
  55.                 .classed("barChart", true);
  56.  
  57.             this.barGroup = this.svg.append("g")
  58.                 .classed("barGroup", true);
  59.            
  60.        
  61.  
  62.         }
  63.  
  64.         public update(options: VisualUpdateOptions) {
  65.             console.log(1);
  66.             let testData: DataPoint[] = [
  67.                 {
  68.                     value: 10,
  69.                     category: 'a'
  70.                 },
  71.                 {
  72.                     value: 20,
  73.                     category: 'b'
  74.                 },
  75.                 {
  76.                     value: 1,
  77.                     category: 'c'
  78.                 },
  79.                 {
  80.                     value: 100,
  81.                     category: 'd'
  82.                 },
  83.                 {
  84.                     value: 500,
  85.                     category: 'e'
  86.                 }];
  87.  
  88.             let viewModel: ViewModel = {
  89.                 dataPoints: testData,
  90.                 maxValue: d3.max(testData, x=>x.value)
  91.             }
  92.  
  93.             console.log(2)
  94.  
  95.             let width = options.viewport.width;
  96.             let height = options.viewport.height;
  97.  
  98.             this.svg.attr({
  99.                 width: width,
  100.                 height: height
  101.             });
  102.  
  103.             console.log(3)
  104.  
  105.             let yScale = d3.scale.linear()
  106.                 .domain([0, viewModel.maxValue])
  107.                 .range([height,0]);
  108.  
  109.             let xScale = d3.scale.ordinal()
  110.                 .domain(viewModel.dataPoints.map(d=>d.category))
  111.                 .rangeRoundBands([0,width], this.xAxisPadding);
  112.            
  113.             //gets all bars from the bar group
  114.             let bars = this.barGroup
  115.                 .selectAll(".bar")
  116.                 .data(viewModel.dataPoints);
  117.  
  118.             //gives list of all new elements that need to be drawn in the svg, barGroup
  119.             //It also enters the new rect bars into the group
  120.             bars.enter()
  121.                 .append("rect")
  122.                 .classed("bar",true);
  123.  
  124.                 console.log(4)
  125.  
  126.             //converts and positions the bars' size and positioning
  127.             //to the screen x-y-axis system rather than the graph's
  128.             bars.attr({
  129.                 width: xScale.rangeBand(),
  130.                 height: d=>height - yScale(d.value),
  131.                 y: d=> yScale(d.value),
  132.                 x: d=> xScale(d.category)
  133.             });
  134.            
  135.             bars.exit()
  136.                 .remove();
  137.  
  138.                 console.log(5)
  139.         }
  140.  
  141.  
  142.         /* public enumerateObjectInstances(options: EnumerateVisualObjectInstancesOptions): VisualObjectInstance[] | VisualObjectInstanceEnumerationObject {
  143.             return VisualSettings.enumerateObjectInstances(this.settings || VisualSettings.getDefault(), options);
  144.         }
  145.  
  146.         private static parseSettings(dataView: DataView): VisualSettings {
  147.             return VisualSettings.parse(dataView) as VisualSettings;
  148.         } */
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement