score:0
For the latest Version 2.6.0, it can be done using legendCallback
Set the template for the legends:
legendCallback: function(chart) {
var text = [];
text.push('<ul>');
for (var i=0; i<chart.data.datasets.length; i++) {
console.log(chart.data.datasets[i]); // see what's inside the obj.
text.push('<li>');
text.push('<span style="background-color:' + chart.data.datasets[i].borderColor + '">' + chart.data.datasets[i].label + '</span>');
text.push('</li>');
}
text.push('</ul>');
return text.join("");
},
Add html element to target for the location of the legends.
<div id="chart-legends"></div>
Then generate the legends:
document.getElementById('chart-legends').innerHTML = myChart.generateLegend();
score:1
I have an answer and it is working fine...
var ctx = document.getElementById("canvas").getContext("2d");
new Chart(ctx).HorizontalBar(barChartLocData, {
responsive: true,
scaleFontColor: "#000",
showTooltips: false,
onAnimationComplete: function() {
var ctx = this.chart.ctx;
ctx.font = this.scale.font;
ctx.fillStyle = this.scale.textColor
ctx.textAlign = "right";
ctx.textBaseline = "right";
this.datasets.forEach(function(dataset) {
dataset.bars.forEach(function(bar) {
ctx.fillText(bar.value, bar.x, bar.y - 5);
});
});
}
});
score:1
Try this
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "My First dataset",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: [28, 48, 40, 19, 86, 27, 90]
}
]
};
var context = document.getElementById('clients').getContext('2d');
var clientsChart = new Chart(context).Bar(data);
HTML:
<canvas id="clients" width="450" height="350"></canvas>
score:16
THE BELOW ANSWER IS FOR CHARTJS 1.X
So below is an example of how you can set up a legend using the labels that need to be given which each dataset, using the default legend template all you have to do is call generateLegend()
on the chart and then place this on the page
var helpers = Chart.helpers;
var canvas = document.getElementById('bar');
var randomScalingFactor = function() {
return Math.round(Math.random() * 100)
};
var barChartData = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
fillColor: "rgba(220,220,220,0.5)",
strokeColor: "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
}, {
label: "My Second dataset",
fillColor: "rgba(151,187,205,0.5)",
strokeColor: "rgba(151,187,205,0.8)",
highlightFill: "rgba(151,187,205,0.75)",
highlightStroke: "rgba(151,187,205,1)",
data: [randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor(), randomScalingFactor()]
}]
}
//
var bar = new Chart(canvas.getContext('2d')).Bar(barChartData, {
tooltipTemplate: "<%if (label){%><%=label%>: <%}%><%= value %>kb",
animation: false,
});
//
var legendHolder = document.createElement('div');
legendHolder.innerHTML = bar.generateLegend();
document.getElementById('legend').appendChild(legendHolder.firstChild);
ul {
list-style: none;
}
ul li {
display: block;
padding-left: 30px;
position: relative;
margin-bottom: 4px;
border-radius: 5px;
padding: 2px 8px 2px 28px;
font-size: 14px;
cursor: default;
height:20px;
width: 140px;
-webkit-transition: background-color 200ms ease-in-out;
-moz-transition: background-color 200ms ease-in-out;
-o-transition: background-color 200ms ease-in-out;
transition: background-color 200ms ease-in-out;
}
li span {
display: block;
position: absolute;
left: 0;
top: 0;
width: 20px;
border-radius: 5px;
height:20px
}
li span.bar-legend-text {
left:25px;
width:120px;
}
#chart-area > *{
float:left
}
<script src="https://raw.githack.com/chartjs/Chart.js/v1.1.1/Chart.min.js"></script>
<div id="chart-area">
<canvas id="bar" width="250" height="250" style="width: 250px; height: 250px;"></canvas>
<div id="legend"></div>
</div>
Source: stackoverflow.com
Related Query
- Chartjs Bar Chart Legend
- ChartJS bar chart with legend which corresponds to each bar
- ChartJS horizontal chart display legend on each bar
- Multi-colored legend stacked bar chart Chartjs
- ChartJS Bar Chart not respecting disabled legend when using cdn
- chartjs bar chart align the legend to left position
- Chartjs Bar Chart showing old data when hovering
- Chartjs v2.0: stacked bar chart
- Changing fontFamily on ChartJS bar chart
- chartjs : how to set custom scale in bar chart
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Click event on stacked bar chart - ChartJs
- ChartJS add tooltip to a grouped bar chart
- ChartJs bar chart - keep bars left instead of equally spread across the width
- Border radius for the bar chart in ChartJS
- Chartjs - data format for bar chart with multi-level x-axes
- Chartjs - show elements in all datasets on hover using bar chart
- Line chart is showing under bar in combochart in chartjs
- How to draw Horizontal line on Bar Chart Chartjs
- Grouping the object by key for Chartjs bar chart
- Multiple stacked bar chart using ChartJs
- ChartJS bar chart fixed width for dynamic data sets
- Stacked bar chart starting from 0 - ChartJS React
- Negative bar chart of highcharts in chartjs
- Remove padding from chartJs horizontal bar chart
- chartjs Adding percentages to Pie Chart Legend
- Grouped Bar Chart from mySQL database using ChartJS and PHP
- Minimum value for x Axis doesn't work for horizontal bar chart | ChartJS
- Chartjs - Set start and end value into a Bar Chart
- Bar Chart of ChartJS does not Render
More Query from same tag
- Zoom chartjs with many X values
- Chart.js - tooltips issue with multiple charts
- HTML Canvas Fill Area Under Line
- Error Message when using ChartJS with React
- chart.js data forming though the API dynamically based on selects
- On click event to show name of pie chart slice in chartsJS
- How to resize chartjs in bootstrap columns for different browsers
- Chart.js ignore options
- angular 4 line chart data is not showing data from http get
- How to make bootstrap 4 column height the same as row height
- Chart Js update legend boxes of graph with graph line style
- Click on chart to expand more information - Chart.js
- Chartjs not rendering in navigation stack
- Prevent size of canvas chart js to equal window height and width
- ChartJS - How to add Text between Pie Chart and Legend
- Javascript window.onload not displaying charts with Chart.js and Flask
- Chart.js Bar Chart with a switch/radio button to group data / Troubleshooting bar color
- How to get a square grid in Chart.js with responsive scatter chart?
- ChartJS: Remove padding to the right of chart caused by tick labels
- Chart.js number Y-AXIS label format for many decimal places
- reactive chart with chart.js in meteor
- change space between horizontal (grid) lines for line chart
- Chart.js - Getting data from HTML
- javascript count daily occurrencies from a datatable for the creation of a linechart
- Should I remove the leading "0" when sending PHP array to JSON?
- Re-running chart.js with different variable
- show more dataset than labels in chart.js
- setting uneven tick stepsize spacing with chart.js
- Updating Chartjs Data with Response from POST Call?
- Undefined x values in chart.js