Hi everyone, I am trying to pass the X and Y values that are in class Chart onto class NodeAvg, but cant come up with the code to do that.Any help appreciated:

package Testing;

import java.awt.Color;
import java.awt.Paint;
import java.io.IOException;
import java.util.ArrayList;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DrawingSupplier;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

@SuppressWarnings("serial")
public class Chart extends ApplicationFrame {

  public Chart(XYSeriesCollection data) {
    super("EEG Scatterplot and Linear Regression");
    setContentPane(new ChartPanel(createChart(data)));
    pack();
  }

  public static JFreeChart createChart(XYSeriesCollection data) {
    JFreeChart chart = ChartFactory.createScatterPlot(null, "Packet Positions", "Value from Sensors", data,
    PlotOrientation.VERTICAL, true, false, false);
    chart.setBackgroundPaint(Color.LIGHT_GRAY); //BACKGROUND COLOR OF WINDOW
    XYPlot plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.black);       //BACKGROUND COLOR OF PLOT
    XYItemRenderer scatterRenderer = plot.getRenderer();
    StandardXYItemRenderer regressionRenderer = new StandardXYItemRenderer();
    regressionRenderer.setBaseSeriesVisibleInLegend(false);
    plot.setDataset(1, regress(data));
    plot.setRenderer(1, regressionRenderer);
    DrawingSupplier ds = plot.getDrawingSupplier();
    for (int i = 0; i < data.getSeriesCount(); i++) {
      Paint paint = ds.getNextPaint();//picks the next color in the series for the plot 
      scatterRenderer.setSeriesPaint(i, paint);
      regressionRenderer.setSeriesPaint(i, paint);      
    }
//
//    try {
//        ChartUtilities.saveChartAsJPEG(new File("DataFiles/generatedImages/testing.jpg"), chart, 1000, 600);
//        System.out.println("Image saved successfully!");
//    } catch (Exception e){
//        System.out.println("Problem occured creating chart.");
//    }


