score:38
no need to use legendcallback
function. you can set usepointstyle = true
to turn that rectangle into a circle.
chart.defaults.global.legend.labels.usepointstyle = true;
var ctx = document.getelementbyid("mychart").getcontext("2d");
var mychart = new chart(ctx, {
type: 'line',
data: {
labels: ["red", "blue", "yellow", "green", "purple", "orange"],
datasets: [{
label: 'link one',
data: [1, 2, 3, 2, 1, 1.5, 1],
backgroundcolor: [
'#d3e4f3'
],
bordercolor: [
'#d3e4f3',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderwidth: 1
}]
},
options: {
legend: {
display: true,
position: 'bottom',
labels: {
fontcolor: '#333'
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.5.0/chart.min.js"></script>
<div class="container">
<canvas id="mychart"></canvas>
</div>
score:1
add this in your options
legend: {
display: true,
position: 'bottom',
labels: {
boxwidth: 9,
fontcolor: '#474747',
fontfamily: '6px montserrat',
},
},
for example :
this.testchart = new chart('testpie', {
type: 'doughnut',
data: {
labels: [ 'success','failure','aborted'],
datasets: [
{
data: [],
backgroundcolor: [ '#45d78f', '#f58220','#ffd403'],
},
],
},
options: {
maintainaspectratio: false,
cutoutpercentage: 50, // for thikness of doughnut
title: {
display: false,
text: 'runs',
fontsize: 14,
fontfamily: 'roboto',
fontcolor: '#474747',
},
legend: {
display: true,
position: 'bottom',
labels: {
boxwidth: 9,
fontcolor: '#474747',
fontfamily: '6px montserrat',
},
},
},
});
score:3
additional information on @grunt 's answer i assign usepointstyle inside legend.labels not what @grunt did chart.defaults.global.legend.labels.usepointstyle = true;
this is useful when you are using react
legend: {
labels: {
usepointstyle: true,
},
}
more info here display point style on legend in chart.js
score:6
use usepointstyle:true
var ctx = document.getelementbyid("mychart");
var mychart = new chart(ctx, {
type: 'line',
data: {
labels: ["red", "blue", "yellow", "green", "purple", "orange"],
datasets: [{
label: 'link one',
data: [1, 2, 3, 2, 1, 1.5, 1],
backgroundcolor: [
'#d3e4f3'
],
bordercolor: [
'#d3e4f3',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderwidth: 1
}]
},
options: {
legend: {
display: true,
position: 'bottom',
labels: {
fontcolor: '#333',
usepointstyle:true
}
}
}
});
score:11
step 1:
change options to this:
options: {
legend: {
display: false,
}
}
step 2:
append to your canvas this code (just after canvas):
<div id='chartjslegend' class='chartjslegend'></div> //or prepend to show the legend at top, if you append then it will show to bottom.
step 3:
generate this legend instead of default with this (just after mychart):
document.getelementbyid('chartjslegend').innerhtml = mychart.generatelegend();
step 4:
make css so it generates as circle:
.chartjslegend li span {
display: inline-block;
width: 12px;
height: 12px;
margin-right: 5px;
border-radius: 25px;
}
step 5:
change css with what ever you feel like should be better.
time for some chimichangas now.
score:20
for angular4-chart.js you could use the options attribute like so:
options = {
legend:{
display: true,
labels: {
usepointstyle: true,
}
}
}
Source: stackoverflow.com
Related Query
- Chart JS Legend Styling
- Pie chart legend styling using Chart js
- Chart.js - Increase spacing between legend and chart
- Chart.js - Styling Legend
- 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
- 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 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
- Chart.js - Styling Legend - ONE legend entry per line
- Chartjs Legend Styling
- 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
More Query from same tag
- Highlight last clicked bar in ChartJS
- Chartjs - onAnimationComplete fires before the animation actually completes
- Ionic using multiple chart.js canvas in the same page
- Show Count on top of the bar graph - ChartJS
- How to pass dynamic values as objects inside data in line chart?
- Changing size of the canvas makes it blurry
- Chartjs doughnut chart rounded corners for half doghnut
- How to fix the number of gridlines in X-Axis as label are too condensed
- Chart.js data values from textbox input only load once
- How to show/hide animation when legend clicked chart js
- Is it possible to avoid the shrinking of Chart.js pie charts when accompanied by labels?
- how to draw line graph with line animation from left to right using chartjs?
- How to make Laravel Charts
- react-chartjs-2 how to set multiple background levels within a line chart
- ChartJS canvas not displaying rgba colors in IE, Safari and Firefox
- Dashboard grid - Chart.JS and CSS- issue widening two small horizontal bar graphs - noob question
- Chartjs Bar Chart showing old data when hovering
- Change data onclick with ChartJS
- Chart.js not showing on second tab
- I have a setInterval function that graphs.
- Chart.js awkward label gaps before last x-axis label
- How to define the Charts in the script?
- javascript count daily occurrencies from a datatable for the creation of a linechart
- How do I manipulate a specific tooltip value of a Chart.js pie chart?
- Chart JS change pointBackgroundColor based on data value
- TopoJson and ChartGeo/ChartJS: Unexpected geomerty
- how to make a chart.js bar chart scrollable
- Can't bind to 'chartType' since it isn't a known property of 'canvas' with angular12
- Chart.js V2.7.2 How to make Horizontal scroll bar without loosing Y-Axis
- Fill Chart.js bar chart with diagonal stripes or other patterns