I am being new to chart js ,trying to implement in angular 4 application. even though no errors in the code chart is not appearing in browser. I referred a lot of documentation and online material to implement chart.i have spent almost 3 days to figure out the cause.Please help me to rectify the same thank you
Here is my code
dependencies:
"chart.js": "^2.7.3"
in angular cli.json
"../node_modules/chart.js/dist/Chart.js",
in Component
import {Chart} from 'chart.js';
ngOnInit() {
const el = <HTMLCanvasElement> document.getElementById('myChart');
const ctx = el.getContext('2d');
console.log(el);
console.log(ctx);
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
}
in html
<canvas id="myChart" width="400" height="400"></canvas>
I am getting no error in broswer and i am able to print both native element and context and Chart object as well .Not sure why chart is not appearing in browser. Please help
nativeEliment on console giving-
<canvas _ngcontent-c1="" height="0" id="myChart" width="0" class="chartjs-render-monitor" style="display: block; height: 0px; width: 0px;"></canvas>
context on console:
CanvasRenderingContext2D {canvas: canvas#myChart, globalAlpha: 1, globalCompositeOperation: "source-over", filter: "none", imageSmoothingEnabled: true, …}
chart object on console.log-Chart {id: 0, ctx: CanvasRenderingContext2D, canvas: canvas#myChart.chartjs-render-monitor, config: {…}, width: 0, …}