Charts.js : How to remove the Vertical Black lines in Bar Chart - javascript

How can I remove the vertical black lines, which are getting displayed both sides of bars in the bar chart?
I tried both
options : {
scaleShowVerticalLines: false
}
options : {
scales : {
xAxes : [ {
gridLines : {
display : false
}
} ]
}
}
here is my code lines:
//Chart Axis's
let scales = {
xAxes: [{
gridLines: { display: false },
ticks: {
font: {
size: config.size,
},
color: "white",
},
}],
yAxes: [{
ticks: {
beginAtZero:true
},
}]
};
let ctx = document.getElementById('how_i_spend_canvas');
if (ctx) { // DOM element is present and not null
let categoriesChart = new Chart(ctx.getContext('2d'), {
type: 'bar',
data: howISpendChartdata,
// Chart pulgins & Options
plugins: [ChartDataLabels],
options: {
scales: scales,
responsive: true,
maintainAspectRatio: false,
aspectRatio: 2,
plugins: plugins,
}
}
onClick: function (evt, element) {}
but iy didn't work for me.
Reference where the arrow is pointing the lines.

Got the Answer now as in Chart JS 3 they have changed to grid instead of the common gridlines command.
Now in chart JS 3 can use :
x: {
grid:{
color:'red',
display: false,
}
},
y:{
ticks: {
beginAtZero: true
},
grid: {
color: 'green',
display: true,
}
}
Result ScreenShot

Related

Options won't load in chart.js

For some reason al my options will not be shown in the chart. Can't find the error myself. The code of the chart is a snippet from this code: https://codepen.io/sietssoo/pen/oNqGpXq
If anyone could find the error, you would be my hero.
//Making chart
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'bar',
data: getChartData(),
options: {
legend: {display: true},
responsive: true,
tooltips: {
mode: 'index',
intersect: false,
callbacks: {
label: function (tooltipItem, data) {
return data.datasets[tooltipItem.datasetIndex].label + '€ ' + tooltipItem.yLabel;
}
}
},
scales: {
x: [{
stacked: true,
scaleLabel: {
isplay: true,
labelString: 'Jaar'
}
}],
y: [{
stacked: true,
scaleLabel: {display: true},
ticks: {
callback: function (value) {
return '€' + value;
}
}
}]
}
}
});
That is because you are using V2 syntax in V3.
the legend and tooltip config have been moved to the options.plugins.legend and options.plugins.tooltip namespace, the scales are not arrays anymore all scales are their own object.
For all the changes and detailed explanation you can read the migration guide

Styling problems with Chart.Js