    System.out.println(data);
    return chart;
  }

  //CALCULATE LINEAR REGRESSION
  public static XYDataset regress(XYSeriesCollection data) {
    // Determine bounds
    double xMin = Double.MAX_VALUE, xMax = 0;
    for (int i = 0; i < data.getSeriesCount(); i++) {
      XYSeries ser = data.getSeries(i);
      for (int j = 0; j < ser.getItemCount(); j++) {
        double x = ser.getX(j).doubleValue();
        if (x < xMin) {
          xMin = x;
        }
        if (x > xMax) {
          xMax = x;
        }
      }
    }
    // Create 2-point series for each of the original series
    XYSeriesCollection coll = new XYSeriesCollection();
    for (int i = 0; i < data.getSeriesCount(); i++) {
      XYSeries ser = data.getSeries(i);
      int n = ser.getItemCount();
      double sx = 0, sy = 0, sxx = 0, sxy = 0, syy = 0;
      for (int j = 0; j < n; j++) {
        double x = ser.getX(j).doubleValue();
        double y = ser.getY(j).doubleValue();
        sx += x;
        sy += y;
        sxx += x * x;
        sxy += x * y;
        syy += y * y;
      }
      double b = (n * sxy - sx * sy) / (n * sxx - sx * sx);
      double a = sy / n - b * sx / n;
      XYSeries regr = new XYSeries(ser.getKey());
      regr.add(xMin, a + b * xMin);
      regr.add(xMax, a + b * xMax);
      coll.addSeries(regr);
    }
    return coll;
  }

  public static XYSeriesCollection getNodeData(String node, int currentPacket, String fileName) throws IOException {

   ReadFile file1 = null;
    //cALLING A FILE CHOOSER SO WE GET A FAST FILE INTO THIS EXAMPLE
//    JFileChooser chooser = new JFileChooser();
//      chooser.showOpenDialog(null);
//    file1 = new ReadFile(chooser.getSelectedFile().getAbsolutePath());
   while(file1 == null)
      file1 = new ReadFile(fileName);

      for (int i = 1; i < currentPacket; i++)
        file1.getNextPacket();


    //CREATING A LOCAL COPY OF THE READ DATA FROM THE CHOOSER:
    double[] localData = new double[file1.getReadData(node).size()];

    for(int i = 0; i < localData.length; ++i)
      localData[i] = file1.getReadData(node).get(i).doubleValue();

    XYSeriesCollection data = new XYSeriesCollection();
    int arraySize = localData.length;

    for (int i = 1; i <= 1; i++) {
      XYSeries series = new XYSeries("Packet: " + currentPacket + " of Node: " + node);
      for (int j = 0; j <= (arraySize - 1); j++) {

        double x = j;
        double y = localData[j];

        series.add(x, y);
      }

      data.addSeries(series);
       System.out.println("the raw data should be: " + data);
    }
    for (int i = 1; i < currentPacket; i++)
        file1.getPreviousPacket();
    return data;
  }

  public static XYSeriesCollection getAllNodeData(int currentPacket, String fileName) throws IOException {

   ReadFile file1 = null;
    //cALLING A FILE CHOOSER SO WE GET A FAST FILE INTO THIS EXAMPLE
//    JFileChooser chooser = new JFileChooser();
//      chooser.showOpenDialog(null);
//    file1 = new ReadFile(chooser.getSelectedFile().getAbsolutePath());
   while(file1 == null)
      file1 = new ReadFile(fileName);

      for (int i = 1; i < currentPacket; i++)
        file1.getNextPacket();

      System.out.println("current packet from multichart should be " + currentPacket);

    String [] allNodes = new String[] {"AF3", "F7", "F3", "FC5", "T7", "P7", "O1", 
                          "O2", "P8", "T8", "FC6", "F4", "F8", "AF4"};
    //CREATING A LOCAL COPY OF THE READ DATA FROM THE CHOOSER:
    XYSeriesCollection data = new XYSeriesCollection();
   for (int p = 0; p < 13; p++)
   {
    double[] localData = new double[file1.getReadData(allNodes[p]).size()];

    for(int i = 0; i < localData.length; ++i)
      localData[i] = file1.getReadData(allNodes[p]).get(i).doubleValue();
    int arraySize = localData.length;

      XYSeries series = new XYSeries(allNodes[p]);
      for (int j = 0; j <= (arraySize - 1); j++) {

        double x = j;
        double y = localData[j];

        series.add(x, y);
      }

      data.addSeries(series);
   }
   for (int i = 1; i < currentPacket; i++)
        file1.getPreviousPacket();
    return data;
  }

public static XYSeriesCollection getFFTNodeData(String node, int currentPacket, String fileName) throws IOException {

   ReadFile file1 = null;

    //cALLING A FILE CHOOSER SO WE GET A FAST FILE INTO THIS EXAMPLE
//    JFileChooser chooser = new JFileChooser();
//      chooser.showOpenDialog(null);
//    file1 = new ReadFile(chooser.getSelectedFile().getAbsolutePath());
   while(file1 == null)
      file1 = new ReadFile(fileName);

      for (int i = 1; i <= currentPacket; i++)
        file1.getNextPacket();

   transformData = null;
   transformData = new FastFourierTransform(file1.getReadData(node));
   transformData.Transform();

    //CREATING A LOCAL COPY OF THE READ DATA FROM THE CHOOSER:
    double[] localData = new double[file1.getReadData(node).size()];
    temp = transformData.ReturnListArray();
    Double[] tempDoubleArray = (Double[])temp.toArray(new Double[temp.size()]);

    XYSeriesCollection data = new XYSeriesCollection();
    int arraySize = localData.length;

    for (int i = 1; i <= 1; i++) {
      XYSeries series = new XYSeries("Packet: " + currentPacket + " of Node: " + node);
      for (int j = 0; j <= (arraySize - 1); j++) {

        double x = j;
        double y = tempDoubleArray[j];

        series.add(x, y);
      }

      data.addSeries(series);
    }
    return data;
  }

