score:6
Accepted answer
legendCallback
method can be used to manipulate how legend's labels are generated. so, using this you can customize legend's box-color as well (such as, using datasets border-color instead of background-color), like so :
legendCallback: function(chart) {
var ul = document.createElement('ul');
var borderColor = chart.data.datasets[0].borderColor;
chart.data.labels.forEach(function(label, index) {
ul.innerHTML += `
<li>
<span style="background-color: ${borderColor[index]}"></span>
${label}
</li>
`; // ^ ES6 Template String
});
return ul.outerHTML;
}
ᴅᴇᴍᴏ ⧩
var chart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: ['Jan', 'Feb', 'Mar'],
datasets: [{
data: [1, 1, 1],
backgroundColor: 'rgba(0, 0, 0, 0)',
borderColor: ['#ff9800', '#e91e63', '#2196f3']
}]
},
options: {
responsive: false,
legend: false,
legendCallback: function(chart) {
var ul = document.createElement('ul');
var borderColor = chart.data.datasets[0].borderColor;
chart.data.labels.forEach(function(label, index) {
ul.innerHTML += `
<li>
<span style="background-color: ${borderColor[index]}"></span>
${label}
</li>
`; // ^ ES6 Template String
});
return ul.outerHTML;
}
}
});
legend.innerHTML = chart.generateLegend();
.chart-container {
display: flex;
}
#legend ul {
list-style: none;
font: 12px Verdana;
white-space: nowrap;
}
#legend li span {
width: 36px;
height: 12px;
display: inline-block;
margin: 0 5px 8px 0;
vertical-align: -9.4px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<div class="chart-container">
<canvas id="ctx"></canvas>
<div id="legend"></div>
</div>
score:1
legend customize with table to display multiple values
var data = [{
value: 2755,
usedValue:100,
color: "#FFE135",
label: "Bare Pipe"
}, {
value: 2256,
usedValue:200,
color: "#3B5323",
label: "Coated Pipe"
}, {
value: 1637,
usedValue:300,
color: "#fc6c85",
label: "Cement Lining"
}];
var str=""
str+='<table>';
str+='<thead><tr><td>Item Name</td><td>Scope</td><td>Used in Construction</td></tr></thead>';
str+='<tbody>';
$.each(data, function(k,v){
if(k =>0){
str+='<tr><td>'+ v.label +'</td><td style="background-color:'+v.color+'">'+v.value+'</td><td style="background-color:'+v.color+'">'+v.usedValue+'</td></tr>';
}
});
str+='</tbody></table>';
//debigger;
var optionsPie = {
scaleBeginAtZero: false,
legendTemplate: str
}
var ctx = $("#top10ItemsChart").get(0).getContext("2d");
var top10PieChart = new Chart(ctx).Pie(data, optionsPie);
$("#top10Legend").html(top10PieChart.generateLegend());
.topleft {
margin-top: -4px;
margin-left: 16px;
margin-bottom: 16px;
padding: 16px;
border: 1px solid black;
}
canvas {
width: 100% !important;
height: auto !important;
margin-left: -25%;
}
.chart {
border: 1px solid forestgreen;
width: 100%;
overflow: hidden;
position: relative;
}
.pie {
position: relative;
padding: 10px 0;
// adjust as necessary
padding-left: 10px;
padding-right: 0;
}
table td{border:1px solid #ddd; text-align:center; padding:5px}
.legend {
position: absolute;
right: 10px;
top: 10px;
height: 100%;
// adjust as necessary:
width: 48%;
}
@media (max-width: 480px) {
.legend {
position: relative;
width: 100%;
}
.pie {
margin: 0;
}
}
.pie-legend ul {
list-style: none;
margin: 0;
padding: 0;
width: 300px;
}
.pie-legend span {
display: inline-block;
width: 14px;
height: 12px;
border-radius: 100%;
margin-right: 4px;
margin-bottom: -2px;
}
.pie-legend li {
margin-bottom: 4px;
display: inline-block;
margin-right: 4px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.1.0/Chart.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="topleft">
<h2 class="sectiontext">Top 10 Items</h2>
<br />
<div class="chart">
<div class="pie">
<canvas id="top10ItemsChart" class="pie"></canvas>
</div>
<div class="legend" id="top10Legend">
</div>
</div>
</div>
</div>
</div>
</div>
enter code here
Source: stackoverflow.com
Related Query
- 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 tooltip hover customization for mixed chart
- Chart.js Legend Customization
- Chart.js HTML custom legend issues with doughnut chart
- 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 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
- 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
- Legend not displaying on Line or Bar chart - React-Chartjs-2
- Removing legend from chart.js Radar chart
- Increase space between legend and chart
- Highlight chart element when hovering over its corresponding legend Item
- Chart.js - Pie chart calculate sum of visible values after legend click
- How can I re-distribute values within a charts.js pie chart when a legend item is turned off/on?
- Legend isnt moving to the right on my doughnut chart created with chart.js
More Query from same tag
- How to use Selenium with "chart.js"
- native element not defined when ngSwitch condition in canvas element
- How to remove colored label square
- Change Vertical Line and Color bar chart in bar chart.js
- Is it possible to update the width of a chart in chart.js?
- Reloading ChartJs Image
- ng2-charts - bar chart, showing only one color
- Side effects from Chartjs for only *some* clients
- How to embed php code in min.js file
- Chart.js - Right use of object array
- Chart.js. Edit bar width -v2.5-
- Firefox is not responding when loading 800+ data sets in Bar Chart with Charts.Js
- Overlay text message on top of a chart
- react-chartjs: difference between `scales.x`/`xAxes`/`xAxes[]`
- set color from datasource like you would set argumentField or valueField?
- How can I show "No Data" message in Pie Chart when there is no data using VueJS?
- Which scenarios might make Chart.js's canvas element resize?
- Data Labels are getting cut off on the top
- Creating mathematical charts using chart.js jQuery
- How to change the labels to the image (icon) in bar chart.js
- Chart.js: Set different options to datasets
- turn off point values in radar chart
- Chart.js configuration
- Chartjs stacked bar single max value
- React component wont re-render ChartJS chart with redux props
- Adding trendlines to existing chart Chart.js
- chart.js renders outside div and canvas
- Chart.js horizontal line chart or modified horizontal bar chart
- Chartsjs multi-axis, make the scale appear/disappear when the dataset is activated/deactivated
- Create Line Chart and populate with database information