ChartJS 2.9.4 can't overlay line data on Horizontal Bar chart - chart.js

We have an issue where we can not overlay multiple graph types on top of each other with ChartJS 2.9.4 . We have narrowed this down specifically to HorizontalBar chart types.
I can't see what's wrong with the script code below, but the ChartJS persists in not showing anything when any line type is added. The current dual horizontalBar work perfectly.
Also frustratingly ChartJS seems to show by default to show no errors or notice information at all in any way to the console.
Solution attempts :
Lots of rearranging of layout, including ordering of data array.
Confirming all options and settings.
Putting "type" data in different areas (Chart/datasets/data)
Reading lots of Stack Overflow on vaguely related issues
Finding that the system does work with 'bar' rather than 'horizontalBar'
This question
Changing "drawing order"
Making it default to "line" and then setting HorizontalBar as the exception value (set within the data array)
Aim:
Code:
<script>
Chart.platform.disableCSSInjection = true;
var ctx = document.getElementById("historicChart");
var historicChart = new Chart(ctx, {
type: "horizontalBar",
data: {
labels: ["Mar 2017","Mar 2016","Mar 2015","Mar 2014","Mar 2013","Mar 2012","Mar 2011"],
datasets: [
{
type: "line",
label: "Visits",
data: ["3", "4", "1", "5", "6","0"],
pointBackgroundColor: '#FFC900',
pointRadius: 8,
pointStyle: 'circle',
showLine: true
},
{
label: "Applicants",
data: ["2","4","5","8","9","4","3"],
backgroundColor: "#436B94",
borderWidth: 0
},
{
label: "Intakes",
data: ["1","1","0","1","3","0","1"],
backgroundColor: "#C40500",
borderWidth: 0
}
]
},
options: {
scales: {
xAxes: [{
stacked: false,
ticks: {
stepSize: 1
}
}],
yAxes: [{
stacked: false
}]
}
}
}
);
</script>
<div class='graphbox'>
<h3>Applicants & Intakes over time</h3>
<canvas id="historicChart"></canvas>
</div>
How can I achieve this?

They fixed this issue in v3 so upgrading to that is 1 solution, the other one is downgrading to version 2.8, in a git issue on their repo someone posted a workaround but that only works till version 2.8.
V3:
HorizontalBar has been removed as a type, use bar chart instead and set the index axis to y in your options
Example:
var options = {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
type: 'line',
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
indexAxis: 'y'
}
}
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.2.0/chart.js"></script>
</body>
V2 solutions, for if stuck on v2 with plugin support
To achieve this in 2.8 you will have to specify your data as objects and specify the x and y coordinates
Example:
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
datasets: [{
label: "Clients Signed",
data: [2, 0, 3, 5, 1, 3, 6, 5, 3, 10]
}, {
label: "Quota",
data: [{
x: 2,
y: 'Q2 2015'
}, {
x: 2,
y: 'Q3 2015'
}, {
x: 2,
y: 'Q4 2015'
}, {
x: 2,
y: 'Q1 2016'
}, {
x: 2,
y: 'Q2 2016'
}, {
x: 2,
y: 'Q3 2016'
}, {
x: 2,
y: 'Q4 2016'
}, {
x: 2,
y: 'Q1 2017'
}, {
x: 2,
y: 'Q2 2017'
}, {
x: 2,
y: 'Q3 2017'
}],
type: 'line'
}],
labels: ["Q2 2015", "Q3 2015", "Q4 2015", "Q1 2016", "Q2 2016", "Q3 2016", "Q4 2016", "Q1 2017", "Q2 2017", "Q3 2017"]
},
options: {
barPercentage: 1.0,
categoryPercentage: 1.0
}
});
<body>
<div class="myChartDiv">
<canvas id="myChart" width="600" height="400"></canvas>
</div>
<script src="https://npmcdn.com/chart.js#2.8.0/dist/Chart.bundle.min.js"></script>
</body>
Git issue: https://github.com/chartjs/Chart.js/issues/4096

Related

How to create a line chart with two line, with one of them being filled, and the other staying on the foreground of the filled line?

I have the following chart:
How can I make sure that the the blue line isn't filled, but at the same time, stays on top of the filled red line?
Taking the line chart example from the documentation, I have tried playing around with the drawTime property, but as you can see, the blue line is on the foreground of the red one, but it is filled (I expect the blue line to be on the foreground, but not filled).
Here's what I have so far:
Data:
const data = {
labels: generateLabels(),
datasets: [
{
label: 'Dataset 1',
data: generateData(),
borderColor: Utils.CHART_COLORS.red,
backgroundColor: Utils.CHART_COLORS.red,
fill: true
},
{
label: 'Dataset 2',
data: generateData(),
borderColor: Utils.CHART_COLORS.blue,
backgroundColor: Utils.transparentize(Utils.CHART_COLORS.blue),
// changed `fill` propery from `true` to `false`
fill: false
}
]
};
Config:
const config = {
type: 'line',
data: data,
options: {
plugins: {
filler: {
propagate: false,
// added `drawTime` property here
drawTime: "beforeDraw",
},
title: {
display: true,
text: (ctx) => 'drawTime: ' + ctx.chart.options.plugins.filler.drawTime
}
},
pointBackgroundColor: '#fff',
radius: 10,
interaction: {
intersect: false,
}
},
};
Setup:
const inputs = {
min: -100,
max: 100,
count: 8,
decimals: 2,
continuity: 1
};
const generateLabels = () => {
return Utils.months({count: inputs.count});
};
Utils.srand(3);
const generateData = () => (Utils.numbers(inputs));
This seems like a bug in chart.js itself.
You have 3 options:
wait until it is fixed within chart.js itself
write your own custom plugin that does the filling
make use of the order property so the blue line gets drawn on the canvas after the red one like so:
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: 'red',
borderColor: 'red',
fill: true,
order: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderColor: 'blue',
order: 0
}
]
},
options: {}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.js"></script>
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>
EDIT:
After looking at the source code it seems indeed like its a bug in chart.js, it does not check if fill has been set to false so it will always fill with the non default modes. Will put in a fix for this so it will be fixed in version 3.8.1

Can I make Y axis begin at specific value in ChartJS?

I have this chart, starting at 0 and I want the Y axis to begin at 20. It surprises me how there is nothing related to this in the official ChartJS docs. Is this possible in any way?
I have tried several ways to do it, eg. with ticks or min value and so on but nothing worked.
The application is written in Nuxt3 and the ChartJS library used is vue-chart-3.
const options = computed(() => ({
responsive: true,
plugins: {
legend: {
display: false,
},
title: {
display: false,
}
},
scales: {
x: {
stacked: true,
},
y: {
stacked: true,
}
}
}));
const chartData = computed(() => ({
labels: ['Provider'],
datasets: [
{
data: [selectedDataset.value?.score.uncustomized],
backgroundColor: ['#00b7c4'],
},
{
data: [selectedDataset.value?.score.difference],
backgroundColor: ['#204992'],
},
],
}));
const { barChartProps, barChartRef } = useBarChart({
chartData,
options,
});
Any help would be appreciated.
If you want the axis to start at a given number you need to use the min property:
const 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'
}
]
},
options: {
scales: {
y: {
min: 6
}
}
}
}
const 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.1/chart.js"></script>
</body>

chartjs-plugin-annotation box in Django app

