Solved

Creating chart from dataset

Posted on 2010-11-15
17
661 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now