Chart.js Example Isn't working - chart.js

I pretty much copy pasted from: http://www.chartjs.org/docs/latest/ aaaaand my html page isnt doing anything...
heres what is have:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<canvas id="myChart" width="400" height="400"></canvas>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/0.2.0/Chart.min.js">
var ctx = document.getElementById("myChart").getContext('2d');
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: /* default options from chart.js site */
});
myChart();
What am i missing...
I have a src in script, I'm not getting any errors in console..I have the canvas.. I've called myChart()... Idk..

You are using a super old version of Chart.js. If you update to a more recent version it will begin working.
I also saw the same thing you explained when I used the old version 0.2.0 and it starts working when using the below 2.7.2 version CDN
https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js
You can find the above url from the Chart.js installation page

Related

When i use charts.js -plugins-datalabels in my page with 2 Charts , Only the first charts show up and the second one is disappeared from the page

When i use charts.js -plugins-datalabels in my page with 2 Charts , Only the first charts show up and the second one is disappeared from the page'
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels#2.1.0/dist/chartjs-plugin- datalabels.min.js"></script>
First Chart
<div class="row">
<div class="col-md-4 text-center"> {{ccmonth}}
<canvas id="myChart7" height ="400"></canvas>
<script>
// setup
const data = {
labels: ['Central', 'Eastern', 'Western', 'Kingdom'],
datasets: [{
label: 'Sales',
data: [{{ordordc_month}}, {{ordorde_month}}, {{ordordw_month}},{{ordord_month}}],
backgroundColor: [
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 0.2)'
],
borderColor: [
'rgba(255, 26, 104, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(0, 0, 0, 1)'
],
borderWidth: 1
},{
label: 'Target',
data: [50000, 50000, 50000, 40000,],
backgroundColor:'rgba(255, 26, 104, 0.2)',
borderColor: 'rgba(255, 26, 104, 1)',
borderWidth: 1
}]
};
// config
const config1 = {
type: 'bar',
data,
options: {
scales: {
x: {
stacked: true
},
y: {
beginAtZero: true,
stacked: true
}
}
},
plugins: [ChartDataLabels],
};
// render init block
const myChart7 = new Chart(
document.getElementById('myChart7'),
config1
);
</script>
</div>
</div>
Second chart (not shown up)
<div class="row">
<div class="col-md-4 text-center"> Week {{lweek}} Achievment
<canvas id="myChart6" height ="400"></canvas>
<script>
// setup
const data = {
labels: ['Central', 'Eastern', 'Western', 'Kingdom'],
datasets: [{
label: 'Sales',
data: [{{ordordc}}, {{ordorde}}, {{ordordw}},{{ordord}}],
backgroundColor: [
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(54, 162, 235, 0.2)'
],
borderColor: [
'rgba(255, 26, 104, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(0, 0, 0, 1)'
],
borderWidth: 1
},{
label: 'Target',
data: [50000, 50000, 50000, 40000,],
backgroundColor:'rgba(255, 26, 104, 0.2)',
borderColor: 'rgba(255, 26, 104, 1)',
borderWidth: 1
}]
};
// config
const config = {
type: 'bar',
data,
options: {
scales: {
x: {
stacked: true
},
y: {
beginAtZero: true,
stacked: true
}
}
},
plugins: [ChartDataLabels],
};
// render init block
const myChart6 = new Chart(
document.getElementById('myChart6'),
config
);
</script>
</div>
</div>
I tried to search in your awesome , and read the docommentation of the plugins-datalabels.
I expect a solution by one of your greatest users

vue3 chart.js Cannot read properties of null (reading 'getContext')

I'm new to Vue3 and Chart.js and I couldn't figure out how to solve this issue. I'm keep getting the same error over and over. Can someone please help me?
TypeError: Cannot read properties of null (reading 'getContext')
Here is my template:
<template>
<div class="border border-dark rounded container m-2">
<canvas id="myChart" width="400" height="400"></canvas>
</div>
</template>
and my script:
import Chart from 'chart.js/auto'
export default {
mounted() {
const ctx = document.getElementById("myChart").getContext('2d');
const 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: {
y: {
beginAtZero: true
}
}
}
});
console.log('mounted')
}
And I have one more question, when I run this code I get null. Is this normal?
console.log(document.getElementById("myChart"))

