Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added jfreechart functionality for the graph #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions driveCycles/brakingDrive.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Time(s) GasPedal(%) BrakePedal(%)
0 0 0
1 100 0
3 0 50
5 100 0
7 0 50
10 0 0
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ public int getSteps() {
return steps;
}

/**
* Determines if the drive cycle has started
* @return true if the cycle has started, false otherwise
*/
public boolean isStarted() {
return steps > 0;
}

/**
* Returns true if the time is out of the defined range
*/
Expand Down
8 changes: 8 additions & 0 deletions src/com/uwecocar2/vehiclesimulation/DriveCycle.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import com.uwecocar2.vehiclesimulation.utils.LookUpTable1D;

import java.io.File;
import java.util.Arrays;

public class DriveCycle implements Driver {
private LookUpTable1D gasPedalPositions;
private LookUpTable1D brakePedalPosition;
private String filename;

public DriveCycle(File file) throws Exception {
filename = file.getName().split(".txt")[0];
gasPedalPositions = new LookUpTable1D(file, 0, 1);
brakePedalPosition = new LookUpTable1D(file, 0, 2);
}
Expand All @@ -26,4 +29,9 @@ public Double getBrakePedalPosition(Double time) {
}
return brakePedalPosition.getValue(time);
}

@Override
public String getName() {
return this.filename;
}
}
2 changes: 2 additions & 0 deletions src/com/uwecocar2/vehiclesimulation/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ public interface Driver {
public Double getGasPedalPosition(Double time);

public Double getBrakePedalPosition(Double time);

public String getName();
}
64 changes: 64 additions & 0 deletions src/com/uwecocar2/vehiclesimulation/ui/DynamicGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.uwecocar2.vehiclesimulation.ui;
import java.awt.BorderLayout;

import java.awt.Color;

import javax.swing.JFrame;


import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import com.uwecocar2.vehiclesimulation.Recorder;

/**
*
*/
public class DynamicGraph extends JFrame {
private XYSeries added = new XYSeries("Data");

/**
* Instantiates a new DynamicGraph
* @param s the name of the graph
*/
public DynamicGraph(String s) {
super(s);
final ChartPanel chartPanel = createPanel();
this.add(chartPanel, BorderLayout.CENTER);
}

/**
* Sets the data stored and displayed in the graph
* @param records the Recorder to set as data
* @param name
*/
public void setData(Recorder records) {
added.clear();
int index = 0;
for (double item : records) {
added.add(index, item);
index++;
}
}

private ChartPanel createPanel() {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
xySeriesCollection.addSeries(added);
JFreeChart jfreechart = ChartFactory.createScatterPlot(
"Drive Cycle", "Time", "", xySeriesCollection,
PlotOrientation.VERTICAL, true, true, false);
XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
xyPlot.setDomainCrosshairVisible(true);
xyPlot.setRangeCrosshairVisible(true);
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setSeriesPaint(0, Color.blue);
NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
domain.setVerticalTickLabels(true);
return new ChartPanel(jfreechart);
}
}
75 changes: 71 additions & 4 deletions src/com/uwecocar2/vehiclesimulation/ui/JGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,34 @@
import com.uwecocar2.vehiclesimulation.Recorder;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.text.DecimalFormat;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class JGraph extends JPanel {

// the data to graph
private Recorder data;
private String yAxis;
XYSeriesCollection dataCollection;

// the graph padding
private static final int OFFSET_X = 75;
private static final int OFFSET_Y = 75;
private static final int SIZE = 2;
private static final int SIZE = 4;
private static final int FONT_SIZE = 20;

private static final DecimalFormat DF = new DecimalFormat("###.##");
Expand All @@ -29,6 +41,47 @@ public JGraph() {
super();
this.setFont(FONT);
METRICS = this.getFontMetrics(FONT);
dataCollection = new XYSeriesCollection();
}

public JPanel getChart() {
XYSeries series = new XYSeries("Efficiency");
int index = 0;

int width = this.getWidth() - OFFSET_X;
int height = this.getHeight() - OFFSET_Y;

double xMult;
double yMult;
try {
xMult = ((double)width - OFFSET_X)/this.data.size();
yMult = ((double)height - OFFSET_Y)/this.data.max();
} catch (Exception e) {
xMult = 0;
yMult = 0;
}


for(Double value : this.data) {
int x = (int)(index*xMult) + OFFSET_X;
int y = (int)(value*yMult) - OFFSET_Y;
series.add(x, y);
index++;
}
dataCollection.addSeries(series);
JFreeChart lineChartObject=ChartFactory.createXYLineChart("Efficiency", "Time", yAxis, dataCollection);
//JFreeChart lineChartObject=ChartFactory.createLineChart("title","x","y", data,PlotOrientation.VERTICAL,true,true,false);
ChartPanel chPanel = new ChartPanel(lineChartObject); //creating the chart panel, which extends JPanel

chPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 10));
chPanel.setLayout(new BoxLayout(chPanel, BoxLayout.Y_AXIS));

//chPanel.setPreferredSize(new Dimension(400, 400)); //size according to my window
chPanel.setMouseWheelEnabled(true);
chPanel.setVisible(true);
JPanel containingPanel = new JPanel();
containingPanel.add(chPanel);
return containingPanel;
}

@Override
Expand All @@ -39,6 +92,7 @@ public void paintComponent(Graphics g) {
g.clearRect(0, 0, this.getWidth(), this.getHeight());

g.setColor(Color.BLACK);
// draw the x and y axis
g.drawLine(OFFSET_X, OFFSET_Y, OFFSET_X, height);
g.drawLine(OFFSET_X, height, width, height);

Expand All @@ -53,12 +107,22 @@ public void paintComponent(Graphics g) {
}

int index = 0;
double prev = 0.0;
for(Double value : data) {
int x = (int)(index*xMult) + OFFSET_X;
int y = (int)(value*yMult) - OFFSET_Y;
if (value < prev) {
g.setColor(Color.RED);
} else {
g.setColor(Color.BLACK);
}
g.fillOval(x - SIZE/2, height - y - OFFSET_Y - SIZE/2, SIZE, SIZE);
index++;
}

prev = value;
}
g.setColor(Color.BLACK);

g.drawString("0", OFFSET_X - FONT_SIZE, height + FONT_SIZE);
if (data.size() > 1) {
g.drawString(Integer.toString(data.size()),(int)(xMult * (data.size() - 1)) + OFFSET_X, height + FONT_SIZE);
Expand All @@ -70,9 +134,12 @@ public void paintComponent(Graphics g) {
* Set the data to graph
* @param data
*/
public void setData(Recorder data) {
public JPanel setData(Recorder data, String yAxis) {
this.data = data;
this.repaint();
this.yAxis = yAxis;
this.dataCollection.removeAllSeries();
return this.getChart();
//this.repaint();
}

}
Loading