public static XYSeriesCollection getFFTAllNodeData(int currentPacket, String fileName) throws IOException {

   ReadFile file1 = null;
    //cALLING A FILE CHOOSER SO WE GET A FAST FILE INTO THIS EXAMPLE
//    JFileChooser chooser = new JFileChooser();
//      chooser.showOpenDialog(null);
//    file1 = new ReadFile(chooser.getSelectedFile().getAbsolutePath());
   while(file1 == null)
      file1 = new ReadFile(fileName);

      for (int i = 1; i < currentPacket; i++)
        file1.getNextPacket();

      System.out.println("current packet from multichart should be " + currentPacket);

    String [] allNodes = new String[] {"AF3", "F7", "F3", "FC5", "T7", "P7", "O1", 
                          "O2", "P8", "T8", "FC6", "F4", "F8", "AF4"};
    //CREATING A LOCAL COPY OF THE READ DATA FROM THE CHOOSER:
    XYSeriesCollection data = new XYSeriesCollection();
   for (int p = 0; p < 13; p++)
   {
    transformData = null;
    transformData = new FastFourierTransform(file1.getReadData(allNodes[p]));
   transformData.Transform();

    //CREATING A LOCAL COPY OF THE READ DATA FROM THE CHOOSER:
    double[] localData = new double[file1.getReadData(allNodes[p]).size()];
    temp = transformData.ReturnListArray();
    Double[] tempDoubleArray = (Double[])temp.toArray(new Double[temp.size()]);

    for(int i = 0; i < localData.length; ++i)
      localData[i] = file1.getReadData(allNodes[p]).get(i).doubleValue();
    int arraySize = localData.length;

      XYSeries series = new XYSeries(allNodes[p]);
      for (int j = 0; j <= (arraySize - 2); j++) {

        double x = j;
        double y = tempDoubleArray[j];
        System.out.println(tempDoubleArray[j]+ "\n");

        series.add(x, y);
      }

      data.addSeries(series);
   }

    return data;
  }

private static FastFourierTransform transformData;
private static ArrayList<Double> temp;
private static BrainWavesAnalyserView dataFile;

//  public static void main(String[] args) throws Exception {
//    String node = "AF3";
//    LinearRegression plot = new LinearRegression(getTestData(node));
//    RefineryUtilities.centerFrameOnScreen(plot);
//    plot.setVisible(true);  }


}

-----------------------------------------------------------------------------------------------------
//NodeAvg class:


package Testing;

import java.util.ArrayList;
import org.jfree.ui.ApplicationFrame;

public class NodeAvg extends ApplicationFrame{

  public static Average FindAvg (//How do I get the data values from Chart?);

          private double data [];

NodeAvg(ArrayList<Double> incomingArray) {

data = new double[incomingArray.size()];

 for(int i = 0; i < data.length; ++i){
        data[i] = incomingArray.get(i).doubleValue();
        //Output test.
        System.out.println("Data is: " + data[i]);
   }
  }
}
//Average: Average by using view from Excel. Average readings with the same readings for each
//node, accross all packets.

//Output to Chart:

Recommended Answers

All 3 Replies

What is the relationship between the two classes? Does one create an instance of the other? If one creates the other then:
When do you want to pass the values? Do you have them when you create an instance of the class? If so pass them to the class in its constructor.
If you want to get them after the class is created, pass a reference to the class with the x&y values to the other class in its constructor and use it to call some getter methods that will return the values.

Yes, I want to get the values after the class Chart has been created. Would I extend ApplicationFrame in the NodeAvg class?

The NodeAvg class needs a reference to the instance of the Chart class so that it can call methods in that class to get the x & y values. I don't see where the ApplicationFrame has anything to do with the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.