0

Currently, vertical line is drawn where the first part of pie ends. Due to this,it is overwriting the heading and in cases, text is hidden. Need to display the vertical line in bottom in all cases.

enter image description here

Below is my component

import {
    Component,
    Input,
    AfterViewInit,
    NgZone,
    ChangeDetectionStrategy,
    OnChanges,
    Output,
    EventEmitter
} from '@angular/core';

declare var Plotly: any;

/**
 * Custom typing for plotly
 * TODO :: Use provided .d.ts
 */
export interface IPlotLayout {
    title: string;
    autosize: boolean;
    showlegend: boolean;
    separators: string;
    hidesources: boolean;
    xaxis: any;
    yaxis: any;
    yaxis2: any;
    margin: any;
    height: number;
    width: number;
    hovermode: 'closest' | 'x' | 'y' | false;
    hoverlabel: any;
    direction: 'clockwise' | 'counterclockwise';
    orientation: 'v' | 'h';
    legend: any;
    font: any;
    barmode: string;
    annotations: any;
}

@Component({
    selector: 'plot',
    template: `
        <div>
            <div id="chart{{ id }}"></div>
        </div>
    `,
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class PlotlyComponent implements AfterViewInit, OnChanges {
    @Input() name: string;
    @Input() data: any[];
    @Input() layout: Partial<IPlotLayout>;
    @Input() options: any;
    @Input() id: string = '';
    @Input() style: {} = {};
    //is the view initialized for plotly.js to take action
    viewInitialized: boolean = false;
    @Input() downloadCSV: boolean = false;
    @Output() exportToCSV = new EventEmitter();

    _container;

    constructor(private zone: NgZone) { }

    ngAfterViewInit() {
        let d3 = Plotly.d3,
            WIDTH_IN_PERCENT_OF_PARENT = 95,
            HEIGHT_IN_PERCENT_OF_PARENT = 95;
        let style = {
            width: WIDTH_IN_PERCENT_OF_PARENT + '%',
            'margin-left': (100 - WIDTH_IN_PERCENT_OF_PARENT) / 2 + '%',

            height: HEIGHT_IN_PERCENT_OF_PARENT + 'vh',
            'margin-top': (100 - HEIGHT_IN_PERCENT_OF_PARENT) / 2 + 'vh'
        };
        if (Object.keys(this.style).length > 0) {
            style = { ...style, ...this.style };
        }
        let gd3 = d3
            .select(`#chart${this.id}`)
            .append('div')
            .style(style);
        this._container = gd3.node();

        /**
         * On the first run, plot the graph after view has been initialized('div' exists)
         * After that, all subsequent plot changes should be handled by OnChanges hook
         */
        this.update();
        this.viewInitialized = true;
    }

    ngOnChanges() {
        if (this.viewInitialized) {
            this.update();
        }
    }

    update() {
        /**
         * Plot the graph outside change detection
         * TODO :: Investigate bubbling up chart events for future scaling
         * TODO :: Try adding animations on update...
         */
        let self = this;
        this.zone.runOutsideAngular(() => {
            self.appendCustomExport();
            Plotly.newPlot(
                self._container,
                self.data,
                self.layout,
                self.options
            );
        });
    }
}

The data is dynamic and pie may contain one or more sections

Another case

enter image description here

2
  • please put your code to question too Commented Apr 27, 2020 at 9:50
  • added. Please check Commented Apr 28, 2020 at 4:19

1 Answer 1

1

I would suggest that you work on you chart size first.

In any case, if your pie chart is same size as your container, it will always create problems showing legends and other information on hover. So make your pie chart smaller. Your pie radius should not be more than 40% of your container's height.

To avoid sorting by plotly, pass in the following to your data array:

data: [sort:false]

I hope it will let you achieve your expected layout.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.