I'm using a D3 brush to update the arcs of a donut chart, but the update is occurring only after I release the brush. I know that D3 is capable of brush-based continuous updating as Mike has done in this example; what part of that code have I missed in my adaptation?
Here's the function that I currently use to do the updating:
function brushended() {
path.select('.donutPath')
.data(pie([brush.extent()[1],100 - brush.extent()[1]]))
.enter().append("path")
.attr("fill", function(d, i) { return color(i); })
.attr("d", arc);
}
The event you need to listen to is .on("brush",..., like:
var brush = d3.svg.brush()
.x(x)
.extent([0,40])
.on("brush", brushended);
You should change the name of the function to better reflect the situation, but this alone will make it work.
FIDDLE with the listener changes, but also with changes to the way the update selection was being handled.
Related
My implementation for Brush & Zoom functionality in my d3 line chart is not working as expected,
I followed this link - https://bl.ocks.org/EfratVil/92f894ac0ba265192411e73f633a3e2f,
Problems what I am facing is -
chart is not showing all the values, I have 4 data but it only shows 3 data
onClick of dot I am showing the rect which is not moving with the brush functionality
minor thing but chart always goes out of the box
My code sandbox - https://codesandbox.io/s/proud-firefly-xy1py
Can someone point out what I am doing wrong? thanks.
Please suggest me what I am doing wrong, thanks.
Your first point is going behind your clip area. For example, if you right click on the first visible circle and inspect element you will see all 4 circle elements are present in the dom. The first circle element is behind the axis.
This means you have to move your plot to the right. Unfortunately, the way you have coded the chart you have not appended a g element for the main chart and then appended the circles and path to that g element. As a result this has to be done in multiple places.
First we adjust your clip path as:
svg
.append("defs")
.append("SVG:clipPath")
.attr("id", "clip")
.append("SVG:rect")
.attr("width", containerWidth)
.attr("height", height)
.attr("x", 40)
.attr("y", 0);
next we adjust your circles
scatter
.selectAll(".foo")
.data(data)
.enter()
.append("circle")
.attr("class", "foo")
.attr("transform", "translate(40,0)")
and then your line
scatter
.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line)
.attr("transform", "translate(40,0)");
You will have to account for this 40 px translate for your other elements as well. Although I am having a hard time destructuring your svg. I think this should give you the idea though. Check the axis matches the time points as well.
Check the code sand box
Update
To make the rectangles move with the brush, you will have to add code to your brushed const function to recalculate the x, y, width and height using the updated scales.
Update2
After going through the codesandbox presented in the comments I was able to add the code to update the rectangles to the brushed const as below to make the rects also move with the brushing:
// update rectangles
scatter
.selectAll(".rect-elements")
.attr("x", d => {
console.log(d);
return xScale(d.startTime) - 12.5;
})
.attr("y", 0)
.attr("width", 24)
.attr("height", height + 5);
Full working Code Sandbox.
I started from a sample Map app at: http://bl.ocks.org/d3noob/raw/5193723/
I want to place a custom pie chart, as shown in the fig below. I created one by adding the code snippet just after the creation of circles is done.
Pie-chart Snippet:
var r=10;
var p = Math.PI*2;
var arc = d3.svg.arc()
.innerRadius(r-3)
.outerRadius(r)
.startAngle(0)
.endAngle(p* d.value1);
var arc2 = d3.svg.arc()
.innerRadius(r-7)
.outerRadius(r-4)
.startAngle(0)
.endAngle(p* d.value2);
g.append("path")
.attr("d", arc)
.attr("fill", "red")
.attr("transform", "translate(400,500)");
g.append("path")
.attr("d", arc2)
.attr("fill", "orange")
.attr("transform", "translate(400,500)");
It comes out nicely as shown in the pic below near Thailand:
Problem
When I zoom or move the map, the pie-chart disappears but the circles remain intact. Can someone help me understand it?
One can notice a very crude way the arcs are plotted. The pie-chart is expected to be plotted for each of the city. I am looking for a cleaner way just like the way circles are drawn.
The code that runs when zoom takes place
g.selectAll("path")
.attr("d", path.projection(projection));
is selecting all paths and modifying their "d" attribute. Since it's "generically" just looking for pathss, then it's also grabbing the donut paths you created and modifying them (probably setting them to empty strings or NaNs).
You can fix this either by taking the donuts out of the same g of the geo paths, so that they don't get selected. OR, you can make your "path" selector more specific, by adding some class (e.g. "geo") to all the geo paths and using that class whenever you select them (e.g. g.selectAll("path.geo")).
I'm having trouble getting the area portions of a difference chart to properly execute transitions.
As I am still learning, the chart is based on Mike Bostock's Difference Chart example and the transitions were guided by d3noob's transitions post.
Relevant bits:
svgchin.select("#clip-above-chin path")
.duration(750)
.attr("d", area.y0(0));
svgchin.select("#clip-below-chin path")
.duration(750)
.attr("d", area.y0(height));
svgchin.select(".area.above")
.duration(750)
.attr("d", area.y0(function(d) { return y(d["Post"]); }));
svgchin.select(".area.below")
.duration(750)
.attr("d", area);
Full jsFiddle here: http://jsfiddle.net/uxb3yq9g/6/
As you can see, the lines and axes update as intended. The areas, however, are not yet on board.
Any ideas?
You haven't actually updated the data bound to the elements. Just do
var svgchin = d3.select("#chinook").datum(data).transition();
and everything works fine. Complete demo here.
I can successfully display some points on a openlayers basemap using d3.js however I want to actually display icons (at the moment maki png icons) instead of an svg point.
is it possible to load a png/jpg/svg image to a map using d3.js?
So far I can load the icon directly onto the svg but all the markers locate on the same spot so I think I'm having a problem converting the coordinates properly.
var feature = svg.selectAll("path")
.data(amenities.features)
.enter()
.append("svg:image")
.attr("xlink:href", "maki/renders/post-18#2x.png")
.attr("x", function(d, i) {return amenities.features[i].geometry.coordinates[0]})
.attr("y", function(d, i) {return amenities.features[i].geometry.coordinates[1]})
.attr("width", "20")
.attr("height", "20")
.attr("class", "amenity");
Previously I have been able to create an svg with image background inside it using a 'pattern' to show the image so that is also a possibility but I couldn't translate the code to use it with the geographic aspect of d3.
I know I'm writing the icons at the moment to the 'svg' div, so they don't transform correctly when I zoom in and out. I'm aiming to write the images to the 'g' div, as in have code like:
var feature = g.selectAll("path")
but when I use this line, the elements appear on the document but the icons don't actually render on the map.
Thanks in advance.
There a few issues here. First, I'm not sure you fully grasp how d3 selections works, as indicated by the fact that you are binding amenities.features to your selection and then accessing it for the x and y attributes via an index. See How Selections Work for more details on this. In addition, you need to translate the geographic coordinates of the features to screen coordinates by passing them through your geographic projection function. This should get you close:
// more projections: https://github.com/d3/d3-geo-projection/
var projection = d3.geoAlbers();
var amenities = svg.selectAll('.amenities')
.data(amenities.features);
amenities.enter().append('image');
amenities
.attr("class", "amenities")
.attr("xlink:href", "maki/renders/post-18#2x.png")
// The data is already bound so use it instead of the index. Als,
// you need to translate geo coordinates to screen coordinates by
// passing them through your projection function.
.attr("x", function(d,i) {return projection(d.geometry.coordinates)[0];})
.attr("y", function(d,i) {return projection(d.geometry.coordinates)[1];})
.attr("width", "20")
.attr("height", "20")
I don't think I appropriately used groups but I think the key is having the transform then translate thing in there.
So my example is http://bl.ocks.org/mpmckenna8/b87df1c44243aa1575cb.
But because I didn't use groups properly I don't know that the icons would handle zooming like you seem to want. In my example I just append the images to the circles I've created.
.attr("transform", function(d) { return "translate(" + projection(d.geometry.coordinates) + ")"; })
.attr('opacity',.3)
.attr('fill', '#fad959')
I have a doughnut chart and I wanted to have an svg circle that serves as a button and scale the doughnut chart down as well as the circle in the middle, how to I target the the other element when I.
nav.on("click", function(d){
nav.transition()
.duration(1000)
.ease("elastic")
.attr("r", 60);
});
Lastly, is there a shorter way to do what I have done so far, here is the fiddle
Well, you simply run the code in the handler that affects the other elements, e.g.
nav.on("click", function(d){
nav.transition()
.duration(1000)
.ease("elastic")
.attr("r", 60);
arc.outerRadius(radius/2);
chart.transition().duration(1000).ease("elastic").attr("d", arc);
});
Jsfiddle here. The code itself looks fine to me.