Chartjs mixed bar/line chart bars widths overlaps other columns - chart.js

I have date chart with a line and a bar datasets.
The problem that I have is that the bar widths is overlapping other columns as the bar dataset will be normally zero.
You can see that the two bars are overlapping with June 4 column.
If I add a bar value in two consecutives days, it will adjust correctly the bar width.
Now the bar widths adjusts for each day column.
If there are more days the bars widths get worst:
You can see that in this chart, it seems that the bars widths overlaps with 4 days in the x axys.
You can check an example code here: https://codepen.io/anon/pen/eKdjay?editors=1010
Uncomment the js code at line 38 to see how the bars width adjusts.
This is the code I use to initialize the chart.
new Chart($('#chart'), {
type: 'bar',
data: {
labels: [],
datasets: [
{
label: "Reach",
data: reachData,
borderColor: 'rgb(255, 99, 132)',
backgroundColor: 'rgb(255, 99, 132)',
borderWidth: 3,
fill: false,
lineTension: 0,
yAxisID: 'y-axis-right',
type: 'line'
}, {
label: "Interactions",
data: interactionsData,
borderColor: 'rgb(139, 195, 74)',
backgroundColor: 'rgb(139, 195, 74)',
borderWidth: 3,
fill: false,
xAxisId: 'x-axis',
lineTension: 0
}
]
},
options: {
bezierCurve : false,
tooltips: {
position: 'nearest',
mode: 'index',
intersect: false,
},
scales: {
xAxes: [{
type: 'time',
time: {
format: 'MM/DD/YYYY',
unit: 'day',
tooltipFormat: 'll'
},
scaleLabel: {
display: true,
labelString: 'Date'
}
}],
yAxes: [{
type: 'linear',
display: true,
position: 'left',
id: 'y-axis-left',
ticks: {
beginAtZero: true
},
scaleLabel: {
display: true,
labelString: "Reach",
fontColor: 'rgb(54, 162, 235)'
}
}, {
type: 'linear',
display: true,
position: 'right',
id: 'y-axis-right',
ticks: {
beginAtZero: true,
},
gridLines: {
drawOnChartArea: false,
},
scaleLabel: {
display: true,
labelString: "Interactions",
fontColor: 'rgb(255, 99, 132)'
}
}]
}
}
});

Related

Draw stacked horizontal bar chart with Average line using ChartJS

