I am using JFreechart to display a graph that changes with time. But the values are getting added at the right of the graph.
- I want the graph to start at the left and stop at some point on the right.
- Also, I want part of the graph line to be solid and some of it to be dotted.
- I want to overlay some other graphs such as a bar graph.
How can I do this? Code:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.DefaultXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
public class MySCCE extends ApplicationFrame {
static Class class$org$jfree$data$time$Millisecond;
public MySCCE(String title) {
super(title);
setContentPane(new DemoPanel());
}
static class DemoPanel extends JPanel {
DemoPanel() {
TimeSeries series1;
series1 = new TimeSeries("ICP", (MySCCE.class$org$jfree$data$time$Millisecond = MySCCE.class$("org.jfree.data.time.Millisecond")));
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(series1);
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Data Monitoring", "Time", "mm Hg", timeseriescollection, true, true, true);
jfreechart.getTitle().setPaint(Color.green);
jfreechart.setBackgroundPaint(Color.black);
XYPlot xyplot = (XYPlot)jfreechart.getPlot();
xyplot.setBackgroundPaint(Color.BLACK);
xyplot.setDomainGridlinePaint(Color.green);
xyplot.setRangeGridlinePaint(Color.white);
xyplot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
ValueAxis valueaxis = xyplot.getDomainAxis();
valueaxis.setAutoRange(true);
valueaxis.setFixedAutoRange(20000D);
XYItemRenderer renderer = xyplot.getRenderer();
renderer.setSeriesPaint(2, Color.GREEN);
xyplot.setRenderer(1, new DefaultXYItemRenderer());
xyplot.mapDatasetToRangeAxis(1, 1);
ChartPanel chartpanel = new ChartPanel(jfreechart);
add(chartpanel);
double d1 = 10D * Math.random() - 5;
try {
Thread.sleep(1000, 0);
series1.add(new Millisecond(), d1);
d1 = 10D * Math.random() - 5;
Thread.sleep(1000, 0);
series1.add(new Millisecond(), d1);
d1 = 10D * Math.random() - 5;
Thread.sleep(1000, 0);
series1.add(new Millisecond(), d1);
d1 = 10D * Math.random() - 5;
Thread.sleep(1000, 0);
series1.add(new Millisecond(), d1);
} catch (InterruptedException e) {
e.printStackTrace();
}
chartpanel.setPreferredSize(new Dimension(1000, 570));
}
}
/**
* @param args
*/
public static void main(String[] args) {
MySCCE myscce = new MySCCE("");
myscce.pack();
RefineryUtilities.centerFrameOnScreen(myscce);
myscce.setVisible(true);
}
static Class class$(String s)
{
Class clazz=null;
try {
clazz= Class.forName(s);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return clazz;
}
}