Bars on second X Axis not showing - chart.js

In the following JsFiddle the second dataset is not showing up when setting xAxisID to "2". It works when set to "1" or commented out:
https://jsfiddle.net/8v0xwj9L/3/
labels: ["Label1", "Label2", "Label3", "Label4", "Label5"],
datasets: [{
"label": "Total V",
"backgroundColor": "rgba(53,81,103,1)",
"borderColor": "rgba(53,81,103,.4)",
"data": [10, 4, 3, 7, 6]
},
{
"label": "Total C",
"xAxisID": "2",
"backgroundColor": "rgba(255,153,0,1)",
"borderColor": "rgba(255,153,0,.4)",
"data": [9, 9, 8, 7, 6]
}
]
};
var options = {
scales: {
xAxes: [{
id: "1",
position: 'bottom'
},
{
offset: true,
id: '2',
position: 'bottom'
}
],
}
};
var ctx = document.getElementById("myChart");
new Chart(ctx, {
type: "bar",
data: data,
options: options
});
What could be the reason?
My end goal is to have a bar chart with 2 datasets that have different ranges on the X axis but still overlap in the graph.
Imagine comparing datapoints from Week 1 with Week 2 by showing a bar for next to each other for each day in the week but their X timestamps obviously don't match hence two X axis.

When you say timestamps, are we talking about days or times? If it's just days, you can use one x-axis like here: https://jsfiddle.net/jbmn01z6/
If you want time, then a bar chart is not a good idea. I don't even know if it's possible the way you would like it. I would use a line chart with a x-axis of the type: 'time'. Then you can use and show your timestamps very precisely.

Related

Chart.js: hide dataset line but keep ticks on y-axis?

I have the following Line chart with two datasets that share the same x-axis labels. I'd like to know if is it possible to hide one of them but keep the labels (e.g. from 100 to 500) on the right y-axis.
My intent is two show just one line that references to two different values (the left one from -800 to 800 and the right one from 100 to 500) for instance.
UPDATE:
I don't know if this is the right way of doing it. In an ideal world I have to show on the same label two different values on y-axis. So maybe there's a better way to show this using just one dataset with two datapoints anchored to the different y-axis.
I'm currently exploiting https://github.com/reactchartjs/react-chartjs-2
You can add a second axis, specify a min and max so that even when you hide the dataset the ticks stay:
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'pink'
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderColor: 'orange',
yAxisID: 'secondY'
}
]
},
options: {
scales: {
y: {
display: true,
min: 2,
max: 20
},
secondY: {
position: 'right',
display: true,
min: 3,
max: 11
}
}
}
}
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.7.0/chart.js"></script>
</body>

Changing borderDash for specific gridLines in radar chart

