D3v4 zoom coordinates of visible area - zooming

I'm trying to calculate the coordinates of the visible area after zooming. Is there a way to calculate it using d3.event.transform?
I tried a lot but couldn't make it work, at least not for d3v4. Using v3 is no opinion since everything else of the project is using v4.
Isolated Code: https://jsfiddle.net/qyvnhvmj/

All you need to do is grab the inverse of the points from the transform,
var width = 800,
height = 400,
Radius = 20;
var Circles, Data = [];
for (var i = 0; i < 3; i++) {
Data.push({
x: 100 + i * Radius * 8,
y: 100,
});
}
var zoom = d3.zoom().scaleExtent([1, 128]).on("zoom", zoomed);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.call(zoom);
svg.append("rect")
.attr("class", "overlay")
.attr("width", width)
.attr("height", height)
.style("fill", d3.color("gray"))
.attr("opacity", 0.1);
Circles = svg.selectAll("circle")
.data(Data)
.enter().append("circle")
.attr("cx", function(d) {
return d.x;
})
.attr("cy", function(d) {
return d.y;
})
.attr("r", Radius);
function getVisibleArea(t) {
var l = t.invert([0, 0]),
r = t.invert([width, height]);
return Math.trunc(l[0]) + " x " + Math.trunc(l[1]) + " - " + Math.trunc(r[0]) + " x " + Math.trunc(r[1]);
}
function zoomed(d) {
Circles.attr("transform", d3.event.transform);
console.log("zoom transform: ", d3.event.transform);
d3.select("#area span").text(getVisibleArea(d3.event.transform));
}
svg {
position: absolute;
top: 50;
}
<svg width="800" height="400"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<a id="area">visible area: <span>?</span></a>
<br>
<br>

Related

How to Add Multiline Text Tooltips in Bar Chart D3 JS

