<commit_before>﻿/// <reference path="../../_references.ts"/>

module powerbi.visuals.samples {
    import SelectionManager = utility.SelectionManager;
    export interface SunburstNode {
        children?: SunburstNode[];
        value?: any;
        color?: string;
        name?: string;
        parent?: SunburstNode;
        selector: SelectionId;
        total: number;
    }

    export interface SunburstViewModel {
        root: SunburstNode;
    }

    export var sunburstRoleNames = {
        nodes: 'Nodes',
        values: 'Values',
    };

    export class Sunburst implements IVisual {
        
        public static capabilities: VisualCapabilities = {
        };
        private static minOpacity = 0.2;
        private disableMouseOut: boolean = false;
        private svg: D3.Selection;
        private g: D3.Selection;
        private viewport: IViewport;
        private colors: IDataColorPalette;
        private selectionManager: SelectionManager;

        public init(options: VisualInitOptions): void {

            this.arc = d3.svg.arc()
                .startAngle(function (d) { return d.x; })
                .endAngle(function (d) { return d.x + d.dx; })
                .innerRadius(function (d) { return Math.sqrt(d.y); })
                .outerRadius(function (d) { return Math.sqrt(d.y + d.dy); });

            this.colors = options.style.colorPalette.dataColors;
            this.selectionManager = new SelectionManager({ hostServices: options.host });
            this.svg = d3.select(options.element.get(0)).append('svg');
            this.svg.attr('class', 'mainDrawArea');
            this.g = this.svg.append('g');
            this.g.attr('class', "container");
            this.g
                .on("mouseleave", (d) => { this.mouseleave(d, this); });
            this.svg.append("text")
                .attr('class', "sunBurstPercentage");
            this.svg.append("text")
                .attr('class', "sunBurstPercentageFixed");

            this.svg.on('mousedown', (d) => {
                this.svg.selectAll("path").style("opacity", 1);
                this.disableMouseOut = false;
                this.svg.select(".sunBurstPercentageFixed").style("opacity", 0);
                this.selectionManager.clear();
            });
            let svg_obj = this.svg;
            svg_obj.on("mousemove", function () {
                let point = d3.mouse(this)
                    , p = { x: point[0], y: point[1] };
                let shift = 20;
                let percentageText = svg_obj.select(".sunBurstPercentage");
                percentageText.attr("y", p.y + shift);
                percentageText.attr("x", p.x + shift);
            });

        }

        private static setAllUnhide(selection): void {
            selection.attr("setUnHide", "true");
        }
      
        public update(options: VisualUpdateOptions): void {
            if (options.dataViews.length > 0) {
                let data = this.converter(options.dataViews[0], this.colors);
                this.viewport = options.viewport;
                this.updateInternal(data);
            }
        }

        private updateInternal(dataRootNode: SunburstNode): void {

            this.svg.attr({
                'height': this.viewport.height,
                'width': this.viewport.width
            });
            this.g.attr("transform", "translate(" + this.viewport.width / 2 + "," + this.viewport.height / 2 + ")");
            let radius = Math.min(this.viewport.width, this.viewport.height) / 2;

            let partition = d3.layout.partition()
                .size([2 * Math.PI, radius * radius])
                .value((d) => { return d.value; });

            let path = this.g.datum(dataRootNode).selectAll("path")
                .data(partition.nodes);
            path.enter().append("path");
            path.attr("display", (d) => { return d.depth ? null : "none"; })
                .attr("d", this.arc)
                .style("stroke", "#fff")
                .style("fill", (d) => { return d.color; })
                .style("fill-rule", "evenodd")
                .each((d) => {
                this.total2 += d.value ? d.value:0;
                    d.x0 = d.x;
                    d.dx0 = d.dx;
                })
                .on("mouseover", (d) => { this.mouseover(d, this, false); })
                .on("mouseleave", (d) => { this.mouseleave(d, this); })
                .on("mousedown", (d) => {

                    this.selectionManager.select(d.selector);
                    d3.selectAll("path").call(Sunburst.setAllUnhide).attr('setUnHide', null);
                    this.svg.select(".container").on("mouseleave", null);
                    this.mouseover(d, this, true);
                    this.disableMouseOut = true;

                    let percentageFixedText = this.svg.select(".sunBurstPercentageFixed");
                    var percentage = (100 * d.total / this.total).toPrecision(3);
                    percentageFixedText.text(d ? percentage + "%" : "");
                    percentageFixedText.style('fill', d.color);
                    this.onResize();
                    event.stopPropagation();
                });
            
            this.onResize();
        }

        private arc: D3.Svg.Arc;
        // Stash the old values for transition.
        private total: number = 0;
        private total2: number = 0;
        // Get all parents of the node
        private static getTreePath(node) {
            let path = [];
            let current = node;
            while (current.parent) {
                path.unshift(current);
                current = current.parent;
            }
            return path;
        }

