Skip to content

Commit 702bd92

Browse files
Brianna SookhooBrianna Sookhoo
authored andcommitted
rendering labels with nodes
1 parent 9394f61 commit 702bd92

1 file changed

Lines changed: 28 additions & 41 deletions

File tree

app/charts/route-trace.jsx

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
11
import * as d3 from 'd3';
2-
3-
let text = [{
4-
t: 'reverseproxy',
5-
}, {
6-
t: 'books',
7-
}, {
8-
t: 'orders',
9-
}, {
10-
t: 'customers'
11-
}]
122
class RouteTrace {
133
containerEl;
144
props;
@@ -27,7 +17,6 @@ class RouteTrace {
2717
this.updateDatapoints();
2818
}
2919

30-
3120
updateDatapoints = () => {
3221
const { svg, props: { data, width, height } } = this;
3322
console.log('data: ', data)
@@ -36,61 +25,59 @@ class RouteTrace {
3625

3726
console.log('1st data point: ', data[0][0])
3827
console.log('4th data point: ', data[0][3])
39-
4028

4129
var simulation = d3.forceSimulation(data[0])
42-
  .force('link', d3.forceLink())
43-
  .force('charge', d3.forceManyBody())
30+
.force('link', d3.forceLink())
31+
.force('charge', d3.forceManyBody())
4432
.force('center', d3.forceCenter(width / 2, height / 2))
45-
46-
const node = svg
47-
.selectAll('circle')
33+
34+
const link = svg.append("g")
35+
.attr("class", "links")
36+
.selectAll("line")
37+
.data(data[1])
38+
.enter()
39+
.append("line")
40+
.style('stroke', 'black')
41+
42+
const node = svg.append("g")
43+
.attr("class", "nodes")
44+
.selectAll("g")
4845
.data(data[0])
4946
.enter()
50-
.append('circle')
47+
.append("g")
48+
49+
const circles = node.append("circle")
50+
.attr("r", 20)
51+
.attr("fill", '#00eda0')
5152
.on('mouseover', () => d3.select(this).text(d.id))
52-
53-
const circleAttr = node
54-
.attr('r', 20)
55-
.attr('cx', 200)
56-
.attr('cy', 200)
57-
.style('fill', '#00eda0')
5853
.on('mouseup', (d, i, nodes) => this.setActiveDatapoint(d, nodes[i]))
5954

60-
const text = svg.selectAll('text')
61-
.data(data[0]).enter().append('text')
62-
63-
const textLabels = text
55+
const lables = node.append("text")
56+
.text(d => d.id)
6457
.attr('x', d => d.x)
6558
.attr('y', d => d.y)
66-
.attr('dx', 100)
67-
.attr('dy','10em')
68-
.text(d => d.id);
6959

70-
const link = svg
71-
.selectAll('line')
72-
.data(data[1])
73-
.enter()
74-
.append('line')
75-
.style('stroke', 'black')
60+
node.append("title")
61+
.text(d => d.id)
7662

7763
simulation
7864
.nodes(data[0])
7965
.on('tick', ticked);
8066

8167
simulation.force('link')
82-
.links(link);
68+
// .links(data[1]); // error: cannot create property vx on books
8369

8470
function ticked() {
8571
link
8672
.attr('x1', function(d) { return d.source.x; })
8773
.attr('y1', function(d) { return d.source.y; })
8874
.attr('x2', function(d) { return d.target.x; })
8975
.attr('y2', function(d) { return d.target.y; });
90-
76+
9177
node
92-
.attr('cx', function(d) { return d.x; })
93-
.attr('cy', function(d) { return d.y; });
78+
.attr("transform", function(d) {
79+
return "translate(" + d.x + "," + d.y + ")";
80+
})
9481
}
9582
}
9683

0 commit comments

Comments
 (0)