/** * Create a new Candle node to represent a single data item * * @param seriesIndex The index of the series the data item is in * @param item The data item to create node for * @param itemIndex The index of the data item in the series * @return New candle node to represent the give data item */ private Node createCandle(int seriesIndex, final Data item, int itemIndex) { Node candle = item.getNode(); // check if candle has already been created if (candle instanceof Candle) { ((Candle) candle).setSeriesAndDataStyleClasses("series" + seriesIndex, "data" + itemIndex); } else { candle = new Candle("series" + seriesIndex, "data" + itemIndex); item.setNode(candle); } return candle; }
@Override protected void dataItemRemoved(Data<String, Number> item, Series<String, Number> series) { final Node candle = item.getNode(); if (shouldAnimate()) { // fade out old candle FadeTransition ft = new FadeTransition(Duration.millis(500), candle); ft.setToValue(0); ft.setOnFinished( (ActionEvent actionEvent) -> { getPlotChildren().remove(candle); }); ft.play(); } else { getPlotChildren().remove(candle); } }
/** Called to update and layout the content for the plot */ @Override protected void layoutPlotChildren() { // we have nothing to layout if no data is present if (getData() == null) { return; } // update candle positions for (int seriesIndex = 0; seriesIndex < getData().size(); seriesIndex++) { Series<String, Number> series = getData().get(seriesIndex); Iterator<Data<String, Number>> iter = getDisplayedDataIterator(series); Path seriesPath = null; if (series.getNode() instanceof Path) { seriesPath = (Path) series.getNode(); seriesPath.getElements().clear(); } while (iter.hasNext()) { Data<String, Number> item = iter.next(); double x = getXAxis().getDisplayPosition(getCurrentDisplayedXValue(item)); double y = getYAxis().getDisplayPosition(getCurrentDisplayedYValue(item)); Node itemNode = item.getNode(); BarData bar = (BarData) item.getExtraValue(); if (itemNode instanceof Candle && item.getYValue() != null) { Candle candle = (Candle) itemNode; double close = getYAxis().getDisplayPosition(bar.getClose()); double high = getYAxis().getDisplayPosition(bar.getHigh()); double low = getYAxis().getDisplayPosition(bar.getLow()); double candleWidth = 10; // update candle candle.update(close - y, high - y, low - y, candleWidth); // update tooltip content candle.updateTooltip(bar.getOpen(), bar.getClose(), bar.getHigh(), bar.getLow()); // position the candle candle.setLayoutX(x); candle.setLayoutY(y); } } } }
public void received(Data data) { // the specific application logic behaves w.r.t. the type of data received System.out.println(data); switch (data.getFunctionCode()) { case SPINEFunctionConstants.FEATURE: { features = ((FeatureData) data).getFeatures(); counter++; // even this simple application shows us up some nice SPINE properties; in fact ... if (counter == 5) { // it's possible to deactivate functions computation at runtime (even when the radio on // the node works in low-power mode) FeatureSpineFunctionReq sfr = new FeatureSpineFunctionReq(); sfr.setSensor(features[0].getSensorCode()); sfr.remove(new Feature(features[0].getFeatureCode(), SPINESensorConstants.ALL)); manager.deactivate(data.getNode(), sfr); } if (counter == 10) { // and, of course, we can activate new functions at runtime FeatureSpineFunctionReq sfr = new FeatureSpineFunctionReq(); sfr.setSensor(features[0].getSensorCode()); sfr.add(new Feature(SPINEFunctionConstants.RANGE, SPINESensorConstants.CH1_ONLY)); manager.activate(data.getNode(), sfr); } if (counter == 20) { // when we are set, we can decide to ... // stop the WSN, forcing a 'software' reset of the nodes /* manager.resetWsn(); */ // or just deregister ourself to further SPINE events. // manager.deregisterListener(this); } break; } case SPINEFunctionConstants.ONE_SHOT: // OneShotData oneShot = (OneShotData)data; // if needed 'data' can be casted to // spine.datamodel.OneShotData break; case SPINEFunctionConstants.ALARM: // AlarmData alarm = (AlarmData)data; // if needed 'data' can be casted to // spine.datamodel.AlarmData counter_alarm++; if (counter_alarm == 20) { AlarmSpineFunctionReq sfr2 = new AlarmSpineFunctionReq(); sfr2.setSensor(SPINESensorConstants.ACC_SENSOR); sfr2.setAlarmType(SPINEFunctionConstants.ABOVE_THRESHOLD); sfr2.setDataType(SPINEFunctionConstants.MAX); sfr2.setValueType((SPINESensorConstants.CH1_ONLY)); manager.deactivate(data.getNode(), sfr2); } break; } }