score:2
This is what works (more or less) using version 2 of Chart.JS:
HTML
<h2 class="sectiontext">Top 10 Items</h2>
<br />
<div class="chart">
<canvas id="top10ItemsChart" class="pie"></canvas>
<div id="pie_legend"></div>
</div>
JQUERY
var data = {
labels: [
"Bananas: 2,755 (18%)",
"Lettuce, Romaine: 2,256 (14%)",
"Melons, Watermelon: 1,637 (10%)",
"Pineapple: 1,608 (10%)",
"Berries: 1,603 (10%)",
"Lettuce, Spring Mix: 1,433 (9%)",
"Broccoli: 1,207 (8%)",
"Melons, Honeydew: 1,076 (7%)",
"Grapes: 1,056 (7%)",
"Melons, Cantaloupe: 1,048 (7%)"
],
datasets: [
{
data: [2755, 2256, 1637, 1608, 1603, 1433, 1207, 1076, 1056, 1048],
backgroundColor: [
"#FFE135",
"#3B5323",
"#fc6c85",
"#ffec89",
"#021c3d",
"#3B5323",
"#046b00",
"#cef45a",
"#421C52",
"#FEA620"
],
}]
};
var optionsPie = {
responsive: true,
scaleBeginAtZero: true
}
var ctx = $("#top10ItemsChart").get(0).getContext("2d");
var top10PieChart = new Chart(ctx,
{
type: 'pie',
data: data,
options: optionsPie
});
$("#top10Legend").html(top10PieChart.generateLegend());
I say, "more or less" because the pie pieces are still pitifully puny:
score:3
As @B.ClayShannon mentioned, version 2 is quite a bit different than verison 1. Here is an example of how to customize the legend template using version 2.
options: {
legendCallback: function (chart) {
var text = [];
text.push('<ul class="' + chart.id + '-legend" style="list-style:none">');
for (var i = 0; i < chart.data.datasets[0].data.length; i++) {
text.push('<li><div style="width:10px;height:10px;display:inline-block;background:' + chart.data.datasets[0].backgroundColor[i] + '" /> ');
if (chart.data.labels[i]) {
text.push(chart.data.labels[i]);
}
text.push('</li>');
}
text.push('</ul>');
return text.join('');
},
legend: {display: false},
}
It's not shown directly in the accepted solution above, but to render your legend elsewhere you'll want to call:
$("#myChartLegend").html(myChart.generateLegend());
Finally, some HTML to pull it together (note clearfix is a Bootstrap class that :
<div class="chart">
<div style="float:left">
<canvas id="myChart" class="pie" style="max-width:300px;"></canvas>
</div>
<div class="legend" id="myChartLegend" style="float:left;"></div>
<div style="clear: both;"/>
</div>
score:19
I think this what you want: DEMO
First, you need to make canvas
responsive by overriding fixed width and height and wrap it in additional div
that can be used for positioning. I used display: table
for centering elements but setting inner divs to inline-block
also works if you wish for chart and legend to take different amount of space than 50:50.
HTML:
<div class="table">
<div class="cell">
<canvas id="top10ItemsChart" class="pie"></canvas>
</div>
<div class="cell" id="top10Legend"></div>
</div>
CSS:
canvas {
width: 100% !important;
height: auto !important;
}
.table {
border: 1px solid red;
display: table;
width: 100%;
table-layout: fixed;
}
.cell {
display: table-cell;
vertical-align: middle;
}
UPDATE: Did some adjustment based on additional information by OP NEW DEMO
HTML:
<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>
CSS:
.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;
}
.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;
}
Source: stackoverflow.com
Related Query
- How can I control the placement of my Chart.JS pie chart's legend, as well as its appearance?
- Chart js: how can I align the legend and the title
- Chart,js Pie Chart can the gap between a pie chart and the legend be adjusted
- How can I re-distribute values within a charts.js pie chart when a legend item is turned off/on?
- How can I change the cursor on pie chart segment hover in ChartJS 3?
- How can i give the full Legend a background color in chart js?
- How can I reduce the spacing between my legend and chart proper in my Chart.JS bar chart, and increase it in the graph area?
- How can I cause a legend to appear to the right of the pie (Chart.JS)?
- How can I remove the white border from Chart.js pie chart when all legends are hidden?
- How to increase the size of the donut or pie chart and keep the legend next to it in chart JS?
- How can I increase the size of the pie (Chart.JS)?
- chart js tooltip how to control the data that show
- How can I remove extra whitespace from the bottom of a line chart in chart.js?
- How can I display the xAxes and yAxes data in the tooltip, Chart JS?
- How can I trigger the hover mode from outside the chart with charts.js 2?
- How can I add some text in the middle of a half doughnut chart in Chart.JS?
- ChartJs - Pie Chart - how to remove labels that are on the pie chart
- Angular chart how to show the legend data value by default along with legend name
- How can I rotate a pie chart in charts.js?
- Chart is too big. How can I reduce size for the chart in vue js?
- How can I load multiple Chartjs charts with different data on the same page?
- how to add percentage value to legend field in pie chart using chart.js
- how to add legend in pie chart
- How can I have different values for the chart and the tooltip in chart.js?
- How can I show a subset of data on pie pieces in Chart.JS while still displaying the superset when hovering?
- Chart JS, ng2-Charts - How to make the label to the right of pie chart instead on top?
- How to style a pie chart in chart js? I want to change the border color, border width and give them shadow
- how can i use chart.js to create a chart that has one time series line and one linear line on it at the same time?
- How to make the chart smaller than its container
- How can I remove the white border from Chart.js V2.6.0 pie chart?
More Query from same tag
- Tooltip callbacks in line chart JS not working
- change axis scale to be round numbers instead of decimal - chart.js
- Chart.js dataset controller 'null' when chart drawn
- Chart JS grouped sub labels
- How to displays item names from the database using a monthly or yearly chart PHP MySQL
- Using JSON file data in Chart.js
- chart.js Line chart with different background colors for each section
- Reactjs- Bar Graph-chartjs- Data not coming in individual blocks
- How to write the value over each bullet in small div permanently in chart js
- Stacked bar chart with rounded corner of bar using Chart.js
- update chart data called through ajax via PHP, MySQL
- Assigning data structures to chartjs properties, NOT individually
- PaddingLeft of afterFit does not work during chart.js option
- How to rotate to center of a dataset using Chart.js (Doughnut chart)
- How can I create a bar chart that has multiple yAxes scales?
- connect filter to multiple charts asp.net core mvc
- ChartJS - Highlight grouped bars
- How can I remove the white border from Chart.js pie chart when all legends are hidden?
- Multiple chart on one page using chart.js and Flask app
- Problems with max and min value of Chart.js [line chart]
- Chart.js Find intersection Point and Draw a Circle
- ChartJS + twig symfony
- Representing multiple subsections within a section of a doughnut chart Chart.js
- Chartjs Nested Pie/Doughnut charts
- Error in created hook (Promise/async): "TypeError: Cannot read property 'position' of undefined"
- Chartjs different row background colors according to Y axis values
- In react hooks, how can i pass the filtered values to the chart?
- Dynamic dataset from MySQL query with Chart.js
- React chart2js Line chart with multiple datasets overlapping
- Changing x axis labels in Chart.js line chart