Skip to content

Commit dcbbc87

Browse files
committed
D3 Implementation
2 parents 3e72539 + a635394 commit dcbbc87

2 files changed

Lines changed: 54 additions & 11 deletions

File tree

app/charts/route-trace.jsx

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,58 @@ class RouteTrace {
1919

2020
updateDatapoints = () => {
2121
const { svg, props: { data, width, height } } = this;
22-
svg.selectAll('circle')
23-
.data(data)
24-
.enter()
25-
.append('circle')
26-
.style('fill', 'red')
22+
console.log('data: ', data)
23+
console.log('nodes: ', data[0])
24+
console.log('links: ', data[1])
25+
26+
27+
var simulation = d3.forceSimulation(data[0])
28+
  .force('link', d3.forceLink())
29+
  .force('charge', d3.forceManyBody())
30+
.force('center', d3.forceCenter(width / 2, height / 2))
31+
32+
const node = svg
33+
.selectAll("circle")
34+
.data(data[0])
35+
.enter()
36+
.append("circle")
37+
.attr("r", 20)
2738
.attr('cx', () => Math.random() * width)
2839
.attr('cy', () => Math.random() * height)
29-
.attr('r', 20)
40+
.style("fill", "#00eda0")
3041
.on('mouseup', (d, i, nodes) => this.setActiveDatapoint(d, nodes[i]));
31-
}
3242

43+
node.append("title")
44+
.text(function(d) { return d.id; });
45+
46+
const link = svg
47+
.selectAll("line")
48+
.data(data[1])
49+
.enter()
50+
.append("line")
51+
.style("stroke", "black")
52+
53+
simulation
54+
.nodes(data[0])
55+
.on('tick', ticked);
56+
57+
simulation.force('link')
58+
.links(link);
59+
60+
61+
function ticked() {
62+
link
63+
.attr("x1", function(d) { return d.source.x; })
64+
.attr("y1", function(d) { return d.source.y; })
65+
.attr("x2", function(d) { return d.target.x; })
66+
.attr("y2", function(d) { return d.target.y; });
67+
68+
node
69+
.attr("cx", function(d) { return d.x; })
70+
.attr("cy", function(d) { return d.y; });
71+
}
72+
73+
}
3374

3475
setActiveDatapoint = (d, node) => {
3576
d3.select(node).style('fill', 'yellow');

app/containers/GraphsContainer.jsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const GraphsContainer = (props) => {
3232
],
3333
};
3434

35-
3635
const [data, setData] = useState(null);
3736
const [width, setWidth] = useState(400);
3837
const [height, setHeight] = useState(400);
@@ -43,11 +42,11 @@ const GraphsContainer = (props) => {
4342
useEffect(initVis, [ data ]);
4443

4544
function fetchData() {
46-
Promise.resolve().then(() => setData(initialData.nodes));
45+
Promise.resolve().then(() => setData(Object.values(initialData)));
4746
}
4847

4948
function initVis() {
50-
if(data && data.length) {
49+
if (data && data.length) {
5150
const d3Props = {
5251
data,
5352
width,
@@ -60,7 +59,10 @@ const GraphsContainer = (props) => {
6059

6160
return (
6261
<div className="graphsGrid">
63-
<div ref={canvas}/>
62+
<div className='routes'>
63+
<div>{active}</div>
64+
<div ref={canvas} />
65+
</div>
6466
<SpeedChart service={service} />
6567
<TemperatureChart service={service} />
6668
<RequestTypesChart service={service} />

0 commit comments

Comments
 (0)