// 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 + "
" + 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 + ")"; } });