I have chartjs v2.9.3 in my Django app and it works great. Now I need to add a box to one of my charts using chartjs-plugin-annotation, and I've followed all the steps, but I can't get it to work.
I've used chartjs-plugin-annotation v0.5.7 which is for v2.9.3. I get no errors in the console log.
First, scripts with chart.js and plugins:
<script src="https://cdn.jsdelivr.net/npm/chart.js#2.9.3/dist/Chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels#0.7.0"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-annotation#0.5.7/chartjs-plugin-annotation.min.js"></script>
My code for chart (chart works, I just can't get box to appear):
<canvas id="myChart" height="250"></canvas>
<script>
function setChart12(){
$('#myChart').remove();
$('#container_glavni').append('<canvas id="myChart" height="200"></canvas>');
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: date,
datasets: [{
label: 'Number of reports',
data: data_nc
}, {
type: 'line',
label: 'Median',
data: [50],
fill: false,
borderColor: 'rgb(54, 162, 235)',
}],
},
options: {
layout: {
padding: {
top: 30
}
},
responsive: true,
legend: {
display: false
},
plugins: {
datalabels: {
anchor: 'end',
align: 'end',
offset: 5,
},
autocolors: false,
annotation: {
annotations: {
box1: {
type: 'box',
xMin: 0,
xMax: 1,
yMin: 0,
yMax: 30,
backgroundColor: 'rgba(255, 99, 132, 0.25)'}
}
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});}
</script>
I've tried:
Different values for min and max
Different colors
Different code formating
This is because you putted the options in the place where they belong in V1 of the annotation plugin and the syntax too. For V0.5.7 the annotation options have to be placed in the root of the options and also all the annotations are an array instead of seperate objects:
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
annotation: {
annotations: [{
id: "hline",
type: "line",
mode: "horizontal",
scaleID: "y-axis-0",
value: 10,
borderColor: "red",
}]
},
}
}
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/2.9.4/Chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-annotation#0.5.7/chartjs-plugin-annotation.min.js"></script>
</body>

Is there anyway to hide the gridlines between ticks and chartArea in ChartJS?

Is there anyway to remove this overflowing section of the gridlines?
This is exactly what I'm looking for:
I've seen a few post from years ago with this question and none of them have really yielded any results. Those were so long ago that I pray that a fix has happened since then.
Cheers!
You can set tickLength to 0 in the grid options. To make the ticks still appear a bit away from the scale you can use the padding option:
const 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'
}
]
},
options: {
scales: {
y: {
ticks: {
padding: 4,
crossAlign: 'far'
},
grid: {
tickLength: 0
}
},
x: {
grid: {
tickLength: 0
}
}
}
}
}
const 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.1/chart.js"></script>
</body>

How to change font size, padding of chart title only, in chart.js 3.x

I am trying to reduce padding and increase title text font size , I went though few example but dont seem to work. Even on placing the parameter at multiple places.
const labels1_1 = ['1','2','3', '4', '5','6','7', '8','9+'];
const data1_1 = {
labels: labels1_1,
datasets: [{
label: 'Trucks',
data: [1,2,3, 4, 5,6,7, 8,9],
backgroundColor: '#4472C4',
borderColor: '#4472C4',
borderWidth: 1
}]
};
const config1_1 = {
type: 'bar',
data: data1_1,
options: {
title:{
padding:5,
fontSize:18
},
plugins: {
title: {
display: true,
text: 'Number of SS vs Number of Trucks',
padding:5,
fontSize:18
},
datalabels: {
align: 'end',
anchor: 'end',
backgroundColor: '#3472C4'
},
},
scales: {
y: {
display:true,
beginAtZero: true,
title: {
display: true,
text: 'SS'
}
},
}
},
};
const myChart1_1 = new Chart(
document.getElementById('myChart1_1'), config1_1);
I went through https://www.chartjs.org/docs/2.8.0/configuration/title.html but didnt help. Also how to mention it globally so all titles have bigger font. text: 'Number of SS vs Number of Trucks' is the title.
The Title options have evolved quite a bit in v2 of Chartjs. I found that the title was hidden entirely until I added padding - for one example
options: {
plugins: {
title: {
display: true,
text: "Title" ,
padding: {
top: 10,
bottom: 10
},
font: {
size: 24,
style: 'italic',
family: 'Helvetica Neue'
}
}
},
... other options here
}
The reason it didn't help is because you are looking at the docs for version 2.8.0 as you can see in the url, the latest version is 3.6.2 and has some breaking changes against v2.
To adjust the font propertys you will need to define them in the font object in the title config like so:
const 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'
}
]
},
options: {
plugins: {
title: {
display: true,
text: 'Number of SS vs Number of Trucks',
font: {
size: 30
}
}
}
}
}
const 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.6.2/chart.js"></script>
</body>
latest V3 documentation: https://www.chartjs.org/docs/latest/