Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NgxCharts resizing on data change

I'm building an app using Swimlane's Ngx Charts for graphical data representation, but I'm getting odd behaviour when I update the data via a refresh button which re-calls a GET function to update the data array. I'm using @angular/flex-layout for a "tile-like" view but even without using it, I'm still encountering this issue - it just grows the parent container. I don't want to use hard-coded px values in the [view] property because I want it to work with any size of graph/tile.

I'm use a pretty basic vertical bar chart:

<div>
    <div>
        <ngx-charts-bar-vertical [results]="dataArray"
                                 [legend]="true"
                                 [xAxis]="true"
                                 [yAxis]="true"
                                 [xAxisLabel]="'Date'"
                                 [yAxisLabel]="'# tickets Raised'"
                                 [showXAxisLabel]="true"
                                 [showYAxisLabel]="true"></ngx-charts-bar-vertical>
    </div>
    <div>
        <!-- Got a table here -->
    </div>
</div>

To update my dataArray I'm doing:

export class DashboardComponent implements AfterViewInit {
    constructor(private httpClient: HttpClient, private datePipe: DatePipe, private dataService: DataService) { }

    dataArray: any[] = [];

    getData(): void {

        this.dataService.getSomeData<any[]>().subscribe(d => {
                this.dataArray = d;
        });
    }

    // Init
    ngAfterViewInit(): void {
        this.getData();
    }
}

On initial load, it's fine and will fill the container (where it can) but when I hit the refresh button this is the behaviour I get: enter image description here

Thank you for your time.

like image 686
Matt Brewerton Avatar asked Jan 24 '18 13:01

Matt Brewerton


Video Answer


2 Answers

We solved it using a div around the chart and using CSS added a height of 40 or 50vh.

The chart no longer changes in height on update.

like image 169
drinu16 Avatar answered Sep 23 '22 04:09

drinu16


The following worked for me:

<div style="max-height: 50vh">
  <ngx-charts-bar-vertical   [results]="dataArray"
                             [legend]="true"
                             [xAxis]="true"
                             [yAxis]="true"
                             [xAxisLabel]="'Date'"
                             [yAxisLabel]="'# tickets Raised'"
                             [showXAxisLabel]="true"
                             [showYAxisLabel]="true"></ngx-charts-bar-vertical>
</div>

Downside on this, is you would need to define a maximum height.

like image 30
Prom0 Avatar answered Sep 22 '22 04:09

Prom0