block by michalskop 5773523

España 2012-2013

Full Screen

a-spain

Analysis of voting records in Spanish Congreso, 2012 - 2013(06)
Data from Colibrí project

chart.css

/*based on http://bost.ocks.org/mike/nations/ */
#chart {
  margin-left: -40px;
}

text {
  font: 10px sans-serif;
}

/*dots*/
.dot {
  stroke: #fff;
  opacity: .5;
  stroke-opacity: .75;
  stroke-width: 1.5px;
}

.highlighted {
  stroke: #000;
  stroke-opacity: 1;
  stroke-width: 3;
  opacity: 1;
}

/*axes*/
.axis path, .axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.label {
  fill: #777;
}

/*year*/
.year.label {
  font: 500 100px "Helvetica Neue";
  fill: #ddd;
}

.year.label.active {
  fill: #aaa;
}

.overlay {
  fill: none;
  pointer-events: all;
  cursor: ew-resize;
}

/*layout of chart and checkboxes of mps*/
.chart-content {
    min-height: 460px;
}
.additional-column {
  max-height: 460px;
  /* from http://stackoverflow.com/questions/5715705/always-show-vertical-scrollbar-in-select */
  overflow-x: hidden;
}
#slider-form {
  width:80%;
  float:right;
}
  
/*computer*/
@media all and (min-width: 770px){
  .additional-column {
		text-align: right;
		float: right;
		width: 25%;
		background: none;
	}
 .main-column {
		width: 70%;
		float: left;
		margin-top: 30px;
		margin-right: 1%;
		padding-right: 1%;
	}
}


/*tooltips*/
/* http://www.d3noob.org/2013/01/adding-tooltips-to-d3js-graph.html */
/* alternative, was not able to use it: bl.ocks.org/ilyabo/1373263 */
div.tooltip {   
  position: absolute;           
  text-align: center;           
  width: 15em;                  
  height: 2.5em;                 
  padding: 2px;             
  font: .8em sans-serif;        
  background: lightsteelblue;   
  border: 0px;      
  border-radius: 8px;           
  pointer-events: none;         
}

colibri.php

<?php
$start = microtime(true);
set_time_limit(0);

/*$db = new PDO('sqlite:colibri.sqlite');
    // Set errormode to exceptions
$db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

//db
   //group
    $query = 'CREATE TABLE IF NOT EXISTS "group"(
      id INTEGER PRIMARY KEY,
      name TEXT,
      short_name TEXT
      )
    ';
    $db->exec($query);
    //party
    $query = 'CREATE TABLE IF NOT EXISTS "party"(
    	id INTEGER PRIMARY KEY,
    	name TEXT
    )
    ';
    $db->exec($query);
    //mps
    $query = 'CREATE TABLE IF NOT EXISTS "mp"(
    	id INTEGER PRIMARY KEY,
    	first_name TEXT,
    	last_name TEXT,
    	party_id INTEGER,
    	group_id INTEGER
    )
    ';
    $db->exec($query);
    //division
    $query = 'CREATE TABLE IF NOT EXISTS "division"(
    	id INTEGER PRIMARY KEY,
    	date BLOB
    	
    )
    ';
    $db->exec($query);
    //mp_vote
    $query = 'CREATE TABLE IF NOT EXISTS "mp_vote"(
		mp_id INTEGER,
		division_id INTEGER,
		vote TEXT,
		  CONSTRAINT mp_vote_pkey PRIMARY KEY (mp_code , division_code )  
    )
    ';
    $db->exec($query);*/
    

//mps
$groups_db = json_decode(file_get_contents("http://proyectocolibri.es/api/v1/group/"));

//new groups
$groups = array();
foreach ($groups_db->objects as $group_db) {
  $groups[$group_db->id]['id'] = $group_db->id;
  $groups[$group_db->id]['short_name'] = trim($group_db->acronym);
  $groups[$group_db->id]['name'] = trim($group_db->name);
}

//members
foreach ($groups_db->objects as $group_db) {
  foreach ($group_db->members as $mp) {
    $mps[$mp->id]['id'] = $mp->id;
    $mps[$mp->id]['group'] = $groups[$group_db->id]['id'];
    $p_ar = explode('/',$mp->party);
    $mps[$mp->id]['party'] = $p_ar[count($p_ar)-2];
    $mps[$mp->id]['region'] = $mp->member->division;
    $mps[$mp->id]['last_name'] = $mp->member->second_name;
    $mps[$mp->id]['first_name'] = $mp->member->name;
  }
}
//echo count($mps);
//print_r($mps);

