Adding multiple shapes in d3 - svg

I would like to add different shapes depending on one of the properties in my json file. I found this approach by Mike:
https://groups.google.com/forum/#!topic/d3-js/4EJDu1xOh8Y
The idea is great, I'm just not sure how to adapt it. I either want to add a circle or an svg:use element (with attr("xlink:href")). They both have (of course) different attributes. So how do I do that? What do I append? In the example, the attr("d") was also used, do I need that also?
That's what I have so far but I'm not sure what to add where.
I really appreciate your help!
var type = d3.scale.ordinal()
.domain(["Q", "C"])
.range("circle","svg:use");
for(l=0;l<4;l++){
currentsvg.selectAll("path")
.data(queryArray[l])
.enter()
.append("svg:path")
.type(function(d,i) {
if (queryArray[l][i].name.substr(0,1) == "Q"){
return type("Q");
}
else if (queryArray[l][i].name.substr(0,1) == "C"){
return type("C");
}
});
}

Below is a different solution without filtering that uses the path to draw the shapes. It doesn't use the "rect" or "circle" of svg but rather just uses the path to draw the shapes. Check out here for more on paths. Note that the circle is two connecting arcs. It also classes each shape based on the data so you can have different colors, etc using CSS. Here is a fiddle.
currentsvg.selectAll("path")
.data(data)
.enter()
.append("path")
.attr("d",function(d,i){
var path,
s = i*50,
r = 10,
w = r*2;
if (data[i] == "Q"){
path = "M" + s + " " + s + " L" + s + " " + (s+w) +
" L" + (s+w) + " " + (s+w) + " L" + (s+w) + " " + s + "Z"
}
else if (data[i] == "C"){
path = "M" + s + " " + s + " m" + -r + ", 0 " +
" a " + r + "," + r + " 0 1,0 " + r*2 + ",0" +
" a " + r + "," + r + " 0 1,0 "+ -r*2 + ",0"
}
return path;
})
.attr("class", function(d){return d == "Q" ? "rec" : "circ";})

The best way to do that is to filter the data how you want into separate data sets for each shape before you create shapes. Then you can create the shapes with that new data set.
var data = ["Q","Q","Q","C","C","Q","Q","C","Q","C"];
var circleSet = data.filter(function(d){return d === "Q";}),
squareSet = data.filter(function(d){return d === "C";});
As Lars said, that is also not how the d attribute works. Here is a working JSFiddle of the whole thing.

Related

NodeJS string concatenation unusual behaviour

I am trying to construct this string for printing one message.
"At position #[" + index + "][" + _subIndex + "] TLPHN_DVC_TYP " +
+ _telNum?.TelephoneDeviceType.toString() + " ,allowed " + telephoneDeviceTypeEnum.join(',');
from watch is VsCode:
where index =0;_subIndex =0;telNum.TelephoneDeviceType =Mobile;telephoneEnum=["Mobile","Landline"];
It's returning :
At position #[0][0] TLPHN_DVC_TYP NaN ,allowed Mobile,Landline
Full Code:
if (_telNum?.TelephoneDeviceType && !(telephoneDeviceTypeEnum.indexOf(_telNum.TelephoneDeviceType) > 0)){
console.log( "At position #[" + index + "][" + _subIndex + "] TLPHN_DVC_TYP " +
+ _telNum?.TelephoneDeviceType.toString() + " ,allowed " + telephoneDeviceTypeEnum.join(','));
}
the condition should not satisfy but not sure why it's going inside the if and NaN returning. any suggestion?
It's the two plus signs: "] TLPHN_DVC_TYP " + + _telNum?// ...etc. The second one is parsed as the unary +, or a conversion to number, which obviously fails. Compare:
console.log("foo" + "bar");
console.log("foo" + + "bar");
Added #1:
if (_telNum?.TelephoneDeviceType && !(telephoneDeviceTypeEnum.indexOf(_telNum.TelephoneDeviceType) >= 0)){
console.log( "At position #[" + index + "][" + _subIndex + "] TLPHN_DVC_TYP " +_telNum?.TelephoneDeviceType.toString() + " ,allowed " + telephoneDeviceTypeEnum.join(','));
}

moving a circle along a d3 path animating at varying speeds

