4

I am using JFreechart to display a graph that changes with time. But the values are getting added at the right of the graph.

  1. I want the graph to start at the left and stop at some point on the right.
  2. Also, I want part of the graph line to be solid and some of it to be dotted.
  3. 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;
    }

}
Bug Killer
  • 661
  • 7
  • 22
  • That `TimeSeries` constructor is deprecated; use `javax.swing.Timer` to pace the animation; see also this [example](http://stackoverflow.com/a/5048863/230513); see also [*Initial Threads*](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Nov 19 '13 at 05:32

1 Answers1

0
  1. I added some time points in the past and zoomed out so that it starts from left and continues

2.Had to extend XYPlot existing classes with my own

  1. Couldn't be done AFAIK
Bug Killer
  • 661
  • 7
  • 22