Commit c01c1378 authored by Julian Zobel's avatar Julian Zobel Committed by Björn Richerzhagen
Browse files

LivePlot Viz:

Now with correct time representation on y-Axis (H:m:s)
Now showing 0 as 0 (and not 0.000000) ;)


(cherry picked from commit dd8884a6)
parent 0ce6cda0
...@@ -27,7 +27,9 @@ import java.text.DecimalFormat; ...@@ -27,7 +27,9 @@ import java.text.DecimalFormat;
import java.text.FieldPosition; import java.text.FieldPosition;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.text.ParsePosition; import java.text.ParsePosition;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel; import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartRenderingInfo; import org.jfree.chart.ChartRenderingInfo;
...@@ -48,8 +50,14 @@ import org.jfree.data.xy.YIntervalSeriesCollection; ...@@ -48,8 +50,14 @@ import org.jfree.data.xy.YIntervalSeriesCollection;
* Assists in creating a very simple XYSeries plot. It basically * Assists in creating a very simple XYSeries plot. It basically
* just holds a reference to the JFreeChart, ChartPanel and the dataset. * just holds a reference to the JFreeChart, ChartPanel and the dataset.
* *
* @author Fabio Zöllner * -----------------
* @version 1.0, 30.07.2012 * UPDATE (JZ):
* - Implemented a new {@link NumberFormat} to correctly show the time in H:m:s on the y/time-axis {@link TimeFormat}.
* - Added a new {@link NumberTickUnit} to represent 0 as 0 (and not 0.0000000).
*
* @author Fabio Zöllner, Julian Zobel
* @version 1.1, 27.09.2018
*
*/ */
public class XYChart { public class XYChart {
private JFreeChart chart; private JFreeChart chart;
...@@ -70,28 +78,24 @@ public class XYChart { ...@@ -70,28 +78,24 @@ public class XYChart {
XYPlot plot = (XYPlot) chart.getPlot(); XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(plotBackgroundColor); plot.setBackgroundPaint(plotBackgroundColor);
NumberAxis axis = ((NumberAxis)plot.getRangeAxis()); // set x-Axis tick units
NumberAxis axis = ((NumberAxis) plot.getRangeAxis());
//axis.setLabelInsets(insets);
//axis.setAutoRange(false);
//axis.setRange(0, 900);
//((NumberAxis)plot.getRangeAxis()).setNumberFormatOverride(new TruncatingNumberFormater());
axis.setStandardTickUnits(createTickUnitSource()); axis.setStandardTickUnits(createTickUnitSource());
//NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); // set y-Axis (time!) tick units
//rangeAxis.setStandardTickUnits(createTickUnitSource()); //NumberAxis.createIntegerTickUnits()); NumberAxis y = ((NumberAxis) plot.getDomainAxis());
//rangeAxis.setLabelFont(new Font("monospace", Font.PLAIN, 9)); TickUnits units = new TickUnits();
//plot.getRangeAxis(0).setUpperMargin(10); TimeFormat tf = new TimeFormat();
//plot.getRangeAxis(0).setLowerMargin(10); units.add(new NumberTickUnit(1, tf));
units.add(new NumberTickUnit(10, tf));
// LegendItemCollection lic = new LegendItemCollection(); units.add(new NumberTickUnit(100, tf));
// lic.add(new LegendItem("Reference Value", Color.blue)); units.add(new NumberTickUnit(1000, tf));
// lic.add(new LegendItem("Avg. Monitored Value", Color.red)); units.add(new NumberTickUnit(10000, tf));
units.add(new NumberTickUnit(100000, tf));
// plot.setFixedLegendItems(lic); units.add(new NumberTickUnit(1000000, tf));
//plot.setFixedLegendItems(new LegendItemCollection()); y.setStandardTickUnits(units);
//
DeviationRenderer errorRenderer = new DeviationRenderer(true, true); DeviationRenderer errorRenderer = new DeviationRenderer(true, true);
errorRenderer.setSeriesStroke(0, new BasicStroke(3F, 1, 1)); errorRenderer.setSeriesStroke(0, new BasicStroke(3F, 1, 1));
errorRenderer.setSeriesStroke(1, new BasicStroke(3F, 1, 1)); errorRenderer.setSeriesStroke(1, new BasicStroke(3F, 1, 1));
...@@ -102,13 +106,7 @@ public class XYChart { ...@@ -102,13 +106,7 @@ public class XYChart {
errorRenderer.setSeriesPaint(0, new Color(255, 0, 0)); errorRenderer.setSeriesPaint(0, new Color(255, 0, 0));
errorRenderer.setSeriesPaint(1, new Color(0, 0, 255)); errorRenderer.setSeriesPaint(1, new Color(0, 0, 255));
errorRenderer.setSeriesPaint(2, new Color(0, 255, 0)); errorRenderer.setSeriesPaint(2, new Color(0, 255, 0));
//errorRenderer.setShapesVisible(false);
//errorRenderer.setLinesVisible(true);
//errorRenderer.setAlpha(0.0f);
// errorRenderer.setDrawYError(false);
// errorRenderer.setDrawXError(false);
plot.setRenderer(errorRenderer); plot.setRenderer(errorRenderer);
chartPanel = new ChartPanel(chart, false); chartPanel = new ChartPanel(chart, false);
...@@ -134,22 +132,31 @@ public class XYChart { ...@@ -134,22 +132,31 @@ public class XYChart {
return plot.getDomainAxis(); return plot.getDomainAxis();
} }
private static class TruncatingNumberFormater extends NumberFormat { private static class TimeFormat extends NumberFormat {
public TimeFormat() {
super();
}
@Override @Override
public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) { public StringBuffer format(double number, StringBuffer toAppendTo,
// TODO Auto-generated method stub FieldPosition pos) {
return null; return this.format((long)number, toAppendTo, pos);
} }
@Override @Override
public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) { public StringBuffer format(long number, StringBuffer toAppendTo,
// TODO Auto-generated method stub FieldPosition pos) {
return null;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("H:mm:ss").withZone((ZoneId.of("UTC")));
Instant instant = Instant.ofEpochMilli(number * 1000);
String time = formatter.format(instant);
return toAppendTo.append(time);
} }
@Override @Override
public Number parse(String source, ParsePosition pos) { public Number parse(String source, ParsePosition parsePosition) {
return null; return Double.valueOf(source);
} }
} }
...@@ -168,6 +175,8 @@ public class XYChart { ...@@ -168,6 +175,8 @@ public class XYChart {
DecimalFormat df8 = new DecimalFormat("#,##0"); DecimalFormat df8 = new DecimalFormat("#,##0");
DecimalFormat df9 = new DecimalFormat("#,###,##0"); DecimalFormat df9 = new DecimalFormat("#,###,##0");
DecimalFormat df10 = new DecimalFormat("#,###,###,##0"); DecimalFormat df10 = new DecimalFormat("#,###,###,##0");
units.add(new NumberTickUnit(0.00000001, new DecimalFormat("0")));
// we can add the units in any order, the TickUnits collection will // we can add the units in any order, the TickUnits collection will
// sort them... // sort them...
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment