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

add pie type options with demo #179

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 18 additions & 8 deletions g.pie.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
o legendothers (string) text that will be used in legend to describe options that are collapsed into 1 slice, because they are too small to render [default `"Others"`]
o legendmark (string) symbol used as a bullet point in legend that has the same colour as the chart slice [default `"circle"`]
o legendpos (string) position of the legend on the chart [default `"east"`]. Other options are `"north"`, `"south"`, `"west"`
o type (string) type of the pie chart can be "half" or "full" [default `full`]
o }
**
= (object) path element of the popup
Expand All @@ -58,7 +59,11 @@
others = 0,
cut = opts.maxSlices || 100,
minPercent = parseFloat(opts.minPercent) || 1,
defcut = Boolean( minPercent );
defcut = Boolean( minPercent ),
type = opts.type || "full",
radSize = 360;

if(type.toLowerCase() =="half") radSize=180;

function sector(cx, cy, r, startAngle, endAngle, fill) {
var rad = Math.PI / 180,
Expand Down Expand Up @@ -118,18 +123,19 @@
others && values.splice(len) && (values[cut].others = true);

for (i = 0; i < len; i++) {
var mangle = angle - 360 * values[i] / total / 2;
var mangle = angle - radSize * values[i] / total / 2;

if (!i) {
angle = 90 - mangle;
mangle = angle - 360 * values[i] / total / 2;
// angle changed to fix pie when it's half
angle = angle - 180;
mangle = angle - radSize * values[i] / total / 2;
}

if (opts.init) {
var ipath = sector(cx, cy, 1, angle, angle - 360 * values[i] / total).join(",");
var ipath = sector(cx, cy, 1, angle, angle - radSize * values[i] / total).join(",");
}

var path = sector(cx, cy, r, angle, angle -= 360 * values[i] / total);
var path = sector(cx, cy, r, angle, angle -= radSize * values[i] / total);
var j = (opts.matchColors && opts.matchColors == true) ? values[i].order : i;
var p = paper.path(opts.init ? ipath : path).attr({ fill: opts.colors && opts.colors[j] || chartinst.colors[j] || "#666", stroke: opts.stroke || "#fff", "stroke-width": (opts.strokewidth == null ? 1 : opts.strokewidth), "stroke-linejoin": "round" });

Expand Down Expand Up @@ -237,8 +243,12 @@
};

var legend = function (labels, otherslabel, mark, dir) {
// cpt added to fix legend y
var cpt = 0;
if(type == "half")
cpt = -65;
var x = cx + r + r / 5,
y = cy,
y = cy + cpt,
h = y + 10;

labels = labels || [];
Expand Down Expand Up @@ -293,4 +303,4 @@
return new Piechart(this, cx, cy, r, values, opts);
}

})();
})();
46 changes: 46 additions & 0 deletions test/piechart3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>g·Raphaël Dynamic Pie Chart</title>
<link rel="stylesheet" href="css/demo.css" type="text/css" media="screen" charset="utf-8">
<link rel="stylesheet" href="css/demo-print.css" type="text/css" media="print" charset="utf-8">
<script src="../raphael-min.js" type="text/javascript" charset="utf-8"></script>
<script src="../g.raphael.js" type="text/javascript" charset="utf-8"></script>
<script src="../g.pie.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
window.onload = function () {
var r = Raphael("holder"),
pie = r.piechart(320, 240, 100, [55, 20, 13, 32, 5, 1, 2, 10], { legend: ["%%.%% - Enterprise Users", "IE Users"], legendpos: "west", href: ["http://raphaeljs.com", "http://g.raphaeljs.com"],type: "half",strokewidth: 0.01});

r.text(320, 100, "Interactive Pie Chart").attr({ font: "20px 'Fontin Sans', Fontin-Sans, sans-serif" });
pie.hover(function () {
this.sector.stop();
this.sector.scale(1.1, 1.1, this.cx, this.cy);

if (this.label) {
this.label[0].stop();
this.label[0].attr({ r: 7.5 });
this.label[1].attr({ "font-weight": 800 });
}
}, function () {
this.sector.animate({ transform: 's1 1 ' + this.cx + ' ' + this.cy }, 500, "bounce");

if (this.label) {
this.label[0].animate({ r: 5 }, 500, "bounce");
this.label[1].attr({ "font-weight": 400 });
}
});
};
</script>
</head>
<body class="raphael" id="g.raphael.dmitry.baranovskiy.com">
<div id="holder"></div>
<p>
Pie chart with legend, hyperlinks on two first sectors and hover effect.
</p>
<p>
Demo of <a href="http://g.raphaeljs.com/">g·Raphaël</a> JavaScript library.
</p>
</body>
</html>