$fout = fopen('es_congreso_mp_vote.csv',"w+");

$vote2vote = array(
  'No' => 'n',
  'Sí' => 'y',
  'Abstención' => 'a',
  'No vota' => 'm'
);

$url = "http://proyectocolibri.es/api/v1/session/?limit=100&offset=0";
$sessions_db = json_decode(file_get_contents($url));

foreach ($sessions_db->objects as $row) {
  foreach ($row->votings as $link) {
    $url = "http://proyectocolibri.es" . $link;
    $division_db = json_decode(file_get_contents($url));
    foreach ($division_db->votes as $v) {
      $m_ar = explode('/',$v->member);
      $mp_id = $m_ar[count($m_ar)-2];
      $division = array(
        'mp_code' => $mp_id,
        'division_code' => $division_db->id,
        'divided_on' => $row->date,
        'vote_kind_code' => $vote2vote[$v->vote],
        'division_name' => $division_db->title,
        'group_name' => $groups[$mps[$mp_id]['group']]['name'],
        'mp_name' => $mps[$mp_id]['last_name'] . ', ' . $mps[$mp_id]['first_name']
      );
      //print_r($division);die();
      fputcsv($fout,$division);
    }
  }
}

fclose($fout);


$time_taken = microtime(true) - $start;
echo "run time:".$time_taken;
?>

motion.js

// Various accessors that specify the four dimensions of data to visualize.
function x(d) { return d.d1; }
function y(d) { return d.d2; }
function z(d) { return d.d3; }
function radius(d) { 
  /*if (("#"+d.id).length > 0) {
  	return 2;
  } else */
    return 1; 
}
function color(d) { return d.color; }
function mid(d) { return d.id; }
function key(d) { return d.id; }
function display(d) { return d.display;}
function mname(d) {return d.name;}


// Chart dimensions.
var margin = {top: 19.5, right: 19.5, bottom: 19.5, left: 39.5},
    width = parameters.width - margin.right,
    height = parameters.height - margin.top - margin.bottom;

// Various scales. These domains make assumptions of data, naturally.
var xScale = d3.scale.linear().domain([parameters.xscale[0], parameters.xscale[1]]).range([0, width]),
    yScale = d3.scale.linear().domain([parameters.yscale[0], parameters.yscale[1]]).range([height, 0]),
    radiusScale = d3.scale.sqrt().domain([0, 1]).range([0, 10]);
    //colorScale = d3.scale.category10();
    var colorScale = d3.scale.category20c();

// The x & y axes.
var xAxis = d3.svg.axis().orient("bottom").scale(xScale).ticks(12, d3.format(",d")),
    yAxis = d3.svg.axis().scale(yScale).orient("left");

// Create the SVG container and set the origin.
var svg = d3.select("#chart svg") //d3.select("#chart").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

// Add the x-axis.
svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

// Add the y-axis.
svg.append("g")
    .attr("class", "y axis")
    .call(yAxis);

// Add an x-axis label.
svg.append("text")
    .attr("class", "x label")
    .attr("text-anchor", "end")
    .attr("x", width)
    .attr("y", height - 6)
    .text("Dimension 1");

// Add a y-axis label.
svg.append("text")
    .attr("class", "y label")
    .attr("text-anchor", "end")
    .attr("y", 6)
    .attr("dy", ".75em")
    .attr("transform", "rotate(-90)")
    .text("Dimension 2");

// Add the year label; the value is set on transition.
tmp_month = Math.ceil((parameters.start - Math.floor(parameters.start))*12);
tmp_year = Math.floor(parameters.start) + '/' + tmp_month;
var label = svg.append("text")
    .attr("class", "year label")
    .attr("text-anchor", "end")
    .attr("y", height - 24)
    .attr("x", width)
    .text(tmp_year);
    
var formatTime = d3.time.format("%e %B");

var div = d3.select("body").append("div")   
    .attr("class", "tooltip")               
    .style("opacity", 0);
    


