Commit dd8884a6 authored by Julian Zobel's avatar Julian Zobel
Browse files

LivePlot Viz:

Now with correct time representation on y-Axis (H:m:s)
Now showing 0 as 0 (and not 0.000000) ;)
parent 465f18d1
......@@ -27,7 +27,9 @@ import java.text.DecimalFormat;
import java.text.FieldPosition;
import java.text.NumberFormat;
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.ChartPanel;
import org.jfree.chart.ChartRenderingInfo;
......@@ -48,8 +50,14 @@ import org.jfree.data.xy.YIntervalSeriesCollection;
* Assists in creating a very simple XYSeries plot. It basically
* 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 {
private JFreeChart chart;
......@@ -70,28 +78,24 @@ public class XYChart {
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(plotBackgroundColor);
NumberAxis axis = ((NumberAxis)plot.getRangeAxis());
//axis.setLabelInsets(insets);
//axis.setAutoRange(false);
//axis.setRange(0, 900);
//((NumberAxis)plot.getRangeAxis()).setNumberFormatOverride(new TruncatingNumberFormater());
// set x-Axis tick units
NumberAxis axis = ((NumberAxis) plot.getRangeAxis());
axis.setStandardTickUnits(createTickUnitSource());
//NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
//rangeAxis.setStandardTickUnits(createTickUnitSource()); //NumberAxis.createIntegerTickUnits());
//rangeAxis.setLabelFont(new Font("monospace", Font.PLAIN, 9));
//plot.getRangeAxis(0).setUpperMargin(10);
//plot.getRangeAxis(0).setLowerMargin(10);
// LegendItemCollection lic = new LegendItemCollection();
// lic.add(new LegendItem("Reference Value", Color.blue));
// lic.add(new LegendItem("Avg. Monitored Value", Color.red));
// plot.setFixedLegendItems(lic);
//plot.setFixedLegendItems(new LegendItemCollection());
// set y-Axis (time!) tick units
NumberAxis y = ((NumberAxis) plot.getDomainAxis());
TickUnits units = new TickUnits();
TimeFormat tf = new TimeFormat();
units.add(new NumberTickUnit(1, tf));
units.add(new NumberTickUnit(10, tf));
units.add(new NumberTickUnit(100, tf));
units.add(new NumberTickUnit(1000, tf));
units.add(new NumberTickUnit(10000, tf));
units.add(new NumberTickUnit(100000, tf));
units.add(new NumberTickUnit(1000000, tf));
y.setStandardTickUnits(units);
//
DeviationRenderer errorRenderer = new DeviationRenderer(true, true);
errorRenderer.setSeriesStroke(0, new BasicStroke(3F, 1, 1));
errorRenderer.setSeriesStroke(1, new BasicStroke(3F, 1, 1));
......@@ -102,13 +106,7 @@ public class XYChart {
errorRenderer.setSeriesPaint(0, new Color(255, 0, 0));
errorRenderer.setSeriesPaint(1, new Color(0, 0, 255));
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);
chartPanel = new ChartPanel(chart, false);
......@@ -134,22 +132,31 @@ public class XYChart {
return plot.getDomainAxis();
}
private static class TruncatingNumberFormater extends NumberFormat {
private static class TimeFormat extends NumberFormat {
public TimeFormat() {
super();
}
@Override
public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
// TODO Auto-generated method stub
return null;
public StringBuffer format(double number, StringBuffer toAppendTo,
FieldPosition pos) {
return this.format((long)number, toAppendTo, pos);
}
@Override
public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
// TODO Auto-generated method stub
return null;
public StringBuffer format(long number, StringBuffer toAppendTo,
FieldPosition pos) {
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
public Number parse(String source, ParsePosition pos) {
return null;
public Number parse(String source, ParsePosition parsePosition) {
return Double.valueOf(source);
}
}
......@@ -168,6 +175,8 @@ public class XYChart {
DecimalFormat df8 = new DecimalFormat("#,##0");
DecimalFormat df9 = 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
// 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