How to show/hide data labels of a certain y-axis for a line graph (using multiple y-axis, one on the left, another on the right) - charts

I am using chartjs 2.9.4. I am trying to add the second y-axis on the right. As you can see below, I added the second y-axis 'B' that is positioned on the right.
var myDataSets = [
{% for key, value in mean_matrix_dic.items %}
{
yAxisID: 'A',
label: [{% for key2, val in value.items %}{% if forloop.first %}'{{ key }}',{% endif %}{% endfor %}],
backgroundColor: fixedColors({{ forloop.counter0 }}),
borderColor: fixedColors({{ forloop.counter0 }}),
borderWidth: 3.5,
data: [{% for key2, val in value.items %}'{{ val }}',{% endfor %}],
fill: false,
lineTension: 0,
tension: 0.0
},
{
yAxisID: 'B',
label: [{% for key2, val in value.items %}{% if forloop.first %}'{{ key }}',{% endif %}{% endfor %}],
backgroundColor: fixedColors({{ forloop.counter0 }}),
borderColor: fixedColors({{ forloop.counter0 }}),
borderWidth: 3.5,
data: [{% for key2, val in value.items %}'{{ val }}',{% endfor %}],
fill: false,
lineTension: 0,
tension: 0.0,
},
{% endfor %}
];
var estimatedMeansLineChart = new Chart(chx, {
type: 'line',
data: {
labels: [{% for var_2 in vars_2 %}'{{ var_2 }}',{% endfor %}],
datasets:myDataSets,//datasets
},//data
options: {
responsive: true,
title: {
display: true,
text: 'Estimated Marginal Means of {{ dep_var }}'
},
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
x: {
display: true,
scaleLabel: {
display: true,
labelString: '{{ ind2 }}'
}
},
yAxes: [{
id: 'A',
position: 'left',
display: true,
scaleLabel: {
display: true,
labelString: 'Estimated Marginal Means'
}
},
{
id: 'B',
position: 'right',
display: true,
scaleLabel: {
display: false,
labelString: 'Estimated Marginal Means'
}
}]
}
}//options
});
I get the following output when I run it:
My question is that how can I get rid of those additional data labels and have only one? E.g., one of the red box, one of the blue box and one of the black box. Since I added an additional y-axis on the right, it automatically added those data labels too.
To hide the data labels, I know that I should use the legend property
options: {
legend: {
display: false
},
......
......
......
}
However, when I use it, all data labels disappears. I only want to show the data labels of the left y-axis 'A' and hide the data labels of the second one (the right y-axis 'B').
Any suggestions?

Yeah, I think I found the solution here.
Basically, I needed add the filter property:
....
....
options: {
legend: {
labels: {
filter: function(item, chart) {
return !item.text.includes('_temp_xyz_abc');
}
},
},
....
....
....
The '_temp_xyz_abc' can be seen here:
var myDataSets = [
{% for key, value in mean_matrix_dic.items %}
{
yAxisID: 'A',
label: [{% for key2, val in value.items %}{% if forloop.first %}'{{ key }}',{% endif %}{% endfor %}],
backgroundColor: fixedColors({{ forloop.counter0 }}),
borderColor: fixedColors({{ forloop.counter0 }}),
borderWidth: 3.5,
data: [{% for key2, val in value.items %}'{{ val }}',{% endfor %}],
fill: false,
lineTension: 0,
tension: 0.0,
},
{
yAxisID: 'B',
label: [{% for key2, val in value.items %}{% if forloop.first %}'_temp_xyz_abc',{% endif %}{% endfor %}],
backgroundColor: fixedColors({{ forloop.counter0 }}),
borderColor: fixedColors({{ forloop.counter0 }}),
borderWidth: 3.5,
data: [{% for key2, val in value.items %}'{{ val }}',{% endfor %}],
fill: false,
lineTension: 0,
tension: 0.0,
},
{% endfor %}
];
And it worked as you can see below.

Related

Chart Js Annotation plugin doesn't show but no error

tried to copy other solutions but doesn't work. Chart shows but without annotation lines. Showing my code below. Really appreciate your attention
Integrated on base.html
<script src="path/to/chartjs/dist/chart.min.js"></script>
<script src="path/to/chartjs-plugin-annotation/dist/chartjs-plugin-annotation.min.js"></script>
On another page after extending base.html:
<script>
// jquery function
$(document).ready(function(){
const ctx = document.getElementById('myChart');
const labels = [{% for item in query_set%}'{{item.analysis_date}}',{% endfor %}];
const data = {
labels: labels,
datasets: [
//Если добавить еще один - будет сдвоенный график
{
label: '{{analysis_info.a_name}} - {{analysis_info.a_measur}}',
data: [{% for item in query_set%}'{{item.analysis_data}}',{% endfor %}],
borderColor: 'rgb(75, 192, 192)',
borderWidth: 4,
},
]
};
const options = {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
min: 0
}
}]
},
animation: {
duration: 1500,
},
plugins: {
autocolors: false,
annotation: {
annotations: {
box1: {
// Indicates the type of annotation
type: 'line',
yMin: 10,
yMax: 10,
backgroundColor: 'rgba(255, 99, 132, 0.25)'
}
}
}
}
}
const config = {
type: 'line',
data,
options,
};
const chart = new Chart(ctx, config);
});
</script>
{% endblock scripts %}
But no reaction
I'm sure that solution is easy but i got stucked
As you repeorted in the comment, "imported via source libs/Chart.js/2.8.0/Chart.min.js libs/chartjs-plugin-annotation/2.1.0", you are using 2 not compatible versions.
See Redame of plugin repo: https://github.com/chartjs/chartjs-plugin-annotation#readme
An annotation plugin for Chart.js >= 3.7.0
For Chart.js 3.0.0 to 3.6.2 support, use version 1.4.0 of this plugin For Chart.js 2.4.0 to 2.9.x support, use version 0.5.7 of this plugin