I really found this question and answer helpful on how to get a line animate at varying speeds.
Changing speed of D3 path animation
Which pointed to this block:
http://bl.ocks.org/explunit/6082362
I've been following this and would like to add an circle which moves along the start of the line.
I've added a marker
var marker = g.append("circle")
.attr("r", 7)
.attr("id", "marker")
but for the life of me I can't get it to follow along the line, at the same speed.
I've added this bit to the end of that block
var p = path.node().getPointAtLength(lengthAt[i-1] );
markerTransition = markerTransition.transition()
.duration(lineData[i].speed)
.ease('linear')
.attr("transform", "translate(" + p.x + "," + p.y + ")");
and now I have a moving circle, but it's not in sync with the line and is located at different coordinates for some reason.
How can I get my circle to correctly follow along the line at (varying speeds)?
UPDATE:
Almost there!
I've added a jsfiddle: http://jsfiddle.net/mbrownshoes/k86fbade/6/
Circle is moving at the correct speed to first point, now I need the circle to start each transition from the previous point and not from the beginning.
Solved (though going about it another way)
http://jsfiddle.net/mbrownshoes/ozpt6dn7/2/
for (var i = 0; i < lineData.length - 1; ++i) {
wait[i] = tottime;
tottime += lineData[i].t;
setTimeout(function () {
temp[0] = lineData[ipath];
temp[1] = lineData[ipath + 1];
time = lineData[ipath].t;
var lineGraph = ss.append("path")
.attr("d", lineFunction(temp))
.attr("stroke", "grey")
.attr("stroke-width", 3)
.attr("fill", "none");
var totalLength = lineGraph.node().getTotalLength();
console.log(totalLength);
console.log(ipath + " " + temp[0].x + " " + temp[1].x + " " + time);
lineGraph.attr("stroke-dasharray", totalLength + " " + totalLength)
.attr("stroke-dashoffset", totalLength)
.transition()
.duration(time)
.ease("linear")
.attr("stroke-dashoffset", 0);
circle.transition()
.duration(time)
.ease("linear")
.attr("transform",
function () {
return "translate(" + temp[1].x + "," + temp[1].y + ")";
});
console.log(ipath + ": " + time + ", " + wait);
ipath++;
}, wait[i]);
}
Thanks to https://groups.google.com/forum/m/#!topic/d3-js/UhaN7HdYTWM

How to go to next line while using a loop to setText in JTextArea?

This is my code
for (int m=0; m < i ; m++){
ta1.setText( s[m].getName().toString() + ", " + s[m].getProgramName().toString() + ", " + s[m].getUni1() + ", " + s[m].getUni2() + ", " + s[m].getUni3() + ", " );
}
It's supposed to print a line from an array of student ( called s) into a JTextArea ( called ta1 ). the problem is that it always only prints the last student in the array.
I need to print each student in a new line. could anyone help me sort it out?
When you set text on an element, the current position in the loop will take over the last one.
Try doing this.
String s = "";
for(int m = 0, m <i; m++){
s += s[m].getName.toString() + ", " + s[m].getprogramName().toString() + "\n;
}
ta1.setText(s);
Create a string and add each entry to it then add new line to end of each entry "\n"
Then do.
ta1.setText(s);
setText overwrites whatever is the current text.
You need append instead; you also need a "\n" at the end of a line.

Drawing a portion of an ellipse programmatically with a Bezier or an Elliptical path — SVG and raphael.js

I'm trying to draw a bezier curve surrounding an ellipse with a given margin :
I want to achieve this programmatically, so if I changes the ellipse size, the curve will follow it.
At the moment I've made this function :
function bezierPathTopRounded(ellipse, margin) {
var box = ellipse.paper.getBBox();
var leftX = box.x - margin;
var rightX = box.x + margin + box.width;
var y = box.y + box.height/2 - margin;
var p = "M "+ leftX + ", "+ y
+ " C " //could be relative too
+ ( box.x - margin + (box.width/15) ) + ", " + ( box.y + margin - (box.height/4.5) ) + " "
+ ( box.x + margin + box.width - (box.width/15) ) + ", " + ( box.y + margin - (box.height/4.5) ) + " "
+ rightX +", " + y;
return p;
}
But I can't figure out how to calculate this direction points values so that it will work with any ellipse :
box.width/15
box.height/4.5
There is a fiddle with this example.
I've read this stackoverflow question and I tried the same on my example, but still can't figure out a simple solution, it remains random...
Edit
Now I'm trying with an elliptical Arc, the result is worser than with a Bezier Path :
There is the function I'm using. If I remove the margin it follows exactly my ellipse... Finally this is the matter is how may I follow the ellipse with a margin ?
function borderPath(ellipse, margin, flag) {
var flag = flag == undefined ? 1 : 0;
var box = ellipse.paper.getBBox();
var leftX = box.x - margin;
var rightX = box.x + margin + box.width;
var y = box.y + box.height/2;
y += (flag == 1) ? -margin : margin;
var rx = box.width/2 + margin;
var ry = box.height/2;
var p = "M "+ leftX + ", "+ y
+ " A "
+ rx + " " + ry
+ " 0 0 "+ flag +" "
+ rightX +", " + y;
return p;
}
See the updated fiddle here.
Really sorry for the awful colors, those are for example purpose.
If you want to do this with Bezier curves, you'll have to decide how "wrong" you want it to look. Bezier curves cannot represent circular (and by extension, elliptical) curves, they can only get really close, in the same way a polygon can, just with higher precision using fewer sections.
I describe both circle-approximation and curve offsetting using Bezier curves in my primer on Bezier curves, http://pomax.github.io/bezierinfo/#circles_cubic and http://pomax.github.io/bezierinfo/#offsetting respectively, but if you're coding this from scratch particularly the offsetting will be overkill if you only need it for what you describe in your example.
Instead, I'd recommend firing up Inkscape or Illustrator, turning on the grid overlay, and drawing a bezier curve around your ellipse. Make it look good, then check what the coordinates are, and use that as reliable-enough information for implementing in your canvas program. You probably don't need mathematically rigidly correct, as long as people don't go "that looks wrong", you should be just fine.
I've manage to make an elliptical arc according to the ellipse and its margin.
Than i'm simply hiding the part I don't want with a rectangle.
Here is the function :
function borderPath(ellipse, flag) {
var flag = flag == undefined ? 1 : flag;
var box = ellipse.paper.getBBox();
var leftX = box.x;
var rightX = box.x + box.width;
var y = box.y + box.height/2;
var rx = box.width/2;
var ry = box.height/2;
var p = "M "+ leftX + ", "+ y
+ " A "
+ rx + " " + ry
+ " 0 0 "+ flag +" "
+ rightX +", " + y;
return p;
}
Using bezier curves to draw elliptical path may cause you headaches. As you said in a comment, you are using path arc which works well with RaphaelJS.
Documentation about all the values it expects, especially the flags, can be found at http://www.svgbasics.com/arcs.html .

