#include <lou> -4 Newbie Poster

Hi everyone I am trying to plot the data within the class XYDataset regress, vertically, right now they plot horizontally. Any help appreciated:

package brainwavesanalyser;

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);      
    }    
    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);
    }
    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;
}
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.