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;
}
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;
}
Aggregations