Search in sources :

Example 1 with ArrayPlotDataProvider

use of org.csstudio.javafx.rtplot.data.ArrayPlotDataProvider in project org.csstudio.display.builder by kasemir.

the class BasicPlotDemo method start.

@Override
public void start(final Stage stage) throws Exception {
    Logger.getLogger("").setLevel(Level.FINE);
    for (Handler handler : Logger.getLogger("").getHandlers()) handler.setLevel(Level.FINE);
    final Plot<Double> plot = new Plot<Double>(Double.class, true);
    plot.setTitle("Plot Demo");
    plot.getXAxis().setName("The horizontal quantities on 'X'");
    plot.addYAxis("Another Axis");
    plot.getYAxes().get(1).setOnRight(true);
    final ArrayPlotDataProvider<Double> data1 = new ArrayPlotDataProvider<>();
    final ArrayPlotDataProvider<Double> data2 = new ArrayPlotDataProvider<>();
    final ArrayPlotDataProvider<Double> data3 = new ArrayPlotDataProvider<>();
    for (double x = -10.0; x <= 10.0; x += 1.0) if (x == 2.0) {
        data1.add(new SimpleDataItem<Double>(x, Double.NaN));
        data2.add(new SimpleDataItem<Double>(x, Double.NaN));
        data3.add(new SimpleDataItem<Double>(x, Double.NaN));
    } else {
        data1.add(new SimpleDataItem<Double>(x, x * x - 5.0));
        data2.add(new SimpleDataItem<Double>(x, 2 * x));
        data3.add(new SimpleDataItem<Double>(x, x * x + 5.0));
    }
    plot.addTrace(new TraceImpl<Double>("Demo Data", "socks", data1, Color.BLUE, TraceType.BARS, 0, PointType.NONE, 15, 0));
    plot.addTrace(new TraceImpl<Double>("Demo Data", "socks", data1, Color.VIOLET, TraceType.BARS, 10, PointType.NONE, 15, 0));
    plot.addTrace(new TraceImpl<Double>("More Data", "pants", data2, Color.RED, TraceType.AREA, 3, PointType.SQUARES, 15, 1));
    plot.addTrace(new TraceImpl<Double>("More Data", "pants", data3, Color.GREEN, TraceType.LINES_DIRECT, 1, PointType.XMARKS, 5, 0));
    plot.getXAxis().setValueRange(-12.0, 12.0);
    // a) Fixed range
    // plot.getYAxes().get(0).setValueRange(-10.0, 120.0);
    // plot.getYAxes().get(1).setValueRange(-25.0, 25.0);
    // b) Autoscale
    // plot.getYAxes().get(0).setAutoscale(true);
    // plot.getYAxes().get(1).setAutoscale(true);
    // c) Stagger
    plot.stagger(false);
    plot.showCrosshair(true);
    plot.setMouseMode(MouseMode.PAN);
    final Pane root = new Pane(plot);
    final ChangeListener<? super Number> resize_listener = (p, o, n) -> plot.setSize(root.getWidth(), root.getHeight());
    root.widthProperty().addListener(resize_listener);
    root.heightProperty().addListener(resize_listener);
    final Scene scene = new Scene(root, 800, 600);
    stage.setScene(scene);
    stage.setTitle("Basic Plot Demo");
    stage.show();
    // Thread that periodically hides an axis and its traces
    final AtomicBoolean run = new AtomicBoolean(true);
    final Runnable hider = () -> {
        try {
            while (run.get()) {
                TimeUnit.SECONDS.sleep(2);
                final int axis_index = 1;
                final YAxisImpl<Double> axis = plot.getYAxes().get(axis_index);
                final boolean visible = !axis.isVisible();
                for (Trace<?> trace : plot.getTraces()) if (trace.getYAxis() == axis_index)
                    trace.setVisible(visible);
                axis.setVisible(visible);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    };
    final Thread thread = new Thread(hider);
    thread.setDaemon(true);
    thread.start();
    stage.setOnCloseRequest(event -> {
        run.set(false);
        try {
            thread.join();
        } catch (Exception ex) {
        // Ignore, shutting down
        }
        plot.dispose();
    });
}
Also used : Scene(javafx.scene.Scene) Color(javafx.scene.paint.Color) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Logger(java.util.logging.Logger) Level(java.util.logging.Level) TimeUnit(java.util.concurrent.TimeUnit) Application(javafx.application.Application) Stage(javafx.stage.Stage) TraceImpl(org.csstudio.javafx.rtplot.internal.TraceImpl) SimpleDataItem(org.csstudio.javafx.rtplot.data.SimpleDataItem) YAxisImpl(org.csstudio.javafx.rtplot.internal.YAxisImpl) MouseMode(org.csstudio.javafx.rtplot.internal.MouseMode) Handler(java.util.logging.Handler) ArrayPlotDataProvider(org.csstudio.javafx.rtplot.data.ArrayPlotDataProvider) Plot(org.csstudio.javafx.rtplot.internal.Plot) ChangeListener(javafx.beans.value.ChangeListener) Pane(javafx.scene.layout.Pane) SimpleDataItem(org.csstudio.javafx.rtplot.data.SimpleDataItem) Plot(org.csstudio.javafx.rtplot.internal.Plot) ArrayPlotDataProvider(org.csstudio.javafx.rtplot.data.ArrayPlotDataProvider) Handler(java.util.logging.Handler) Scene(javafx.scene.Scene) Pane(javafx.scene.layout.Pane) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) YAxisImpl(org.csstudio.javafx.rtplot.internal.YAxisImpl)

Example 2 with ArrayPlotDataProvider

use of org.csstudio.javafx.rtplot.data.ArrayPlotDataProvider in project org.csstudio.display.builder by kasemir.

the class TraceAnalyzerTest method testFindSample.

@Test
public void testFindSample() throws Exception {
    final ArrayPlotDataProvider<Instant> data = new ArrayPlotDataProvider<>();
    for (int i = 0; i <= 10; ++i) data.add(new SimpleDataItem<Instant>(Instant.ofEpochSecond(10 * i), 10.0 * i - 50));
    for (int i = 0; i < data.size(); ++i) System.out.println(data.get(i));
    TimeDataSearch search = new TimeDataSearch();
    int index = search.findClosestSample(data, Instant.ofEpochSecond(39));
    assertThat(index, not(equalTo(-1)));
    PlotDataItem<Instant> sample = data.get(index);
    assertThat(sample.getPosition().getEpochSecond(), equalTo(40L));
    assertThat(sample.getValue(), equalTo(-10.0));
    index = search.findClosestSample(data, Instant.ofEpochSecond(32));
    assertThat(index, not(equalTo(-1)));
    sample = data.get(index);
    assertThat(sample.getPosition().getEpochSecond(), equalTo(30L));
    assertThat(sample.getValue(), equalTo(-20.0));
}
Also used : SimpleDataItem(org.csstudio.javafx.rtplot.data.SimpleDataItem) Instant(java.time.Instant) ArrayPlotDataProvider(org.csstudio.javafx.rtplot.data.ArrayPlotDataProvider) TimeDataSearch(org.csstudio.javafx.rtplot.data.TimeDataSearch) Test(org.junit.Test)

Aggregations

ArrayPlotDataProvider (org.csstudio.javafx.rtplot.data.ArrayPlotDataProvider)2 SimpleDataItem (org.csstudio.javafx.rtplot.data.SimpleDataItem)2 Instant (java.time.Instant)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Handler (java.util.logging.Handler)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Application (javafx.application.Application)1 ChangeListener (javafx.beans.value.ChangeListener)1 Scene (javafx.scene.Scene)1 Pane (javafx.scene.layout.Pane)1 Color (javafx.scene.paint.Color)1 Stage (javafx.stage.Stage)1 TimeDataSearch (org.csstudio.javafx.rtplot.data.TimeDataSearch)1 MouseMode (org.csstudio.javafx.rtplot.internal.MouseMode)1 Plot (org.csstudio.javafx.rtplot.internal.Plot)1 TraceImpl (org.csstudio.javafx.rtplot.internal.TraceImpl)1 YAxisImpl (org.csstudio.javafx.rtplot.internal.YAxisImpl)1 Test (org.junit.Test)1