Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 678
  • Last Modified:

Creating chart from dataset

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
gbcbr
Asked:
gbcbr
  • 9
  • 8
1 Solution
 
CEHJCommented:
Make sure you're not creating instances of AlgoBot every 3 secs
0
 
gbcbrAuthor Commented:
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
 
CEHJCommented:
Search your code on the string 'new AlgoBot'
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gbcbrAuthor Commented:
But nothing related to charts there:
AlgoBot aBot;
aBot = new AlgoBot();
aBot.algoBot(openBid, openAsk);
0
 
CEHJCommented:
Creating a new AlgoBot will create a new chart
0
 
gbcbrAuthor Commented:
How to avoid this?
0
 
CEHJCommented:
Use the same AlgoBot instance each time if you can
0
 
gbcbrAuthor Commented:
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
 
CEHJCommented:
>>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
 
gbcbrAuthor Commented:
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
 
CEHJCommented:
You have to create it *once* and once only. You basically have to take new AlgoBot out of whatever loop it's in
0
 
gbcbrAuthor Commented:
I move it main class, it create new chart window, but after appear the same error
0
 
CEHJCommented:
>>but after appear the same error

That error being what - multiple charts appearing?
0
 
gbcbrAuthor Commented:
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
 
CEHJCommented:
You need to create it so that the reference to AlgoBot is in scope or you'll obviously get a NPE
0
 
gbcbrAuthor Commented:
Thank you for the help!
0
 
CEHJCommented:
:-)
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now