Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Creating chart from dataset

Posted on 2010-11-15
17
670 Views
Last Modified: 2012-05-10
I have datasets which arrives every 3 seconds
I try to create chart from these datasets, but instead of creating chart line it create new chart panel every 3 seconds.
Class AlgoBot call AlgoChart2.
 
public class AlgoBot {

    AlgoChart2 ach2 = new AlgoChart2();

    public AlgoBot() throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException, FieldNotFound {
    }
    double Avg[] = new double[3];
    double inX[] = new double[3];
    double outX[] = new double[3];

    public void algoBot(double[] openBid, double[] openAsk)
            throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException, FieldNotFound, NotDefinedException, Exception {

...........................

ach2.doEvent(inX, outX);
  }
}

Open in new window

public class AlgoChart2 extends javax.swing.JFrame {

    public AlgoChart2() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException, FieldNotFound {

        setSize(800, 600);
        setContentPane(chartPanel);
        show();
    }

    TimeSeries s1 = new TimeSeries("s1");
    TimeSeries s2 = new TimeSeries("s2");
    int sc = 0;

    public void doEvent(double[] openBid, double[] openAsk) {
        sc++;

        s1.addOrUpdate(new FixedMillisecond(new Date().getTime()), new Float(openAsk[2]));

        s2.addOrUpdate(new FixedMillisecond(new Date().getTime() + 5000), new Float(openBid[2]));

        if (sc > 100) {
            s1.delete(0, 0);
            s2.delete(0, 0);
        }
    }

    private JFreeChart createChart(XYDataset dataset) {
        
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
                "Quotes", // title
                "Time", // x-axis label
                "Rate s1", // y-axis label
                dataset, // data
                true, // create legend?
                true, // generate tooltips?
                false // generate URLs?
                );
        XYPlot plot = (XYPlot) chart.getPlot();
        XYDataset dataset2 = createDataset2();
        plot.setDataset(0, dataset);
        plot.setDataset(1, dataset2);
        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("hh:mm:ss"));
        XYItemRenderer r = plot.getRenderer();
        XYItemRenderer renderer2 = new XYLineAndShapeRenderer();
        r.setSeriesPaint(0, Color.blue);
        r.setSeriesPaint(1, Color.red);
        renderer2.setSeriesPaint(0, Color.red);
        plot.setRenderer(1, renderer2);
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        plot.setAxisOffset(new RectangleInsets(1.0, 1.0, 1.0, 1.0));
        NumberAxis axis2 = new NumberAxis("Rate s2");
        axis2.setAutoRangeIncludesZero(false);
        axis2.setLabelPaint(Color.red);
        axis2.setTickLabelPaint(Color.green);
        plot.setRangeAxis(1, axis2);
        plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
        plot.mapDatasetToRangeAxis(1, 1);
        
        return chart;
    }

    private XYDataset createDataset() {

        TimeSeriesCollection dataset = new TimeSeriesCollection();

        dataset.addSeries(s1);

        return dataset;
    }

    private XYDataset createDataset2() {

        TimeSeriesCollection dataset2 = new TimeSeriesCollection();

           dataset2.addSeries(s2);

        return dataset2;
    }

    public JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        ChartPanel panel = new ChartPanel(chart);
        panel.setFillZoomRectangle(true);
        panel.setMouseWheelEnabled(true);
        return panel;
    }
    ChartPanel chartPanel =  (ChartPanel) createDemoPanel();

}

Open in new window

Please advice
Thank you
0
Comment
Question by:gbcbr
  • 9
  • 8
17 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 34139189
Make sure you're not creating instances of AlgoBot every 3 secs
0
 

Author Comment

by:gbcbr
ID: 34139295
This what I suppose also, but when I check these codes I can't find why it's create new instance. I spent 3 days to find where it calls new instance, but I can't find.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34139320
Search your code on the string 'new AlgoBot'
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:gbcbr
ID: 34139355
But nothing related to charts there:
AlgoBot aBot;
aBot = new AlgoBot();
aBot.algoBot(openBid, openAsk);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34139417
Creating a new AlgoBot will create a new chart
0
 

Author Comment

by:gbcbr
ID: 34139453
How to avoid this?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34139624
Use the same AlgoBot instance each time if you can
0
 

Author Comment

by:gbcbr
ID: 34139718
But how I can call it only once?
Yes, it's located in SelectData class which query data every 3 seconds, so when new data arrive it calls every time AlgoBot. And AlgoBot calls every time AlgoChart.
Please advice how to break this circle.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34139757
>>when new data arrive it calls every time AlgoBot.

There's nothing wrong with that. The problem is *creating* AlgoBot. Just call the method(s) of it without creating it each time
0
 

Author Comment

by:gbcbr
ID: 34139806
I tried but it makes error:
AlgoBot aBot;
//  aBot = new AlgoBot();
aBot.algoBot(openBid, openAsk);

java.lang.NullPointerException
        at lionfx_algo.dbconnect.SelectData.run(SelectData.java:49) => aBot.algoBot(openBid, openAsk);<=
        at java.util.TimerThread.mainLoop(Timer.java:512)
        at java.util.TimerThread.run(Timer.java:462)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34139876
You have to create it *once* and once only. You basically have to take new AlgoBot out of whatever loop it's in
0
 

Author Comment

by:gbcbr
ID: 34139989
I move it main class, it create new chart window, but after appear the same error
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34140009
>>but after appear the same error

That error being what - multiple charts appearing?
0
 

Author Comment

by:gbcbr
ID: 34140042
java.lang.NullPointerException
        at lionfx_algo.dbconnect.SelectData.run(SelectData.java:49) => aBot.algoBot(openBid, openAsk);<=
        at java.util.TimerThread.mainLoop(Timer.java:512)
        at java.util.TimerThread.run(Timer.java:462)
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 34140197
You need to create it so that the reference to AlgoBot is in scope or you'll obviously get a NPE
0
 

Author Closing Comment

by:gbcbr
ID: 34140282
Thank you for the help!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34140295
:-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 168
Facing this issue for maven proxy setting 2 20
mysql jsp example issue 32 48
Tagging and Merging on Branch 1 29
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question