Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Power BI Visual CLI
- *
- * Copyright (c) Microsoft Corporation
- * All rights reserved.
- * MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the ""Software""), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
- module powerbi.extensibility.visual {
- 'use strict';
- interface DataPoint {
- category: string;
- value: number;
- };
- interface ViewModel{
- dataPoints: DataPoint[];
- maxValue: number;
- };
- export class Visual implements IVisual {
- private host: IVisualHost;
- private svg: d3.Selection<SVGElement>;
- private barGroup: d3.Selection<SVGElement>;
- private settings: VisualSettings;
- xAxisPadding: number = 0.1;
- constructor(options: VisualConstructorOptions) {
- this.host = options.host;
- this.svg = d3.select(options.element)
- .append("svg")
- .classed("barChart", true);
- this.barGroup = this.svg.append("g")
- .classed("barGroup", true);
- }
- public update(options: VisualUpdateOptions) {
- console.log(1);
- let testData: DataPoint[] = [
- {
- value: 10,
- category: 'a'
- },
- {
- value: 20,
- category: 'b'
- },
- {
- value: 1,
- category: 'c'
- },
- {
- value: 100,
- category: 'd'
- },
- {
- value: 500,
- category: 'e'
- }];
- let viewModel: ViewModel = {
- dataPoints: testData,
- maxValue: d3.max(testData, x=>x.value)
- }
- console.log(2)
- let width = options.viewport.width;
- let height = options.viewport.height;
- this.svg.attr({
- width: width,
- height: height
- });
- console.log(3)
- let yScale = d3.scale.linear()
- .domain([0, viewModel.maxValue])
- .range([height,0]);
- let xScale = d3.scale.ordinal()
- .domain(viewModel.dataPoints.map(d=>d.category))
- .rangeRoundBands([0,width], this.xAxisPadding);
- //gets all bars from the bar group
- let bars = this.barGroup
- .selectAll(".bar")
- .data(viewModel.dataPoints);
- //gives list of all new elements that need to be drawn in the svg, barGroup
- //It also enters the new rect bars into the group
- bars.enter()
- .append("rect")
- .classed("bar",true);
- console.log(4)
- //converts and positions the bars' size and positioning
- //to the screen x-y-axis system rather than the graph's
- bars.attr({
- width: xScale.rangeBand(),
- height: d=>height - yScale(d.value),
- y: d=> yScale(d.value),
- x: d=> xScale(d.category)
- });
- bars.exit()
- .remove();
- console.log(5)
- }
- /* public enumerateObjectInstances(options: EnumerateVisualObjectInstancesOptions): VisualObjectInstance[] | VisualObjectInstanceEnumerationObject {
- return VisualSettings.enumerateObjectInstances(this.settings || VisualSettings.getDefault(), options);
- }
- private static parseSettings(dataView: DataView): VisualSettings {
- return VisualSettings.parse(dataView) as VisualSettings;
- } */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement