score:11
First off, set canvas's width and height using it's native attributes (do not use style
attribute), like so :
<canvas id="brandChart" width="700" height="350"></canvas>
note: width should be twice the height
Then, set responsive
property to false
in your chart options, as such :
options: {
responsive: false,
...
}
ᴅᴇᴍᴏ
var chart = new Chart(brandChart, {
type: 'doughnut',
data: {
labels: ['Etronin Home Appliances Service & trading Pte Ltd', 'Giant'],
datasets: [{
data: [30, 70],
backgroundColor: ['#2196f3', '#4caf50']
}]
},
options: {
responsive: false,
legend: {
display: true,
position: 'right',
onClick: null
},
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="brandChart" width="700" height="350"></canvas>
score:0
This post is 9 months old but for the ones who are looking for a solution, use the following and this will make the pie/doughnut bigger in mobile/responsive view
Chart.defaults.global.maintainAspectRatio = false;
score:2
I bumped into a similar issue while using a bar chart with several items in legend positioned at the bottom, I set responsive:true
options: {
responsive: true,
legend: {
display: true,
position: "bottom",
labels: {
fontColor: "#3f5761"
}
},
scales: {
yAxes: [
{
ticks: {
beginAtZero: true
},
scaleLabel: {
display: true,
labelString: "No. of tasks",
fontSize: 10
}
}
]
}
}
and set the height to the canvas as I wanted the width to be responsive. It looks better now.
<canvas #taskCountsChart height="350"></canvas>
score:2
There's no option that can be defined to easily achieve what you're looking for. An open feature request exists for such an option since 2016.
You'll have to generate custom HTML legend using legendCallback
together with some CSS
. The following code expects multi-line labels to be defined as arrays inside data.labels
. It is aimed to be used for charts that have a unique dataset
and where each legend label represents a value of its data (In case you need a solution for a chart with multiple datasets, please take a look at this answer).
legendCallback: chart => {
let html = '<ul>';
chart.data.labels.forEach((l, i) => {
const ds = chart.data.datasets[0];
const bgColor = ds.backgroundColor[i];
const border = ds.borderWidth + 'px solid ' + ds.borderColor[i];
html += '<li>' +
'<span style="width: 36px; height: 14px; background-color:' + bgColor + '; border:' + border + '" onclick="onLegendClicked(event, \'' + i + '\')"> </span>' +
'<span id="legend-label-' + i + '" onclick="onLegendClicked(event, \'' + i + '\')">' +
(Array.isArray(l) ? l.join('<br/>') : l) + '</span>' +
'</li>';
});
return html + '</ul>';
}
To make this behave the same as standard Chart.js charts, the function
onLegendClicked
is invoked when a mouse click occurs on a legend label. This function toggles the hidden state of individual doughnut slices and changes label text style between normal and strike-through.
function onLegendClicked(e, i) {
let hidden = !chart.getDatasetMeta(0).data[i].hidden;
chart.getDatasetMeta(0).data[i].hidden = hidden;
const legendLabelSpan = document.getElementById("legend-label-" + i);
legendLabelSpan.style.textDecoration = hidden ? 'line-through' : '';
chart.update();
};
Please take a look at the executable code below and see how it works in action:
function onLegendClicked(e, i) {
let hidden = !chart.getDatasetMeta(0).data[i].hidden;
chart.getDatasetMeta(0).data[i].hidden = hidden;
const legendLabelSpan = document.getElementById("legend-label-" + i);
legendLabelSpan.style.textDecoration = hidden ? 'line-through' : '';
chart.update();
};
const chart = new Chart('chart', {
type: 'doughnut',
data: {
labels: [
['Label on', 'two lines'],
['Legend label', 'spread over', 'three lines'],
'A short label'
],
datasets: [{
data: [4, 5, 3],
backgroundColor: ['rgba(255, 99, 132, 0.2)', 'rgba(255, 159, 64, 0.2)', 'rgba(54, 162, 235, 0.2)'],
borderColor: ['rgb(255, 99, 132)', 'rgb(255, 159, 64)', 'rgb(54, 162, 235)'],
borderWidth: 1
}]
},
options: {
legend: {
display: false
},
tooltips: {
callbacks: {
title: (tooltipItems, data) => data.labels[tooltipItems[0].index],
label: (tooltipItems, data) => 'Count: ' + data.datasets[0].data[tooltipItems.index]
}
},
legendCallback: chart => {
let html = '<ul>';
chart.data.labels.forEach((l, i) => {
const ds = chart.data.datasets[0];
const bgColor = ds.backgroundColor[i];
const border = ds.borderWidth + 'px solid ' + ds.borderColor[i];
html += '<li>' +
'<span style="width: 36px; height: 14px; background-color:' + bgColor + '; border:' + border + '" onclick="onLegendClicked(event, \'' + i + '\')"> </span>' +
'<span id="legend-label-' + i + '" onclick="onLegendClicked(event, \'' + i + '\')">' +
(Array.isArray(l) ? l.join('<br/>') : l) +'</span>' +
'</li>';
});
return html + '</ul>';
}
}
});
document.getElementById("legend").innerHTML = chart.generateLegend();
#legend>ul {
display: flex;
justify-content: center;
}
#legend li {
cursor: pointer;
margin: 10px 10px;
display: flex;
}
#legend li span {
padding-left: 8px;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<div>
<div id="legend"></div>
<canvas id="chart" height="90"></canvas>
</div>
score:4
Long legend nowrap is a known issue check https://github.com/chartjs/Chart.js/issues/3641
May be they can cover it in next releases. For now the solution is to remove the native legend and draw your custom one
I've created this plunk as an example for doghnut chart with custom legend https://embed.plnkr.co/5nuGS2KEV6hvESwGrOse/
Source: stackoverflow.com
Related Query
- Chart.js legend took up too much spaces
- chart.js 2.7.1 - polar area chart has too much padding
- Chart JS - Make chart scrollable if too much data
- Chart.js - Increase spacing between legend and chart
- Pie Chart Legend - Chart.js
- Chartjs Bar Chart Legend
- chart js how to fill legend box with colour
- ChartJS bar chart with legend which corresponds to each bar
- Chart.js HTML custom legend issues with doughnut chart
- ChartJs Bubble chart - on hover bubble becomes too big
- Chart js: how can I align the legend and the title
- Increase padding between legend and chart with react-chartjs-2
- Chart,js Pie Chart can the gap between a pie chart and the legend be adjusted
- Chart.js Radar chart legend label font size doesn't work
- Using Chart.js - Creating Legend for Doughnut Chart
- Angular chart how to show the legend data value by default along with legend name
- Chart.js: Pie chart legend "onclick" gets overwritten by "options.onclick" if both are present
- Chart JS Legend Styling
- Chart.js chart onclick disables legend onclick
- Chartjs with zoom plugin zooms too much with wheel just with single turn
- Chart is too big. How can I reduce size for the chart in vue js?
- chartjs Adding percentages to Pie Chart Legend
- Chart Js update legend boxes of graph with graph line style
- ChartJS horizontal chart display legend on each bar
- How to Remove Unnecessary Spaces around a semi-Doughnut Chart.js Chart
- React chartjs-2 - Increase spacing between legend and chart
- how to add percentage value to legend field in pie chart using chart.js
- how to add legend in pie chart
- How to display large amount of information properly without cramping too much in Chart.js
- Legend not displaying on Line or Bar chart - React-Chartjs-2
More Query from same tag
- Can we remove bar chart line on click on pie chart legend label?
- chart.JS i want to put different color for each Y axis value grid line color
- Chart area background color chartjs
- Visualization of charts using real time data from MSSQL with node.js webserver
- Line chart doesn't work with type time chart.js
- Charts.js - Display data label only for the last value
- error : "Uncaught TypeError: Cannot read property 'length' of null" Chart.js
- Why can I not see a data value when hovering over a point on the radar chart?
- Time series line chart is not displayed
- Removing text shadow/blur from chart.js charts
- Why are more columns on one labels in Angular ChartJS?
- Floating clone does not show in chrome if dragged element contains a chartjs component
- Custom label from using separate array in Chart JS
- Cannot read property 'reactiveProp' of undefined in vue-chartjs
- Given point in tooltiptext
- How to add vertical line in bar chart using chartJs and ng2-charts?
- ChartJS multiple annotations (vertical lines)
- y-axis values are way to high
- React with chart js, labelling problems in multiple datasets for doughnut chart
- Regarding stacked chart in chart.js
- Dynamic chart codeigniter
- How to line break a label object in ChartsJS (nested arrays won't work)
- Testing Chart.js Plugin with React and Jest/Enzyme
- Rounded corners on chartJS v.2 - bar charts (with negative values)
- How to plot Json data in Chart js
- Chart.js displaying time data
- Chart.js with handlebar.js shows empty canvas
- How to parse data to graph in MVC
- How to align vertical axis area on two charts in chart.js
- chartjs default background color to columns