Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApexChart Angular doesn't render anything #197

Open
vgpastor opened this issue Feb 25, 2022 · 7 comments
Open

ApexChart Angular doesn't render anything #197

vgpastor opened this issue Feb 25, 2022 · 7 comments

Comments

@vgpastor
Copy link

vgpastor commented Feb 25, 2022

Hi!

I'm trying to integrate apexchart into my webapp, but doen't render anything and no error are show in console.

I based the test in https://apexcharts.com/angular-chart-demos/line-charts/basic/

My stack
"@angular/common": "~13.0.0", "@ionic/angular": "^6.0.0", "apexcharts": "^3.33.1", "ng-apexcharts": "^1.7.0",
The steps that i make:
1º Install npm libraries
2º Add NgApexchartsModule to app.module.ts
3º In a tab page

` import {AfterViewInit, Component, ViewChild} from '@angular/core';
import {
ApexAxisChartSeries,
ApexChart,
ApexDataLabels,
ApexGrid,
ApexStroke, ApexTitleSubtitle,
ApexXAxis,
ChartComponent
} from 'ng-apexcharts';

export type ChartOptions = {
series: ApexAxisChartSeries;
chart: ApexChart;
xaxis: ApexXAxis;
dataLabels: ApexDataLabels;
grid: ApexGrid;
stroke: ApexStroke;
title: ApexTitleSubtitle;
};

@component({
selector: 'app-profile',
templateUrl: 'profile.page.html',
styleUrls: ['profile.page.scss']
})
export class ProfilePage implements AfterViewInit {
@ViewChild('chart') chart: ChartComponent;
public chartOptions: Partial;

constructor() {
this.chartOptions = {
series: [
{
name: 'Desktops',
data: [10, 41, 35, 51, 49, 62, 69, 91, 148]
}
],
chart: {
height: 350,
type: 'line',
zoom: {
enabled: false
}
},
dataLabels: {
enabled: false
},
stroke: {
curve: 'straight'
},
title: {
text: 'Product Trends by Month',
align: 'left'
},
grid: {
row: {
colors: ['#f3f3f3', 'transparent'], // takes an array which will be repeated on columns
opacity: 0.5
}
},
xaxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep'
]
}
};

}

}
`

In profile.page.html

<apx-chart [series]="chartOptions.series" [chart]="chartOptions.chart" [xaxis]="chartOptions.xaxis" [dataLabels]="chartOptions.dataLabels" [grid]="chartOptions.grid" [stroke]="chartOptions.stroke" [title]="chartOptions.title" ></apx-chart>

I'm make any thing wrong?? Thanks!!

@tguimmaraess
Copy link

tguimmaraess commented Mar 2, 2022

The problem is that you are using chartOptions inside the constructor, use it inside ngOnInit instead


import {OnInit, AfterViewInit, Component, ViewChild} from '@angular/core';
import {
ApexAxisChartSeries,
ApexChart,
ApexDataLabels,
ApexGrid,
ApexStroke, ApexTitleSubtitle,
ApexXAxis,
ChartComponent
} from 'ng-apexcharts';

export type ChartOptions = {
series: ApexAxisChartSeries;
chart: ApexChart;
xaxis: ApexXAxis;
dataLabels: ApexDataLabels;
grid: ApexGrid;
stroke: ApexStroke;
title: ApexTitleSubtitle;
};

@Component({
selector: 'app-profile',
templateUrl: 'profile.page.html',
styleUrls: ['profile.page.scss']
})
export class ProfilePage implements OnInit {
@ViewChild('chart') chart: ChartComponent = {} as ChartComponent;
public chartOptions: Partial<any> = {} as Partial<any>;

constructor() {

}

ngOnInit() {
  this.chartOptions = {
  series: [
  {
  name: 'Desktops',
  data: [10, 41, 35, 51, 49, 62, 69, 91, 148]
  }
  ],
  chart: {
  height: 350,
  type: 'line',
  zoom: {
  enabled: false
  }
  },
  dataLabels: {
  enabled: false
  },
  stroke: {
  curve: 'straight'
  },
  title: {
  text: 'Product Trends by Month',
  align: 'left'
  },
  grid: {
  row: {
  colors: ['#f3f3f3', 'transparent'], // takes an array which will be repeated on columns
  opacity: 0.5
  }
  },
  xaxis: {
  categories: [
  'Jan',
  'Feb',
  'Mar',
  'Apr',
  'May',
  'Jun',
  'Jul',
  'Aug',
  'Sep'
  ]
  }
  };
}

}

@benboughton1
Copy link

I have same issue even when chartOptions is inside ngOnInit. I can get chart to render when I resize the page.

@tguimmaraess
Copy link

tguimmaraess commented May 22, 2022

I have same issue even when chartOptions is inside ngOnInit. I can get chart to render when I resize the page.

Try to add this to your chartOptions

chart: {
  height: 350,
  width: '100%',
  type: 'bar'
}

Example:

 //Chart options
 this.chartOptions = {

   .....

   chart: {
       height: 350, //Define the height
       width: '100%', //Also give the chart a width
       type: 'bar'
   },

   .....

 }

and in your template, in the apex chart component tag, you could have this:

[chart]="chartOptions.chart

Example:

<apx-chart [chart]="chartOptions.chart"></apx-chart>

@benboughton1
Copy link

benboughton1 commented May 23, 2022 via email

@voettingen
Copy link

Any solution for this issue available? We have the same problem, only rendering after resizing

@tguimmaraess
Copy link

tguimmaraess commented Nov 2, 2022

Any solution for this issue available? We have the same problem, only rendering after resizing

Have you tried setting up the chart in your ngOnInit and giving it a height and width? Also you could try @benboughton1's solution

@D3Dall
Copy link

D3Dall commented Nov 30, 2023

i have the same problem. Setting up the chart in the ngOnInit do not resolve the problem. In my example, i update the series and the xaxis. If i update only one of these (series or xaxis), the chart render correctly. When i want to update the series and the xaxis at the same time, it doesn't work until i resize the browser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants