Reputation: 275
I am trying to display the chart js line chart inside the tabs( angular material) but the chart is not displaying, it is displaying empty.
Here is my code :
HTML:
<mat-tab-group animationDuration="0ms">
<mat-tab label="First">
<div class="chart-container" style="height:50vh;">
<canvas id="demo"></canvas>
</div>
</mat-tab>
<mat-tab label="Second">Content 2</mat-tab>
<mat-tab label="Third">Content 3</mat-tab>
</mat-tab-group>
js file:
var chartColors = {
test_1: '#fba66c',
test_2: '#86b9ce',
test_3: '#4274fd'
};
var canvas : any = document.getElementById("demo");
var ctx = canvas.getContext("2d");
var test_1_data = [1, 0, 3, 0, 5, 6, 8];
var test_2_data = [5, 1, 8, 0, 3, 6, 5];
var test_3_data = [9, 8, 3, 4, 5, 6, 2];
var lineChartData = {
labels: ['Mon', 'Tue', 'Wed', 'Thu','Fri', 'Sat', 'Sun'],
datasets: [{
label: 'test_1',
borderColor: chartColors.test_1,
backgroundColor: chartColors.test_1,
fill: false,
data: test_1_data ,
yAxisID: 'y-axis-1'
},
{
label: 'test_2',
borderColor: chartColors.test_2,
backgroundColor: chartColors.test_2,
fill: false,
data: test_2_data ,
yAxisID: 'y-axis-1'
},
{
label: 'test_3',
borderColor: chartColors.test_3,
backgroundColor: chartColors.test_3,
fill: false,
data: test_3_data ,
yAxisID: 'y-axis-1'
}
]
};
var demo= Chart.Line(ctx, {
data: demolineChartData,
options: {
responsive: true,
hoverMode: 'index',
stacked: false,
maintainAspectRatio: false,
title: {
display: true,
text: 'Last updated at : '+ new Date().toLocaleString()
},
scales: {
yAxes: [{
ticks:{
min: 0,
max:15,
stepSize:3
},
type: 'linear',
position: 'left',
id: 'y-axis-1'
}],
}
}
});
I have used the tabs from angular material and charts from charts js. I tried lot of ways but nothing is working, how can I do that.
Thanks in advance.
Upvotes: 0
Views: 2238
Reputation: 182
Solution for me was to just wrap tab contents in ng-template with matTabContent directive and like magic charts are drawn.
<mat-tab-group animationDuration="0ms">
<mat-tab label="First">
<ng-template matTabContent>
<div class="chart-container" style="height: 50vh">
<canvas id="demo"></canvas>
</div>
</ng-template>
</mat-tab>
<mat-tab label="Second">
<ng-template matTabContent> Content 2 </ng-template>
</mat-tab>
<mat-tab label="Third">
<ng-template matTabContent> Content 3 </ng-template>
</mat-tab>
</mat-tab-group>
Upvotes: 6
Reputation: 7004
You should init your chart into ngOnInit method. You can create a function that instantiate the chart:
chart.ts:
export const myChart = () => {
var chartColors = {
test_1: '#fba66c',
test_2: '#86b9ce',
test_3: '#4274fd'
};
var canvas : any = document.getElementById("demo");
var ctx = canvas.getContext("2d");
var test_1_data = [1, 0, 3, 0, 5, 6, 8];
var test_2_data = [5, 1, 8, 0, 3, 6, 5];
var test_3_data = [9, 8, 3, 4, 5, 6, 2];
var lineChartData = {
labels: ['Mon', 'Tue', 'Wed', 'Thu','Fri', 'Sat', 'Sun'],
datasets: [{
label: 'test_1',
borderColor: chartColors.test_1,
backgroundColor: chartColors.test_1,
fill: false,
data: test_1_data ,
yAxisID: 'y-axis-1'
},
{
label: 'test_2',
borderColor: chartColors.test_2,
backgroundColor: chartColors.test_2,
fill: false,
data: test_2_data ,
yAxisID: 'y-axis-1'
},
{
label: 'test_3',
borderColor: chartColors.test_3,
backgroundColor: chartColors.test_3,
fill: false,
data: test_3_data ,
yAxisID: 'y-axis-1'
}
]
};
var demo= Chart.Line(ctx, {
data: demolineChartData,
options: {
responsive: true,
hoverMode: 'index',
stacked: false,
maintainAspectRatio: false,
title: {
display: true,
text: 'Last updated at : '+ new Date().toLocaleString()
},
scales: {
yAxes: [{
ticks:{
min: 0,
max:15,
stepSize:3
},
type: 'linear',
position: 'left',
id: 'y-axis-1'
}],
}
}
});
}
Then into your component, you import it:
...
ngOnInit() {
myChart();
}
...
Note: Remember, you can import directly the chart.js lib into your component, and use it.
Hope the workflow helps you.
Upvotes: 0