/*
* Datei: ShowLoadProfile.java
* Autor(en): Marc Mültin, Lukas König
* Java-Version: 6.0
* Erstellt: ??.06.2010
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
/**
* @author Marc
*
*/
package eas.users.other.marc.smartCity;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
@SuppressWarnings("serial")
public class ShowLoadProfile extends ApplicationFrame {
DBConnection con = new DBConnection();
//SQL query for h0 profile
String qH0Profile = "SELECT slotid, consumption FROM h0profile";
String[] qH0MaxPowerValues = {
"SELECT max(consumption) AS maxCons FROM h0profile WHERE slotID <= 1440", //summer weekday
"SELECT max(consumption) AS maxCons FROM h0profile WHERE slotID > 1440 AND slotID <= 2880", //summer saturday
"SELECT max(consumption) AS maxCons FROM h0profile WHERE slotID > 2880 AND slotID <= 4320", //summer sunday
"SELECT max(consumption) AS maxCons FROM h0profile WHERE slotID > 4320 AND slotID <= 5760", //winter weekday
"SELECT max(consumption) AS maxCons FROM h0profile WHERE slotID > 5760 AND slotID <= 7200", //winter saturday
"SELECT max(consumption) AS maxCons FROM h0profile WHERE slotID > 7200 AND slotID <= 8460", //winter sunday
"SELECT max(consumption) AS maxCons FROM h0profile WHERE slotID > 8460 AND slotID <= 10080", //transition weekday
"SELECT max(consumption) AS maxCons FROM h0profile WHERE slotID > 10080 AND slotID <= 11520", //transition saturday
"SELECT max(consumption) AS maxCons FROM h0profile WHERE slotID > 11520 AND slotID <= 12960" //transition sunday
};
//SQL queries for photovoltaic profiles; here we need only 3 instead of 9 different profiles
String qPVProfileTrans = "SELECT power FROM pvProfile WHERE date = '21.03.2008'"; //transition (spring is equal to autumn)
String qPVProfileSummer = "SELECT power FROM pvProfile WHERE date = '21.06.2008'"; //summer
String qPVProfileWinter = "SELECT power FROM pvProfile WHERE date = '21.12.2008'"; //winter
String[] qPVMaxPowerValues = {
"SELECT max(power) as maxPower FROM pvprofile WHERE date = '21.03.2008'", //transition (spring is equal to autumn)
"SELECT max(power) as maxPower FROM pvprofile WHERE date = '21.06.2008'", //summer
"SELECT max(power) as maxPower FROM pvprofile WHERE date = '21.12.2008'", //winter
};
//ResultSets of executed SQL-Queries
ResultSet rsH0Profile = con.getResultsetFromDB(qH0Profile);
ResultSet rsH0MaxPowerValues = null;
ResultSet rsPVProfileTrans = con.getResultsetFromDB(qPVProfileTrans);
ResultSet rsPVProfileSummer = con.getResultsetFromDB(qPVProfileSummer);
ResultSet rsPVProfileWinter = con.getResultsetFromDB(qPVProfileWinter);
ResultSet rsPVMaxPowerValues = null;
//the dataset collection which holds all datasets (one dataset = 1 spline) to be drawn in the chart
XYSeriesCollection xySeriesCollDataset = new XYSeriesCollection();
public ShowLoadProfile(String title) {
super(title);
createDatasetH0(rsH0Profile);
//those methods will fill the xySeriesCollDataset object with the data
createDatasetPV(rsPVProfileTrans, "PV Transition", qPVMaxPowerValues[0]);
createDatasetPV(rsPVProfileSummer, "PV Summer", qPVMaxPowerValues[1]);
createDatasetPV(rsPVProfileWinter, "PV Winter", qPVMaxPowerValues[2]);
//some instructions to draw the chart
XYDataset dataset = xySeriesCollDataset;
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
private void createDatasetH0(ResultSet rs) {
XYSeries[] seriesH0Profile = new XYSeries[9];
seriesH0Profile[0] = new XYSeries("h0 Summer WD");
seriesH0Profile[1] = new XYSeries("h0 Summer SA");
seriesH0Profile[2] = new XYSeries("h0 Summer SU");
seriesH0Profile[3] = new XYSeries("h0 Winter WD");
seriesH0Profile[4] = new XYSeries("h0 Winter SA");
seriesH0Profile[5] = new XYSeries("h0 Winter SU");
seriesH0Profile[6] = new XYSeries("h0 Transition WD");
seriesH0Profile[7] = new XYSeries("h0 Transition SA");
seriesH0Profile[8] = new XYSeries("h0 Transition SU");
try {
int slotID = 0;
int h0Profile = -1;
double maxValue = 0;
while (rs.next()) {
if (slotID % 1440 == 0) {
/* every h0-profile, distinguished by (summer, winter, transition) on the one hand and
* (weekday, saturday, sunday) on the other hand, contains 1440 datapoints (60 minutes * 24 hours);
* the h0-profiles are written into the database-table in the order given by the series-array above
*/
h0Profile++;
rsH0MaxPowerValues = con.getResultsetFromDB(qH0MaxPowerValues[h0Profile]);
while (rsH0MaxPowerValues.next()) { maxValue = rsH0MaxPowerValues.getDouble(1); }
}
if (slotID % 15 == 0) {
/* the h0-profile is given in 15-minute-intervals, but the database entries represent minute-values;
* in order to draw a nice spline and not have the same value repeated 15 times for each interval,
* we catch only every 15th value;
*
* we also need to normalized the power values to 1 for each h0-profile (current power value / maxPowerValue)
*/
double xValue = (double) slotID / 60;
double yValue = rs.getDouble(2) / maxValue;
seriesH0Profile[h0Profile].add(xValue - (h0Profile * 24), yValue);
}
slotID++;
}
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
xySeriesCollDataset.addSeries(seriesH0Profile[0]); //Summer WD
xySeriesCollDataset.addSeries(seriesH0Profile[1]); //Summer SA
xySeriesCollDataset.addSeries(seriesH0Profile[2]); //Summer SU
}
public void createDatasetPV(ResultSet rs, String name, String maxValueQuery) {
XYSeries seriesPVProfile = new XYSeries(name);
try {
int counter = 1;
double maxValue = 0;
rsPVMaxPowerValues = con.getResultsetFromDB(maxValueQuery);
while (rsPVMaxPowerValues.next()) { maxValue = rsPVMaxPowerValues.getDouble(1); }
while (rs.next()) {
seriesPVProfile.add((double) counter / 4, rs.getDouble(1) / maxValue);
counter++;
}
} catch (SQLException e) {
e.printStackTrace();
}
xySeriesCollDataset.addSeries(seriesPVProfile);
}
private JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createXYLineChart(
"Load profiles", //chart title
"hour", //x-axis label
"Power(W) (normalized to 1)", //y-axis label
dataset, //data
PlotOrientation.VERTICAL,
true, //include legend
false, //tooltips
false //urls (only needed for creation of html image maps)
);
// BufferedImage b = chart.createBufferedImage(100, 100);
// JFrame frame = new JFrame();
// frame.getContentPane().getGraphics().drawImage(b, 0, 0, null);
// frame.pack();
// frame.setVisible(true);
chart.getXYPlot().setRenderer(new XYSplineRenderer());
return chart;
}
public static void main(String[] args) {
ShowLoadProfile frame = new ShowLoadProfile("Load profiles");
frame.pack();
RefineryUtilities.centerFrameOnScreen(frame);
frame.setVisible(true);
frame.con.closeDB();
}
}