How can I remove the slopes from the curve

So basically the point placement in the chart is correct, the only issue is the shape of the curve since it should follow only the integers values.
What it looks like:
What it should look like
)
Chart code
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'line',
data: {
labels: [{% for value in x %} '{{value}}', {% endfor %}],
datasets: [{
label: '# Des employes',
data: [{% for c in y %} '{{c}}', {% endfor %} ],
fill: true,
backgroundColor: [
'rgba(54, 162, 235, 0.5)',
],
borderColor: [
'rgba(54, 162, 235, 1)',
],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true,
ticks:{stepSize:1}
}
}
}
});
Set stepped to after
(or before, middle, true):
...
datasets: [{
stepped: 'after',
👆
...

Handle multiple datasets with one tooltip in Chart JS

I need the Holiday tooltip to be grouped, that is to say, pressing a tooltip stops seeing all the Holidays. In this way, not having the "Holiday" tooltip repeated twice and showing / hiding with a single tooltip
Is it possible with chart js?
Actual:
Expected:
a single tooltip handling the "Holiday" datasets
Actual code:
var options = {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple"],
datasets: [
{
label: 'HOLIDAY',
data: [10, 10,10,10,10],
borderWidth: 1,
backgroundColor: "red",
stack: 1
},
{
label: 'HOLIDAY',
data: [10, 10,10,10,10],
borderWidth: 1,
backgroundColor: "red",
stack: 2
},
{
label: 'IN OFFICE',
data: [7, 11, 5, 8, 3],
borderWidth: 1,
backgroundColor: "blue",
stack:1
},
{
label: 'HOME OFFICE',
data: [7, 11, 5, 8, 3],
borderWidth: 1,
backgroundColor: "yellow",
stack:2
}
]
},
options: {
indexAxis: 'y',
// Elements options apply to all of the options unless overridden in a dataset
// In this case, we are setting the border of each horizontal bar to be 2px wide
elements: {
bar: {
borderWidth: 2,
}
},
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="400" height="100"></canvas>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</body>

How to add space Between Columns in Bar chartjs and remove the space in the end

Space between column in chart js
There's the screen of my output and what i want to do
enter image description here
Anyone can help please ?
You can achieve this by setting the barPercentage property
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,
backgroundColor: 'blue',
barPercentage: 0.3
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1,
backgroundColor: 'red',
barPercentage: 0.3
}
]
},
options: {
scales: {
y: {
stacked: true
},
x: {
stacked: true
}
}
}
}
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.3.2/chart.js"></script>
</body>
Now i have something like this but i need to add space between each column

ChartJS - ignore labels

I am using ChartJS and need a chart like this: https://tppr.me/OE08p
Meaning it should ignore 4 of the labels (flexibility, external, stability, internal) and connect the dots from the other 4 labels (like the red lines show on the screenshot).
Can I ignore these 4 labels data-wise somehow, but keep them?
Other chart packages/solutions are welcome, if it is not possible in chartjs.
You can use highchart.js library, see:
docs: https://www.highcharts.com/docs/chart-and-series-types/polar-chart
example: https://www.highcharts.com/demo/polar-spider
with these options:
plotOptions: {
series: {
connectNulls: true
}
}
and filtering data with map function like below (just for example):
data.map(filter)
<omissis>
function filter(item, index) {
if (index==2)
return null;
else
return item;
}
here is a jsfiddle showing this approach: http://jsfiddle.net/beaver71/w6ozog1c/
or a snippet here:
// original data
var data1 = [43000, 19000, 60000, 35000, 17000, 10000],
data2 = [50000, 39000, 42000, 31000, 26000, 14000];
var chart = Highcharts.chart('container', {
chart: {
polar: true,
type: 'line'
},
title: {
text: 'Budget vs spending',
x: -80
},
pane: {
size: '80%'
},
xAxis: {
categories: ['Sales', 'Marketing', 'Development', 'Customer Support',
'Information Technology', 'Administration'],
tickmarkPlacement: 'on',
lineWidth: 0
},
yAxis: {
gridLineInterpolation: 'polygon',
lineWidth: 0,
min: 0
},
tooltip: {
shared: true,
pointFormat: '<span style="color:{series.color}">{series.name}: <b>${point.y:,.0f}</b><br/>'
},
legend: {
align: 'right',
verticalAlign: 'top',
y: 70,
layout: 'vertical'
},
series: [{
name: 'Allocated Budget',
data: data1.map(filter), // filtered data
pointPlacement: 'on',
color: 'red'
}, {
name: 'Actual Spending',
data: data2,
pointPlacement: 'on',
color: 'green'
}],
plotOptions: {
series: {
lineWidth: 2,
connectNulls: true // connects also null value (bypassing)
}
}
});
var filterOn = true;
$('#button').click(function () {
filterOn = !filterOn;
if (filterOn)
chart.series[0].setData(data1.map(filter));
else
chart.series[0].setData(data1);
});
// filter function with your criteria
function filter(item, index) {
if (index==2)
return null;
else
return item;
}
.highcharts-grid-line {
stroke-width: 2;
}
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<button id="button">Toggle filter (ignoring a point in red serie)</button>
<div id="container" style="min-width: 400px; max-width: 600px; height: 400px; margin: 0 auto"></div>