// Load the data.
d3.json(parameters.data, function(nations) {

  // A bisector since many nation's data is sparsely-defined.
  var bisect = d3.bisector(function(d) { return d[0]; });

  // Add a dot per nation. Initialize the data at 1800, and set the colors.
  var dot = svg.append("g")
      .attr("class", "dots")
    .selectAll(".dot")
      .data(interpolateData(parameters.start))	//here we set up the dots at the beggining
    .enter().append("circle")
      .attr("class", "dot")
      //.attr("id",function(d) {return "dot-"+key(d)})	//wrong!, shall be in position() and interpolateData
      .call(position)
      .call(checkCheckboxes)
      .sort(order)
      .on("mouseover", showTooltip)                
      .on("mouseout", hideTooltip)
      .on("mousedown", switchHighlight);

  /*FF keeps the checkboxes on reload, so we need to check them at the beginning*/
  function checkCheckboxes() {
	  $(".checkbox-mp").each(function(i) {
		a = $(this).attr('id').split('-');
		id = a[a.length-1];
		if ($('#checkbox-h-'+id).prop('checked'))
		   highlight($('#dot-'+id));
	  });
  }    
  
  //switch highligth
  function switchHighlight(d) {
    a = d.id.split('-');
	id = a[a.length-1];
    if ($('#'+d.id).attr("class") == 'dot') {
       highlight($('#'+d.id));
       $("input#checkbox-h-"+id).attr("checked",true).checkboxradio("refresh");
    } else {
      dehighlight($('#'+d.id));
      $("input#checkbox-h-"+id).attr("checked",false).checkboxradio("refresh");
    } 
  }
    
  // show/hide tooltip      
  function showTooltip(d) {      
            div.transition()        
                .duration(200)      
                .style("opacity", .9);      
            div .html(d.name + "<br/>" + d.group)  
                .style("left", (d3.event.pageX) + "px")     
                .style("top", (d3.event.pageY - 28) + "px");
  }

  function hideTooltip(d) {      
            div.transition()        
                .duration(500)      
                .style("opacity", 0)
  }



  var i=0;
  var playing = false;
  $("#play").click(function() {
	  if(playing === false) {
		    startPlaying();
		} else {
			stopPlaying();
		}

  });
  
  function startPlaying() {
  	playing = true;
    $("#playText").html("Stop ||");
    $('#slider').slider('disable');
    // Start a transition that interpolates the data based on year.
    svg.transition()
	  .duration(30000*(parameters.untilchart-parseFloat($('#slider').val()))/(parameters.untilchart-parameters.sincechart+0.0001))
	  .ease("linear")
	  .tween("year", function() {return tweenYear($('#slider').val()) })
	  .each("end", stopPlaying);
  }
  
  function stopPlaying() {
	playing = false;
    $("#playText").html("Play >");
    svg.transition().duration(0);
    $('#slider').slider('enable');
  }


  // Positions the dots based on data.
  function position(dot) {
    dot .attr("cx", function(d) { return xScale(x(d)); })
        .attr("cy", function(d) { return yScale(y(d)); })
        .attr("r", function(d) { return radiusScale(radius(d)) })
        .attr("id", function(d) {return mid(d);})
        .style("fill", function (d) { return gradient(color(d)) })
		.attr("display", function (d) { return display(d);})
		.attr("title", function (d) {return mname(d);});
  }

  // Defines a sort order so that the smallest dots are drawn on top.
  function order(a, b) {
    return radius(b) - radius(a);
  }

  // Tweens the entire chart by first tweening the year, and then the data.
  // For the interpolated data, the dots and label are redrawn.
  function tweenYear(start) {
    //if ((parseFloat(start)+0.01) >= parameters.untilchart) start=parameters.sincechart;
    var year = d3.interpolateNumber(parseFloat(start),parameters.untilchart);
    return function(t) { displayYear(year(t)); };
  }
  
  // Updates the display to show the specified year.
  function displayYear(year) {
    dot.data(interpolateData(year), key).call(position).sort(order);
    month = Math.ceil((year - Math.floor(year))*12);
    label.text(Math.floor(year) + '/' + month);
    i++;
    if ((i%25) == 0) {	//to prevent jumping and for speed of animation -> "25"
      $("#slider").val(year);
      $('#slider').slider('refresh');
    }
  }

  // Interpolates the dataset for the given (fractional) year.
  function interpolateData(year) {
    return nations.map(function(d) {
      return {
        name: d.name,
        id: "dot-"+d.id,
        d1: interpolateValues(d.d1, year),
        d2: interpolateValues(d.d2, year),
        color: findColor(d.color, year, true),
        display: isDisplayed(d.d1, year),
        title: d.name,
        group: findColor(d.color, year, false)
      };
    });
  }

  // Finds (and possibly interpolates) the value for the specified year.
  function interpolateValues(values, year) {
    var i = bisect.left(values, year, 0, values.length - 1),
        a = values[i];
    if (i > 0) {
      var b = values[i - 1],
          t = (year - a[0]) / (b[0] - a[0]);
      return a[1] * (1 - t) + b[1] * t;
    }
    return a[1];
  }
  
  function findColor(values, year, color) {
     var i = bisect.left(values, year, 0, values.length - 1);
     if (color)
       return values[i][2];
     else
       return values[i][1];
  }
  
  function isDisplayed(values, year) {
     if ( (year < values[0][0]) || (year > values[values.length - 1][0]))
       return 'none';
     else 
       return 'inherit';
  }
	
	function highlight(d) {
	  d.attr("class","dot highlighted");
	  //d.addClass("highlight"); we cannot use this, because http://bugs.jquery.com/ticket/10329
	}
	
	function dehighlight(d) {
	  d.attr('class','dot');
	  //d.removeClass("highlight"); we cannot use this, because http://bugs.jquery.com/ticket/10329
	}


	//checkboxes
	$('.checkbox-mp').click (function () {
	  var thisCheck = $(this);
	  a = $(this).attr('id').split('-');
	  id = a[a.length-1];
	  if (thisCheck.is (':checked')) {
		highlight($("#dot-"+id));
	  } else {
	    dehighlight($("#dot-"+id));
	  }
	});

	
	//slider
	//see http://michalskop.tumblr.com/post/37352195911/strange-behaviour-of-jquery-change
	$('#slider').ready(function() {
	  $('#slider').change(function(){
		displayYear($(this).val());
	  });
	});
	


	
	//color gradients
	//http://dexvis.wordpress.com/2012/12/25/motion-charts-revisited/
	function shadeColor(color, percent) {

		var R = parseInt(color.substring(1,3),16)
		var G = parseInt(color.substring(3,5),16)
		var B = parseInt(color.substring(5,7),16);

		R = parseInt(R * (100 + percent) / 100);
		G = parseInt(G * (100 + percent) / 100);
		B = parseInt(B * (100 + percent) / 100);

		R = (R<255)?R:255;  
		G = (G<255)?G:255;  
		B = (B<255)?B:255;  

		var RR = ((R.toString(16).length==1)?"0"+R.toString(16):R.toString(16));
		var GG = ((G.toString(16).length==1)?"0"+G.toString(16):G.toString(16));
		var BB = ((B.toString(16).length==1)?"0"+B.toString(16):B.toString(16));

		return "#"+RR+GG+BB;
	}

	function gradient(baseColor)
	{
	  var gradientId = "gradient" + baseColor.substring(1)
	  console.log("COLOR: " + gradientId);

	  //var lightColor = shadeColor(baseColor, -10)
	  var darkColor = shadeColor(baseColor, -20) 
	  
	  var grad = d3.select("#gradients").selectAll("#" + gradientId)
		.data([ gradientId ])
		.enter()
		.append("radialGradient")
		.attr("id", gradientId)
		.attr("gradientUnits", "objectBoundingBox")
		.attr("fx", "30%")
		.attr("fy", "30%")

	  grad.append("stop")
		.attr("offset", "0%")
		.attr("style", "stop-color:#FFFFFF")
	  
	  // Middle
	  grad.append("stop")
		.attr("offset", "40%")
		.attr("style", "stop-color:" + baseColor)

	  // Outer Edges
	  grad.append("stop")
		.attr("offset", "100%")
		.attr("style", "stop-color:" + darkColor)
	  
	  console.log("url(#" + gradientId + ")")
	  return "url(#" + gradientId + ")";
	}
	
});

parameters.json

{"width":600,
"height":400,
"xscale":[-25,25],
"yscale":[-20,20],
"data":"es_congreso_1h.json",
"start":2012.25,
"sincechart":2012.25,
"untilchart":2013.25
}