@model C2.Models.ChartModel
@{
ViewBag.Title = "Chart";
}
<h2>Chart</h2>
<div>
<h3>Data</h3>
@*@Html.Raw(Model.ChartHtmlDisplay)*@
</div>
<div style="width:50%">
<div>
<canvas id="canvas" height="450" width="600"></canvas>
</div>
<div id="divLegend">
</div>
</div>
<script>
var lineChartData = {
@Html.Raw(Model.ChartHtml)
};
window.onload = function(){
var options = {
legendTemplate : '<ul>'
+'<% for (var i=0; i<datasets.length; i++) { %>'
+'<li>'
+'<span style=\"color:<%= datasets[i].pointColor %>;font-family:Arial;font-size:1em\">'
+'<% if (datasets[i].label) { %><%= datasets[i].label %><% } %>'
+'</span>'
+'</li>'
+'<% } %>'
+'</ul>'
}
var ctx = document.getElementById("canvas").getContext("2d");
//don't forget to pass options in when creating new Chart
var lineChart = new Chart(ctx).Line(lineChartData, options);
//then you just need to generate the legend
var legend = lineChart.generateLegend();
//and append it to your page somewhere
$('#divLegend').html(legend);
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx).Line(lineChartData, {
responsive: true
});
}
</script>
20 December 2015
How to add legend to Chart.js graph
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment