score:3
short answer
yes, but you have to define the starting value for the d
attribute.
long answer
a transition goes from a starting value to a target value. right now, in your "enter" selection, you have no starting value (that is, before transition()
) for the d
attribute:
// enter
this.line
.enter().append( 'path' )
.attr( 'class', 'valueline' )
.transition()//no 'd' attribute before this line
.attr( 'd', this.valueline );
therefore, the d
attribute for that path is null
(read more about this in this answer).
let's see it:
var data = [10, 180, 30, 60, 190, 180, 50, 110, 40, 90, 90];
var svg = d3.select("svg");
var linegenerator = d3.line()
.y(function(d) {
return d
})
.x(function(d, i) {
return i * 60
});
var path = svg.selectall(null)
.data([data])
.enter()
.append("path");
console.log(path.attr("d"))
path {
fill: none;
stroke-width: 2px;
stroke: teal;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg width="600" height="200"></svg>
here the same code, setting the d
attribute:
var data = [10, 180, 30, 60, 190, 180, 50, 110, 40, 90, 90];
var svg = d3.select("svg");
var linegenerator = d3.line()
.y(function(d) {
return d
})
.x(function(d, i) {
return i * 60
});
var path = svg.selectall(null)
.data([data])
.enter()
.append("path")
.attr("d", linegenerator);
path {
fill: none;
stroke-width: 2px;
stroke: teal;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg width="600" height="200"></svg>
back to your question:
you cannot transition the d
attribute from null
to your string. it's impossible. there are some possible workarounds, though.
one of them is creating a default d
attribute before the transition. for instance:
var data = [10, 180, 30, 60, 190, 180, 50, 110, 40, 90, 90];
var svg = d3.select("svg");
var linegenerator = d3.line()
.y(function(d) {
return d
})
.x(function(d, i) {
return i * 60
});
var path = svg.selectall(null)
.data([data])
.enter()
.append("path")
.attr("d", "m0,100 l600,100")
.transition()
.duration(2000)
.attr("d", linegenerator);
path {
fill: none;
stroke-width: 2px;
stroke: teal;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg width="600" height="200"></svg>
but that's a horrible transition! the problem is that the initial d
attribute has too few points. because of that, the string interpolator cannot create the interpolation we wish.
so, we can use a custom function, like this one from mike bostock. here is the demo:
var data = [10, 180, 30, 60, 190, 180, 50, 110, 40, 90, 90];
var svg = d3.select("svg");
var linegenerator = d3.line()
.y(function(d) {
return d
})
.x(function(d, i) {
return i * 60
});
var path = svg.selectall(null)
.data([data])
.enter()
.append("path")
.attr("d", "m0,100 l600,100")
.transition()
.duration(2000)
.attrtween("d", function(d) {
return pathtween(linegenerator(d), 4, this)()
});
function pathtween(d1, precision, self) {
return function() {
var path0 = self,
path1 = path0.clonenode(),
n0 = path0.gettotallength(),
n1 = (path1.setattribute("d", d1), path1).gettotallength();
// uniform sampling of distance based on specified precision.
var distances = [0],
i = 0,
dt = precision / math.max(n0, n1);
while ((i += dt) < 1) distances.push(i);
distances.push(1);
// compute point-interpolators at each distance.
var points = distances.map(function(t) {
var p0 = path0.getpointatlength(t * n0),
p1 = path1.getpointatlength(t * n1);
return d3.interpolate([p0.x, p0.y], [p1.x, p1.y]);
});
return function(t) {
return t < 1 ? "m" + points.map(function(p) {
return p(t);
}).join("l") : d1;
};
};
}
path {
fill: none;
stroke-width: 2px;
stroke: teal;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg width="600" height="200"></svg>
Source: stackoverflow.com
Related Query
- Can the 'enter' selection for a line graph be animated?
- How do I graph an individual line for each the groups within a Javascript map object using D3.js
- How can I properly aggregate / group multiple line graphs into one overall graph with d3.js when the x-values aren't matching exactly?
- Is D3.js the right choice for real-time visualization of Neo4j Graph DB data
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- Can d3.js draw two scatterplots on the same graph using data from the same source?
- Can I use images as the background rectangles for d3 treemaps?
- Adding a horizontal line to d3 graph displays at the wrong value
- Reference line for line graph
- How can I set the each line color or width in SVG path
- Where can I get the .geojson file for India and not separate files for each state/territory or any other distinction?
- D3.js - How can I add a new line to the text in this Collapsible Tree?
- How to plot animated line chart using d3 while the data table is required to be updated every 1 second?
- How can I remove a line from the 110m TopoJson world map?
- How can I connect two parent node into one child node and how to make a tooltip for each node in the tree pragmatically? in D3 js (SVG)
- d3.js duplicate code for enter selection and update selection
- How can I get access to the current selection inside a D3 callback?
- Where can in find the locale objects for d3.js for different countries
- d3.v4.js Line Graph Interactive Legend - Only works for first line
- Can the 'webagg' backend for matplotlib work with my django site?
- What's the best force-directed network graph engine for large data sets?
- How can I color one Y-axis line different from the other grid-lines?
- How can we add the space between bars for multiple groups of bars in c3js?
- Changing the line style in d3js for every segment?
- How can I determine the overall size of a force directed graph
- Different tick sizes on the Y axis of a D3js line graph
- how can i get a label on my lines in a line graph using d3.js
- how to make the x axis scrollable in a d3 line chart graph
- How to Increase the width of C3 pie chart Legend only. Not for the whole graph
- Where can I find a good label placement algorithm for line charts?
More Query from same tag
- nvd3 not displaying graph for data with value of 1000 or higher
- D3.js highlight related nodes / links
- D3js - Reuse line definition/function for multiple data categories in file
- How to set the Origin (drag.origin) for drag behavior in d3 JavaScript library
- Select an element in contained DOM tree using d3.js
- How to make graphs in dc.js scrollable within a fixed dimension div?
- [Help]: d3.js Scatterplot traverse through 3 different arrays
- d3js Moving SVG elements inside a transforming group
- Using d3.json to query a sinatra app works fine on localhost, but fails with forbidden when deployed
- Issue with scale module d3 on react
- Scale SVG but nothing else?
- mpld3: How to change the location of the toolbar using a plugin?
- nvd3 interlock element click event with other chart
- How do I change the value of the y-axis by 30 instead of 10?
- How to implement D3 scales to have children inherit colour from parent with graduations?
- D3.js enable/disable dragging programmatically
- text align right relatively to parent G in SVG
- In a D3 tree, how to close all nodes at a selected depth (ex. only grandchildren) programmatically (without clicking on a node)
- unresolved variable d3.event.translate and d3.event.scale
- d3 Workaround for svg transform in chrome
- D3.js: Import file changing with a slider
- issue in testing html file with javascript jasmine
- D3 Scatterplot from all circles to different shapes
- D3.js X axis line not displaying
- d3 transform-origin with two needles in a clock
- how do i add a zoom in and zoom out button in a graph on d3
- Create database E-R diagram with D3js
- Constraining map panning with zoom.translateExtent in D3 v4
- Typescript expects undefined to be returned from helper function in d3.nest().rollup()
- MeteorJS : Template rendered & Meteor method call