state-management.component.ts 6.57 KB
Newer Older
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { Component, OnInit } from '@angular/core';
import * as d3 from 'd3';
import {StateService} from '../state.service';
import {style} from '@angular/animations';
import {State} from '../api.service';

@Component({
  selector: 'app-state-management',
  templateUrl: './state-management.component.html',
  styleUrls: ['./state-management.component.css']
})
export class StateManagementComponent implements OnInit {

  private treeCreated = false;
  private selectedNode;
  private treemap;

  constructor(private state: StateService) { }

  ngOnInit(): void {
    if (this.state.lshData) {
      this.createStateTree();
    }
    this.state.onNewLshData.subscribe(() => {
      if (!this.treeCreated) {
        this.createStateTree();
      } else {
        this.update();
      }
    });
  }

  createStateTree() {
    console.log('Creating tree!');

    const margin = {top: 20, right: 90, bottom: 30, left: 90};
    const width = 960 - margin.left - margin.right;
    const height = 500 - margin.top - margin.bottom;

    const svg = d3.select('.state').append('svg')
      .attr("width", width + margin.right + margin.left)
      .attr("height", height + margin.top + margin.bottom)
      .append("g")
      .attr("transform", "translate("
        + margin.left + "," + margin.top + ")");

    let root;
    const data = this.state.states;
    this.selectedNode = d3.hierarchy(data[1], function(d) { return d.children ? d.children.map(function(child) {return data[child]}) : undefined });
    this.selectedNode.x0 = height / 2;
    this.selectedNode.y0 = 20;

    this.treeCreated = true;
    this.update();
  }

  update() {
    const margin = {top: 20, right: 90, bottom: 30, left: 90};
    const width = 960 - margin.left - margin.right;
    const height = 500 - margin.top - margin.bottom;

    if (!this.treemap) {
      this.treemap = d3.tree().size([height, width]);
    }
    let i = 0;
    const duration = 750;

    const svg = d3.select('.state').select('svg');
    console.log(svg);

    const data = this.state.states;
    const currIndex = this.state.currStateIndex;
    const root = d3.hierarchy(data[1], function(d) { return d.children ? d.children.map(function(child) {return data[child]}) : undefined });
    const source = this.selectedNode;

    const loadState = (id) => {
      this.state.loadState(id);
    };
    // // Assigns the x and y position for the nodes
    // let root;
    // const data = this.state.states;
    // root = d3.hierarchy(data[1], function(d) { return d.children ? d.children.map(function(child) {return data[child]}) : undefined });

    const treeData = this.treemap(root);

    // Compute the new tree layout.
    const nodes = treeData.descendants();
    console.log('descendants');
    console.log(nodes);
    const links = treeData.descendants().slice(1);

    // Normalize for fixed-depth.
    nodes.forEach(function(d) {
      d.y = d.depth * 180
    });

    // ****************** Nodes section ***************************

    svg.selectAll('g.node').select('circle').style("fill", "#fff");
    // Update the nodes...
    const node = svg.selectAll('g.node')
      .data(nodes, function(d: any) {
        console.log('all-nodes');
        console.log(d);
        return d.id;
      });

    console.log('node');
    console.log(node);



    // Enter any new modes at the parent's previous position.
    const nodeEnter = node.enter().append('g')
      .attr('class',function(d) {
        console.log('new nodes');
        return 'node';
      })
      // .attr("transform", function(d) {
      //   console.log('new nodes');
      //   return "translate(" + source.y0 + "," + source.x0 + ")";
      // })
      .on('click', function(d: any) {
        loadState(d.data.id);
      });

    // Add Circle for the nodes
    nodeEnter.append('circle')
      .attr('class', 'node')
      .attr('r', 1e-6)
      .style("fill", function(d:any) {
        return d.data.id === currIndex ? "lightsteelblue" : "#fff";
      });

    // Add labels for the nodes
    nodeEnter.append('text')
      .attr("dy", ".35em")
      .attr("x", function(d:any) {
        return d.children ? -13 : 13;
      })
      .attr("text-anchor", function(d:any) {
        return d.children ? "end" : "start";
      })
      .text(function(d: any) {
        return d.data.id;
      });

    // // UPDATE
    const nodeUpdate = nodeEnter.merge(node as any);

    // Transition to the proper position for the node
    nodeUpdate.transition()
      .duration(duration)
      .attr("transform", function(d: any) {
        return "translate(" + d.y + "," + d.x + ")";
      });

    // Update the node attributes and style
    nodeUpdate.select('circle.node')
      .attr('r', 10)
      .style("fill", function(d:any) {
        return d.data.id === currIndex ? "lightsteelblue" : "#fff";
      })
    .attr('cursor', 'pointer');


    // Remove any exiting nodes
    const nodeExit = node.exit().transition()
      .duration(duration)
      .attr("transform", function(d) {
        return "translate(" + source.y + "," + source.x + ")";
      })
      .remove();

    // On exit reduce the node circles size to 0
    nodeExit.select('circle')
      .attr('r', 1e-6);

    // On exit reduce the opacity of text labels
    nodeExit.select('text')
      .style('fill-opacity', 1e-6);

    // ****************** links section ***************************

    // Update the links...
    const link = svg.selectAll('path.link')
      .data(links, function(d: any) {
        return d.id;
      });

    // // Enter any new links at the parent's previous position.
    const linkEnter = link.enter().insert('path', "g")
      .attr("class", "link")
      .attr('d', function(d) {
        const o = {x: source.x0, y: source.y0}
        return diagonal(o, o);
      });

    // UPDATE
    const linkUpdate = linkEnter.merge(link as any);

    // Transition back to the parent element position
    linkUpdate.transition()
      .duration(duration)
      .attr('d', function(d: any) {
        return diagonal(d, d.parent)
      });

    // Remove any exiting links
    const linkExit = link.exit().transition()
      .duration(duration)
      .attr('d', function(d) {
        var o = {x: source.x, y: source.y};
        return diagonal(o, o);
      })
      .remove();

    // Store the old positions for transition.
    nodes.forEach(function(d: any) {
      d.x0 = d.x;
      d.y0 = d.y;
    });

    // Creates a curved (diagonal) path from parent to the child nodes
    function diagonal(s, d) {

      const path = `M ${s.y} ${s.x}
            C ${(s.y + d.y) / 2} ${s.x},
              ${(s.y + d.y) / 2} ${d.x},
              ${d.y} ${d.x}`;

      return path;
    }


  }

  // Toggle children on click

}