transitioning lines in d3.js - transition

Sorry if this question seems stupid, but it's stumped me for a couple days, so I would really appreciate any simple input from anyone! :)
I'm trying to construct a line graph in d3.js that transitions between different datasets. However, whenever I hit the transition function, it selects the original line on the graph, AND the axes of the graph, and transitions all of it. I tried to do selectAll("svg:path") instead of selectAll("path") but it just returned a DOMException 12, indicating that the selection didn't exist.
//beginning of code to append axis to graph
graph.append("svg:g").attr("class", "x axis")
//draws the original line on the graph
graph.append("svg:path").attr("d", line(dataset1));
//line function
var line = d3.svg.line()
.x(function(d) {return x(d.age);})
.y(function(d) {return y(d.freq);})
.interpolate("basis");
//transition function
function transition(newData) {
graph.selectAll("path")
.data(newData)
.transition()
.duration(2000)
.attr("d", line(newData));
}
Thanks for any responses! :)

Just give the thing you want to change a unique class/identifier, e.g.
graph.append("svg:path").attr("d", line(dataset1)).classed("line", true);
...
graph.selectAll("path.line")
.data(newData)
...

Related

Is there a way to get the ECharts series symbol to be passed in as the marker in the tooltip

I've got an ECharts chart that has multiple line series and a scatter series that represents events. For the line series, we left the symbol at the default, but for the event scatter series we set the symbol to 'diamond'. These symbols show up on the chart & in the series legend as expected. However, the tooltip always shows a 10 pixel dot for the marker. The color however is picked up from the series (and even from the visualMap config!). My initial assumption had been that the symbol would also be picked up from the series. In our case we wanted to use a function for tooltip.formatter and even in that function we couldn't figure out how to access and swap in the series symbol for the marker. We ended up working around it by just custom styling our own html to plug in for the marker that matched the diamond symbol pretty well.
So the open questions are:
Is there a way to have the tooltip marker pick up the symbol from the series?
If not, is this a bug? I'm happy to put in an issue on the project but I don't want to do that until I understand a bit more.
Here's some code to illustrate what we had to do to get the diamond in there in case it helps the discussion or if others want to leverage this workaround:
formatTooltip(args){
let time = DateTime.fromISO(args[0].data[0], { zone: this.user.timeZone })
let tooltip = `<div><b>${time.toFormat(TOOLTIP_FORMAT)}</b></div>`
args.forEach(({ marker, seriesName, value }) => {
if (seriesName === 'Events'){
let myMarker = `<span style="display:inline-block;margin-right:4px;width:10px;height:10px;background:${value[4]};transform:rotate(45deg);"></span>`
tooltip += `<div>${myMarker} ${value[3] ? value[3]: ''}</div>`
} else {
value = value || [0, 0]
tooltip += `<div>${marker} ${seriesName}: ${value[1]}</div>`
}
})
return tooltip
},

Making a support structure "manifold" in openscad

I am trying to build a support structure around a cylinder in openscad, but I cannot seem to make the angled part of the structure "manifold"
inner_slide_tube_inner_radius=14.9/2;
leadpipe_wall_thickness=14.9/2;
leadpipe_length=200;
mouthpiece_receiver_large_radius=0.546*25.4/2;
NoSpokes = 4;
SpokesWide = 3;
SpokesHigh = 3;
SpokesLong = leadpipe_length/2*0.75;
SpokesLong2 = leadpipe_length/2;
//if I comment out this section, then I can render a single support angle part when NoSpokes=1
for (i=[1:NoSpokes])
rotate([0,0,360/NoSpokes*i])
translate([mouthpiece_receiver_large_radius+leadpipe_wall_thickness,-SpokesWide/2,0])
cube([SpokesLong, SpokesWide, SpokesHigh]);
//
for (i=[1:NoSpokes])
rotate([0,0,360/NoSpokes*i])polyhedron(
points=[
[mouthpiece_receiver_large_radius+SpokesLong+leadpipe_wall_thickness-SpokesHigh, -SpokesWide/2, SpokesHigh],
[mouthpiece_receiver_large_radius+SpokesLong+leadpipe_wall_thickness-SpokesHigh, SpokesWide/2, SpokesHigh],
[inner_slide_tube_inner_radius, SpokesWide/2, SpokesLong2],
[inner_slide_tube_inner_radius, -SpokesWide/2, SpokesLong2],
[mouthpiece_receiver_large_radius+SpokesLong+leadpipe_wall_thickness, -SpokesWide/2, SpokesHigh],
[mouthpiece_receiver_large_radius+SpokesLong+leadpipe_wall_thickness, SpokesWide/2, SpokesHigh],
[inner_slide_tube_inner_radius, SpokesWide/2, SpokesLong2+SpokesHigh],
[inner_slide_tube_inner_radius, -SpokesWide/2, SpokesLong2+SpokesHigh]],
faces=[[1,0,3,2],
[1,5,4,0],
[2,3,7,6],
[1,5,6,2],
[0,4,7,3],
[4,5,6,7]
]);
I know that this is really naive question, but I am rather stuck as I keep getting the warning WARNING: Object may not be a valid 2-manifold and may need repair!
Any help would be greatly appreciated to get rid of the warning.
The reason your design is not manifold is that some of your polygon don't have the correct winding order. In OpenSCAD, if you preview your design using F2 (Thrown Together), such wrongly winded polygons will be highlighted in pink.

Physicsjs Screen wrap

