-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
757 lines (641 loc) · 28.2 KB
/
App.js
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
import React, { Component } from 'react';
import './App.css';
import { Form, ControlLabel, FormGroup, FormControl, Button, DropdownButton, MenuItem, ButtonToolbar, Alert, Label, Glyphicon} from 'react-bootstrap';
import {ComposedChart, Scatter, Line, XAxis, YAxis, CartesianGrid, Legend, LabelList} from 'recharts';
// import DefaultTooltipContent from 'recharts/lib/component/DefaultTooltipContent';
import 'rc-slider/assets/index.css';
import 'rc-tooltip/assets/bootstrap.css';
import Slider from 'rc-slider';
import Tooltip from 'rc-tooltip';
const Handle = Slider.Handle;
class App extends Component {
constructor() {
super();
this.state = {
driver_location: null,
driver_path: [],
legs: null,
legs_dict: {},
stops: null,
stops_dict: {},
// Alert/error message
alert_info: {status: false,
type: "warning",
reason: "",
message: ""
},
// Bonus 1: Bonus driver info
bonus_driver_location: null,
bonus_driver_x_new: '',
bonus_driver_y_new: '',
path_to_complete: null,
// Bonus 2: Driver path info
bonus2_time_complete_all_legs: null,
bonus2_time_complete: null,
// Bonus 3: New driver's location
driver_loc_activeLegID_new: null,
sliderVal: null,
newDriverLoc: null,
};
this.backEndUrl = "http://localhost:8080/"
// Store info about overlaping stops
this.pointOverlapDict = {}
this.iCheckFormatLabel = 0
this.renderDropdownButton = this.renderDropdownButton.bind(this)
this._onPutDriverButtonPressed = this._onPutDriverButtonPressed.bind(this)
this.resetErrorMessage = this.resetErrorMessage.bind(this)
this.setInputNumberErrorMessage = this.setInputNumberErrorMessage.bind(this)
this._onLegDropdownButtonPressed = this._onLegDropdownButtonPressed.bind(this)
this.computePath = this.computePath.bind(this)
this._onBonusDriverCoordinatesChanged = this._onBonusDriverCoordinatesChanged.bind(this)
this._onPutBonusDriverButtonPressed = this._onPutBonusDriverButtonPressed.bind(this)
this._onGetBonusDriverButtonPressed = this._onGetBonusDriverButtonPressed.bind(this)
this.computeBonusRemainingPath = this.computeBonusRemainingPath.bind(this)
this.computePathToComplete = this.computePathToComplete.bind(this)
this._onSliderChange = this._onSliderChange.bind(this)
this.computeNewDriverLocOnLegChanged = this.computeNewDriverLocOnLegChanged.bind(this)
}
// Handle alert/error messages
setInputNumberErrorMessage(err_reason, err_msg){
this.setState({alert_info: {status: true,
type: "warning",
reason: err_reason,
message: err_msg}})
}
resetErrorMessage(full_reset=false){
// Reset of the error mesage
if (full_reset){
this.setState({alert_info: {status: false,
type: "warning",
reason: "",
message: ""}})
} else{
this.setState({alert_info: {status: false,
type: this.state.alert_info["type"],
reason: this.state.alert_info["reason"],
message: this.state.alert_info["message"]}})
}
}
// Parts 1 & 3: REST GET
getData(url = ``) {
return fetch(url, {
method: "GET",
dataType: 'json'
})
}
// Part 7: REST PUT
putData(url = ``, data = {}) {
return fetch(url, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data)
})
}
// Part 4 & 5: Compute driver's current location and completed path
computePath(){
var driver_path = []
for (var i=0; i < this.state.stops.length; i++){
driver_path.push({
"name": this.state.stops[i]["name"],
"x": this.state.stops[i]["x"],
"y": this.state.stops[i]["y"],
})
if (this.state.stops[i]["name"] === this.state.driver_location["activeLegID"][0]){
break
}
}
// Compute driver's current position
var leg_progress = parseInt(this.state.driver_location["legProgress"]) / 100
var delta_x = this.state.stops[i+1]["x"] - this.state.stops[i]["x"]
var delta_y = this.state.stops[i+1]["y"] - this.state.stops[i]["y"]
var x = delta_x * leg_progress + this.state.stops[i]["x"]
var y = delta_y * leg_progress + this.state.stops[i]["y"]
driver_path.push({
"name": "loc",
"x": x,
"y": y,
})
this.setState({
newDriverLoc : {
"x": x,
"y": y,
},
sliderVal: parseInt(this.state.driver_location["legProgress"]),
driver_loc_activeLegID_new: this.state.driver_location["activeLegID"]
})
// Update driver's path
this.setState({driver_path: driver_path})
}
// Bonus 1: Compute the closest stop to the bonus driver and the remaining path from the closest stop to the final stop
computeBonusRemainingPath(){
var path_to_complete = []
var x = this.state.bonus_driver_location["x"]
var y = this.state.bonus_driver_location["y"]
// Find the closest stop
var min_i = 0
var min = Infinity
for (var i = 0; i < this.state.stops.length; i++ ){
var dist = Math.sqrt(Math.pow(this.state.stops[i]["x"] - x, 2) + Math.pow(this.state.stops[i]["y"] - y, 2))
if (dist < min){
min = dist
min_i = i
}
}
// Compute the remaining path
path_to_complete.push({
"name": "loc",
"x": x,
"y": y,
})
for (i = min_i; i < this.state.stops.length; i++ ){
path_to_complete.push({
"name": this.state.stops[i]["name"],
"x": this.state.stops[i]["x"],
"y": this.state.stops[i]["y"],
})
}
this.setState({path_to_complete: path_to_complete})
}
// Bonus 2: compute how long it will take the driver to complete the entire path (all of the legs) and
// how much time is left for the driver based on the current position
computePathToComplete(){
if (Object.keys(this.state.legs_dict).length !== 0 &&Object.keys(this.state.stops_dict).length !== 0 && this.state.driver_location !== null) {
// Time required to complete all legs
var time_complete_all_legs = 0
// Time required to complete driver's remaining path
var time_complete = 0
// Append legs dictionary with start and stop locations and compute time required to drive from start to stop.
// Also, compute times to complete driver's path and all legs
var new_legs_dict = {}
var is_passed_driver_leg = false
for (var key in this.state.legs_dict) {
var dict_specs = this.state.legs_dict[key]
dict_specs["startStop_x"] = this.state.stops_dict[dict_specs["startStop"]]["x"]
dict_specs["startStop_y"] = this.state.stops_dict[dict_specs["startStop"]]["y"]
dict_specs["endStop_x"] = this.state.stops_dict[dict_specs["endStop"]]["x"]
dict_specs["endStop_y"] = this.state.stops_dict[dict_specs["endStop"]]["y"]
// Distance between subsequent stops
var dist = Math.sqrt(Math.pow(dict_specs["endStop_x"] - dict_specs["startStop_x"], 2) +
Math.pow(dict_specs["endStop_y"] - dict_specs["startStop_y"], 2))
// Time required to drive from the start to the stop point
dict_specs["time"] = dist / dict_specs["speedLimit"]
// Update leg info
new_legs_dict[key] = dict_specs
// Update time to complete the entire path and the remaining path
time_complete_all_legs += new_legs_dict[key]["time"]
if (is_passed_driver_leg) time_complete += new_legs_dict[key]["time"]
if (key === this.state.driver_location["activeLegID"]) is_passed_driver_leg = true
}
// Update time to complete the remaining path with the time to complete the current leg
time_complete += new_legs_dict[this.state.driver_location["activeLegID"]]["time"] * (100 - parseInt(this.state.driver_location["legProgress"])) / 100
this.setState({legs_dict: new_legs_dict,
bonus2_time_complete_all_legs: time_complete_all_legs,
bonus2_time_complete: time_complete
})
} else {
this.setState({alert_info: {status: true,
type: "warning",
reason: "computePath",
message: "Legs, stops and driver location are reqired for this feature to work"}})
}
}
// Bonus 3: Compute new driver location
computeNewDriverLocOnLegChanged(newLeg){
if (this.state.legs_dict[newLeg]["startStop_x"] !== undefined){
var start_x = this.state.legs_dict[newLeg]["startStop_x"]
var start_y = this.state.legs_dict[newLeg]["startStop_y"]
var end_x = this.state.legs_dict[newLeg]["endStop_x"]
var end_y = this.state.legs_dict[newLeg]["endStop_y"]
var delta_x = end_x - start_x
var delta_y = end_y - start_y
var new_driver_x = start_x + delta_x * this.state.sliderVal / 100
var new_driver_y = start_y + delta_y * this.state.sliderVal / 100
this.setState({newDriverLoc:{
"x": new_driver_x,
"y": new_driver_y,
}
})
}
}
// Convert a list of dictionaries to a dictionary of dictionaries with the specified key values
convert_list_to_dict(list_of_dicts, id){
var new_dict = {}
for (var i = 0; i < list_of_dicts.length; i++) {
var dict_specs = {}
for (var key in list_of_dicts[i]) {
if (key !== id){
dict_specs[key] = list_of_dicts[i][key]
}
}
dict_specs["index"] = i
new_dict[list_of_dicts[i][id]] = dict_specs
}
return new_dict
}
// Handle numerical input to be in specified range
checkNumInput(txt, min, max, err_reason, err_msg){
var val
// Handle input with errors
if (txt === ''){
val = ''
}
else if (Number.isNaN(parseInt(txt))){
val = ''
this.setInputNumberErrorMessage(err_reason, err_msg)
} else if (parseInt(txt).toString() !== txt){
val = parseInt(txt)
this.setInputNumberErrorMessage(err_reason, err_msg)
} else if (parseInt(txt) < min){
val = min
this.setInputNumberErrorMessage(err_reason, err_msg)
} else if (parseInt(txt) > max){
val = max
this.setInputNumberErrorMessage(err_reason, err_msg)
} else {
val = parseInt(txt)
}
return val
}
renderDropdownButton(title, i) {
return (
<MenuItem
key={"key_" + title}
onSelect={(evtKey, evt) => {
this.setState({driver_loc_activeLegID_new: evt.target.innerText})
this.computeNewDriverLocOnLegChanged(evt.target.innerText)
}}
>{title}</MenuItem>
)
}
formatFigureLabel(labelData){
// The figure is rendered two times: (1) animation; (2) static figure
if (this.iCheckFormatLabel > this.state.stops.length - 1){
this.iCheckFormatLabel = 0
this.pointOverlapDict = {}
}
this.iCheckFormatLabel +=1
var thisPointKey = parseInt(labelData.x).toString() + parseInt(labelData.y).toString()
// Handle overlapping stop lables
var overlapDeltaX = 0
var overlapDeltaY = 0
if (thisPointKey in this.pointOverlapDict){
this.pointOverlapDict[thisPointKey] += 1
if (this.pointOverlapDict[thisPointKey] === 2){
overlapDeltaY = 2 * labelData.offset
overlapDeltaX = 2 * labelData.offset
} else if (this.pointOverlapDict[thisPointKey] === 3){
overlapDeltaY = labelData.offset + 2*labelData.height
} else if (this.pointOverlapDict[thisPointKey] === 4){
overlapDeltaY = 2 * labelData.offset
overlapDeltaX = -2 * labelData.offset
}
} else {
this.pointOverlapDict[thisPointKey] = 1
}
return <text x={labelData.x + labelData.offset + overlapDeltaX} y={labelData.y - labelData.offset + overlapDeltaY} textAnchor="middle" dominantBaseline="middle">
{labelData.value}
</text>
}
// Potential feature: as of now, needs some debuging due to the potential bug in Recharts library
// The bug is reported in this thread: https://github.com/recharts/recharts/issues/1620
// renderTooltip(data){
// if (data.payload != null) {
// if (data.payload[0] != null) {
// const newPayload = [
// {
// name: 'Name',
// value: data.payload[0].payload.name,
// },
// {
// name: 'x',
// value: data.payload[0].payload.x,
// },
// {
// name: 'y',
// value: data.payload[0].payload.y,
// },
// ];
// return <DefaultTooltipContent {...data} payload={newPayload} />
// }
// }
// }
_onGetLegsButtonPressed(){
this.resetErrorMessage()
this.getData(this.backEndUrl + "legs")
.then(data => data.json())
.then(data => {
this.setState({legs: data["legs"]})
var legs_dict = this.convert_list_to_dict(this.state.legs, "legID")
this.setState({legs_dict: legs_dict})
// Bonus 2: compute path time to complete
if (Object.keys(this.state.stops_dict).length !== 0 && this.state.driver_location !== null) this.computePathToComplete()
})
.catch(error => {
this.setState({alert_info: {status: true,
type: "danger",
reason: "GetLegs",
message: error["message"]}})
console.error(error)
})
}
_onGetStopsButtonPressed(){
this.resetErrorMessage()
this.getData(this.backEndUrl + "stops")
.then(data => data.json())
.then(data => {
this.setState({stops: data["stops"]})
var stops_dict = this.convert_list_to_dict(this.state.stops, "name")
this.setState({stops_dict: stops_dict})
// Update driver's location
if (this.state.driver_location !== null) this.computePath()
// Update bonus driver's remaining path
if (this.state.bonus_driver_location !== null) this.computeBonusRemainingPath()
// Bonus 2: compute path time to complete
if (Object.keys(this.state.legs_dict).length !== 0 && this.state.driver_location !== null) this.computePathToComplete()
})
.catch(error => {
this.setState({alert_info: {status: true,
type: "danger",
reason: "GetStops",
message: error["message"]}})
console.error(error)
})
}
_onGetDriverButtonPressed(){
this.resetErrorMessage()
this.getData(this.backEndUrl + "driver")
.then(data => data.json())
.then(data => {
this.setState({driver_location: data["Driver’s current position"]})
// Update driver's location
if (this.state.stops !== null) this.computePath()
// Bonus 2: compute path time to complete
if (Object.keys(this.state.stops_dict).length !== 0 && Object.keys(this.state.legs_dict).length !== 0) this.computePathToComplete()
})
.catch(error => {
this.setState({alert_info: {status: true,
type: "danger",
reason: "GetDriverLoc",
message: error["message"]}})
console.error(error)
})
}
_handleSlider (props){
const { value, dragging, index, ...restProps } = props;
return (
<Tooltip
prefixCls="rc-slider-tooltip"
overlay={value + "%"}
visible={dragging}
placement="top"
key={index}
>
<Handle value={value} {...restProps} />
</Tooltip>
)
}
_onSliderChange(val){
if (this.state.newDriverLoc !== null && this.state.legs !== null){
this.resetErrorMessage()
this.setState({sliderVal: val})
var start_x = this.state.legs_dict[this.state.driver_loc_activeLegID_new]["startStop_x"]
var start_y = this.state.legs_dict[this.state.driver_loc_activeLegID_new]["startStop_y"]
var end_x = this.state.legs_dict[this.state.driver_loc_activeLegID_new]["endStop_x"]
var end_y = this.state.legs_dict[this.state.driver_loc_activeLegID_new]["endStop_y"]
var delta_x = end_x - start_x
var delta_y = end_y - start_y
var new_driver_x = start_x + delta_x * this.state.sliderVal / 100
var new_driver_y = start_y + delta_y * this.state.sliderVal / 100
this.setState({newDriverLoc:{
"x": new_driver_x,
"y": new_driver_y,
}
})
} else {
this.setState({alert_info: {status: true,
type: "warning",
reason: "changeSliderLoc",
message: "Legs, stops and driver location are reqired for this feature to work"}})
}
}
_onLegDropdownButtonPressed(isVisible){
if (isVisible){
this.resetErrorMessage()
if (this.state.legs == null || this.state.legs.length === 0){
this.setState({alert_info: {status: true,
type: "warning",
reason: "newLegID",
message: "There is no leg ID data available, get leg IDs from the server first"}})
}
}
}
_onPutDriverButtonPressed(){
this.resetErrorMessage()
if (this.state.sliderVal !== null && this.state.driver_loc_activeLegID_new !== null){
var new_driver_location = {"Driver’s current position":
{
"activeLegID": this.state.driver_loc_activeLegID_new,
"legProgress": this.state.sliderVal.toString()
}
}
this.putData(this.backEndUrl + "driver", new_driver_location)
.then(data => data.json())
.then(data => {
this.setState({driver_location: data["Driver’s current position"]})
// Update driver's location
if (this.state.driver_location !== null && this.state.stops !== null) this.computePath()
// Bonus 2: compute path time to complete
if (Object.keys(this.state.stops_dict).length !== 0 && Object.keys(this.state.legs_dict).length !== 0) this.computePathToComplete()
})
.catch(error => {
this.setState({alert_info: {status: true,
type: "danger",
reason: "OytDriverLoc",
message: error["message"]}})
console.error(error)
})
} else {
this.setState({alert_info: {status: true,
type: "warning",
reason: "putLoc",
message: "New leg progress and ID should be specified first"}})
}
}
_onBonusDriverCoordinatesChanged(newVal, axis="y"){
this.resetErrorMessage()
var txt = newVal.target.value
var err_reason = "inputNumBonusDriver"
var err_msg = "x and y are integers in [0, 200]"
var val = this.checkNumInput(txt, 0, 200, err_reason, err_msg)
// Set the proper values to corresponding coordinates
if (axis === "x"){
this.setState({bonus_driver_x_new: val})
} else {
this.setState({bonus_driver_y_new: val})
}
}
_onPutBonusDriverButtonPressed(){
this.resetErrorMessage()
if (this.state.bonus_driver_x_new !== '' && this.state.bonus_driver_y_new !== ''){
var new_bonus_driver_location = {"Driver’s current position":
{
"x": parseInt(this.state.bonus_driver_x_new),
"y": parseInt(this.state.bonus_driver_y_new)
}
}
this.putData(this.backEndUrl + "bonusdriver", new_bonus_driver_location)
.then(data => data.json())
.then(data => {
this.setState({bonus_driver_location: data["Driver’s current position"]})
// Update remaining path
if (this.state.stops !== null) this.computeBonusRemainingPath()
})
.catch(error => {
this.setState({alert_info: {status: true,
type: "danger",
reason: "PutBonusDriverLoc",
message: error["message"]}})
console.error(error)
})
} else {
this.setState({alert_info: {status: true,
type: "warning",
reason: "putLoc",
message: "New bonus driver's location should be specified first"}})
}
}
_onGetBonusDriverButtonPressed(){
this.resetErrorMessage()
this.getData(this.backEndUrl + "bonusdriver")
.then(data => data.json())
.then(data => {
this.setState({bonus_driver_location: data["Driver’s current position"]})
// Update remaining path
if (this.state.stops !== null) this.computeBonusRemainingPath()
})
.catch(error => {
this.setState({alert_info: {status: true,
type: "danger",
reason: "GetBonusDriverLoc",
message: error["message"]}})
console.error(error)
})
}
render() {
return (
<div className="App">
{/* Bonus: error handling */}
{this.state.alert_info["status"] ?
<Alert bsStyle={this.state.alert_info["type"]}>
<strong>{this.state.alert_info["type"] === "warning"? "Holy guacamole! " : "Oh snap! "}</strong>
{this.state.alert_info["message"]}
</Alert> :
null
}
<h3>Driver Location Challenge</h3>
{/* Parts 1 & 3: REST GET interface */}
<ButtonToolbar style={{display: 'flex', justifyContent:'center', alignItems:'center', marginTop: 20}}>
<Button style={{width: 150, margin: 5}} variant="flat" size="xxl" onClick={() => this._onGetLegsButtonPressed()}>
Get legs {this.state.legs == null ? <Glyphicon glyph="glyphicon glyphicon-save" /> : <Glyphicon glyph="glyphicon glyphicon-saved" />}
</Button>
<Button style={{width: 150, margin: 5}} variant="flat" size="xxl" onClick={() => this._onGetStopsButtonPressed()}>
Get stops {this.state.stops == null ? <Glyphicon glyph="glyphicon glyphicon-save" /> : <Glyphicon glyph="glyphicon glyphicon-saved" />}
</Button>
<Button style={{width: 150, margin: 5}} variant="flat" size="xxl" onClick={() => this._onGetDriverButtonPressed()}>
Get driver location {this.state.driver_location == null ? <Glyphicon glyph="glyphicon glyphicon-save" /> : <Glyphicon glyph="glyphicon glyphicon-saved" />}
</Button>
</ButtonToolbar>
{/* Parts 6, 8 & Bonus 3: Driver location update */}
<div style={{display: 'flex', justifyContent:'center', alignItems:'center', marginTop: 20}}>
<ControlLabel bsStyle="default" style={{marginRight: 10, marginTop: 10}}>New Driver's Location</ControlLabel>
<div style={{width: 300, marginRight: 5, marginLeft: 5, marginBottom:1}}>
<p style={{height: 11, marginLeft: 55}} >Leg progress</p>
<Slider min={0}
max={100}
defaultValue={this.state.sliderVal}
value={this.state.sliderVal}
disabled={this.state.sliderVal == null ? true : false}
handle={(props) => this._handleSlider(props)}
onChange={(data) => this._onSliderChange(data)}
onAfterChange={(data) => this._onSliderChange(data)}/>
</div>
<DropdownButton
style={{marginRight: 10, marginLeft: 5, width: 100}}
id="dropdown_new_leg_ID"
title={this.state.driver_loc_activeLegID_new == null ? "New leg ID" : this.state.driver_loc_activeLegID_new}
onToggle={(isVisible) => this._onLegDropdownButtonPressed(isVisible)}
>
{Object.keys(this.state.legs_dict).map(this.renderDropdownButton)}
</DropdownButton>
<Button style={{marginLeft: 5}} onClick={() => this._onPutDriverButtonPressed()}>
Put location
</Button>
</div>
{/* Part 4 & others: Vizualization of legs, paths, driver current location, driver new location and bonus driver location */}
<div style={{display: 'flex', justifyContent:'center', alignItems:'center', marginTop: 20, marginBottom: 0 }}>
<h4><Label>City Map</Label></h4>
</div>
<div style={{display: 'flex', justifyContent:'center', alignItems:'center'}}>
<ComposedChart width={650} height={650} >
<CartesianGrid />
<XAxis dataKey={'x'} type="number" name='x' domain={[0, 200]}/>
<YAxis dataKey={'y'} type="number" name='y' domain={[0, 200]}/>
<Scatter name='stops' data={this.state.stops} fill='#8884d8' shape="cross" fillOpacity={0.7} legendType={this.state.stops == null ? "none" : "cross" }>
<LabelList dataKey="name" position="top" content={(labelData) => this.formatFigureLabel(labelData)}/>
</Scatter>
<Line name="path" dataKey="y" data={this.state.driver_path} fillOpacity={0.7} dot={false} legendType={this.state.driver_path.length ===0 ? "none" : "line" }/>
<Scatter data={this.state.driver_path.length === 0 ? null : [this.state.driver_path[this.state.driver_path.length-1]]} fill='#FF4500' shape="star" fillOpacity={0.60} legendType="none"/>
<Scatter name='driver' data={this.state.newDriverLoc == null ? null : [this.state.newDriverLoc]} fill='#FF4500' shape="star" legendType="star" fillOpacity={0.95} legendType={this.state.newDriverLoc == null ? "none" : "star"}/>
<Scatter name='bonus driver' data={this.state.bonus_driver_location == null ? null : [this.state.bonus_driver_location]} fill='#FF00FF' shape="diamond" fillOpacity={0.85} legendType={this.state.bonus_driver_location == null ? "none" : "diamond" }/>
<Line name="bonus path to complete" dataKey="y" data={this.state.path_to_complete} fillOpacity={0.7} dot={false} strokeDasharray="5 5" stroke="#FF00FF" legendType={this.state.path_to_complete == null ? "none" : "line" }/>
{/* <Tooltip cursor={{strokeDasharray: '3 3'}} content={(data) => this.renderTooltip(data)}/> */}
{/* <Tooltip content={({payload}) => (<div>{JSON.stringify(payload[0])}</div>)} /> */}
<Legend height={5} align="center" iconSize={10}/>
</ComposedChart>
</div>
{/* Bonus 1: Bonus driver location */}
<Form componentClass="fieldset" inline style={{marginTop: 40}}>
<FormGroup>
<ControlLabel bsStyle="default" style={{marginRight: 10}}><Label bsStyle="info">Bonus 1</Label> Bonus Driver's Location</ControlLabel>
<FormControl
style={{width: 130}}
type="text"
value={this.state.bonus_driver_x_new}
placeholder="Enter x"
onChange={(newVal) => this._onBonusDriverCoordinatesChanged(newVal, "x")}
/>
<FormControl
type="text"
value={this.state.bonus_driver_y_new}
placeholder="Enter y"
style={{width: 130}}
onChange={(newVal) => this._onBonusDriverCoordinatesChanged(newVal, "y")}
/>
</FormGroup>
<Button style={{marginLeft: 10}} onClick={() => this._onPutBonusDriverButtonPressed()}>
Put location
</Button>
<Button style={{marginLeft: 10}} onClick={() => this._onGetBonusDriverButtonPressed()}>
Get location {this.state.bonus_driver_location == null ? <Glyphicon glyph="glyphicon glyphicon-save" /> : <Glyphicon glyph="glyphicon glyphicon-saved" />}
</Button>
</Form>
{/* Bonus 2: Path completion time */}
<ControlLabel bsStyle="default" style={{marginTop: 30, marginRight: 45}}><Label bsStyle="info">Bonus 2</Label> Driver's Trip Data</ControlLabel>
<div style={{display: 'flex', justifyContent:'center', alignItems:'center'}}>
<ControlLabel bsStyle="default" style={{marginRight: 10}}>Time to complete the entire path: </ControlLabel>
<h4><Label>{this.state.bonus2_time_complete_all_legs == null ? "*" : (this.state.bonus2_time_complete_all_legs).toFixed(2)}</Label></h4>
</div>
<div style={{display: 'flex', justifyContent:'center', alignItems:'center'}}>
<ControlLabel bsStyle="default" style={{marginRight: 35}}>Time left to complete the trip:</ControlLabel>
<h4><Label>{this.state.bonus2_time_complete == null ? "*" : this.state.bonus2_time_complete.toFixed(2)}</Label></h4>
</div>
<Button style={{marginLeft: 10}} onClick={() => this.computePathToComplete()}>
Compute time {this.state.bonus2_time_complete_all_legs == null ? <Glyphicon glyph="glyphicon glyphicon-save" /> : <Glyphicon glyph="glyphicon glyphicon-saved" />}
</Button>
</div>
);
}
}
export default App;