I am using ChartJS to generate a radar chart.
I wish for the last gridLine (the outermost) to remain solid, while all the others are dashed.
Picture example:
I've seen that it is possible to have varying borderDash in this pull request which also shows an image of it in action but have yet to find a way to script it. (edit: it seems that the linked pull request has made it onto master, planned for release with V3.0 - meaning it is not yet available(?))
Quick example code of a script:
scale: {
gridLines: {
borderDash: function(context) {
return context.index % 2 === 0 ? [2, 3] : [10, 10];
},
},
You can already use Chart.js version 3, it's available as Chart.js 3.0.0-beta.
Simply add the following script tag to your HTML page.
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.0-beta/chart.min.js"></script>
Please take a look at the following code snippet and see how it works.
new Chart("radar-chart", {
type: 'radar',
data: {
labels: ["A", "B", "C", "D", "E"],
datasets: [{
data: [25.48, 54.16, 7.61, 8.06, 4.45],
backgroundColor: "rgba(255,99,132,0.2)",
borderColor: "rgba(255,99,132,1)"
}]
},
options: {
legend: {
display: false
},
scale: {
ticks: {
min: 0,
max: 60,
stepSize: 10
},
gridLines: {
lineWidth: 2,
color: "lightgreen",
borderDash: context => context.index == 6 ? [] : [6, 4]
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.0-beta/chart.min.js"></script>
<canvas id="radar-chart" height="120"></canvas>

Chartjs bar chart trying to get labels from datasets

I am trying to implement chartJS bar chart and use the legend to filter bars.
I want to set the labels list to be empty because this allows me to remove bars clearly.
I am looking for a way to set the ticks with labels on X axis, since it is empty from text now.
JSfiddle:
https://jsfiddle.net/m1eorjwv/1/
var options = {
type: 'bar',
data: {
labels: [],
datasets: [
{
label: 'red',
data: [12],
borderWidth: 1
},
{
label: 'blue',
data: [7],
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
reverse: false,
beginAtZero: true
}
}]
}
}
}
Thanks a lot, Alon
I managed to do this, here is the ansert:
enter code here
code
https://jsfiddle.net/ehacujny

chart.js horzontalBar Stacked and Grouped - data not showing - a bug?

can anyone help me understand why the first chart in this fiddle isn't showing correctly?. It should have two grouped horizontal stacks, but the two datasets assigned to the secondary X axis (x-axis-1) of the datasets are not showing.
https://jsfiddle.net/c2jtL7gz/5/
var myChart = new Chart(ctx, {
type: "horizontalBar",
data: {
"datasets":[{
"label":"One",
"data":[100],
"stack":"Stack 0",
"backgroundColor":"green"
},{
"label":"Two",
"data":[200],
"stack":"Stack 0",
"backgroundColor":"orange"
},{
"label":"Three",
"data":[150],
"stack":"Stack 1",
"xAxisID":"x-axis-1",
"backgroundColor":"lightgrey"
},{
"label":"Four",
"data":[300],
"stack":"Stack 1",
"xAxisID":"x-axis-1",
"backgroundColor":"darkgrey"}]},
options: {
scales: {
xAxes: [{
stacked: true,
id: "x-axis-0",
},{
stacked: true,
id: "x-axis-1",
}],
yAxes: [{
stacked: true,
id: "y-axis-0",
}],
}
}
});
The second chart has the same configuration, but with minimal changes to make it a normal (vertical) bar chart, and it works exactly as expected.
Is this a bug in the library, or something I'm missing?
Welcome to StackOverflow! According to the docs it looks like the data structure for the horizontal bar chart is slightly different. You want data to look like:
data: {
"labels": ["One", "Two", "Three", "Four"],
"datasets":[{
"label": "First Dataset",
"data":[100,200,150,300],
"backgroundColor":["green","orange","lightgrey","darkgrey"]
}]
},
I've attached a fiddle here: https://jsfiddle.net/c2jtL7gz/6/

ChartJS: two axes in horizontalBar chart

Using a standard bar chart I am able to successfully create a chart with two datasets plotted on two y-axis. Working Example here: https://jsfiddle.net/pe8kxjqc/12/ How can I create the same chart as a horizontalBar chart with the labels on the left and the two dataset axes on the top and bottom?
The horizontalBar chart doesn't appear to handle the second axis as expected. I've tried the following but it doesn't work like expected:
var data = {
labels: ["Label1", "Label2", "Label3", "Label4", "Label5"],
datasets: [
{
"label": "Total V",
"xAxisID": "v",
"backgroundColor": "rgba(53,81,103,1)",
"borderColor": "rgba(53,81,103,.4)",
"data": [100,90,80,70,60]
},
{
"label": "Total C",
"xAxisID": "c",
"backgroundColor": "rgba(255,153,0,1)",
"borderColor": "rgba(255,153,0,.4)",
"data": [10,9,8,7,6]
}]};
var options = {scales:{
xAxes:[
{position:"top",id:"v", gridLines:{color:"rgba(53,81,103,.4)"}},
{position:"bottom",id:"c", gridLines:{color:"rgba(255,153,0,.4)"}}
]}};
var ctx = document.getElementById("myChart");
new Chart(ctx, {type: "horizontalBar",data:data, options:options});
You can have dynamic configuration for bar as well as horizontalBar you can use this configurations
Fiddle demo
var data = {
labels: ["Label1", "Label2", "Label3", "Label4", "Label5"],
datasets: [{
"label": "Total V",
"yAxisID": "A",
"backgroundColor": "rgba(53,81,103,1)",
"borderColor": "rgba(53,81,103,.4)",
"data": [100, 90, 80, 70, 60]
}, {
"label": "Total C",
"yAxisID": "A",
"backgroundColor": "rgba(255,153,0,1)",
"borderColor": "rgba(255,153,0,.4)",
"data": [10, 9, 8, 7, 6]
}]
};
var options = {
scales: {
yAxes: [{
id: 'A',
position: 'left'
}]
}
};
var ctx = document.getElementById("myChart");
new Chart(ctx, {
type: "horizontalBar",
data: data,
options: options
});
coming to your question the actual problem is with var options={...} it is made to work only with bar charts. Hope you understand
Update
Replace following as option in the chart above for required axis scales
var options = {scales:{yAxes:[{id: 'A',position: 'left',gridLines:{color:"rgba(53,81,103,.4)"}},{position:"top",id:"c", gridLines:{color:"rgba(255,153,0,.4)"},ticks: { min: 0,max: 100,}},]}};
updated fiddle
Basically you will need a y-axis for the first dataset which going to hold the labels. And you will need two x-axis for showing the values in the top and in the bottom.
The second dataset will go to the x-axis which is shown at the top.
Working fiddle included:
{
scales: {
yAxes: [{
id: 'A',
position: 'left',
display: true,
gridLines: {
color: "rgba(53,81,103,.4)"
}
}, ],
xAxes: [{
barPercentage: 0.4,
display: true,
id: "1",
position: 'bottom',
ticks: {
fontColor: 'rgb(0, 0, 0)'
},
fontSize: 500,
}, {
stacked: false,
barPercentage: 1,
display: true,
type: 'linear',
id: '2',
position: 'top',
ticks: {
fontColor: 'rgb(0, 0, 0)'
},
fontSize: 500,
}],
}
};
fiddle for two axis horizontal bar chart