Skip to content

Resizing and Scalable Values #1

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

Open
wants to merge 2 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
107 changes: 84 additions & 23 deletions PieChart/piechart.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,94 @@
<head>
<title>Pie Chart</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script src="piechart.js"></script>
<style>
<style type="text/css">
.piechart{
display:block;
margin:0 auto;
}
</style>
<script>



// kick things off
window.onload = createPieChart;
window.onresize = createPieChart;

function sizeCanvas(scale){
if(scale == null){scale=100}
winW = window.innerWidth;
winW = winW * (scale/100);

var graphs = document.getElementsByClassName("piechart");
var numberOfGraphs = graphs.length;

if(window.devicePixelRatio == 2) { winW *= 2;
for(var i = 0; i < numberOfGraphs; i++){
graphs[i].style.width = scale + "%";
}
}

for(var i = 0; i < numberOfGraphs; i++){


graphs[i].width = winW;
graphs[i].height = winW;
}
}
function createPieChart() {

sizeCanvas(80);
//
// create a PieChart.
//
var pieChart = new PieChart( "piechart",
var globalMarketShare = new PieChart( "piechart",
{
includeLabels: true,
data: [120, 100, 140],
labels: ["120%", "100%", "140%"],
colors: [
["#FFDAB9", "#FFDAB9"],
["#E6E6FA", "#E6E6FA"],
["#E0FFFF", "#E0FFFF"]
]
data: [2.4, 2.6, 3.0, 5.3, 13.9, 72.4],
labels: ["Microsoft", "Symbian", "Bada", "Blackberry OS", "iOS", "Android", ],
colors: [
["#d37d66", "#ff0000"],
["#d3a166", "#ddeeff"],
["#4e75b5", "#E0FFFF"],
["#9164a4", "#ff0000"],
["#85bbe4", "#E6E6FA"],
["#74b616", "#E0FFFF"]
]

}
);

//
// nothing appears until you call draw().
//
pieChart.draw();
globalMarketShare.draw();
var androidGlobal = new PieChart( "piechart2",
{
includeLabels: true,
data: [10.3, 50.6, 27.5, 6.7],
labels: ["2.2 Froyo", "2.3.3 - 2.3.7 Gingerbread", "4.0.3 - 4.0.4 ICS", "4.1 - 4.2 Jelly Bean" ],
colors: [
["#00cc33", "#ff0000"],
["#00bb05", "#ddeeff"],
["#00aa66", "#E0FFFF"],
["#00dd34", "#ff0000"]
]

}
);
androidGlobal.draw();
var android4Global = new PieChart( "piechart3",
{
includeLabels: true,
data: [27.5, 5.9, .8],
labels: ["4.0 ICS", "4.1 Jelly Bean", "4.2 Jelly Bean" ],
colors: [
["#00cc33", "#ff0000"],
["#00bb05", "#ddeeff"],
["#00aa66", "#E0FFFF"]
]

}
);
android4Global.draw();

/*
* If you want to draw the labels separately, you can set
Expand All @@ -48,25 +106,28 @@
* If you want to select a segment to highight it, you can
* call select() for a given segment. Here's a little snippet
* that animates selecting each segment.
*
**
var segment = 0;
function nextSegment() {
pieChart.select(segment);
globalMarketShare.select(segment);
segment++;
if (segment < pieChart.data.length) {
if (segment < globalMarketShare.data.length) {
setTimeout(nextSegment, 1000);
}
}
setTimeout(nextSegment, 1000);
*/
setTimeout(nextSegment, 1000);*/

}
</script>
</head>
<body>

<canvas id="piechart" width="400" height="400">
<canvas id="piechart" class="piechart">
</canvas>
<canvas id="piechart2" class="piechart">
</canvas>
<canvas id="piechart3" class="piechart">
</canvas>


</body>
</html>
</html>
30 changes: 22 additions & 8 deletions PieChart/piechart.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ function PieChart(id, o) {
this.includeLabels = o.includeLabels;
}
this.data = o.data ? o.data : [30, 70, 45, 65, 20, 130]; // in degrees

var dataLength = this.data.length,
totalCount = 0;
for(i = 0; i < dataLength; i++)
{
totalCount += this.data[i]
}
this.multiplier = 360/totalCount;


this.labels = o.labels ? o.labels : ["First", "Second", "Third", "Fourth", "Fifth", "Sixth"];
this.colors = o.colors ? o.colors : [
["#bbddb3", "#1d8e04"], // green
Expand All @@ -36,6 +46,7 @@ function PieChart(id, o) {
];

this.canvas = document.getElementById(id);

}

PieChart.prototype = {
Expand All @@ -49,20 +60,24 @@ PieChart.prototype = {
draw: function() {
var self = this;
var context = this.canvas.getContext("2d");
if(window.devicePixelRatio == 2) {context.scale(1,1);}
for (var i = 0; i < this.data.length; i++) {
this.drawSegment(this.canvas, context, i, this.data[i], false, this.includeLabels);
}
},

drawSegment: function(canvas, context, i, size, isSelected, includeLabels) {
size *= this.multiplier;
var self = this;
context.save();
var centerX = Math.floor(canvas.width / 2);
var centerY = Math.floor(canvas.height / 2);
radius = Math.floor(canvas.width / 2);

var startingAngle = self.degreesToRadians(self.sumTo(self.data, i));
startingAngle *= this.multiplier;
var arcSize = self.degreesToRadians(size);

var endingAngle = startingAngle + arcSize;

context.beginPath();
Expand All @@ -73,7 +88,7 @@ PieChart.prototype = {
isSelected ?
context.fillStyle = self.colors[i][1] :
context.fillStyle = self.colors[i][0];

context.fill();
context.restore();

Expand All @@ -87,16 +102,16 @@ PieChart.prototype = {
context.save();
var x = Math.floor(canvas.width / 2);
var y = Math.floor(canvas.height / 2);
var angle = self.degreesToRadians(self.sumTo(self.data, i));

var angle = self.degreesToRadians(self.sumTo(self.data, i)*this.multiplier);
context.translate(x, y);
context.rotate(angle);
context.textAlign = "right";
var fontSize = Math.floor(canvas.height / 25);
var fontSize = Math.floor(canvas.height / 30);
context.font = fontSize + "pt Helvetica";

var dx = Math.floor(canvas.width * 0.5) - 10;
var dy = Math.floor(canvas.height * 0.05);
var dy = Math.floor(canvas.height * 0.04);
context.fillText(self.labels[i], dx, dy);

context.restore();
Expand All @@ -106,7 +121,7 @@ PieChart.prototype = {
var self = this;
var context = this.canvas.getContext("2d");
var size = self.data[i];

size *= this.multiplier;
self.drawSegmentLabel(this.canvas, context, i, size, false);
},

Expand All @@ -125,4 +140,3 @@ PieChart.prototype = {


}