Search in sources :

Example 16 with XYSeries

use of org.knowm.xchart.XYSeries in project openhab-core by openhab.

the class DefaultChartProvider method addItem.

private boolean addItem(XYChart chart, QueryablePersistenceService service, ZonedDateTime timeBegin, ZonedDateTime timeEnd, Item item, int seriesCounter, ChartTheme chartTheme, int dpi, LegendPositionDecider legendPositionDecider) {
    Color color = chartTheme.getLineColor(seriesCounter);
    // Get the item label
    String label = itemUIRegistry.getLabel(item.getName());
    if (label == null) {
        label = item.getName();
    } else if (label.contains("[") && label.contains("]")) {
        label = label.substring(0, label.indexOf('['));
    }
    Iterable<HistoricItem> result;
    FilterCriteria filter;
    // Generate data collections
    List<Date> xData = new ArrayList<>();
    List<Number> yData = new ArrayList<>();
    // Declare state here so it will hold the last value at the end of the process
    State state = null;
    // First, get the value at the start time.
    // This is necessary for values that don't change often otherwise data will start
    // after the start of the graph (or not at all if there's no change during the graph period)
    filter = new FilterCriteria();
    filter.setEndDate(timeBegin);
    filter.setItemName(item.getName());
    filter.setPageSize(1);
    filter.setOrdering(Ordering.DESCENDING);
    result = service.query(filter);
    if (result.iterator().hasNext()) {
        HistoricItem historicItem = result.iterator().next();
        state = historicItem.getState();
        xData.add(Date.from(timeBegin.toInstant()));
        yData.add(convertData(state));
    }
    // Now, get all the data between the start and end time
    filter.setBeginDate(timeBegin);
    filter.setEndDate(timeEnd);
    filter.setPageSize(Integer.MAX_VALUE);
    filter.setOrdering(Ordering.ASCENDING);
    // Get the data from the persistence store
    result = service.query(filter);
    // Iterate through the data
    for (HistoricItem historicItem : result) {
        // to avoid diagonal lines
        if (state instanceof OnOffType || state instanceof OpenClosedType) {
            xData.add(Date.from(historicItem.getTimestamp().toInstant().minus(1, ChronoUnit.MILLIS)));
            yData.add(convertData(state));
        }
        state = historicItem.getState();
        xData.add(Date.from(historicItem.getTimestamp().toInstant()));
        yData.add(convertData(state));
    }
    // Lastly, add the final state at the endtime
    if (state != null) {
        xData.add(Date.from(timeEnd.toInstant()));
        yData.add(convertData(state));
    }
    // The chart engine will throw an exception if there's no data
    if (xData.isEmpty()) {
        return false;
    }
    // If there's only 1 data point, plot it again!
    if (xData.size() == 1) {
        xData.add(xData.iterator().next());
        yData.add(yData.iterator().next());
    }
    XYSeries series = chart.addSeries(label, xData, yData);
    float lineWidth = (float) chartTheme.getLineWidth(dpi);
    series.setLineStyle(new BasicStroke(lineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
    series.setMarker(new None());
    series.setLineColor(color);
    legendPositionDecider.addData(series, yData);
    return true;
}
Also used : XYSeries(org.knowm.xchart.XYSeries) BasicStroke(java.awt.BasicStroke) Color(java.awt.Color) ArrayList(java.util.ArrayList) FilterCriteria(org.openhab.core.persistence.FilterCriteria) Date(java.util.Date) OnOffType(org.openhab.core.library.types.OnOffType) State(org.openhab.core.types.State) OpenClosedType(org.openhab.core.library.types.OpenClosedType) HistoricItem(org.openhab.core.persistence.HistoricItem) None(org.knowm.xchart.style.markers.None)

Example 17 with XYSeries

use of org.knowm.xchart.XYSeries in project dialogue by palantir.

the class SimulationMetricsReporter method createBasicChart.

private XYChart createBasicChart() {
    XYChart chart = new XYChartBuilder().width(800).height(600).xAxisTitle(X_AXIS.safeName()).build();
    chart.getStyler().setLegendPosition(Styler.LegendPosition.InsideNW);
    chart.getStyler().setDefaultSeriesRenderStyle(XYSeries.XYSeriesRenderStyle.Line);
    chart.getStyler().setMarkerSize(3);
    chart.getStyler().setYAxisLabelAlignment(Styler.TextAlignment.Right);
    chart.getStyler().setPlotMargin(0);
    chart.getStyler().setPlotContentSize(.95);
    chart.getStyler().setToolTipsEnabled(true);
    chart.getStyler().setToolTipsAlwaysVisible(true);
    if (!simulation.events().getEvents().isEmpty()) {
        double[] eventXs = simulation.events().getEvents().keySet().stream().mapToDouble(SimulationMetricsReporter::nanosToFractionalSeconds).toArray();
        double[] eventYs = new double[eventXs.length];
        String[] strings = simulation.events().getEvents().values().stream().toArray(String[]::new);
        XYSeries what = chart.addSeries(" ", eventXs, eventYs);
        what.setToolTips(strings);
        what.setXYSeriesRenderStyle(XYSeries.XYSeriesRenderStyle.Scatter);
    }
    return chart;
}
Also used : XYSeries(org.knowm.xchart.XYSeries) XYChart(org.knowm.xchart.XYChart) XYChartBuilder(org.knowm.xchart.XYChartBuilder)

Aggregations

XYSeries (org.knowm.xchart.XYSeries)17 XYChart (org.knowm.xchart.XYChart)12 XYChartBuilder (org.knowm.xchart.XYChartBuilder)12 ArrayList (java.util.ArrayList)9 Exchange (org.knowm.xchange.Exchange)8 SwingWrapper (org.knowm.xchart.SwingWrapper)8 BigDecimal (java.math.BigDecimal)5 Date (java.util.Date)5 OrderBook (org.knowm.xchange.dto.marketdata.OrderBook)5 LimitOrder (org.knowm.xchange.dto.trade.LimitOrder)5 MarketDataService (org.knowm.xchange.service.marketdata.MarketDataService)5 BasicStroke (java.awt.BasicStroke)3 ExchangeSpecification (org.knowm.xchange.ExchangeSpecification)3 BitcoiniumExchange (org.knowm.xchange.bitcoinium.BitcoiniumExchange)3 BitcoiniumTicker (org.knowm.xchange.bitcoinium.dto.marketdata.BitcoiniumTicker)3 IOException (java.io.IOException)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 BitcoiniumTickerHistory (org.knowm.xchange.bitcoinium.dto.marketdata.BitcoiniumTickerHistory)2 BitcoiniumMarketDataServiceRaw (org.knowm.xchange.bitcoinium.service.BitcoiniumMarketDataServiceRaw)2