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 coloring by scales #3

Open
wants to merge 16 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
31 changes: 25 additions & 6 deletions d3-parliament.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ d3.parliament = function() {
/* params */
var width,
height,
innerRadiusCoef = 0.4;
innerRadiusCoef = 0.4,
scale;

/* animations */
var enter = {
Expand Down Expand Up @@ -40,8 +41,7 @@ d3.parliament = function() {
/* init the svg */
var svg = d3.select(this);

/***
* compute number of seats and rows of the parliament */
/* compute number of seats and rows of the parliament */
var nSeats = 0;
d.forEach(function(p) { nSeats += (typeof p.seats === 'number') ? Math.floor(p.seats) : p.seats.length; });

Expand Down Expand Up @@ -114,7 +114,6 @@ d3.parliament = function() {
});
})();


/***
* helpers to get value from seat data */
var seatClasses = function(d) {
Expand All @@ -132,9 +131,14 @@ d3.parliament = function() {
return r;
};

// if scale is present, use it:
if (scale) {
scale.domain(d.map(function(row) { return(row.legend) }));
var seatColor = function(d) { return scale(d.party.legend); }
}


/***
* fill svg with seats as circles */
/* fill svg with seats as circles */
/* container of the parliament */
var container = svg.select(".parliament");
if (container.empty()) {
Expand All @@ -153,6 +157,11 @@ d3.parliament = function() {
circlesEnter.attr("cx", enter.fromCenter ? 0 : seatX);
circlesEnter.attr("cy", enter.fromCenter ? 0 : seatY);
circlesEnter.attr("r", enter.smallToBig ? 0 : seatRadius);

if (scale) {
circlesEnter.attr("fill", seatColor);
}

if (enter.fromCenter || enter.smallToBig) {
var t = circlesEnter.transition().duration(function() { return 1000 + Math.random()*800; });
if (enter.fromCenter) {
Expand Down Expand Up @@ -181,6 +190,10 @@ d3.parliament = function() {
.attr("cy", seatY)
.attr("r", seatRadius);

if (scale) {
circlesUpdate.attr("fill", seatColor);
}

/* animation removing seats from the parliament */
if (exit.toCenter || exit.bigToSmall) {
var t = circles.exit().transition().duration(function() { return 1000 + Math.random()*800; });
Expand Down Expand Up @@ -215,6 +228,12 @@ d3.parliament = function() {
return parliament;
};

parliament.scale = function(value) {
if (!arguments.length) return scale;
scale = value;
return parliament;
}

parliament.enter = {
smallToBig: function (value) {
if (!arguments.length) return enter.smallToBig;
Expand Down
2 changes: 1 addition & 1 deletion d3-parliament.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "A parliament chart based on d3",
"main": "d3-parliament.js",
"dependencies": {
"uglify-js": "^2.6.2"
"uglify-js": "^2.6.2",
"d3-scale" : "2.0.0",
"d3-scale-chromatic" : "1.2.0"
},
"devDependencies": {},
"scripts": {
Expand All @@ -19,6 +21,7 @@
"parliament"
],
"author": "Geoffrey Brossard <[email protected]>",
"contributors": "Oğuzhan Öğreden <[email protected]> http://www.oguzhanogreden.com/",
"license": "MIT",
"bugs": {
"url": "https://github.com/geoffreybr/d3-parliament/issues"
Expand Down