Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8256477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:52:22+00:00 2026-06-08T01:52:22+00:00

how can i create dynamic line chart using jFree chart in java which is

  • 0

how can i create dynamic line chart using jFree chart in java which is show data of 2 hours before and also provide a blank space where data show for 2 hours later from current time.For example suppose current time is 4pm ,so the chart display data from 2pm to 6pm.Here 2pm to 4pm the chart show a line and 4pm to 6pm provide a blank space which is fill time to time when the graph is moving that mean the tail of this graph is starting from middle and move right. similar like stock market chart.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:52:23+00:00Added an answer on June 8, 2026 at 1:52 am

    Yes, you can do it. I was facing a similar problem a few days ago.

    DynamicLineAndTimeSeriesChart.java

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.Timer;
    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.data.time.Millisecond;
    import org.jfree.data.time.TimeSeries;
    import org.jfree.data.time.TimeSeriesCollection;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;
    
    /**
     * An example to show how we can create a dynamic chart.
    */
    public class DynamicLineAndTimeSeriesChart extends ApplicationFrame implements ActionListener {
    
        /** The time series data. */
        private TimeSeries series;
    
        /** The most recent value added. */
        private double lastValue = 100.0;
    
        /** Timer to refresh graph after every 1/4th of a second */
        private Timer timer = new Timer(250, this);
    
        /**
         * Constructs a new dynamic chart application.
         *
         * @param title  the frame title.
         */
        public DynamicLineAndTimeSeriesChart(final String title) {
    
            super(title);
            this.series = new TimeSeries("Random Data", Millisecond.class);
    
            final TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
            final JFreeChart chart = createChart(dataset);
    
            timer.setInitialDelay(1000);
    
            //Sets background color of chart
            chart.setBackgroundPaint(Color.LIGHT_GRAY);
    
            //Created JPanel to show graph on screen
            final JPanel content = new JPanel(new BorderLayout());
    
            //Created Chartpanel for chart area
            final ChartPanel chartPanel = new ChartPanel(chart);
    
            //Added chartpanel to main panel
            content.add(chartPanel);
    
            //Sets the size of whole window (JPanel)
            chartPanel.setPreferredSize(new java.awt.Dimension(800, 500));
    
            //Puts the whole content on a Frame
            setContentPane(content);
    
            timer.start();
    
        }
    
        /**
         * Creates a sample chart.
         *
         * @param dataset  the dataset.
         *
         * @return A sample chart.
         */
        private JFreeChart createChart(final XYDataset dataset) {
            final JFreeChart result = ChartFactory.createTimeSeriesChart(
                "Dynamic Line And TimeSeries Chart",
                "Time",
                "Value",
                dataset,
                true,
                true,
                false
            );
    
            final XYPlot plot = result.getXYPlot();
    
            plot.setBackgroundPaint(new Color(0xffffe0));
            plot.setDomainGridlinesVisible(true);
            plot.setDomainGridlinePaint(Color.lightGray);
            plot.setRangeGridlinesVisible(true);
            plot.setRangeGridlinePaint(Color.lightGray);
    
            ValueAxis xaxis = plot.getDomainAxis();
            xaxis.setAutoRange(true);
    
            //Domain axis would show data of 60 seconds for a time
            xaxis.setFixedAutoRange(60000.0);  // 60 seconds
            xaxis.setVerticalTickLabels(true);
    
            ValueAxis yaxis = plot.getRangeAxis();
            yaxis.setRange(0.0, 300.0);
    
            return result;
        }
        /**
         * Generates an random entry for a particular call made by time for every 1/4th of a second.
         *
         * @param e  the action event.
         */
        public void actionPerformed(final ActionEvent e) {
    
            final double factor = 0.9 + 0.2*Math.random();
            this.lastValue = this.lastValue * factor;
    
            final Millisecond now = new Millisecond();
            this.series.add(new Millisecond(), this.lastValue);
    
            System.out.println("Current Time in Milliseconds = " + now.toString()+", Current Value : "+this.lastValue);
        }
    
        /**
         * Starting point for the dynamic graph application.
         *
         * @param args  ignored.
         */
        public static void main(final String[] args) {
    
            final DynamicLineAndTimeSeriesChart demo = new DynamicLineAndTimeSeriesChart("Dynamic Line And TimeSeries Chart");
            demo.pack();
            RefineryUtilities.centerFrameOnScreen(demo);
            demo.setVisible(true);
    
        }
    
    }  
    

    Please also check here :

    http://blog.odoobiz.com/2012/07/how-to-draw-dynamic-line-or-timeseries.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can create and use dynamic two dimensional array in Fortran (in 77 standard).
how can I create a dynamic lambda expression to pass to use in my
Most things are dynamic in SSRS i.e you can create a custom expression for
How I can implement dynamic Jtree, which shows created instances of clases? For example,
I want to create a dynamic queue (using the keyword new ) with an
I am trying to create some dynamic sql using the following code block firstSqlStatement
I am trying to create a dynamic image of my friends using PHP's GD
I'm using PHP to create user-agent based dynamic stylesheets with: AddHandler application/x-httpd-php .css And
How can I make the clocks on http://torwiki.com/page/User:Technical_13/SandBox#Dynamic_Clock dynamic to show the actual current
You can create a table model and add it to a table TableModel tm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.