Charts doesn't appear - javascript

I trying to use Chart.js in my web, but the chart won't appear and there's no error code in chrome console. I'm using Chart.js v2.7.2.
I tried any solution but doesn't help me, i also copy the code from chart.js documentation. what's wrong?
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue"],
datasets: [{
label: '# of Votes',
data: [12, 19],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>

Try this:
<html>
<head>
<script type="text/javascript">
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer", {
theme: "light1", // "light2", "dark1", "dark2"
animationEnabled: false, // change to true
title:{
text: "Basic Column Chart"
},
data: [
{
// Change type to "bar", "area", "spline", "pie",etc.
type: "column",
dataPoints: [
{ label: "apple", y: 10 },
{ label: "orange", y: 15 },
{ label: "banana", y: 25 },
{ label: "mango", y: 30 },
{ label: "grape", y: 28 }
]
}
]
});
chart.render();
}
</script>
</head>
<body>
<div id="chartContainer" style="height: 370px; width: 100%;"></div>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"> </script>
</body>
</html>

Related

Chartjs bar chart got clipped

I have a bar chart and I wanted to stacking the yellow bar over the purple and pink bar. Somehow the xAxes of the yellow bar start/end at the left/right corner so it got clipped on each sides. I want it to be at center like at value = 3 on the image below:
here's my code :
var ctx = document.getElementById('myChart');
var mixedChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['1', '2', '3', '4', '5'],
datasets: [
{
label: 'Total Trip',
yAxisID: 'Total-Trip',
data: [1000, 1350, 1230, 940, 640],
type: 'line',
backgroundColor: ['transparent']
},
{
label: 'Orang',
xAxisID: 'x-B',
yAxisID: 'Orang-y',
data: [50, 46, 44, 46, 49],
backgroundColor: 'rgba(255, 206, 86)',
barThickness: 30,
clip: {
left: 50,
right: 50
}
},
{
label: 'Travel Costs',
xAxisID: 'x-A',
yAxisID: 'Orang-y',
data: [100, 96, 84, 76, 69],
backgroundColor: 'rgba(255, 99, 132)',
},
{
label: 'Travel Costs USD',
xAxisID: 'x-A',
yAxisID: 'Orang-y',
data: [150, 106, 94, 96, 99],
backgroundColor: 'rgba(155, 19, 232)',
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
labels: {
render: 'value'
}
},
scales: {
yAxes: [
{
id: 'Total-Trip',
display: false,
type: 'linear',
position: 'right',
ticks: {
min: 0,
}
},
{
id: 'Orang-y',
stacked: false,
ticks: {
beginAtZero: true
},
}
],
xAxes: [
{
id: 'x-A',
},
{
id: 'x-B',
stacked: true,
clip: {
left: 50,
right: 50
}
},
]
}
}
});
or you can see here, please click Show Files and then choose bar-side-to-side.html
https://replit.com/#panjigemilang/html?v=1
I think offset and display properties will help:
{
id: 'x-B',
offset:true,
display:false
},
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>
Transaksi CBT - Non CBT
</h1>
<div class="panel">
<canvas id="myChart" width="400" height="400"></canvas>
</div>
<style>
.panel {
height: 400px;
width: 400px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js" integrity="sha512-s+xg36jbIujB2S2VKfpGmlC3T5V2TF3lY48DX7u2r9XzGzgPsa6wTpOQA7J9iffvdeBN0q9tKzRxVxw1JviZPg==" crossorigin="anonymous" referrerpolicy="no-referrer">
</script>
<script src="https://cdn.jsdelivr.net/gh/emn178/chartjs-plugin-labels/src/chartjs-plugin-labels.js" crossorigin="anonymous" referrerpolicy="no-referrer">
</script>
<script type="text/javascript">
var ctx = document.getElementById('myChart');
var mixedChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['1', '2', '3', '4', '5'],
datasets: [{
label: 'Total Trip',
yAxisID: 'Total-Trip',
data: [1000, 1350, 1230, 940, 640],
type: 'line',
backgroundColor: ['transparent']
},
{
label: 'Orang',
xAxisID: 'x-B',
yAxisID: 'Orang-y',
data: [50, 46, 44, 46, 49],
backgroundColor: 'rgba(255, 206, 86)',
barThickness: 30,
},
{
label: 'Travel Costs',
xAxisID: 'x-A',
yAxisID: 'Orang-y',
data: [100, 96, 84, 76, 69],
backgroundColor: 'rgba(255, 99, 132)',
},
{
label: 'Travel Costs USD',
xAxisID: 'x-A',
yAxisID: 'Orang-y',
data: [150, 106, 94, 96, 99],
backgroundColor: 'rgba(155, 19, 232)',
}
]
},
options: {
layout: {
margin: 20
},
responsive: true,
maintainAspectRatio: false,
plugins: {
labels: {
render: 'value'
}
},
scales: {
yAxes: [{
id: 'Total-Trip',
display: false,
type: 'linear',
position: 'right',
ticks: {
beginAtZero: true
}
},
{
id: 'Orang-y',
stacked: false,
ticks: {
beginAtZero: true
}
},
],
xAxes: [{
id: 'x-A',
},
{
id: 'x-B',
offset: true,
display: false
},
]
}
}
});
</script>
</body>
</html>