ChartJS Create New chart from variable

This seems pretty straightforward but I'm not understanding why it's not rendering the chart correctly. Is there a way to pass the configuration section below to a new chart as a variable? If I load the chart using the coding below it works perfectly. I've tried putting the configuration section into a variable and then placing that within the configuration section, surrounding it by brackets. When I do there's no errors it's just blank like it's not receiving it correctly.
new Chart(myCanvas, {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: {
y: {
beginAtZero: true
}
}
}})
you must add document.getElementById("MyChart") inside Chart() object and you will get something like this
<script src="https://cdn.jsdelivr.net/npm/chart.js#3.6.0/dist/chart.min.js"></script>
<canvas id="MyChart" width="400" height="200"></canvas>
<script>
new Chart(document.getElementById("MyChart"), {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: {
y: {
beginAtZero: true
}
}
}})
;
</script>

Ember.Computed is not a function

I'm trying to setup simple application with Ember using ember-chart:
https://www.npmjs.com/package/ember-cli-chart
I have my charts.hbs file:
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
{{#toggle-section}}
<div class="chart-container">
{{ember-chart type=CHARTTYPE data=CHARTDATA options=CHARTOPTIONS width=CHARTWIDTH height=CHARTHEIGHT}}
</div>
{{/toggle-section}}
</div>
</div>
</div>
and my charts.js controller with sample from documentation of Chart.js:
import Controller from '#ember/controller';
import Ember from "ember";
export default Controller.extend({
CHARTTYPE: 'bar',
CHARTDATA: Ember.Computed('', function () {
return {
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
}]
}
}),
CHARTOPTIONS: Ember.Computed('', function () {
return {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
}),
CHARTWIDTH: 500,
CHARTHEIGHT: 500,
});
In index.js route i have redirection to charts route:
beforeModel() {
this.replaceWith('charts');
}
When I am trying to open my localhost:4200 after ember s it gives me an error in console:
router.js:927 Error while processing route: index Ember.Computed is not a function TypeError: Ember.Computed is not a function
I tried to search for an answer but nothing seems to work.
The function is lowercase: Ember.computed
It would also be better to use this import:
import { computed } from '#ember/object';
to avoid having to bring in all of the Ember framework just to access the computed function.

canvas does not display proper width and height chart.js

I use canvas width and highth as follow
<canvas id="myChart" height="400" width="400" ></canvas>
However, when the page is rendered the canvas somehow becomes
<canvas height="1643" id="myChart" width="1643" style="display: block; width: 1643px; height: 1643px;"></canvas>
I found similar problem and such solution works for me too,
options: {
responsive: false
}
But I'm not sure that that is proper solution just to turn off responsive option becaouse that is one of the cool thing in chart.js
my code
<Html>
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.2/Chart.js" ></script>
</head>
<Body>
<canvas id="myChart" height="400" width="400" ></canvas>
</Body>
<script>
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Steps',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 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
},
{
label: "Goal Line",
type:'line',
data: [{x:0,y:10},{x:0,y:10},{x:0,y:10},{x:0,y:10},{x:0,y:10},{x:0,y:10}],
fill: false,
pointRadius:[0,0,0,0,0,0,0],
borderWidth: 5,
borderColor: "rgba(243, 52, 52,1)",
borderJoinStyle: 'miter',
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
},
}],
xAxes: [{
ticks: {
beginAtZero:true
},
}]
},
}
});
</script>
</Html>
You can use css max-width and min-width if you want to avoid your chart from being too big or too small:
<canvas id="myChart" height="400" width="400" style="min-width: 200px; max-width: 600px"></canvas>
Note: You don't need to set a width via css. You can place the element within a grid column. Charts are responsive, so they will adjust the width to the size of the parent.
<div class="col-md-5">
<canvas id="myChart" ></canvas>
</div>