I am currently having a bit of trouble making objects in my world wrap. It sort of works, but very often objets appear to get stuck on the boundaries. My wrap code is as follows:
// Wrap our position if we are outside of the world bounds
if (this.state.pos.get(0) > 860) {
this.state.pos.set(0, this.state.pos.get(1));
}
else if (this.state.pos.get(0) < 0) {
this.state.pos.set(860, this.state.pos.get(1));
}
if (this.state.pos.get(1) > 640) {
this.state.pos.set(this.state.pos.get(0), 0);
}
else if (this.state.pos.get(1) < 0) {
this.state.pos.set(this.state.pos.get(0), 640);
}
Is there a better way of doing this? Should I use a translation on the object's position vector rather than simply setting it?
Without a jsfiddle it's a bit hard to diagnose, however this might be due to the this.state.old.pos not being set too. If the position (only) is set, then the velocity will be calculated as the difference between the current and the previous positions (in accordance with verlet integration). In that case, you're implicitly giving the body a huge velocity.
I'd recommend adding/subtracting an amount rather than setting, then you can do the same with the old position.
Here's a working example: http://labs.minutelabs.io/Relativistic-Space-Sheep/
With the relevant line of code: https://github.com/minutelabsio/Relativistic-Space-Sheep/blob/master/library/js/mediators/boilerplate.js#L743

Line chart/graph with an irregular threshold field

Looking to create a bar chart with an irregular, colored threshold field in the background, so that each data point has its own individual set of min/max thresholds, which ultimately would look something like this: http://dcalvitti.webs.com/plant/SAMPLE.png
Looked at D3 examples like this one: http://bl.ocks.org/mbostock/4062844
Can the latter example be manipulated to look more like the image I created?
Thanks in advance..
The graph shown in your sample image is actually much easier than the linked example; for that, you don't need to create a clipping path and you don't need to draw the line twice with two different colours.
For drawing the coloured background, use an area-path generator, created with d3.svg.area(). Set the y0 accessor function to be extract your minimum value for each point in your data array, and the y1 accessor function to extract the maximum value.
Then draw the line overtop as a normal line graph with a d3.svg.line() path generator.
Working example, adapted from the fiddles in the comments: http://jsfiddle.net/h45CD/12/
(Note: I commented out half the dataset, since the "year" values were repeated, not sure what that was supposed to represent.)
Key code:
// Define the value line path generator
var line = d3.svg.line()
.x( function(d) { return x(d.year); } )
.y( function(d) { return y(d.temp); } );
// Define the area path generator
var area = d3.svg.area()
.x( function(d) { return x(d.year); } )
.y0( function(d) { return y(d.min); } )
.y1( function(d) { return y(d.max); } );
/* ... */
// Add the background area showing the historic range
svg.append("path")
.datum(data)
.attr("class", "historicRange")
.attr("d", area);
// Add the value line
svg.append("path")
.datum(data)
.attr("class", "dataline")
.attr("d", line);
Edit based on comments
If you do want a line that changes colour depending on historic values, as opposed to a line drawn overtop of a background range, the most straight-forward solution is probably to create a <pattern> element consisting of the different coloured regions, and use this to stroke the value line.
You'll want to familiarize yourself with the different options for the pattern element. This MDN tutorial has a good intro, or you could dive into the full W3 specs.
For this situation, we want the pattern to be sized and positioned relative to the coordinate system used for drawing the line, regardless of the size or shape of the line itself. That means we will be setting both the patternUnits and the patternContentUnits to be userSpaceOnUse. The height and width of the pattern will be the height and width of the plotting area.
Within the pattern we will draw the area that represents the max-min range, but we also need to draw separate areas, with different colours, for values above the max and values below the min. We can use the same area generator for each, but need to change the y0/y1 accessor functions each time.
Key code:
// Add the pattern showing the historic range
var pattern = defs.append("pattern")
.datum(data) //add the data to the <pattern> element
//so it will be inherited by each <path> we append
.attr({
"patternUnits":"userSpaceOnUse",
"patternContentUnits":"userSpaceOnUse",
"width": width,
"height": height
})
.attr("id", "strokePattern");
pattern.append("path")
.attr("class", "historicRange between")
.attr("d", area);
pattern.append("path")
.attr("class", "historicRange above")
.attr("d", area.y1( 0 )
.y0( function(d){return y(d.max);} )
);
pattern.append("path")
.attr("class", "historicRange below")
.attr("d", area.y1( function(d){return y(d.min);} )
.y0( height )
);
// Add the value line
plot.append("path")
.datum(data)
.attr("class", "dataline")
.attr("d", line)
.style("stroke", "url(#strokePattern)");
Working example: http://jsfiddle.net/h45CD/14/
I'm including a web page link with charts authored by myself based on AMCharts and with the help of that web site's founder. Contains several examples of the above question and more..
http://dcalvitti.webs.com/SAMPLE/NEWWEBINDEX.html
The charts provided are still being worked on. For example, AMcharts does have a function that clips the color of a line above/below a certain value which I didn't know about, so there is still work to be done. I spent many weeks on the charts and thought I'd share. I'm sure someone will find something new here down the road...

d3 Rickshaw Graph Disable Hover effect

I'm using Rickshaw with d3. I would like to show the x axis value when I'm hovering over a bar on the bar graph, but I would like to disable to hover effect that shows the series type and the y axis value.
Is there a way to only display one and not the other? I tried doing this and it doesn't work (i'm compiling this coffeescript to javascript):
hoverDict =
graph: graph
xFormatter: (x) ->
today = new Date()
today.setDate(today.getDate()- x + 1)
return today.toDateString()
yFormatter: null
hoverDetail = new Rickshaw.Graph.HoverDetail(hoverDict)
I figured it out. You have to edit rickshaw.css for the .item.active field:
.rickshaw_graph .detail .item.active {
opacity: 1;
display:none;
}
Making display:none will make the yFormatter not display.