I am trying to create a stacked horizontal bar chart with an average line on each bar using the ChartJS library. I tried several times and searched on the Internet but I did not find out any satisfying answers. I appreciate your helping. Thanks.
Example:
.
In this example, the red line on each bar implies the average value of its group.
To create the stacked horizontal bar chart, please see
https://jsfiddle.net/lamtn862004/9wm1krqf/10/.
var data = {
labels: ["January", "February", "March"],
datasets: [
{
label: "Dogs",
backgroundColor: "rgba(237, 192, 169)",
data: [20, 10, 25],
stack: 1,
xAxisID: 'x-axis-0',
yAxisID: 'y-axis-0'
},
{
label: "Cats",
backgroundColor: "rgba(253, 234, 175)",
data: [70, 85, 65],
stack: 1,
xAxisID: 'x-axis-0',
yAxisID: 'y-axis-0'
},
{
label: "Birds",
backgroundColor: "rgba(179, 211, 200)",
data: [10, 5, 10],
stack: 1,
xAxisID: 'x-axis-0',
yAxisID: 'y-axis-0'
},
]
};
var ctx = document.getElementById("myChart").getContext("2d");
new Chart(ctx, {
type: 'groupableHBar',
data: data,
options: {
scales: {
yAxes: [{
stacked: true,
type: 'category',
id: 'y-axis-0'
}],
xAxes: [{
stacked: true,
type: 'linear',
ticks: {
beginAtZero:true
},
gridLines: {
display: false,
drawTicks: true,
},
id: 'x-axis-0'
},
{
stacked: true,
position: 'top',
type: 'linear',
ticks: {
beginAtZero:true
},
id: 'x-axis-1',
gridLines: {
display: true,
drawTicks: true,
},
display: false
}]
}
}
});
Now, I want to add the average line to each bar (as shown in the image).
Also, do you know the other solutions with open-source libraries for doing this?
The lines can be drawn using floating bars tied to the separate axis y2. To do so, you need to add another dataset as follows (I don't exactly understand what values you want to display, hence I randomly chose 40, 65 and 55):
{
label: "Lines",
backgroundColor: "rgb(255, 0, 0)",
data: [40, 65, 55].map(v => [v - 0.3, v + 0.3]),
yAxisID: 'y2',
order: -1
}
Further you must define a new scales option as shown below:
y2: {
display: false
}
Please take a look at your amended runnable code and see how it works.
var data = {
labels: ["January", "February", "March"],
datasets: [{
label: "Dogs",
backgroundColor: "rgba(237, 192, 169)",
data: [20, 10, 25]
},
{
label: "Cats",
backgroundColor: "rgba(253, 234, 175)",
data: [70, 85, 65]
},
{
label: "Birds",
backgroundColor: "rgba(179, 211, 200)",
data: [10, 5, 10]
},
{
label: "Lines",
backgroundColor: "rgb(255, 0, 0)",
data: [40, 65, 55].map(v => [v - 0.3, v + 0.3]),
yAxisID: 'y2',
order: -1
}
]
};
new Chart('myChart', {
type: 'bar',
data: data,
options: {
indexAxis: 'y',
plugins: {
legend: {
display: false
}
},
scales: {
y: {
stacked: true,
grid: {
display: false
}
},
y2: {
display: false
},
x: {
stacked: true,
beginAtZero: true,
grid: {
display: false
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.min.js"></script>
<canvas id="myChart" height="100"></canvas>

Chartjs: Why do I have 2 Y Axis labels?

My chart data is working, but I would like the Y axis to start below my lowest value and extend just a bit above the largest value and I was looking at the yAxes-> ticks-> min/max to do this. But when I add this to the options, a second second Y axis label is added with a scale of -1.0 to 1.0; I'm not sure how to get rid of that. I have attached some screen shots and some mocked up js code. Thanks
var chartData = {
labels: ['January', 'February', 'March'],
datasets: [{
type: 'line',
label: 'Dataset 1',
borderColor: 'blue',
borderWidth: 2,
fill: false,
data: [
85, 80, 75
]
}, {
type: 'bar',
label: 'Dataset 2',
backgroundColor: 'red',
data: [
90, 85, 80
],
borderColor: 'white',
borderWidth: 2
}, {
type: 'bar',
label: 'Dataset 3',
backgroundColor: 'green',
data: [
95, 90, 85
]
}]
};
var ctx = document.getElementById('canvas').getContext('2d');
window.myMixedChart = new Chart(ctx, {
type: 'bar',
data: chartData,
options: {
title: {
display: true,
text: 'A Funky Label'
},
tooltips: {
mode: 'index',
intersect: false
},
responsive: true,
scales: {
xAxes: [{
stacked: false
}],
yAxes: [
{
ticks: {
max: 100,
min: 50,
stepSize: 10,
callback: function (value, index, values) {
return value + " HP";
}
}
},
{
stacked: false
}
]
},
layout: {
padding: {
left: 10,
right: 10,
top: 0,
bottom: 0
}
}
}
});
}
I discovered the answer:
I was trying to determine if I wanted a stacked bar chart or regular bar chart with multiple series chart. Anyway, after deciding to stick with the regular bar chart with multiple series I removed this from my options section:
stacked: false
Once removed the second Y axis disappeared

How to center bars on x-axis of Chart.js bar graph?

I have a really simple Chart.js bar graph where I'm not labeling the x-axis. It looks something like this:
I want my bars to be centered along the x-axis instead of left-aligned as is shown in the pic above. Any ideas? Here's what my data looks like:
datasets: [{
label: 'Blueberries',
data: [this.get('fruit').bluebs[0]],
borderColor: 'blue',
fill: false,
lineTension: 0,
borderDash: [10,5]
}, {
label: 'Apples',
data: [this.get('fruit').apples[1]],
borderColor: 'red',
fill: false,
lineTension: 0.1
}]
And here is my config:
return {
responsive: true,
maintainAspectRatio: false,
scales: {
xAxes: [{
display: false,
ticks: {
scaleBeginAtZero: false
}
}],
yAxes: [{
display: true,
ticks: {
beginAtZero: true,
fontSize: 12,
fontFamily: 'Lato, sans-serif'
},
scaleLabel: {
display: true,
labelString: 'Fruits Counted',
fontStyle: 'bold',
fontFamily: 'Lato, sans-serif'
}
}]
},
legend: {
display: true,
position: 'bottom',
labels: {
fontColor: '#333',
fontFamily: 'Lato, sans-serif',
}
}
};
Also would love it if the person who downvoted me would tell me why so I can actually take action on it. :/
The default behavior of chart.js is to center charts, Looking at the picture I am doubting that you have supplied two values in the labels array of data (Not sure) if that's not the case please see this fiddle (http://jsfiddle.net/m5rq6bdj/2/) or the below code, which may help.
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
datasets: [{
label: 'Blueberries',
data: [2],
borderColor: 'blue',
fill: false,
lineTension: 0,
borderDash: [10,5]
}, {
label: 'Apples',
data: [1],
borderColor: 'red',
fill: false,
lineTension: 0.1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
xAxes: [{
display: false,
ticks: {
scaleBeginAtZero: false
}
}],
yAxes: [{
display: true,
ticks: {
beginAtZero: true,
fontSize: 12,
fontFamily: 'Lato, sans-serif'
},
scaleLabel: {
display: true,
labelString: 'Fruits Counted',
fontStyle: 'bold',
fontFamily: 'Lato, sans-serif'
}
}]
},
legend: {
display: true,
position: 'bottom',
labels: {
fontColor: '#333',
fontFamily: 'Lato, sans-serif',
}
}
}
});

How to define the starting position of every bar in chartjs bar

I am trying to build a graph similar to the one in the image
but I can't find the right way to do it, I want to join the line with the bar on the same axis, and for every bar I want to define the starting Y position and the ending Y position, I tried to do it using multi axis but it didn't work, what I tried is something similar to this
const data = {
labels: ['January', 'February', 'March', 'April', 'May', 'June', July'],
datasets: [{
label: 'Sales',
type:'line',
data: [51, 65, 40, 49, 60, 37, 40],
fill: false,
borderColor: '#EC932F',
backgroundColor: '#EC932F',
pointBorderColor: '#EC932F',
pointBackgroundColor: '#EC932F',
pointHoverBackgroundColor: '#EC932F',
pointHoverBorderColor: '#EC932F',
yAxisID: 'y-axis-2'
},{
type: 'bar',
label: 'Visitor',
data: [200, 185, 590, 621, 250, 400, 95],
fill: false,
backgroundColor: '#71B37C',
borderColor: '#71B37C',
hoverBackgroundColor: '#71B37C',
hoverBorderColor: '#71B37C',
yAxisID: 'y-axis-1'
}]
};
const options = {
responsive: true,
tooltips: {
mode: 'label'
},
elements: {
line: {
fill: false
}
},
scales: {
xAxes: [
{
display: true,
gridLines: {
display: false
},
labels: {
show: true
}
}
],
yAxes: [
{
type: 'linear',
display: true,
position: 'left',
id: 'y-axis-1',
gridLines: {
display: false
},
labels: {
show: true
}
},
{
type: 'linear',
display: true,
position: 'right',
id: 'y-axis-2',
gridLines: {
display: false
},
labels: {
show: true
}
}
]
}
};
but this way, there will be two axes, one on the left and the other on the right side, but both start from the same position

Draw two plots using chartjs over one another with transparency

In Chartjs I have two plots, as shown here:
var config = {
type: 'line',
data: {
labels: [
"2017-07-03T01:05:00+0100",
....
],
datasets: [
{
label: "Consumption",
fill: 'origin',
pointRadius: 0,
borderColor: "#0000ff",
backgroundColor: "rgba(255,10,13,255)",
data: [
0.015625,
0.0199861111111,
...
],
}
,
{
fill: 'origin',
label: "PV",
pointRadius: 0,
borderColor: "#ebf909",
backgroundColor: "rgba(29,241,13,210)",
data: [
0.0,
.....
],
}
]
},
options: {
responsive: true,
title:{
display:true,
text:"Chart.js Line Chart - Stacked Area"
},
tooltips: {
mode: 'index',
},
hover: {
mode: 'index'
},
scales: {
xAxes: [{
scaleLabel: {
display: true,
labelString: 'Time'
}
}],
yAxes: [{
stacked: false,
scaleLabel: {
display: true,
labelString: 'kWh'
}
}]
}
}
};
var ctx = document.getElementById("canvas").getContext("2d");
var myChart = new Chart(ctx, config);
Is there any way I can make the green plot show through the red one in places where the latter completely obscures the former?
You need to set fill property to false for the first dataset (the red one), to make it transparent.
datasets: [{
label: "Consumption",
fill: false,
pointRadius: 0,
borderColor: "#0000ff",
backgroundColor: "rgba(255,10,13,255)",
...
or, you can also reduce the opacity of background color, like so ...
backgroundColor: "rgba(255, 10, 13, 0.1)"
Here is the working codepen