Therefore I want to remove zero from the auto range using "yAxis.setAutoRangeIncludesZero(false)", but every time I do this the y axis labels disappear.
I can solve this by manually entering the range/tick unit/ and number format but really I want it to all be auto tuned for me? Can I do this?
Code: Select all
package damofx.statservergui.gui.components;
import java.awt.Color;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.Range;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleInsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class uses JFreeGraph to create Graph Panels for displaying TimeSeries
* data
*
* @author chris.jones
*
*/
public class StepGraph {
private static final Logger LOG = LoggerFactory.getLogger(StepGraph.class);
private final ChartPanel chartPanel;
private final String title;
private final boolean includeLegend;
private final DateFormat dateFormat;
private final JFreeChart chart;
private XYSeriesCollection dataset;
public StepGraph(String title, DateFormat dateFormat, boolean includeLegend) {
this.title = title;
this.includeLegend = includeLegend;
this.dateFormat = dateFormat;
this.dataset = new XYSeriesCollection();
chart = createChart(dataset, title);
chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
}
private JFreeChart createChart(final XYDataset dataset, String xAxisTitle) {
JFreeChart chart = ChartFactory.createXYStepChart(null, // chart title
xAxisTitle, // x axis label
null, // y axis label
dataset, // data
PlotOrientation.VERTICAL, includeLegend, // include legend
true, // tooltips
false // urls
);
chart.setBackgroundPaint(Color.LIGHT_GRAY);
// chart.setPadding(new RectangleInsets(0, 0, 0, 0));
final XYPlot plot = chart.getXYPlot();
plot.setBackgroundPaint(Color.WHITE);
plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
//This will remove range axis labels
// yAxis.setAutoRangeIncludesZero(false);
//This will solve the problem manually, but I want jgraph to auto calculate range and tick unit
// yAxis.setRange(105.9029, 105.9051);
// yAxis.setTickUnit(new NumberTickUnit(0.001));
// yAxis.setNumberFormatOverride(new DecimalFormat("#.0000"));
plot.getRangeAxis().setLabelInsets(new RectangleInsets(0, 0, 0, 0));
final DateAxis dateaxis = (DateAxis) plot.getDomainAxis();
dateaxis.setDateFormatOverride(dateFormat);
dateaxis.setLabelInsets(new RectangleInsets(0, 0, 0, 0));
return chart;
}
public void addTimeSeries(XYSeries series) {
dataset.addSeries(series);
chartPanel.setChart(createChart(dataset, title));
chartPanel.repaint();
}
public ChartPanel getChart() {
return chartPanel;
}
public static void main(String[] args) {
XYSeries t2 = new XYSeries("2");
t2.add(29540604, 105.904);
t2.add(29540904, 105.903);
t2.add(29541404, 105.903);
t2.add(29541504, 105.904);
t2.add(29550308, 105.904);
t2.add(29562505, 105.904);
t2.add(29564717, 105.905);
t2.add(29564809, 105.905);
StepGraph graph = new StepGraph("Blah", new SimpleDateFormat("HH:mm"),
true);
graph.addTimeSeries(t2);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(graph.getChart());
frame.pack();
frame.setVisible(true);
}
}