Labels shrink my PieChart size in react-chartjs - reactjs

I'm using react-chartjs-2 (version 4.3.1 ). As you can see in image, even if I use same component, width & height aren't same anymore because of labels. I want to display like left side piechart even though there're many labels in right side. How can I solve that?
codes
<Pie
height={400}
data={{
labels: labels,
datasets: [
{
label: "Label 1",
data: data,
backgroundColor: [
palette.primary.main,
palette.utility.maroonRed,
palette.text.primary,
palette.primary.grey,
palette.secondary.dark,
palette.primary.subtle
],
},
],
}}
options={{
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
display: true,
position: "bottom"
},
}
}}
/>
)

Since the chart is limited to the size of the canvas you have 3 options:
Limit the amount of legend items shown with the filter callback
Make the canvas bigger so the chart has more space to render everything depending on the labels
Disable the legend so it will always render the same size

If acceptable for this use case, you could have only a subset of datasets in the legend (for instance top5) and all others in another "others" legend item.
const pieGenerateLabelsLegendHandler = Chart.controllers.doughnut.overrides.plugins.legend.labels.generateLabels;
const pieLegendClickHandler = Chart.controllers.doughnut.overrides.plugins.legend.onClick;
let others = [];
const chart = new Chart("myChart", {
type: 'pie',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
datasets: [{
data: [65, 1, 80, 80, 10, 55, 15, 20, 40, 50, 8, 40],
backgroundColor: ['#3366cc','#dc3912','#ff9900','#109618','#990099','#0099c6','#dd4477','#66aa00','#b82e2e','#316395','#994499','#22aa99'],
}],
},
options: {
plugins: {
legend: {
labels: {
generateLabels(chart) {
const labels = pieGenerateLabelsLegendHandler(chart);
const sorted = labels.sort((a, b) => chart.data.datasets[0].data[a.index] <= chart.data.datasets[0].data[b.index]);
const top5 = sorted.filter((el, index) => index <= 5);
others = sorted.filter((el, index) => index > 5);
top5.push({text: 'Others', hidden: others[0].hidden});
return top5;
}
},
onClick(e, legendItem, legend) {
if (legendItem.text === 'Others'){
const ci = legend.chart;
others.forEach(function(item) {
ci.toggleDataVisibility(item.index);
});
ci.update();
return;
}
pieLegendClickHandler(e, legendItem, legend);
}
}
}
}
});
.myChartDiv {
max-width: 600px;
max-height: 400px;
}
<script src="https://cdn.jsdelivr.net/npm/chart.js#3.9.1/dist/chart.min.js"></script>
<html>
<body>
<div class="myChartDiv">
<canvas id="myChart" width="600" height="400"/>
</div>
</body>
</html>

Related

How to use a static color for Chart.js bar chart's legend, when using an array of background colors [duplicate]

I have a bar chart that always shows 4 bars. The bars are coloured dynamically. It looks like the coloured box takes its colour from the first data. I would like to use the colour from the 4th (last) data value. Maybe the options:plugins:legend:label:sort function helps but I don't understand what it does.
options
const options = {
scales: {
x: {
grid: {
display: false,
color: 'rgba(0,0,0)'
}
},
y: {
display: false,
min: 0,
max: 4
},
},
plugins: {
legend: {
position: 'bottom'
}
}
}
So I don't know if I can change the data that the box color comes from, or if there is a config option somewhere where I can change it manually.
You can use the generateLabels function as described here.
Please take a look at below runnable sample code and see how it works.
new Chart('myChart', {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow'],
datasets: [{
label: 'My Dataset',
data: [300, 50, 100],
backgroundColor: ['#FF6384', '#36A2EB', '#FFCE56']
}]
},
options: {
responsive: false,
plugins: {
legend: {
labels: {
generateLabels: chart => {
let ds = chart.data.datasets[0];
let color = ds.backgroundColor[ds.backgroundColor.length - 1];
return [{
datasetIndex: 0,
text: ds.label,
fillStyle: color,
strokeStyle: color
}];
}
}
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.1/chart.min.js"></script>
<canvas id="myChart" height="180"></canvas>
Following up with #uminder's answer, if you want to keep the hide/show chart and the line-through style after clicking on the legend, you can add the following line:
options: {
responsive: false,
plugins: {
legend: {
labels: {
generateLabels: chart => {
let ds = chart.data.datasets[0];
let color = ds.backgroundColor[ds.backgroundColor.length - 1];
return [{
datasetIndex: 0,
text: ds.label,
fillStyle: color,
strokeStyle: color,
+ hidden: !chart.isDatasetVisible(0)
}];
}
}
}
}
}

ChartJs React - How do I stop ChartJs from rounding my figures to 3 decimal points on the tooltip?

I'm having rounding issues with the tooltip in my Line chart for ChartJs. When I hover over my datapoints on the chart, the tooltip displays a rounded version of my data (usually 3 decimal points, or less if there's trailing 0s). Is there a way to stop the auto rounding in the tooltip and show the full number?
Here's my code and a screenshot of the issue.
const LineChart = () => {
const [dataForChart, setDataForChart]=useState<any[]>([1.0023, 1.0231, 1.0347412, 1.03541, 1.0434, 1.04001, 1.0459])
const [labelsForChart, setlabelsForChart]=useState<any[]>(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July'])
const options = {
responsive: true,
plugins: {
legend: {
display:false
},
title: {
display: true,
text: 'Price',
},
},
};
const data = {
labels: labelsForChart,
datasets: [
{
label: 'Price',
data: dataForChart,
borderColor: 'green',
backgroundColor: 'green',
},
],
};
return (
<div>
<Line options={options} data={data} />
</div>
);
};
export default LineChart;
Screenshot of the tooltip
try to use label callback it can modify your tooltip text
change your options to
const options = {
responsive: true,
plugins: {
legend: {
display:false
},
title: {
display: true,
text: 'Price',
},
tooltip: {
callbacks: {
label: function(context) {
return context.dataset.label;
}
}
}
},
};

Chartjs React Typescript moving x axis labels above chart

I'm using chartjs to generate a bar chart, and one of the requirements is to have the x-axis labels appear at the top of the chart rather than the bottom. (Even though I disagree with this, apparently there is no leeway from the client) and I'm having trouble following the documentation and any examples I find are for older versions.
The relative packages I'm using are:
"chart.js": "^3.7.1",
"react-chartjs-2": "^4.0.0",
Secondly they want the labels to wrap rather being a single line based on bar width, since it's responsive. I was experimenting with using arrays to break up the words as an example, but wondering if this can be done within chartjs. Here is the code I have setup so far (I had to use some lorem ipsum cause of sensitive data):
import * as React from 'react';
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
BarElement,
Title,
Tooltip,
Legend,
ChartOptions
} from 'chart.js';
import { Bar } from 'react-chartjs-2';
import { Chart } from 'react-chartjs-2';
ChartJS.register(
CategoryScale,
LinearScale,
BarElement,
Title,
Tooltip,
Legend
);
// added as an example. if there is a negative value, the bar color changes.
const generateBgColors = (data : number[]) => {
const bgColors = [];
for(let i = 0; i < data.length; i++) {
const value = data[i];
if(value > 0) {
bgColors.push('rgba(53, 162, 235, 0.5)');
} else {
bgColors.push('rgba(255, 99, 132, 0.5)');
}
}
return bgColors;
}
const options = {
responsive: true,
maintainAspectRatio: true,
plugins: {
legend: {
display: false
}
},
scales: {
}
}
var data = [2.0, -2.0, 0.5, 1.5];
const labels = [
['Lorem ipsum dolor sit amet,', ' consectetur adipiscing elit.', ' Integer eget auctor felis.'],
['label2'],
['label3'],
['label4'],
];
const chartData = {
labels: labels,
datasets: [
{
data: [2.0, -2.0, 0.5, 1.5],
backgroundColor: generateBgColors(data)
}
]
}
const BeliefsChart : React.FunctionComponent = (props) => {
const chartRef = React.useRef<ChartJS>(null);
return (
<React.Fragment>
<Chart type="bar" ref={chartRef} options={options} data={chartData} height={50} />
</React.Fragment>
);
}
To make the labels appear on top in V3 of chart.js you need to specify the position to top in options.scales.x like so:
options: {
scales: {
x: {
position: 'top'
}
}
}
To achieve multi line labels you will need to define your labels array as arrays for the the labels that have to be multi lined. Each entry in the nested arrray will be a new line
labels = ['singel line', ['line 1', 'line 2'], ['line 1', 'line 2', 'line3']]
Live Example:
const options = {
type: 'bar',
data: {
labels: ['singel line', ['line 1', 'line 2'], ['line 1', 'line 2', 'line3']],
datasets: [{
label: '# of Votes',
data: [12, 19, 3],
backgroundColor: 'orange'
},
{
label: '# of Points',
data: [7, 11, 5],
backgroundColor: 'pink'
}
]
},
options: {
scales: {
x: {
position: 'top'
}
}
}
}
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>
Apologies in advance since I'm not familiar with Typescript, but hopefully the following answers will be of help to you regardless.
To position the x-axis labels, try adding the following scales details to your options:
const options = {
responsive: true,
maintainAspectRatio: true,
plugins: {
legend: {
display: false
}
},
scales: {
xAxes: [{
position: "top"
}]
}
}
I don't know if it's possible to make the labels wrap, but an alternative responsive solution might be to use a variable for the fontSize of the ticks (labels) and vary the fontSize value based on the size of the screen using a media query. For example:
import { useMediaQuery } from "react-responsive";
const isMobile = useMediaQuery({ query: '(max-width: 480px)' });
const responsiveFontSize = isMobile ? 12 : 16;
const options = {
responsive: true,
maintainAspectRatio: true,
plugins: {
legend: {
display: false
}
},
scales: {
xAxes: [{
position: "top"
}]
ticks: {
fontSize: responsiveFontSize,
}
}
}

Chart.js how to highlight a part of a label

i am trying to highlight part of a label from the axis labels based on what the user has searched for.
However the label is being rendered as text so the html tags are shown like plain text. any ideas on how to achieve this?
You can use the Plugin Core API. It offers different hooks that may be used for executing custom code. In below code snippet, I use the afterDraw hook to draw text of different styles underneath each bar.
When drawing your own tick labels, probably want to define the text rotation. Further you need to instruct Chart.js not to display the default labels. This can be done through the following definition inside the chart options.
scales: {
xAxes: [{
ticks: {
display: false,
rotation: 40
}
}],
You also need to define some padding for the bottom of the chart, otherwise you won't see your custom tick labels.
layout: {
padding: {
bottom: 60
}
},
Please take a look at below code sample and see how it works. Tick labels that need to be drawn with two different styles are separated with a semicolon inside data.labels.
new Chart(document.getElementById('myChart'), {
type: 'bar',
plugins: [{
afterDraw: chart => {
let ctx = chart.chart.ctx;
let xAxis = chart.scales['x-axis-0'];
let yAxis = chart.scales['y-axis-0'];
chart.data.labels.forEach((l, i) => {
let labelTokens = l.split(';');
let rotation = xAxis.options.ticks.rotation * -Math.PI / 180;
let x = xAxis.getPixelForValue(l);
if (labelTokens.length == 2) {
ctx.save();
let width = ctx.measureText(labelTokens.join(' ')).width;
ctx.translate(x, yAxis.bottom + 10);
ctx.rotate(rotation);
ctx.font = 'italic 12px Arial';
ctx.fillStyle = 'blue';
ctx.fillText(labelTokens[0], -width, 0);
ctx.restore();
}
ctx.save();
let labelEnd = labelTokens[labelTokens.length - 1];
let width = ctx.measureText(labelEnd).width;
ctx.translate(x, yAxis.bottom + 10);
ctx.rotate(rotation);
ctx.font = '12px Arial';
ctx.fillText(labelEnd, -width, 0);
ctx.restore();
});
}
}],
data: {
labels: ['NASTY!!;Errors', 'Warnings'],
datasets: [{
label: 'Result',
data: [30, 59],
fill: false,
backgroundColor: ['rgba(255, 99, 132, 0.2)', 'rgba(255, 159, 64, 0.2)'],
borderColor: ['rgb(255, 99, 132)', 'rgb(255, 159, 64)'],
borderWidth: 1
}]
},
options: {
layout: {
padding: {
bottom: 60
}
},
legend: {
display: false
},
tooltips: {
callbacks: {
title: tooltipItem => tooltipItem[0].xLabel.split(';').join(' ')
}
},
scales: {
xAxes: [{
ticks: {
display: false,
rotation: 40
}
}],
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
canvas {
max-width: 250px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<canvas id="myChart" width="10" height="8"></canvas>

make every labels different in doughnut chart (react js)

i have double doughnut chart in my react js project code. in that double chart dougnut i want to have 3 different labels in 3 different colour, what should i write to make that?
this my code now
var dataDoughnut1 = {
labels: ["Blue", "Green", "Red"],
datasets: [{
data: [1000],
backgroundColor: [
"#36A2EB"
],
labels: [
'Blue',
]
}, {
data: [400,600],
backgroundColor: [
"#C4D34C",
"#F7464A"
],
labels: [
'Green',
'Red'
],
}],
};
until now with that code the output labels is "blue","green" and "green" again
i want to make 3 different labels in 3 different colour too, anyone can help me?
result :
Ok, so i have read the docs of react-chartjs-2 and their Doughnut Chart does not support rendering two datasets like you want, i came up with this workaround that will help you to achieve what you want.
import React from "react";
import { Doughnut } from "react-chartjs-2";
import { MDBContainer } from "mdbreact";
const App = () => {
var dataDoughnut1 = {
labels: ["Blue", "Green", "Red"],
datasets: [
{
data: [1000, 0, 0],
//You should set here all the other colors desired from the other datasets so it can be interpreted by the component
backgroundColor: ["#36A2EB"],
labels: ["Blue"],
},
{
data: [400, 600],
backgroundColor: ["#C4D34C", "#F7464A"],
labels: ["Green", "Red"],
},
],
};
const options = {
responsive: true,
legend: {
display: false,
},
tooltips: {
callbacks: {
label: function (tooltipItem, data) {
console.log(tooltipItem.datasetIndex);
var dataset = data.datasets[tooltipItem.datasetIndex];
var index = tooltipItem.index;
return dataset.labels[index] + ": " + dataset.data[index];
},
},
},
};
return (
<MDBContainer>
<h3 className="mt-5">Doughnut chart</h3>
<Doughnut data={dataDoughnut1} options={options} />
</MDBContainer>
);
};
export default App;
Here is the result

Resources