I am looking a way how to add multiline text in one box tooltips using d3.v3.min.js . My illustration behavior like this :
And the code that I made is like this :
HTML, CSS, Javascript
function responsivefy(svg) {
// get container + svg aspect ratio
var container = d3.select(svg.node().parentNode),
width = parseInt(svg.style("width")),
height = parseInt(svg.style("height")),
aspect = width / height;
// add viewBox and preserveAspectRatio properties,
// and call resize so that svg resizes on inital page load
svg.attr("viewBox", "0 0 " + width + " " + height)
.attr("perserveAspectRatio", "xMinYMid")
.call(resize);
d3.select(window).on("resize." + container.attr("id"), resize);
// get width of container and resize svg to fit it
function resize() {
var targetWidth = parseInt(container.style("width"));
svg.attr("height", Math.round(targetWidth / aspect));
svg.attr("width", targetWidth);
}
}
var arrData = [
{"category":"Diversity & Inlusion 1", "actual":4.2, "target":5, "prediction":40, "skala":"20%"},
{"category":"Image 1", "actual":4.5, "target":4.2,"prediction":60, "skala":"40%"},
{"category":"Image 2", "actual":4.1, "target":4,"prediction":80, "skala":"60%"},
{"category":"Job Security 1", "actual":4.4, "target":4.3,"prediction":60, "skala":"100%"},
{"category":"Job Security 2", "actual":4.4, "target":4.3,"prediction":40, "skala":"100%"},
{"category":"Job Security 3", "actual":4.4, "target":4.3,"prediction":20, "skala":"100%"},
{"category":"Image 3", "actual":4.4, "target":4.3,"prediction":10, "skala":"100%"},
{"category":"Diversity & Inlusion 2", "actual":4.4, "target":4.3,"prediction":30, "skala":"100%"},
{"category":"Values", "actual":4.4, "target":4.3,"prediction":75, "skala":"100%"},
{"category":"Collaboration", "actual":4.4, "target":4.3,"prediction":45, "skala":"100%"}
];
//console.log(arrData);
//set up svg using margin conventions - we'll need plenty of room on the left for labels
var margin = {
top: 15, right: 95, bottom: 15, left: 60
};
var marginBar2 = {
top:10
};
var width = 650 - margin.left - margin.right;
var height = 768 - margin.top - margin.bottom;
var svgColor = "white";
var barHeight = 200;
//Create Main SVG
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("background-color", svgColor);
// .call(responsivefy);
//Create xScale and yScale
var xScale1 = d3.scale.linear()
.range([0, width * 0.70])
.domain([0, d3.max(arrData, function (d) {
return d.prediction;
})]);
var xScale2 = d3.scale.linear()
.range([0, width * 0.70])
.domain([0, d3.max(arrData, function (d) {
return d.target;
})]);
var xScale3 = d3.scale.ordinal()
.rangeRoundBands([0, width * 0.93])
.domain(arrData.map(function (d) {
return d.skala;
}));
var yScale = d3.scale.ordinal()
.rangeRoundBands([0, height])
.domain(arrData.map(function (d) {
return d.category;
}));
//Make yAxis to Show Category
var yAxis = d3.svg.axis()
.scale(yScale)
.tickSize(0)
.orient("left");
var gyAxis = svg.append("g")
//.attr("class", "y axis")
.attr("transform", "translate(" + width * 0.35 + "," + height * 0 + ")")
.style("text-anchor", "start")
.style("font-size", 12)
.style("font-weight", "bold")
.style("fill", "#212121")
.call(yAxis);
//Make xAxis to Show Category
// var xAxis = d3.svg.axis()
// .scale(xScale3)
// .tickSize(5)
// .orient("bottom");
// var gxAxis = svg.append("g")
// .attr("class", "x axis")
// .style("font-size", 14)
// .style("font-weight", "bold")
// .style("fill", "#212121")
// .attr("transform", "translate(" + width * 0.385 + "," + height * 1 + ")")
// .call(xAxis);
var bars1 = svg.selectAll(".bar")
.data(arrData).enter()
.append("g")
.attr("transform", "translate(" + width * 0.50 + "," + 0 + ")");
bars1.append("rect")
.attr("class", "bar")
.attr("y", function (d) {
return yScale(d.category);
})
.attr("height", barHeight - 140)
.attr("x", 0)
// .style("fill", "#c0c0c0")
.attr("width", function(d){
return xScale2(d.target);
})
.on("mouseover", function(){
tooltip1.style("display", null);
})
.on("mouseout", function(){
tooltip1.style("display", "none");
})
.on("mousemove", function(d){
var xPos = d3.mouse(this)[0] - 10;
var yPos = d3.mouse(this)[1] - 15;
tooltip1.attr("transform", "translate(" + xPos + "," + yPos +")");
tooltip1.selectAll("text").text("DI4");
tooltip1.selectAll("rect")
.attr("width", 250)
.attr("width", (function(d) {
return this.parentNode.getBBox().width;
}));
});
var bars2 = svg.selectAll(".bar2nd")
.data(arrData).enter()
.append("g")
.attr("transform", "translate(" + width * 0.50 + "," + marginBar2.top + ")");
bars2.append("rect")
.attr("class", "bar2nd")
.attr("y", function (d) {
return yScale(d.category);
})
.attr("height", barHeight - 160)
.attr("x", 0)
.attr("width", function (d) {
return xScale1(d.prediction);
})
.on("mouseover", function(){
tooltip2.style("display", null);
})
.on("mouseout", function(){
tooltip2.style("display", "none");
})
.on("mousemove", function(d){
var xPos = d3.mouse(this)[0] - 10;
var yPos = d3.mouse(this)[1] - 15;
tooltip2.attr("transform", "translate(" + xPos + "," + yPos +")");
tooltip2.selectAll("text").html("DI3");
tooltip2.selectAll("rect")
.attr("width", 250)
.attr("width", (function(d) {
return this.parentNode.getBBox().width;
}));
});
var barValue1 = svg.selectAll(".barValue1")
.data(arrData).enter()
.append("g")
.attr("transform", "translate(" + width * 0.40 + "," + height * 0.055 + ")");
barValue1.append("text")
.attr("class", "barValue1")
.attr("y", function (d) {
return yScale(d.category);
})
.attr("x", 0)
.style("font-size", 18)
.style("font-weight", "bold")
.style("fill", "#212121")
.html(function(d){
return d3.format(",.2r")(d.actual);
});
var barValue2 = svg.selectAll(".barValue2")
.data(arrData).enter()
.append("g")
.attr("transform", "translate(" + width * 0.51 + "," + height * 0.055 + ")");
barValue2.append("text")
.attr("class", "barValue2")
.attr("y", function (d) {
return yScale(d.category);
})
.attr("x", 0)
.style("font-size", 18)
// .style("font-weight", "bold")
.style("fill", "white")
.text(function(d){
return d3.format(".2%")(d.prediction / 100);
});
var tooltip1 = svg.selectAll("g.tooltip1")
.data(arrData)
.enter()
.append("g")
.attr("class", "tooltip")
.style("display", "none");
tooltip1.append("rect")
// .attr("width", width * 0.7)
.attr("height", height * 0.2)
.style("fill", "white")
.style("stroke", "#969696")
.attr("stroke-width",1)
.style("opacity", 1);
tooltip1.append("text")
.attr("id", "txt1")
.attr("x", width * 0.05)
.attr("dy", height * 0.05)
.attr("font-size", 18)
.attr("font-weight", "bold");
var tooltip2 = svg.selectAll("g.tooltip1")
.data(arrData)
.enter()
.append("g")
.attr("class", "tooltip")
.style("display", "none");
tooltip2.append("rect")
// .attr("width", width * 0.7)
.attr("height", height * 0.2)
.style("fill", "white")
.style("stroke", "#969696")
.attr("stroke-width",1)
.style("opacity", 1);
tooltip2.append("text")
.attr("id", "txt1")
.attr("x", width * 0.05)
.attr("dy", height * 0.05)
.attr("font-size", 18)
.attr("font-weight", "bold");
body{
font-family: "Arial", sans-serif;
}
.bar{
fill: #c0c0c0;
}
.bar:hover{
fill:rgb(95, 109, 148);
}
.bar2nd{
fill:#00315b;
}
.bar2nd:hover{
fill:#3e73b8;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
is there anyone could help about my problem ? cause I need to get the code for multiline text inside tooltips box.
It might be more straightforward to append the tooltips using divs rather than svg rects - for instance, if you added:
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
Into your code, then you could make it appear and disappear plus add multi-line text using:
.on("mouseover", function(d){
var xPos = d3.mouse(this)[0];
var yPos = d3.mouse(this)[1];
tooltip.style("left", xPos + "px")
.style("top", yPos + "px")
.html("<p class='tooltip'>" + d.category + "<br><br>Line 2<br><br>Etc</p>")
.transition().delay(200).style("opacity", 0.9);
})
.on("mouseout", function(){
tooltip.transition().delay(0).style("opacity", 0);
})
You can add the same styling you had before using CSS properties, as below:
div.tooltip {
width: 200px;
height: 125px;
background-color: white;
border: 1px solid #969696;
position: absolute;
}
p.tooltip {
font-size: 18;
font-weight: 1em;
padding-left: 10px;
}
I've mocked this up in a JSFiddle here - it might have some of your code missing, apologies if so - but it should give a good idea how to merge it in if you want to use the code. Let me know if you have any questions! You should be able to get some more information from here if you need it too.

Adding transition Image

I am trying to make a d3 visual through block builder.
I want the picture of tourdefrance to appear first in the whole container and then fade away and then the bars to appear.
I know I have made it a little complicated by copying from various sources.
Would be great if someone could help out and also explain the problem.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.bar {
fill: steelblue;
}
.bar:hover {
fill: gold;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// Mike Bostock "margin conventions"
var margin = {top: 10, right: 20, bottom: -500, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// D3 scales = just math
// x is a function that transforms from "domain" (data) into "range" (usual pixels)
// domain gets set after the data loads
var x = d3.scale.ordinal()
.rangeRoundBands([0, width], .1);
var y = d3.scale.linear()
.range([height, 0]);
// D3 Axis - renders a d3 scale in SVG
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(10, "%");
// create an SVG element (appended to body)
// set size
// add a "g" element (think "group")
// annoying d3 gotcha - the 'svg' variable here is a 'g' element
// the final line sets the transform on <g>, not on <svg>
body = d3.select("body");
show_image("https://upload.wikimedia.org/wikipedia/en/e/eb/Tour_de_France_logo.svg")
function show_image(source) {
var img = body.append("img").attr("src", source).style("opacity", 0)
; img.transition().duration(5000).ease(d3.easeLinear).style("opacity", 1)
}
var svg = d3.select("body").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 + ")");
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
svg.append("g")
.attr("class", "y axis")
.append("text") // just for the title (ticks are automatic)
.attr("transform", "rotate(-90)") // rotate the text!
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Frequency");
// d3.tsv is a wrapper around XMLHTTPRequest, returns array of arrays (?) for a TSV file
// type function transforms strings to numbers, dates, etc.
d3.csv("https://gist.githubusercontent.com/vaibhavjaitly/1418c9beb1c71f6acffdaf21669e1956/raw/45af8ebcc87d6641eee8b4a60b7bafb8326607a0/data.csv", type, function(error, data) {
replay(data);
});
function type(d) {
// + coerces to a Number from a String (or anything)
d.Frequency = +d.Frequency;
return d;
}
function replay(data) {
var slices = [];
for (var i = 0; i < data.length; i++) {
slices.push(data.slice(0, i+1));
}
slices.forEach(function(slice, index){
setTimeout(function(){
draw(slice);
}, index * 300);
});
}
function draw(data) {
// measure the domain (for x, unique letters) (for y [0,maxFrequency])
// now the scales are finished and usable
x.domain(data.map(function(d) { return d.Letter; }));
y.domain([0, d3.max(data, function(d) { return d.Frequency; })]);
// another g element, this time to move the origin to the bottom of the svg element
// someSelection.call(thing) is roughly equivalent to thing(someSelection[i])
// for everything in the selection\
// the end result is g populated with text and lines!
svg.select(".x.axis").transition().duration(300).call(xAxis);
// same for yAxis but with more transform and a title
svg.select(".y.axis").transition().duration(300).call(yAxis)
// THIS IS THE ACTUAL WORK!
var bars = svg.selectAll(".bar").data(data, function(d) { return d.Letter; }) // (data) is an array/iterable thing, second argument is an ID generator function
bars.exit()
.transition()
.duration(300)
.attr("y", y(0))
.attr("height", height - y(0))
.style('fill-opacity', 0.00001024)
.remove();
// data that needs DOM = enter() (a set/selection, not an event!)
bars.enter().append("rect")
.attr("class", "bar")
.attr("y", y(0))
.attr("height", height - y(0));
// the "UPDATE" set:
bars.transition().duration(300).attr("x", function(d) { return x(d.Letter); }) // (d) is one item from the data array, x is the scale object from above
.attr("width", x.rangeBand()) // constant, so no callback function(d) here
.attr("y", function(d) { return y(d.Frequency); })
.attr("height", function(d) { return height - y(d.Frequency); }); // flip the height, because y's domain is bottom up, but SVG renders top down
}

Trying to draw a bar chart in D3 and I am not seeing the output [duplicate]

This question already has an answer here:
Cannot use attr with an object in D3 v4
(1 answer)
Closed 4 years ago.
I am learning D3 and trying to create a bar chart. I dont know why there is no output.
var w = 300;
var h = 100;
var padding = 2;
var dataset = [5, 10, 20, 12, 19];
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr({
x: function(d, i) {
return i * w / dataset.length;
},
y: function(d) {
return h - d;
},
width: function(d) {
return (w / dataset.length) - padding;
},
height: function(d) {
return d;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>
The issue is that you are using d3v4, which does not let you set multiple attributes with selection.attr(). This functionality was present in d3v3, but removed starting with d3v4.
With d3v4/5 you can either:
use d3.selection-multi
set attributes one at a time
To keep almost the same code as you have, you can import d3.selection-multi, this allows you to use selection.attrs({}) (not .attr) to set multiple attributes at once:
.attrs({
x: function(d, i) {
return i * w / dataset.length;
},
y: function(d) {
return h - d;
},
width: function(d) {
return (w / dataset.length) - padding;
},
height: function(d) {
return d;
}
})
The module is not included in the base d3 library so you'll need to import it for this approach to work:
var w = 300;
var h = 100;
var padding = 2;
var dataset = [5, 10, 20, 12, 19];
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attrs({
x: function(d, i) {
return i * w / dataset.length;
},
y: function(d) {
return h - d;
},
width: function(d) {
return (w / dataset.length) - padding;
},
height: function(d) {
return d;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>
<script src="https://d3js.org/d3-selection-multi.v1.min.js"></script>
The other option is to use selection.attr() to set one attribute at a time, rather than all at once:
var w = 300;
var h = 100;
var padding = 2;
var dataset = [5, 10, 20, 12, 19];
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x",function(d, i) {
return i * w / dataset.length;
})
.attr("y",function(d) {
return h - d;
})
.attr("width", function(d) {
return (w / dataset.length) - padding;
})
.attr("height",function(d) {
return d;
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>

d3.scale.quantize producing unexpected result, how to debug?

I have a working example of a choropleth that takes data values from .csv file and calculates the color ranges.
There are four possible colors that a region can take, depending on the population.
For application I am trying to develop, I need data to be loaded by server from a database. As a dummy example, I made a route that returns json.
json.features contains the geojson information, and json.features.properties.value - population value.
Here is the function I use. It draws a map correctly, and the population values on tooltips are correct as well. The color domain is [98, 629725]. However there is just one region that takes the very dark color, only four regions that take a little brighter one, and all other regions are taking the other color. If you look at the link I provide at the top - there the distribution of color is more uniform. But in the map made with this code - I have the same color for place with 5000 population and 90000 population.
I'm stuck right now because I don't really see where could the problem come from, and I don't have a good idea on how to investigate it. Can you suggest what would be the first things to check in this situation? Maybe you already see something wrong with this code?
function draw_map (dataroute) {
var w = 500;
var h = 800;
var projection = d3.geo.transverseMercator()
.rotate([-27,-65,0])
.translate([w/2, h/2])
.scale([3500])
var path = d3.geo.path()
.projection(projection);
var color = d3.scale.quantize()
.range(["#c6dbef","#6baed6","#2171b5","#084594"])
.domain([0, 100]);
var svg = d3.select("#map")
.attr("width", w)
.attr("height", h);
svg.append("rect")
.attr("class", "background")
.attr("width", w)
.attr("height", h);
var g = svg.append("g")
queue()
.defer(d3.json, dataroute)
.await(ready);
function ready(error, json) {
if (error) throw error;
color.domain([
d3.min(json.features, function(d) { return d.properties.value; }),
d3.max(json.features, function(d) { return d.properties.value; })
]);
console.log([
d3.min(json.features, function(d) { return d.properties.value; }),
d3.max(json.features, function(d) { return d.properties.value; })
]);
// returns [98, 629725]
g.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d",path)
.style("fill", colormap)
.style("stroke", "#08306b")
.attr("class", "feature")
.on("mouseover", function(d) {
d3.select(this)
.style("fill", "#08306b");
var coordinates = [0, 0];
coordinates = d3.mouse(this);
var target = d3.select("#tooltip")
.style("left", coordinates[0] + "px")
.style("top", coordinates[1]-80 + "px");
target.select("#name")
.text(d.properties.text);
target.select("#stat")
.text(json.statdata_name + ": " + d.properties.value);
d3.select("#tooltip").classed("hidden", false);
})
.on("mouseout", function(d){
// If this is active node, keep the color on mouseout
d3.select(this)
.style("fill", colormap(d));
d3.select("#tooltip").classed("hidden", true);
});
}
//Return color for path
var colormap = function(d) {
var value = d.properties.value;
//console.log(value);
// If value exists, map to color
if (value) {
return color(value);
//console.log(color(value));
// if not, set color red
} else {
console.log("Undefined: " + d.properties.text + " " + value)
return "red";
}
}
}
In the original version that uses .csv file, the code is like this:
//Width and height
var w = 800;
var h = 800;
active = d3.select(null);
previous = d3.select(null);
var projection = d3.geo.transverseMercator()
.rotate([-27,-65,0])
.translate([w/2, h/2])
.scale([3500])
var path = d3.geo.path()
.projection(projection);
var color = d3.scale.quantize()
.range(["#c6dbef","#6baed6","#2171b5","#084594"])
.domain([0, 100]);
var svg = d3.select("#map")
.attr("width", w)
.attr("height", h);
svg.append("rect")
.attr("class", "background")
.attr("width", w)
.attr("height", h)
.on("click", reset);
var g = svg.append("g")
// Click to zoom
function clicked(d) {
// If this node is zoomed, unzoom
if (active.node() === this) {
d3.select(this).style("fill",colormap)
return reset();
} else {
// Otherwise recolor previous one, to avoid contamination
previous.style("fill",colormap)
}
active.classed("active", false);
active = d3.select(this).classed("active", true);
// store previous to uncolor it after clicking new one
previous = d3.select(this)
d3.select(this).style("fill","#08306b")
var bounds = path.bounds(d),
dx = bounds[1][0] - bounds[0][0],
dy = bounds[1][1] - bounds[0][1],
x = (bounds[0][0] + bounds[1][0]) / 2,
y = (bounds[0][1] + bounds[1][1]) / 2,
scale = .3 / Math.max(dx / w, dy / h),
translate = [w / 2 - scale * x, h / 2 - scale * y];
g.transition()
.duration(750)
.style("stroke-width", 1.5 / scale + "px")
.attr("transform", "translate(" + translate + ")scale(" + scale + ")");
}
function reset() {
active.classed("active", false);
active = d3.select(null);
g.transition()
.duration(750)
.style("stroke-width", "1px")
.attr("transform", "");
}
queue()
.defer(d3.json, "/static/finland.geojson")
.defer(d3.csv, "/static/kuntavakiluku.csv")
.await(ready);
var finland_geojson_data
var csv_data
function ready(error, json, data) {
if (error) throw error;
finland_geojson_data = json;
csv_data = data;
color.domain([
d3.min(data, function(d) { return d.Vakiluku; }),
d3.max(data, function(d) { return d.Vakiluku; })
]);
console.log(data.length);
for (var i = 0; i < data.length; i++) {
var dataState = data[i].Kunta;
var dataValue = parseInt(data[i].Vakiluku);
//Find the corresponding state inside the GeoJSON
for (var j = 0; j < json.features.length; j++) {
var jsonState = json.features[j].properties.text;
//console.log(jsonState)
if (dataState == jsonState ) {
//Copy the data value into the JSON
json.features[j].properties.value = dataValue;
//Stop looking through the JSON
break;
}
}
}
g.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d",path)
.style("fill", colormap)
.style("stroke", "#08306b")
.attr("class", "feature")
.on("mouseover", function(d) {
d3.select(this)
.style("fill", "#08306b");
var coordinates = [0, 0];
coordinates = d3.mouse(this);
var target = d3.select("#tooltip")
.style("left", coordinates[0] + "px")
.style("top", coordinates[1]-80 + "px");
target.select("#kunta")
.text(d.properties.text);
target.select("#vakiluku")
.text("Väestö: " + d.properties.value);
d3.select("#tooltip").classed("hidden", false);
})
.on("mouseout", function(d){
// If this is active node, keep the color on mouseout
if (active.node() === this) {
d3.select(this).style("fill","#08306b")
} else {
d3.select(this)
.style("fill", colormap(d));
}
d3.select("#tooltip").classed("hidden", true);
})
.on("click",clicked);
}
//Return color for path
var colormap = function(d) {
var value = d.properties.value;
// If value exists, map to color
if (value) {
return color(value);
// if not, set color red
} else {
console.log("Undefined: " + d.properties.text + " " + value)
return "red";
}
}
Quantized scales are linear scales with discreet ranges (as you can see in the link that #seb provided in the comments).
So in your case, if you provide 4 colors, the domain, figuratively speaking, will be split up in 4 sections (and your scale is a "map" for that).
Thus, the first quarter of the domain will be the first color, etc.
In your expl, the domain is [98, 629725], so the first color will be starting at 98 and end at (629725-98)/4+98 = 157'504.75
In code:
var scale = d3.scale.quantize()
.range(["#c6dbef", "#6baed6", "#2171b5", "#084594"])
.domain([98, 629725]);
So you can test that, e.g. here (check console output!)
You can see that 157504 yields col1, 157505 the second.
So it it no surprise that 5000 and 90000 are in the same "section".

White space between values of a line graph

I've created a line graph where the area under the graph is filled based on the rank of the values from data, but there is white space between data point assignments. I'm using d3.nest() to create sub dataGroups. Is there a better way to go about this that will eliminate the white space?
Here's a Plunker.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 12px Arial;
}
text.shadow {
stroke: #fff;
stroke-width: 2.5px;
opacity: 0.9;
}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
.grid .tick {
stroke: lightgrey;
stroke-opacity: 0.7;
shape-rendering: crispEdges;
}
.grid path {
stroke-width: 0;
}
.area {
stroke-width: 0;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 30, right: 20, bottom: 35, left: 50},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
var parseDate = d3.time.format("%d-%b-%y").parse;
var color = d3.scale.category20();
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(5);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5);
var area = d3.svg.area()
.x(function(d) { return x(d.date); })
.y0(height)
.y1(function(d) { return y(d.close); });
var valueline = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.close); });
var svg = d3.select("body")
.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 + ")");
// function for the x grid lines
function make_x_axis() {
return d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(5)
}
// function for the y grid lines
function make_y_axis() {
return d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5)
}
// Get the data
d3.csv("data.csv", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
var dataGroup = d3.nest()
.key(function(d) {
return d.rank;
})
.entries(data);
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain([0, d3.max(data, function(d) { return d.close; })]);
dataGroup.forEach(function(d, i){
svg.append("path")
.datum(d.values)
.attr("class", "area")
.attr("d", area);
});
svg.selectAll(".area")
.style("fill",function() {
return "hsl(" + Math.random() * 360 + ",100%,50%)";
})
// Draw the x Grid lines
svg.append("g")
.attr("class", "grid")
.attr("transform", "translate(0," + height + ")")
.call(make_x_axis()
.tickSize(-height, 0, 0)
.tickFormat("")
)
// Draw the y Grid lines
svg.append("g")
.attr("class", "grid")
.call(make_y_axis()
.tickSize(-width, 0, 0)
.tickFormat("")
)
// Add the valueline path.
svg.append("path")
.attr("d", valueline(data));
// 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 the text label for the X axis
svg.append("text")
.attr("transform",
"translate(" + (width/2) + " ," +
(height+margin.bottom) + ")")
.style("text-anchor", "middle")
.text("Distance");
// Add the text label for the Y axis
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("x", margin.top - (height / 2))
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("");
// Add the title
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("Elevation Graph");
});
</script>
</body>
Nest is insufficient for this. The problem with nest is that each data point (i.e. each day) belongs to only one subgroup (i.e. element of dataGroup). However, rendering it requires the first day of each group to also be the last day of the preceding group. So you need to get more sophisticated with how you generate dataGroup. At first I was going to suggest using a somewhat elaborate loop in place of .nest() (which you can still try to figure out how to do, if you'd like). However, I realized that you can still use nest just like you have it and then do a simple pass through the resulting groups and append the first element of each group to the group before it:
dataGroup.forEach(function(group, i) {
if(i < dataGroup.length - 1) {
group.values.push(dataGroup[i+1].values[0])
}
})
Here's the updated example.