I wish to use a chartjs chart (a pie chart and a line chart) within an ion-slide (will one chart in each slide), but am having problems getting the size correct. If I have the chart directly inside the ion-content, then all is fine, it draws the correct size and resizes when I change the orientation etc. However, as soon as I introduce the slide I get problems (I also want a select box above the chart)
As an example, I have the following markup…
<ion-header>
<ion-navbar>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>
<ion-content (window:resize)="onResize()">
<ion-slides pager>
<ion-slide >
<ion-list id='select-list'>
<ion-item>
<ion-label >select me</ion-label>
<ion-select >
</ion-select>
</ion-item>
<ion-item>
<button ion-button >Click me</button>
</ion-item>
</ion-list>
<canvas style='height:100%;width:100%' (click)='pieCanvasClick($event)' #pie></canvas>
</ion-slide>
<ion-slide style="background-color: blue">
<h2>Slide 2</h2>
</ion-slide>
</ion-slides>
</ion-content>
The code to create is…
private createPie(): void {
Chart.defaults.global.tooltips.enabled = false;
Chart.defaults.global.legend.position = 'bottom';
this.pieChart = new Chart(this.pieCanvas.nativeElement, {
type: 'pie',
options: {
responsive: true,
maintainAspectRatio: false,
},
data: {
labels: ["Red", "Blue", "Yellow" ],
datasets: [{
label: '# of Votes',
data: [12, 19, 3 ],
backgroundColor: [
"#FF6384",
"#36A2EB",
"#FFCE56"
],
}]
}
});
}
II have attempted to resize in the window.size event handler…
public onResize() {
let chart = this.content.getContentDimensions();
let h = chart.contentHeight - 100;
let w = chart.contentWidth - 100;
let el : HTMLCanvasElement = this.pieCanvas.nativeElement;
if (this.pieChart != undefined) {
if (h < w) {
el.height = h - 130;
el.width = w - 130;
} else {
el.height = h;
el.width = w;
}
}
}
Below is how it looks (screen shot running in chrome)
And when I rotate it is even worse…
The code in the onResize() does not seem to do anything.
Does anyone have a way to use this chart inside the slide so I get all the resizing correct?
Thanks in advance for any suggestions.