How do I adjust my SVG transform based on the viewport?

I'm working with the d3 library and have had success working with the chloropleth example, as well as getting a click action to zoom in to a particular state (see this question for details). In particular, here is the code I'm using for my click to zoom event on a state:
// Since height is smaller than width,
var baseWidth = 564;
var baseHeight = 400;
d3.selectAll('#states path')
.on('click', function(d) {
// getBBox() is a native SVG element method
var bbox = this.getBBox(),
centroid = [bbox.x + bbox.width/2, bbox.y + bbox.height/2],
// since height is smaller than width, I scale based off of it.
zoomScaleFactor = baseHeight / bbox.height,
zoomX = -centroid[0],
zoomY = -centroid[1];
// set a transform on the parent group element
d3.select('#states')
.attr("transform", "scale(" + scaleFactor + ")" +
"translate(" + zoomX + "," + zoomY + ")");
});
However, when I click to view on the state, my transform is not in the center of my viewport, but off to the top left, and it might not have the proper scale to it as well. If I make minor adjustments manually to the scaleFactor or zoomX/zoomY parameters, I lose the item altogether. I'm familiar with the concept that doing a scale and transform together can have significantly different results, so I'm not sure how to adjust.
The only other thing I can think of is that the original chloropleth image is set for a 960 x 500 image. To accomodate for this. I create an albersUSA projection and use my d3.geo.path with this projection and continue to add my paths accordingly.
Is my transform being affected by the projection? How would I accomodate for it if it was?
The scale transform needs to be handled like a rotate transform (without the optional cx,cy parameters), that is, the object you want to transform must first be moved to the origin.
d3.select('#states')
.attr("transform",
"translate(" + (-zoomX) + "," + (-zoomY) + ")" +
"scale(" + scaleFactor + ")" +
"translate(" + zoomX + "," + zoomY + ")");
For futher reference,
I found this article where you should find how to use the matrix transformation to achieve zoom and pan effects very simple.
Excerption:
<script type="text/ecmascript">
<![CDATA[
var transMatrix = [1,0,0,1,0,0];
function init(evt)
{
if ( window.svgDocument == null )
{
svgDoc = evt.target.ownerDocument;
}
mapMatrix = svgDoc.getElementById("map-matrix");
width = evt.target.getAttributeNS(null, "width");
height = evt.target.getAttributeNS(null, "height");
}
]]>
</script>
function pan(dx, dy)
{
transMatrix[4] += dx;
transMatrix[5] += dy;
newMatrix = "matrix(" + transMatrix.join(' ') + ")";
mapMatrix.setAttributeNS(null, "transform", newMatrix);
}
function zoom(scale)
{
for (var i=0; i<transMatrix.length; i++)
{
transMatrix[i] *= scale;
}
transMatrix[4] += (1-scale)*width/2;
transMatrix[5] += (1-scale)*height/2;
newMatrix = "matrix(" + transMatrix.join(' ') + ")";
mapMatrix.setAttributeNS(null, "transform", newMatrix);
}

Resources