How can I add these styles to my graph? I'm having trouble finding it in the documentation for it, and the other forums aren't very helpful. I am using Flask for the backend, so that's why the data values look like that.
Labels on top of bars
Change font size on both axis
Set y scale as a fixed value (0,8)
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var options = {
scales: {
yAxes: [{
ticks: {
fontSize: 40
}
}]
}
}
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Lithium Battery', 'Other Mfg', 'Delivery', 'Tailpipe', 'Fuel Cycle', 'Disposal', 'Total'],
datasets: [{
label: 'ICEV CAR (MTCO2e/Year)',
labels: [
"{{data['battery_icev_car']}}",
"{{data['other_mfg_icev_car']}}",
"{{data['delivery_icev_car']}}",
"{{data['tailpipe_icev_car']}}",
"{{data['fuel_cycle_icev_car']}}",
"{{data['disposal_icev_car']}}",
"{{data['total_icev_car']}}"
],
data: [ /* errors are normal, fixes when server ru
[0, {{data['y1_icev']}}],
[{{data['y1_icev']}}, {{data['y2_icev']}}],
[{{data['y2_icev']}}, {{data['y3_icev']}}],
[{{data['y3_icev']}}, {{data['y4_icev']}}],
[{{data['y4_icev']}}, {{data['y5_icev']}}],
[{{data['y5_icev']}}, {{data['y6_icev']}}],
[0, {{data['y6_icev']}}]
],
backgroundColor: [
'rgba(0,0,0,0.9)'
],
borderColor: [
'rgba(0,0,0,1)'
],
borderWidth: 1,
borderSkipped: false
}]
},
options: {
responsive: true,
plugins: {
legend: {
labels: {
// This doesn't work on axis
font: {
size: 20
}
}
}
},
y: {
beginAtZero: true
}
}
});
</script> </script>
Since your config is not working I assume you are using V3, in which case the solution is as followed:
For point one you need to use an external plugin, you can write your own or use the datalabels plugin
For your second problem you need to style it as a font object as told in the documentation like so:
options: {
scales: {
y:{
ticks:{
font:{
size: 50
}
}
}
}
}
For your last part you can set the stepSize to 0.8

How to hide label for chart.js

I have chart which show 3 types of label
I want to keep two of them and want to hide one Invoice Income Report. How can I hide that one label? I am using chart.js v2
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Invoice Income Report',
data: bar_chart_data,
backgroundColor: colors,
borderWidth: 1
}, {
label: 'Below Average',
backgroundColor: ['rgba(255, 99, 132, 1)']
}, {
label: 'Above Average',
backgroundColor: ['rgba(11, 156, 49, 1)']
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
},
}
});
You can simply make display false.
const options = {
responsive: true,
plugins: {
legend: {
position: 'top' as const,
display: false,
},
title: {
display: false,
text: 'Chart.js Line Chart',
},
},
};
To hide the labels on version 4.2.1 of chart.js use this:
// Example chart.
const chart = new Chart(id, {
type: 'bar', // Chart type.
data: data, // Your data.
options: {
// Add plugins to options.
plugins: {
legend: {
display: false // This hides all text in the legend and also the labels.
}
}
// add your scales or other options.
}
});
For more info use the doc: https://www.chartjs.org/docs/latest/configuration/legend.html
In chart.js, You can hide the labels using legend attribute. Add the following code in options of chart.js
legend: {
display: false
}
According to your code, after adding legend the options will be .....
options: {
scales: {
y: {
beginAtZero: true
}
},
legend: {
display: false
}
}
Add legend option to options configuration, set display to false:
, options: {
scales: {
y: {
beginAtZero: true
}
},
legend: {
display: false
}
}
docs3:https://www.chartjs.org/docs/latest/configuration/legend.html
docs2:https://www.chartjs.org/docs/2.6.0/configuration/legend.html

Chart.js mixed chart : Lines not starting at zero

current situation
chart
what i want : chart
I build a mixed chart with lines and bars. I have problem with lines.
Lines not starting at zero.
Here is my codes;
https://jsfiddle.net/ameydan/vb4fsyqp/31/
` options: {
legend: {
display: false,
position: 'top',
},
elements: {
point: {
radius: 0
}
},
scales: {
yAxes: [{
stacked: true,
ticks: {
beginAtZero: true
}
}
],
xAxes: [{
stacked: true,
ticks: {
beginAtZero: true
}
}
]
}
}`
thanks in advance.
beginAtZero : true , not solved problem.
That is cause you setted stacked to true.
scales: {
yAxes: [{
stacked: **false**,
ticks: {
beginAtZero: true
}
}
],
xAxes: [{
stacked: true,
ticks: {
beginAtZero: true
}
}
]
}
EDIT:
Second thought coming from => https://www.chartjs.org/docs/latest/charts/mixed.html
At this point we have a chart rendering how we'd like. It's important to note that the default options for a line chart are not merged in this case. Only the options for the default type are merged in. In this case, that means that the default options for a bar chart are merged because that is the type specified by the type field.
I found a solution!
Just add this :
myChart.options.scales.xAxes[0].offset = false;
myChart.update();
The only counterpart is that if there's data # index 0 of a bar dataset, it will be only half drawn and the last one as well as shown here : https://jsfiddle.net/0qsh8znu/1/
I had the same issue. When I followed #rOulito 's solution, I found that the lack of offset for my line graph made it no longer line up with the data points of my bar graph, so that didn't work, sadly.
My solution was to follow the docs for a chartjs plugin for annotations
This is how I drew a line from the first data point of my line graph to the bottom left of the graph:
let options = {
plugins: {
annotation: {
annotations: {
line1: {
type: 'line',
yMax: 50000,
xMax: 0,
borderColor: 'black',
borderWidth: 3,
},
},
},
},
};

Remove top horizontal line in a horizontal bar chart (Chart.js 2)

I created a horizontal grouped bar chart in Chart.js but am getting stuck on a weird problem.
The problem is that while I disabled all grid lines and borders the chart is still showing a top horizontal line which I would like to get rid off.
It is visible in this screenshot: http://imgur.com/41YGxA8.
I also made a JSFiddle: https://jsfiddle.net/bsocw1v9/
function create_horizontal_bar_chart(){
/**
* Draw the chart on the correct canvas. Add the correct data sets
* and set the correct draw type.
*/
let ctx = document.getElementById('monthly_subscription_revenue_chart').getContext("2d");
let data = {
labels: ['Diensten'],
datasets: [
{
type: 'horizontalBar',
backgroundColor: "rgb(0,33,86)",
data: [2250],
stack: 1
},
{
type: 'horizontalBar',
backgroundColor: "rgb(219,219,219)",
data: [3000],
stack: 2
},
{
type: 'horizontalBar',
backgroundColor: "rgb(240,95,0)",
data: [750],
stack: 3
},
]
};
new Chart(ctx, {
type: 'horizontalBar',
data: data,
showScale: false,
options: {
legend: {
display: false
},
tooltips: {
enabled: false
},
hover: {
mode: null
},
scales: {
xAxes: [{
stacked: true,
display: false,
gridLines: {
drawBorder: false,
},
}],
yAxes: [{
stacked: true,
barPercentage: 0.9,
gridLines: {
drawBorder: false,
},
}]
}
}
});
}
I hope someone could help me with this problem as I simply can't figure out whats causing this.
You have only disabled borders on the edge of the chart (by setting drawBorder to false), not grid lines. The line that bugs you is a grid line. To disable grid lines for an axis use:
gridLines: {
display: false
}
Look at the docs under "Grid Line Configuration".

Categories

Resources