        private onResize(): void {
            let width = this.viewport.width;
            let height = this.viewport.height;
            let percentageText = this.svg.select(".sunBurstPercentage");
            let percentageFixedText = this.svg.select(".sunBurstPercentageFixed");
            percentageText.style("opacity", 1);
            percentageFixedText.style("opacity", 1);
            percentageFixedText.attr("y", (height / 2));
            percentageFixedText.attr("x", ((width / 2) - (percentageFixedText.node().clientWidth / 2)));<commit_msg>remove redundand Stash function<commit_after>﻿/// <reference path="../../_references.ts"/>

module powerbi.visuals.samples {
    import SelectionManager = utility.SelectionManager;
    export interface SunburstNode {
        children?: SunburstNode[];
        value?: any;
        color?: string;
        name?: string;
        parent?: SunburstNode;
        selector: SelectionId;
        total: number;
    }

    export interface SunburstViewModel {
        root: SunburstNode;
    }

    export var sunburstRoleNames = {
        nodes: 'Nodes',
        values: 'Values',
    };

    export class Sunburst implements IVisual {
        
        public static capabilities: VisualCapabilities = {
        };
        private static minOpacity = 0.2;
        private disableMouseOut: boolean = false;
        private svg: D3.Selection;
        private g: D3.Selection;
        private viewport: IViewport;
        private colors: IDataColorPalette;
        private selectionManager: SelectionManager;

        public init(options: VisualInitOptions): void {

            this.arc = d3.svg.arc()
                .startAngle(function (d) { return d.x; })
                .endAngle(function (d) { return d.x + d.dx; })
                .innerRadius(function (d) { return Math.sqrt(d.y); })
                .outerRadius(function (d) { return Math.sqrt(d.y + d.dy); });

            this.colors = options.style.colorPalette.dataColors;
            this.selectionManager = new SelectionManager({ hostServices: options.host });
            this.svg = d3.select(options.element.get(0)).append('svg');
            this.svg.attr('class', 'mainDrawArea');
            this.g = this.svg.append('g');
            this.g.attr('class', "container");
            this.g
                .on("mouseleave", (d) => { this.mouseleave(d, this); });
            this.svg.append("text")
                .attr('class', "sunBurstPercentage");
            this.svg.append("text")
                .attr('class', "sunBurstPercentageFixed");

            this.svg.on('mousedown', (d) => {
                this.svg.selectAll("path").style("opacity", 1);
                this.disableMouseOut = false;
                this.svg.select(".sunBurstPercentageFixed").style("opacity", 0);
                this.selectionManager.clear();
            });
            let svg_obj = this.svg;
            svg_obj.on("mousemove", function () {
                let point = d3.mouse(this)
                    , p = { x: point[0], y: point[1] };
                let shift = 20;
                let percentageText = svg_obj.select(".sunBurstPercentage");
                percentageText.attr("y", p.y + shift);
                percentageText.attr("x", p.x + shift);
            });

        }

        private static setAllUnhide(selection): void {
            selection.attr("setUnHide", "true");
        }
      
        public update(options: VisualUpdateOptions): void {
            if (options.dataViews.length > 0) {
                let data = this.converter(options.dataViews[0], this.colors);
                this.viewport = options.viewport;
                this.updateInternal(data);
            }
        }

        private updateInternal(dataRootNode: SunburstNode): void {

            this.svg.attr({
                'height': this.viewport.height,
                'width': this.viewport.width
            });
            this.g.attr("transform", "translate(" + this.viewport.width / 2 + "," + this.viewport.height / 2 + ")");
            let radius = Math.min(this.viewport.width, this.viewport.height) / 2;

            let partition = d3.layout.partition()
                .size([2 * Math.PI, radius * radius])
                .value((d) => { return d.value; });

            let path = this.g.datum(dataRootNode).selectAll("path")
                .data(partition.nodes);
            path.enter().append("path");
            path.attr("display", (d) => { return d.depth ? null : "none"; })
                .attr("d", this.arc)
                .style("stroke", "#fff")
                .style("fill", (d) => { return d.color; })
                .style("fill-rule", "evenodd")
                .on("mouseover", (d) => { this.mouseover(d, this, false); })
                .on("mouseleave", (d) => { this.mouseleave(d, this); })
                .on("mousedown", (d) => {

                    this.selectionManager.select(d.selector);
                    d3.selectAll("path").call(Sunburst.setAllUnhide).attr('setUnHide', null);
                    this.svg.select(".container").on("mouseleave", null);
                    this.mouseover(d, this, true);
                    this.disableMouseOut = true;

                    let percentageFixedText = this.svg.select(".sunBurstPercentageFixed");
                    var percentage = (100 * d.total / this.total).toPrecision(3);
                    percentageFixedText.text(d ? percentage + "%" : "");
                    percentageFixedText.style('fill', d.color);
                    this.onResize();
                    event.stopPropagation();
                });
            
            this.onResize();
        }

        private arc: D3.Svg.Arc;
        // Stash the old values for transition.
        private total: number = 0;
        // Get all parents of the node
        private static getTreePath(node) {
            let path = [];
            let current = node;
            while (current.parent) {
                path.unshift(current);
                current = current.parent;
            }
            return path;
        }

        private onResize(): void {
            let width = this.viewport.width;
            let height = this.viewport.height;
            let percentageText = this.svg.select(".sunBurstPercentage");
            let percentageFixedText = this.svg.select(".sunBurstPercentageFixed");
            percentageText.style("opacity", 1);
            percentageFixedText.style("opacity", 1);
            percentageFixedText.attr("y", (height / 2));
            percentageFixedText.attr("x", ((width / 2) - (percentageFixedText.node().clientWidth / 2)));
{'fraction_non_alphanumeric': 0.13651050080775445, 'fraction_numerical': 0.005815831987075929, 'mean_word_length': 1.6683189655172415, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3623883', 'n_tokens_mistral': 3442, 'n_tokens_neox': 3278, 'n_words': 656}