package org.dgpf.gui.charts;
import java.awt.EventQueue;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Random;
import org.dgpf.gui.charts.descriptors.PortDescriptorBase;
import org.dgpf.gui.utils.LanguageSwitcher;
import org.dgpf.search.api.SearchState;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
/**
* This class represents a 3d bar chart.
*
* @author Alexander Podlich
*/
public class Bar3DChart implements IChart
{
/**
* The name of the 3d bar chart.
*/
private String m_name;
/**
* The x value.
*/
private PortDescriptorBase m_xValue;
/**
* The y values.
*/
private List<PortDescriptorBase> m_yValues;
/**
* The status for showing the legend.
*/
private boolean m_showLegend = true;
/**
* The status, if the x achsis is bounded.
*/
private boolean m_xAchsisIsBounded;
/**
* The bounded value of the x achsis.
*/
private int m_boundedValue;
/**
* The chart category dataset.
*/
private DefaultCategoryDataset m_dataSet;
/**
* The status, whether the actual chart is a time chart.
*/
private boolean m_isTimeChart = false;
/**
* The time pattern.
*/
private String m_timePattern;
/**
* The plot orientation.
*/
private PlotOrientation m_plotOrientation = PlotOrientation.VERTICAL;
/**
* Constructor.
*
* @param p_n
* The name of the 3d bar chart.
*/
public Bar3DChart(String p_n)
{
this.m_name = p_n;
}
/**
* Updates the 3d bar chart.
*
* @param p_searchState
* The search state.
*/
public void update(SearchState<?> p_searchState)
{
EventQueue.invokeLater(new ChartRunner(p_searchState, this));
}
/**
* Updates the 3d bar chart. This method has to be called from the
* ChartRunner.
*
* @param p_searchState
* The search state.
*/
public void doUpdate(SearchState<?> p_searchState)
{
double l_xVal;
double l_yVal;
boolean l_removed = false;
for(int l_i = 0; l_i < this.m_yValues.size(); ++l_i)
{
l_xVal = this.m_xValue.get(p_searchState);
l_yVal = this.m_yValues.get(l_i).get(p_searchState);
String l_value;
if(this.m_isTimeChart)
{
Date l_date = new Date(new Double(l_xVal).longValue());
SimpleDateFormat l_dayFormat = new SimpleDateFormat(
this.m_timePattern);
l_value = l_dayFormat.format(l_date);
}
else
{
l_value = String.valueOf(l_xVal);
}
if(!l_removed && this.m_xAchsisIsBounded
&& (this.m_dataSet.getColumnCount()) > this.m_boundedValue)
{
this.m_dataSet.removeColumn(0);
l_removed = true;
}
this.m_dataSet.addValue(l_yVal, this.m_yValues.get(l_i).toString(),
l_value);
}
}
/**
* Creates the 3d bar chart.
*
* @return The bar chart.
*/
public JFreeChart createChart()
{
this.m_dataSet = new DefaultCategoryDataset();
JFreeChart l_chart = ChartFactory.createBarChart3D(this.m_name,
this.m_xValue.toString(),
LanguageSwitcher.getString("chrt_value"), this.m_dataSet,
this.m_plotOrientation, this.m_showLegend, true, false);
return l_chart;
}
/**
* Returns a sample 3d bar chart for a preview.
*
* @return The sample chart.
*/
public JFreeChart getSampleChart()
{
DefaultCategoryDataset l_dSet = new DefaultCategoryDataset();
Random l_rand = new Random();
l_dSet.addValue((l_rand.nextDouble() % 50), LanguageSwitcher
.getString("chrt_1_dataset"), LanguageSwitcher
.getString("chrt_1_category"));
l_dSet.addValue((l_rand.nextDouble() % 50), LanguageSwitcher
.getString("chrt_1_dataset"), LanguageSwitcher
.getString("chrt_2_category"));
l_dSet.addValue((l_rand.nextDouble() % 50), LanguageSwitcher
.getString("chrt_2_dataset"), LanguageSwitcher
.getString("chrt_1_category"));
l_dSet.addValue((l_rand.nextDouble() % 50), LanguageSwitcher
.getString("chrt_2_dataset"), LanguageSwitcher
.getString("chrt_2_category"));
l_dSet.addValue((l_rand.nextDouble() % 50), LanguageSwitcher
.getString("chrt_3_dataset"), LanguageSwitcher
.getString("chrt_1_category"));
l_dSet.addValue((l_rand.nextDouble() % 50), LanguageSwitcher
.getString("chrt_3_dataset"), LanguageSwitcher
.getString("chrt_2_category"));
JFreeChart l_chart = ChartFactory.createBarChart3D(this.m_name,
LanguageSwitcher.getString("chrt_x_achsis"), LanguageSwitcher
.getString("chrt_value"), l_dSet, PlotOrientation.VERTICAL,
true, true, false);
return l_chart;
}
/**
* Returns the description of this chart.
*
* @return The description of this chart.
*/
public String getDescription()
{
return LanguageSwitcher.getString("barc_description");
}
/**
* Returns the string representation of the chart.
*
* @return The name of the 3d bar chart.
*/
@Override
public String toString()
{
return this.m_name;
}
/**
* @param p_name
* The new name of the chart.
*/
public void setName(String p_name)
{
this.m_name = p_name;
}
/**
* @param p_xValue
* The port descriptor as x value.
*/
public void setXValue(PortDescriptorBase p_xValue)
{
this.m_xValue = p_xValue;
}
/**
* @param p_yValues
* The list of port descriptors as y values.
*/
public void setYValues(List<PortDescriptorBase> p_yValues)
{
this.m_yValues = p_yValues;
}
/**
* @param p_showLegend
* The status for showing the legend.
*/
public void setShowLegend(boolean p_showLegend)
{
this.m_showLegend = p_showLegend;
}
/**
* @param p_xAchsisIsBounded
* The status, if the x achsis is bounded.
*/
public void setXAchsisIsBounded(boolean p_xAchsisIsBounded)
{
this.m_xAchsisIsBounded = p_xAchsisIsBounded;
}
/**
* @param p_boundedValue
* The bounded value of the x achsis.
*/
public void setBoundedValue(int p_boundedValue)
{
this.m_boundedValue = p_boundedValue;
}
/**
* @param p_isTimeChart
* The status, whether the actual chart is a time chart.
*/
public void setIsTimeChart(boolean p_isTimeChart)
{
this.m_isTimeChart = p_isTimeChart;
}
/**
* @param p_timePattern
* The time pattern of the time chart.
*/
public void setTimePattern(String p_timePattern)
{
this.m_timePattern = p_timePattern;
}
/**
* @param p_plotOrientation
* The plot orientation of teh bar chart.
*/
public void setPlotOrientation(PlotOrientation p_plotOrientation)
{
this.m_plotOrientation = p_plotOrientation;
}
}