Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for startAngle and percentageTotal in the Doughnut chart. #1082

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/05-Pie-Doughnut-Chart.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ These are the customisation options specific to Pie & Doughnut charts. These opt

//Boolean - Whether we animate scaling the Doughnut from the centre
animateScale : false,

//Number - The angle where the first segment will start (0-360)
startAngle: 0,

//The percentage of the complete pie.
percentageTotal: 100,
{% raw %}
//String - A legend template
legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>"
Expand Down
74 changes: 74 additions & 0 deletions samples/gauge.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<!doctype html>
<html>
<head>
<title>Doughnut Chart</title>
<script src="../Chart.js"></script>
<style>
body {
padding: 0;
margin: 0;
}

#canvas-holder {
width: 30%;
}
</style>
</head>
<body>
<div id="canvas-holder">
<canvas id="chart-area" width="500" height="500"/>
</div>


<script>

var doughnutData = [
{
value: 300,
color: "#F7464A",
highlight: "#FF5A5E",
label: "Red"
},
{
value: 50,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green"
},
{
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow"
},
{
value: 40,
color: "#949FB1",
highlight: "#A8B3C5",
label: "Grey"
},
{
value: 120,
color: "#4D5360",
highlight: "#616774",
label: "Dark Grey"
}

];

var options = {
responsive: true,
startAngle: -90,
percentageTotal: 50
};

window.onload = function () {
var ctx = document.getElementById("chart-area").getContext("2d");

window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, options);
};


</script>
</body>
</html>
9 changes: 8 additions & 1 deletion src/Chart.Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1270,8 +1270,15 @@
y: chartY
});

//Normalize Arc to start from PI*1.5 to support custom start angle.
var diff = (Math.PI * 1.5 - this.startAngle),
endAngle = this.endAngle + diff,
angle = pointRelativePosition.angle + diff;
if (angle > Math.PI * 3.5) {
angle -= Math.PI * 2;
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was to support the segments that starts before PI*1.5

//Check if within the range of the open/close angle
var betweenAngles = (pointRelativePosition.angle >= this.startAngle && pointRelativePosition.angle <= this.endAngle),
var betweenAngles = (angle >= Math.PI * 1.5 && angle <= endAngle),
withinRadius = (pointRelativePosition.distance >= this.innerRadius && pointRelativePosition.distance <= this.outerRadius);

return (betweenAngles && withinRadius);
Expand Down
11 changes: 9 additions & 2 deletions src/Chart.Doughnut.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
//Boolean - Whether we animate scaling the Doughnut from the centre
animateScale : false,

//Number - The angle where the first segment will start (0-360)
startAngle: 0,

//The percentage of the complete pie.
percentageTotal: 100,

//String - A legend template
legendTemplate : "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>"

Expand Down Expand Up @@ -101,7 +107,7 @@
showStroke : this.options.segmentShowStroke,
strokeWidth : this.options.segmentStrokeWidth,
strokeColor : this.options.segmentStrokeColor,
startAngle : Math.PI * 1.5,
startAngle : 0,
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this to 0 since it gets override in the draw function anyway

circumference : (this.options.animateRotate) ? 0 : this.calculateCircumference(segment.value),
label : segment.label
}));
Expand All @@ -122,6 +128,7 @@
helpers.each(data,function(segment){
this.total += Math.abs(segment.value);
},this);
this.total *= (100 / this.options.percentageTotal);
},
update : function(){
this.calculateTotal(this.segments);
Expand Down Expand Up @@ -171,7 +178,7 @@

segment.draw();
if (index === 0){
segment.startAngle = Math.PI * 1.5;
segment.startAngle = Math.PI * (1.5 + this.options.startAngle / 180);
}
//Check to see if it's the last segment, if not get the next and update the start angle
if (index < this.segments.length-1){
Expand Down