score:6
There are two possible solutions to your problem:
1: This is for a line chart, but can easily tailored to a bar chart.
The legend is part of the default options of the ChartJs library. So you do not need to explicitly add it as an option.
The library generates the HTML. It is merely a matter of adding that to the your page. For example, add it to the innerHTML of a given DIV. (Edit the default options if you are editing the colors, etc)
<div>
<canvas id="chartDiv" height="400" width="600"></canvas>
<div id="legendDiv"></div>
</div>
<script>
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
label: "The Flash's Speed",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
},
{
label: "Superman's Speed",
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: [28, 48, 40, 19, 86, 27, 90]
}
]
};
var myLineChart = new Chart(document.getElementById("chartDiv").getContext("2d")).Line(data);
document.getElementById("legendDiv").innerHTML = myLineChart.generateLegend();
</script>
2: Adding a legend template in chart options
You'll also need to add some basic css to get it looking ok.
//legendTemplate takes a template as a string, you can populate the template with values from your dataset
var options = {
legendTemplate : '<ul>'
+'<% for (var i=0; i<datasets.length; i++) { %>'
+'<li>'
+'<span style=\"background-color:<%=datasets[i].lineColor%>\"></span>'
+'<% if (datasets[i].label) { %><%= datasets[i].label %><% } %>'
+'</li>'
+'<% } %>'
+'</ul>'
}
//don't forget to pass options in when creating new Chart
var lineChart = new Chart(element).Line(data, options);
//then you just need to generate the legend
var legend = lineChart.generateLegend();
//and append it to your page somewhere
$('#chart').append(legend);
Either of these two options will work.
In your case the legend code will look something like this
(Assuming you already have a BarChart Generated)
<div id="legendDiv"></div>
document.getElementById("legendDiv").innerHTML = BarChart.generateLegend();
Then use css to format the legend however you would prefer (including adding spacing between the legend in the chart)
Source: stackoverflow.com
Related Query
- Chartjs v2 xAxes label overlap with scaleLabel
- ChartJS - Draw chart with label by month, data by day
- How to Change the Label Strike-Through with light gray on a ChartJS Doughnut?
- label too long in yAxis with chartJs
- Chartjs not working with d3 from csv source
- How to introduce newline character in xAxes Label with chart js
- How to set ChartJS Axis label with PrimeFaces
- ChartJS have xAxes labels match data source
- Updating Chartjs to 2.5 with custom code
- add a unit to label with chartjs plugin datalabels
- Chartjs display multiple info with different units in label
- ChartJS 2.7.3 stacked bar chart with overlap
- Chartjs v3 overlap stacked bar chart with groups
- Insert value into a ChartJS with different data label
- chartjs maintain points position, put image alongside with label
- Style a specific X-axis label with ChartJS
- ChartJS Bar chart with time scale - Bars overlap with each other
- Chartjs line chart gets all scrambled up when an x value coincides with a label
- ChartJS plugin with live label update
- How do I draw horizontal bars with a label using either ChartJS or D3?
- Custom Legend with ChartJS v2.0
- Chartjs random colors for each part of pie chart with data dynamically from database
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Truncating canvas labels in ChartJS while keeping the full label value in the tooltips
- Make x label horizontal in ChartJS
- Display line chart with connected dots using chartJS
- react-chartjs-2 with chartJs 3: Error "arc" is not a registered element
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Show bar with zero value in ChartJs v2
- Chartjs linechart with only one point - how to center
More Query from same tag
- Angular Dynamic Chart Generate
- How do i make a new chart by pressing a button (chart.js)?
- The real time chart sometimes does not display when I switch the target
- Popup windows for histogram with AngularJS
- How I can use a list of a string array in JavaScript into blade view?
- Limit numbers of labels on Chart.js in "smaller display only"
- ChartJs not showing top label
- Chart.js - generateLegend() call results in "undefined is not a function"
- ChartJS set default options only to specific Canvas, not globally
- Chart.js: A value is empty, but the line must not be broken
- Want to show the dataset labels
- Dotted lines using Chart.js
- Charting with chartjs, can't get it working correctly
- Remove x-axis label/text in chart.js
- Nesting directives and using the data inside the local scope
- Stacked horizontal chart. How to trigger individual hover in the stacked element?
- how to get multiple x-axis labels for a react-chartjs-2 chart
- Draw multiple chart in one page with Chart.js
- Data not displaying properly in Chart.js line chart
- How to set minimal value for bar chart in Chart.js? (ver. 2.0.2)
- How to create range based ticks in chart js? (like in attached image)
- Javascript : ""Uncaught TypeError: Cannot read property 'x' of undefined"" in canvasjs.min.js
- How to show all dates on xAxis between two dates in chart.js
- How to show text likt this in Chartjs Dougnet chart
- CharJs scrolling page on mobile
- How to commaize the data values provided to a chart in Chart.JS?
- How to retrieve data from mysql to chart.js
- Vue.js this.$el.getContext('2d') is not a function
- How to display data by current month and display no data message if data not exists
- Chart JS can't access data from array within an object