Chart js show data on hovering legend

I have a pie chart and I want to get the number/data of each section of pie chart when hovered upon. I have seen the implementation of this in version 2.9.x but in version 3.x it is not working. I have also tried doing this here:
var pieChartHome = new Chart(
"myChart",
{
type: 'doughnut',
data: {
labels: ['One', 'Two', 'Three'],
datasets: [{
data: [4, 5, 3],
backgroundColor: ['rgba(255, 99, 132, 0.2)', 'rgba(255, 159, 64, 0.2)', 'rgba(54, 162, 235, 0.2)'],
borderColor: ['rgb(255, 99, 132)', 'rgb(255, 159, 64)', 'rgb(54, 162, 235)'],
hoverBackgroundColor: ['rgba(255, 99, 132, 0.4)', 'rgba(255, 159, 64, 0.4)', 'rgba(54, 162, 235, 0.4)'],
borderWidth: 1,
hoverBorderWidth: 3
}]
},
options: {
responsive:false,
plugins: {
legend: {
position:'right',
onHover: (evt, legendItem,legend) => {
const index = pieChartHome.data.labels.indexOf(legendItem.text);
const rect = pieChartHome.canvas.getBoundingClientRect();
const point = pieChartHome.getDatasetMeta(0).data[index].getCenterPoint();
const e = new MouseEvent('mousemove', {
clientX: rect.left + point.x,
clientY: rect.top + point.y
});
pieChartHome.canvas.dispatchEvent(e);
},
}
}
},
}
);
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<div>
<canvas id="myChart"></canvas>
</div>
But this shows me flickering data and not something like this:
Chart.js - show tooltip when hovering on legend
You can set the tooltip via chart.js public methods itself:
var options = {
type: 'doughnut',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
}]
},
options: {
plugins: {
legend: {
position: 'left',
onHover: (evt, item, legend) => {
const chart = legend.chart;
const tooltip = chart.tooltip;
const chartArea = chart.chartArea;
tooltip.setActiveElements([{
datasetIndex: 0,
index: item.index,
}], {
x: (chartArea.left + chartArea.right) / 2,
y: (chartArea.top + chartArea.bottom) / 2,
});
chart.update();
},
}
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.4.1/chart.js"></script>
</body>

How to hide grid lines and x-axis labels in chart.js?

I'm using chart.js v3.2.0 and I want to disable the grid lines and x-axis labels. I've tried various examples from other stack overflow posts but none seem to work.
https://jsfiddle.net/gcp95hjf/1/
html
<div style = "width: 600px; height: 400px;">
<canvas id="chartJSContainer" width="1" height="400"></canvas>
</div>
js
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1,
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255, 99, 132,1)'
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1,
backgroundColor: 'rgba(54, 162, 235, 0.2)',
borderColor: 'rgba(54, 162, 235, 1)'
}
]
},
options: {
scales: {
xAxes: [{
display: false,
ticks: {
display: false //this will remove only the label
}
}],
yAxes: [{
display:false
}]
},
responsive: true,
maintainAspectRatio: false
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
Does anyone know how to do it?
Thanks
Chart.js v3 has multiple breaking changes, one of them is how you define the scales, it is stated in the migration guide and shown in a lot of the examples (https://www.chartjs.org/docs/3.2.1/getting-started/v3-migration.html)
You will have to change scales: { yAxes: [], xAxes: []} to scales: { y: {}, x: {}}
Working fiddle: https://jsfiddle.net/Leelenaleee/r26h71fm/2/
Hide GridLines on Both Axis -
ChartJS v2 (older version) -
scales: {
x: [
{
gridLines: {
display: false,
},
},
],
y: [
{
gridLines: {
display: false,
},
},
],
},
Newer Version ChartJS v3 -
scales: {
x: {
grid: {
display: false,
},
},
y: {
grid: {
display: false,
},
},
},

Chart.js showing nothing on canvas

I'm trying to create a graphic in my .html file in the altervista ftp.
Here's my code:
<canvas id="myChart" width="400" height="200"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.js" />
<script>
var ctx = document.getElementById("myChart");
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)',
...
],
borderColor: [
'rgba(255,99,132,1)',
...
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
</script>
Why I'm getting nothing in canvas?
Also the console does not show errors.
You should remove the ... under backgroundColor and borderColor or add colors according to the number of labels.
You need to call myChart.update() after initializing the chart.
I solved my error editing the first script from this:
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.js" />
to this:
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.3/Chart.js" ></script>
as suggested by #j.ian.le
And adding
responsive: false,
under options: {

error : "Uncaught TypeError: Cannot read property 'length' of null" Chart.js

I made several graphic with Chart.js that works. But the polar graphic and the radar does not work. I get the following error : "Uncaught TypeError: Cannot read property 'length' of null"
I use charjs 2.3.0
I do not understand where the error can come .
<div class="col-lg-6 col-md-6">
<canvas class="box box-warning" id="myChart4" width="400" height="400"></canvas>
</div>
<div class="col-lg-6 col-md-6">
<canvas class="box box-warning" id="myChart5" width="400" height="400"></canvas>
</div>
//////////////////////////////////////////////
// Chart Polar
//////////////////////////////////////////////
var ctx = document.getElementById("myChart4");
new Chart(ctx, {
type: 'polarArea',
data: {
labels: [
"Red",
"Blue",
"Yellow"
],
datasets: [{
data: [300, 50, 100],
backgroundColor: [
"#FF6384",
"#36A2EB",
"#FFCE56"
],
hoverBackgroundColor: [
"#FF6384",
"#36A2EB",
"#FFCE56"
]
}]
},
options: {
cutoutPercentage: 50,
animation: {
animateScale: false
}
}
});
//////////////////////////////////////////////
// Radar
//////////////////////////////////////////////
var ctx = document.getElementById("myChart5");
var scatterChart = new Chart(ctx, {
type: 'radar',
data: data = {
labels: ["Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche", ],
datasets: [{
label: 'the first dataset',
backgroundColor: 'rgba(255, 99, 132, 0.2)',
borderColor: 'rgba(255,99,132,1)',
data: [10, 34, 50, 34, 56, 65, 43]
}, {
label: 'the second dataset',
backgroundColor: 'rgba(54, 162, 235, 0.2)',
borderColor: 'rgba(54, 162, 235, 1)',
data: [54, 72, 100, 36, 76, 23, 21]
}]
},
options: {
scales: {
xAxes: [{
type: 'linear',
position: 'bottom'
}]
}
}
});
the problem is this line:
var ctx = document.getElementById('sexe');
you do not have any element with id "sexe" on the page
The error was due to the element not being present in the HTML when Chart.js was trying to attach the chart on it.
If u are using Angular 2+, try to build the options and data chart in. I needed to do this because I used an #Input() attribute to get the data shown in the chart:
ngOnChanges(changes: SimpleChanges){
this.options = {
responsive: true,
maintainAspectRatio: false,
...
}
}
That's works for me. Hope it helps someone.

Categories

Resources