How can I make my ngx-line-chart responsive?

Loufi

I'm using ngx-admin and I'm trying to make my ngx-line-chart responsive.

My chart is in a nb-card, and when I resize the window the nb-card is totaly responsive. So I want my chart to be resized to fit inside the nb-card.

My html code:

<div style="margin: 100px auto auto">
    <nb-card>
        <nb-card-header>XXXXXXXXXX</nb-card-header>
            <nb-card-body>
                <ngx-line-chart></ngx-line-chart>
            </nb-card-body>
    </nb-card>
</div>

my component:

import { Component } from '@angular/core';

@Component({
    selector: 'ngx-line-chart',
    template: `
    <ngx-charts-line-chart
      [scheme]="colorScheme"
      [results]="multi"
      [xAxis]="showXAxis"
      [yAxis]="showYAxis"
      [showXAxisLabel]="showXAxisLabel"
      [showYAxisLabel]="showYAxisLabel"
      [xAxisLabel]="xAxisLabel"
      [yAxisLabel]="yAxisLabel">
    </ngx-charts-line-chart>
  `,
})
export class LineChartComponent {
    showXAxis = true;
    showYAxis = true;
    showXAxisLabel = true;
    xAxisLabel = 'Date';
    showYAxisLabel = true;
    yAxisLabel = 'Services effectués';
    colorScheme = {
        domain: ['blue'],
    };
    themeSubscription: any;
    multi = [
        {
            name: 'Services effectués',
            series: [
                {
                    name: '01/01/2019',
                    value: 156,
                },
                {
                    name: '02/01/2019',
                    value: 134,
                },
                {
                    name: '03/01/2019',
                    value: 140,
                },
                {
                    name: '04/01/2019',
                    value: 167,
                },
                {
                    name: '05/01/2019',
                    value: 158,
                },
                {
                    name: '06/01/2019',
                    value: 178,
                },
                {
                    name: '07/01/2019',
                    value: 310,
                },
            ],
        },
    ];

    constructor() {
    }
}

I already try to get the screen size to change my chart size with the screen size but it wasn't perfectly responsive. To change the size of the chart I can use a variable view=[x, y]. I read in the ngx-line-chart documentation that if no size is defined the chart fit to his container, but in my case it doesn't work.

Thank you for your help !

Loufi

After some research I found the solution to my problem.

1) To change the chart size when the window is resized:

To change the chart's size I used a "onResize(event)" method. This method take in parameter the window resize event. In this method I simply get the width of the window, I divide it by a ratio (in my case it's 1.35) and I assign it to the width of my chart.

onResize(event) method:

// view is the variable used to change the chart size (Ex: view = [width, height])

onResize(event) {
    this.view = [event.target.innerWidth / 1.35, 400];
}

My html template:

<ngx-charts-line-chart
  (window:resize)="onResize($event)"
  [scheme]="colorScheme"
  [results]="multi"
  [xAxis]="showXAxis"
  [yAxis]="showYAxis"
  [showXAxisLabel]="showXAxisLabel"
  [showYAxisLabel]="showYAxisLabel"
  [xAxisLabel]="xAxisLabel"
  [yAxisLabel]="yAxisLabel"
  [view]="view">
</ngx-charts-line-chart>

2) To change the chart size on different device:

To change the chart's size on different device I have to define the size of the chart into the constructor. I get the window size and like for "window resize" i divide it by a ratio and i assign it to "view".

My constructor:

constructor() {
    this.view = [innerWidth / 1.3, 400];
}

This answer work for me. I hope it will help you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I make my navbar responsive?

From Dev

How can I make this section of my webpage responsive?

From Dev

How can I make my form responsive on all sizes

From Dev

How can I make two of my lines in Chart JS thicker

From Dev

How can I make my area chart trasparent in jqplot?

From Dev

how can a make my gridster responsive

From Dev

How can I make my webpage responsive when I minimize the page

From Dev

How can I make an image in a div responsive?

From Dev

How can I make this image responsive?

From Dev

How can i make my functions stops executing at a certain line?

From Dev

How can I make sure my power line network is secure?

From Dev

How can I make my HTML page Flexible/Responsive for iPhone 6s

From Dev

can I make my recycler view responsive with respect to screen sizes?

From Dev

How can i plot line chart in python?

From Dev

How to make svg line responsive

From Dev

How can I make a dynamic line chart with JavaFX using a socket input

From Java

How do I make my absolute-positioned elements responsive?

From Dev

How do I make my website's background image responsive?

From Dev

How can I make sure my Google chart data gets removed from memory in Javascript?

From Dev

How can I display my javascript inside my <p> tags to make it a one line sentence?

From Dev

How to make my website responsive

From Dev

How can I combine "verticalRangeArea" with line chart in Kendo UI Chart?

From Dev

How can I make the background image to be responsive without losing the proportions

From Dev

How can I make an image responsive in a bootstrap modal?

From Dev

How can I make Woocommerce shopping cart table responsive?

From Dev

how can i make text wrapping in responsive div?

From Dev

How can I make this container and table responsive for mobile and ipad size?

From Dev

How Can I Force IE to Make Joomla Article Images Responsive?

From Dev

How can I make Ubuntu more responsive on a netbook?

Related Related

  1. 1

    How can I make my navbar responsive?

  2. 2

    How can I make this section of my webpage responsive?

  3. 3

    How can I make my form responsive on all sizes

  4. 4

    How can I make two of my lines in Chart JS thicker

  5. 5

    How can I make my area chart trasparent in jqplot?

  6. 6

    how can a make my gridster responsive

  7. 7

    How can I make my webpage responsive when I minimize the page

  8. 8

    How can I make an image in a div responsive?

  9. 9

    How can I make this image responsive?

  10. 10

    How can i make my functions stops executing at a certain line?

  11. 11

    How can I make sure my power line network is secure?

  12. 12

    How can I make my HTML page Flexible/Responsive for iPhone 6s

  13. 13

    can I make my recycler view responsive with respect to screen sizes?

  14. 14

    How can i plot line chart in python?

  15. 15

    How to make svg line responsive

  16. 16

    How can I make a dynamic line chart with JavaFX using a socket input

  17. 17

    How do I make my absolute-positioned elements responsive?

  18. 18

    How do I make my website's background image responsive?

  19. 19

    How can I make sure my Google chart data gets removed from memory in Javascript?

  20. 20

    How can I display my javascript inside my <p> tags to make it a one line sentence?

  21. 21

    How to make my website responsive

  22. 22

    How can I combine "verticalRangeArea" with line chart in Kendo UI Chart?

  23. 23

    How can I make the background image to be responsive without losing the proportions

  24. 24

    How can I make an image responsive in a bootstrap modal?

  25. 25

    How can I make Woocommerce shopping cart table responsive?

  26. 26

    how can i make text wrapping in responsive div?

  27. 27

    How can I make this container and table responsive for mobile and ipad size?

  28. 28

    How Can I Force IE to Make Joomla Article Images Responsive?

  29. 29

    How can I make Ubuntu more responsive on a netbook?

HotTag

Archive