Skip to content

Commit a635394

Browse files
Merge pull request #12 from briannasookhoo/routesd3
some force simulation added
2 parents 677b561 + 7a6ff33 commit a635394

2 files changed

Lines changed: 56 additions & 13 deletions

File tree

app/charts/route-trace.jsx

Lines changed: 50 additions & 9 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')
27-
.attr('cx', 20)
28-
.attr('cy', 200)
29-
.attr('r', 20)
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)
38+
.attr('cx', () => Math.random() * width)
39+
.attr('cy', () => Math.random() * height)
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
@@ -31,7 +31,6 @@ const GraphsContainer = (props) => {
3131
],
3232
};
3333

34-
3534
const [data, setData] = useState(null);
3635
const [width, setWidth] = useState(400);
3736
const [height, setHeight] = useState(400);
@@ -42,11 +41,11 @@ const GraphsContainer = (props) => {
4241
useEffect(initVis, [ data ]);
4342

4443
function fetchData() {
45-
Promise.resolve().then(() => setData(initialData.nodes));
44+
Promise.resolve().then(() => setData(Object.values(initialData)));
4645
}
4746

4847
function initVis() {
49-
if(data && data.length) {
48+
if (data && data.length) {
5049
const d3Props = {
5150
data,
5251
width,
@@ -59,7 +58,10 @@ const GraphsContainer = (props) => {
5958

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

0 commit comments

Comments
 (0)