Is it possible to add markers ?

Sett 586 ganger
Hopp til første uleste melding

Emeric

ulest,
20. juni 2017, 10:52:5620.06.2017
til d3-js
Hello,

I'm a D3js beginner.
I would like to know if it's possible to add vertical markers into a line graph like this picture (the marker is the vertical line in orange):


I searched and I found nothing. Maybe there is another name than "marker" ?

Thank you in advance !

Ian Johnson

ulest,
20. juni 2017, 14:49:0020.06.2017
til d3...@googlegroups.com
I think you want annotations: http://d3-annotation.susielu.com/

--
You received this message because you are subscribed to the Google Groups "d3-js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to d3-js+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Ian Johnson - 周彦

Emeric

ulest,
21. juni 2017, 07:54:4321.06.2017
til d3-js
Hi Ian,

Thank you I think I'm getting closer.
If I'm right, annotations are used to annotate/describe points.
I searched more and I found similar topics, they called it "limits" (i called it "markers") but it looks the same:
But it didn't really help me (i tried but certainly wrong).

Japhy Bartlett

ulest,
21. juni 2017, 14:01:0421.06.2017
til d3...@googlegroups.com
You might be overthinking it a bit, it's certainly possible to add a vertical line!

If you can show us some code that's close, it's much more likely that someone can help you finish it off.

--

Emeric

ulest,
22. juni 2017, 03:53:0322.06.2017
til d3-js
Hi,
Here is my code:
 
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>D3JS Example</title>
<style type="text/css">

.area {
fill: none;
stroke:  steelblue;
clip-path: url(#clip);
}
.zoom {
cursor: move;
fill: none;
pointer-events: all;
}


</style>
</head>
<body>
<svg width="1560" height="500"></svg>
<script src="https://d3js.org/d3.v4.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 110, left: 40}, 
margin2 = {top: 430, right: 20, bottom: 30, left: 40}, 
width = +svg.attr("width") - margin.left - margin.right, 
height = +svg.attr("height") - margin.top - margin.bottom,
height2 = +svg.attr("height") - margin2.top - margin2.bottom; 
var x = d3.scaleLinear().rangeRound([0, width]), 
x2 = d3.scaleLinear().rangeRound([0, width]),
y = d3.scaleLinear().rangeRound([height, 0]), 
y2 = d3.scaleLinear().rangeRound([height2, 0]); 
var xAxis = d3.axisBottom(x),
xAxis2 = d3.axisBottom(x2),
yAxis = d3.axisLeft(y);
var brush = d3.brushX()
.extent([[0, 0], [width, height2]])
.on("brush end", brushed);

var zoom = d3.zoom()
.scaleExtent([1, Infinity])
.translateExtent([[0, 0], [width, height]])
.extent([[0, 0], [width, height]])
.on("zoom", zoomed);
var area = d3.line()
.x(function(d) { return x(d.date); })
//.y0(height)
.y(function(d) { return y(d.price); });
var area2 = d3.line()
.x(function(d) { return x2(d.date); })
    //.y0(height2)
.y(function(d) { return y2(d.price); });

svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
var focus = svg.append("g")
.attr("class", "focus")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

var context = svg.append("g")
.attr("class", "context")
.attr("transform", "translate(" + margin2.left + "," + margin2.top + ")");


d3.tsv("test.tsv", type, function(error, data) {
if (error) throw error;
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.price; }));
x2.domain(x.domain());
y2.domain(y.domain());
focus.append("path")
.datum(data)
.attr("fill", "none")

.attr("stroke", "steelblue")

.attr("class", "area")
.attr("d", area);
 
focus.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
focus.append("g")
.attr("class", "axis axis--y")
.call(yAxis);
context.append("path")
.datum(data)
.attr("class", "area")
.attr("d", area2);
context.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height2 + ")")
.call(xAxis2);
context.append("g")
.attr("class", "brush")
.call(brush)
.call(brush.move, x.range());
svg.append("rect")
.attr("class", "zoom")
.attr("width", width)
.attr("height", height)
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.call(zoom);
});
function brushed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom
var s = d3.event.selection || x2.range();
x.domain(s.map(x2.invert, x2));
focus.select(".area").attr("d", area);
focus.select(".axis--x").call(xAxis);
svg.select(".zoom").call(zoom.transform, d3.zoomIdentity
.scale(width / (s[1] - s[0]))
.translate(-s[0], 0));
}
function zoomed() {
if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush
var t = d3.event.transform;
x.domain(t.rescaleX(x2).domain());
focus.select(".area").attr("d", area);
focus.select(".axis--x").call(xAxis);
context.select(".brush").call(brush.move, x.range().map(t.invertX, t));
}
var parseDate = d3.timeParse("%b %Y");
function type(d) {
d.date = d.abs;
d.price = +d.ord;
return d;
}

</script>
</body>
</html>

With test.tsv:

abs ord
0 -7
1 -8
2 -8
3 -8
4 -6
5 -5
6 -7
7 -6
8 -5
9 -5
10 -6
11 -6
12 -7
13 -10

I tried jsfiddle but having problems to add test.tsv

Here I have only my graph loading my data, with zoom. I would like to add a functionality: markers (vertical line as you said it) on a point, (and then eventually move the marker with the mouse)

Ian Johnson

ulest,
22. juni 2017, 12:25:3022.06.2017
til d3...@googlegroups.com
may I suggest http://blockbuilder.org
when you put code there it is also viewable on bl.ocks.org which is the standard way of sharing in the d3 community

--

Emeric

ulest,
23. juni 2017, 03:13:2023.06.2017
til d3-js
Nice, here it is:
http://blockbuilder.org/anonymous/df360fbce1fb6c0d20a57c7d66d98fdb
(I don't know why we can't see all points, it works in my navigator)

Emeric

ulest,
23. juni 2017, 11:08:1123.06.2017
til d3-js
Hello,

Sorry for the second post.
I create just a line like that:


The problem is: when I zoom/unzoom, the line is still here (it has no link with points).

To create the line i add that:


var marker = svg.append("line")
.attr("x1", 800)
.attr("y1", 0)
.attr("x2", 800)
.attr("y2", 390)
.attr("stroke-width", 2)
.attr("stroke", "black");
To summarize, I would like to have a marker (=a vertical line) in a point (like the picture) and when I zoom or when I manipulate the graph, the marker stays on the point.


Alex Macy

ulest,
24. juni 2017, 13:34:3724.06.2017
til d3-js
I think it's not moving because the x1 and x2 attributes need to respond to the scale rather than be assigned static numbers.

Check this out: http://blockbuilder.org/alexmacy/65f66d540a4bfb23ca0ea60027122182


I added the marker on line 137 like this:
focus.append("line")
   
.attr("class", "marker")
   
.style("stroke", "red")
   
.attr("y1", 0)
   
.attr("y2", height)
   
.attr("x1", x(3))
   
.attr("x2", x(3));

It's just assigned to an arbitrary number, but notice that x1 and x2 are calculated using x scale.


You then just need to update x1 and x2 in the brush and zoom events (see lines 153 & 170)
focus.select(".marker")
   
.attr("x1", x(3))
   
.attr("x2", x(3));

You would have to make changes in order to use multiple markers, but hopefully this points you in the right direction!

- Alex

Emeric

ulest,
26. juni 2017, 05:10:3426.06.2017
til d3-js
Thank you Alex that's nice !
Svar alle
Svar til forfatter
Videresend
0 nye meldinger