|
| 1 | +package edu.nr; |
| 2 | + |
| 3 | +import dashfx.lib.controls.Category; |
| 4 | +import dashfx.lib.controls.Control; |
| 5 | +import dashfx.lib.controls.Designable; |
| 6 | +import dashfx.lib.data.DataCoreProvider; |
| 7 | +import dashfx.lib.data.SmartValue; |
| 8 | +import dashfx.lib.data.SupportedTypes; |
| 9 | +import javafx.beans.property.SimpleStringProperty; |
| 10 | +import javafx.beans.property.StringProperty; |
| 11 | +import javafx.beans.value.ChangeListener; |
| 12 | +import javafx.beans.value.ObservableValue; |
| 13 | +import javafx.event.ActionEvent; |
| 14 | +import javafx.event.EventHandler; |
| 15 | +import javafx.geometry.Pos; |
| 16 | +import javafx.scene.Node; |
| 17 | +import javafx.scene.chart.LineChart; |
| 18 | +import javafx.scene.chart.NumberAxis; |
| 19 | +import javafx.scene.control.Button; |
| 20 | +import javafx.scene.layout.GridPane; |
| 21 | + |
| 22 | +@Designable(value="SmartChart", image = "/smartchart.png", description="Uses built-in graph and manual list storing. Includes a reset button (wow!)") |
| 23 | +@SupportedTypes({dashfx.lib.data.SmartValueTypes.Number}) |
| 24 | +@Category("General") |
| 25 | +public class SmartChart |
| 26 | + extends GridPane |
| 27 | + implements Control, ChangeListener<Object> |
| 28 | +{ |
| 29 | + StringProperty name = new SimpleStringProperty(); |
| 30 | + |
| 31 | + @Designable(value="Name", description="The name the control binds to") |
| 32 | + public StringProperty nameProperty() |
| 33 | + { |
| 34 | + return this.name; |
| 35 | + } |
| 36 | + |
| 37 | + public String getName() |
| 38 | + { |
| 39 | + return this.name.getValue(); |
| 40 | + } |
| 41 | + |
| 42 | + public void setName(String value) |
| 43 | + { |
| 44 | + this.name.setValue(value); |
| 45 | + } |
| 46 | + |
| 47 | + ChartImpl chartImpl; |
| 48 | + |
| 49 | + public SmartChart() |
| 50 | + { |
| 51 | + setAlignment(Pos.CENTER); |
| 52 | + |
| 53 | + chartImpl = new ChartImpl(); |
| 54 | + add(chartImpl, 0, 0, 3, 1); |
| 55 | + |
| 56 | + Button resetButton = new Button("Reset Graph"); |
| 57 | + resetButton.setOnAction(new EventHandler<ActionEvent>() |
| 58 | + { |
| 59 | + @Override |
| 60 | + public void handle(ActionEvent event) |
| 61 | + { |
| 62 | + chartImpl.reset(); |
| 63 | + } |
| 64 | + }); |
| 65 | + add(resetButton, 0, 1, 3, 1); |
| 66 | + } |
| 67 | + |
| 68 | + public void registered(final DataCoreProvider provider) |
| 69 | + { |
| 70 | + if (getName() != null) { |
| 71 | + provider.getObservable(getName()).addListener(this); |
| 72 | + } |
| 73 | + this.name.addListener(new ChangeListener<String>() |
| 74 | + { |
| 75 | + public void changed(ObservableValue<? extends String> ov, String t, String t1) |
| 76 | + { |
| 77 | + if (t != null) { |
| 78 | + provider.getObservable(t).removeListener(SmartChart.this); |
| 79 | + } |
| 80 | + provider.getObservable(t1).addListener(SmartChart.this); |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + public void changed(ObservableValue<? extends Object> ov, Object old, Object t1) |
| 86 | + { |
| 87 | + SmartValue sv = (SmartValue)ov; |
| 88 | + double x = sv.getData().asNumber().doubleValue(); |
| 89 | + |
| 90 | + chartImpl.addValue(Double.valueOf(x)); |
| 91 | + } |
| 92 | + |
| 93 | + public Node getUi() |
| 94 | + { |
| 95 | + return this; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +class ChartImpl extends LineChart<Number, Number> |
| 100 | +{ |
| 101 | + private Series series = new Series(); |
| 102 | + private long startTimeMillis; |
| 103 | + |
| 104 | + public ChartImpl() |
| 105 | + { |
| 106 | + super(new NumberAxis(), new NumberAxis()); |
| 107 | + setAnimated(false); |
| 108 | + ((NumberAxis)getXAxis()).setForceZeroInRange(false); |
| 109 | + ((NumberAxis)getYAxis()).setForceZeroInRange(false); |
| 110 | + setLegendVisible(false); |
| 111 | + getData().add(this.series); |
| 112 | + } |
| 113 | + |
| 114 | + public void addValue(double x) |
| 115 | + { |
| 116 | + if(this.series.getData().size() == 0) |
| 117 | + { |
| 118 | + startTimeMillis = System.currentTimeMillis(); |
| 119 | + } |
| 120 | + |
| 121 | + double currentTime = (System.currentTimeMillis() - startTimeMillis)/1000d; |
| 122 | + |
| 123 | + |
| 124 | + this.series.getData().add(new Data(currentTime, x)); |
| 125 | + if (this.series.getData().size() > 500) { |
| 126 | + this.series.getData().remove(0); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + public void reset() |
| 131 | + { |
| 132 | + this.series.getData().clear(); |
| 133 | + } |
| 134 | +} |
| 135 | + |
0 commit comments