Can't get Chart.js to run in a Vue.js component - chart.js

So I am trying to display a nice chart using Chart.js inside a Vue.js component... Should not be so complicated but I am hitting a wall there.
Here is my component code (syntax is based on Vue.js webpack boilerplate)
<template>
<canvas v-el:canvas style="width: 100%; height: 200px"></canvas>
</template>
<script>
import Chart from 'chart.js'
export default {
compiled: function () {
// fetch canvas DOM element
let canvas = this.$els.canvas
// init chart.js
let chart = new Chart(canvas, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3]
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
})
console.log(chart)
}
}
</script>
I simplified the component as much as I could, and used data from the doc example (will bind real data using props later).
So I keep getting this error in the console :
Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.
I looked it up, it seems that this has something to do with the variable passed to getComputedStyle used in Vue.js.
My best guess would be that this.$els.canvas does not return the right object. But if I output it in the console using console.log it seems right.
console.log(canvas)
// outputs <canvas style="width: 300px; height: 150px;"></canvas>
console.log(canvas.tagName)
// outputs CANVAS
console.log(canvas.style.height)
// outputs 200px
Hmmm this is actually weird and could be helpful : did you notice that I set the canvas style.height in the template, but if I do console.log(canvas) I see height: 150px but if console.log(canvas.style.height) 2 lines below, I get 200px as an output ?!! WTF is going on ?.. Vue is messing with my head right now
Link to jsFiddle for you to play with (check out the result in the console)
https://jsfiddle.net/kartsims/g8s12yd2/3/#&togetherjs=CQrzT996AR

I figured out why and will let you know, just in case someone happens to be in the same position :
I couldn't get this to work inside a vue component displayed by Vue Router... I figured that it was because I should have used ready hook instead of compiled... At compiled, the DOM is not fully ready yet.

Related

Charts.js is automatically adding comma as thousands separator when it shouldn't

Charts.js is abusively adding comma separator between thousands while is nowhere set to do this. To all floats. To the axis, labels, tooltips, everywhere possible and on all types of charts.
I am in EU, so the computer, browser, locales have nothing to do with EN/US. Nowhere in the code is specified to add these commas to numbers.
The underlying data does not contain commas either:
[{continent:"Africa",value:14802.32},{continent:"Asia",value:6783.37},...]
This is very annoying, unacceptable for my (French) customer.
Is it a bug ? Or please be kind and provide the way how to remove comma everywhere.
You must be doing something wrong because by default the data is seperated by dots, so you must have specified it somewhere. But you can configure it by using the locale property:
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12000.5, 19000, 3000, 5000, 2000, 3000],
borderColor: 'pink'
}]
},
options: {
//locale: 'en-EN' // Uncomment this line for "wrong" options
}
}
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.5.1/chart.js"></script>
</body>

Line is rendering only vertical line instead of sloped line

Line is not rendering correctly. It's rendering as instead of
My code is as follows:
LineGraph.js:
import React from 'react'
import {Line} from 'react-chartjs-2';
function Linegraph() {
return (
<div className="linegraph">
<Line
data={{
datasets:[{
type:"line",
data:[{x:10,y:20},{x:15,y:10},{x:12,y:4}],
backgroundColor:"black",
borderColor:'#5AC53B',
borderWidth:2,
pointBorderColor:'rgba(0,0,0,0)',
pointBackgroundColor:'rgba(0,0,0,0)',
pointHoverBackgroundColor:'#5AC53B',
pointHoverBorderColor:"#000000",
pointHoverBorderWidth:4,
pointHoverRadius:6,
}]
}}
/>
</div>
)
}
export default Linegraph
I'm following a tutorial here and at 1:33:17 they were successfully able to implement it while mine remained as the vertical line going straight down.
Here's also a screenshot of my project set-up:
Your help is greatly appreciated!
You could change the chart type to 'scatter' and add the property drawLine: true to the dataset.
datasets:[{
type: "scatter",
drawLine: true,
data:[{x:10,y:20},{x:15,y:10},{x:12,y:4}],
...
The Chart.js documentation states that scatter charts are based on basic line charts with the x axis changed to a linear axis.
Therefore, assuming you're using react-chartjs-2 together with Chart.js v3, you may also keep type: 'line' but will have to define the x-axis as type: 'linear' as follows:
data={{
type: "line",
...
}}
options={{
scales: {
x: {
type: "linear"
}
}
}}
We ended up getting this to work by going inside package.json and setting the following versions:
'chart.js':'^2.9.4',
'react-chartjs-2':'^2.11.1'
=)

Add Extra label in a pie chart

Is there a away to add an extra label at the bottom if a pie graph like this:
Yes, you can do it as follows:
options: {
legend: {
display: true,
title: {
display: true,
text: 'Custom Chart Title',
position: 'bottom'
}
}
You can also find some more details on the official documentation, here:
Chart JS Legend Official Documentation

Can I arrange labels of legend in Doughnut chart one by one?

I am using Doughnut chart of chart.js in a react js application. I would need the same look and feel for the legend than illustrated by:.
I have gone through the Chart.js documentation but I am helpless!
You have to add this at options, see next:
options: {
legend: {
display: true,
position: 'bottom',
labels: {
boxWidth: 10
}
}
}
https://www.chartjs.org/docs/latest/configuration/legend.html#legend-label-configuration here you can see all options what you can add to labels, see padding also, default is 10 you need only add boxWidth.

ChartJS - Pie/Doughnut Chart Top Cutoff

I'm using the latest chartJS and attempting to create a pie or doughnut chart. This seems like it should be relatively simple to me, but whenever the chart is created the top is cutoff like the image below. I've tried many things to fix this, and searched around with no luck. Has anyone run into this before? Is ChartJS no longer supported in most browsers?
I've seen other pie charts in JSfiddle that people have working, copying the exact same code into new files results in the same chopped off top graphs.
Other graphs work such as line/bar.
Edit: made some progress by changing the chart to load using windows.load. It will now display correctly when the page is resized at all, but will display the same way below initially.
Here is the HTML and JS code.
<div class="container-graph-window">
<canvas id="pieChart" width="435" height="435"></canvas>
<script>
pieChart();
</script>
</div> <!-- End continer-graph-window-->
/////////////////////////////////////////////JS CODE
function pieChart() {
var doughnutData = [
{
value: 300,
color:"#F7464A",
highlight: "#FF5A5E",
label: "Red"
},
{
value: 50,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green"
},
{
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow"
},
{
value: 40,
color: "#949FB1",
highlight: "#A8B3C5",
label: "Grey"
},
{
value: 120,
color: "#4D5360",
highlight: "#616774",
label: "Dark Grey"
}
];
var ctx = document.getElementById("pieChart").getContext("2d");
window.pieChart = new Chart(ctx).Doughnut(doughnutData, {responsive : true});
} //end pie chart
Note: I must pass data into the JS function and use it to create the graph.
SOLVED: Update all of your Chart.js files if you run into this issue to the latest version.