Logo
Distributed Genetic Programming Framework
print print

File org.dgpf.gui.charts.XYLineChart.java

Here you can find all the information about the file org.dgpf.gui.charts.XYLineChart.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2006 Alexander Podlich
 * 
 * E-Mail           : podlich@student.uni-kassel.de
 * Creation Date    : 22.07.2006 22:18:01
 * Original Filename: org.dgpf.gui.charts.DGPFLineChart.java
 * Version          : 1.0
 * Last modification: 22.07.2006
 * Last modified by : Alexander Podlich
 * 
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *                    
 * Warranty         : This software is provided "as is" without any
 *                    warranty; without even the implied warranty of
 *                    merchantability or fitness for a particular purpose.
 *                    See the Gnu Lesser General Public License for more
 *                    details.
 */


package org.dgpf.gui.charts;

import java.awt.Color;
import java.awt.EventQueue;
import java.text.SimpleDateFormat;
import java.util.Date;
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.axis.DateAxis;
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.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.sfc.collections.Collections;

/**
 * This class represents a line chart
 * 
 * @author Alexander Podlich
 */

public class XYLineChart implements IChart
  {

  /**
   * The serial version UID.
   */

  private static final long          serialVersionUID  = 1L;

  /**
   * The x value.
   */

  private PortDescriptorBase         m_xValue;

  /**
   * The y values.
   */

  private List<PortDescriptorBase>   m_yValues;

  /**
   * List of time-series collections.
   */

  private List<TimeSeriesCollection> m_timeDataSet;

  /**
   * List of xy-series collections.
   */

  private List<XYSeriesCollection>   m_xyDataSet;

  /**
   * The list of time-series.
   */

  private List<TimeSeries>           m_timeSeriesList;

  /**
   * The list of xy-series.
   */

  private List<XYSeries>             m_xySeriesList;

  /**
   * The name of the line chart.
   */

  private String                     m_name;

  /**
   * The status for showing the legend.
   */

  private boolean                    m_showLegend      = true;

  /**
   * The status, whether the actual chart is a time chart.
   */

  private boolean                    m_isTimeChart     = false;

  /**
   * The time pattern.
   */

  private String                     m_timePattern;

  /**
   * The bounded value of the x achsis.
   */

  private int                        m_boundedValue;

  /**
   * The status, if the x achsis is bounded.
   */

  private boolean                    m_xAchsisIsBounded;

  /**
   * The status of the line visibility of the chart.
   */

  private boolean                    m_lineVisibility  = true;

  /**
   * The status of the shape visibility of the chart.
   */

  private boolean                    m_shapeVisibility = false;

  /**
   * Constructor.
   * 
   * @param p_name
   *          The name of the line chart.
   */

  public XYLineChart(String p_name)
    {
    this.m_name = p_name;
    }

  /**
   * Updates the line chart.
   * 
   * @param p_searchState
   *          The search state.
   */

  public void update(SearchState<?> p_searchState)
    {
    EventQueue.invokeLater(new ChartRunner(p_searchState, this));
    }

  /**
   * Updates the line 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;

    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);
      if(this.m_isTimeChart)
        {
        TimeSeries l_tempTS = this.m_timeSeriesList.get(l_i);
        if(this.m_xAchsisIsBounded
            && (l_tempTS.getItemCount()) > this.m_boundedValue)
          {
          l_tempTS.delete(l_tempTS.getTimePeriod(0));
          }
        l_tempTS.addOrUpdate(new Second(new Date(new Double(l_xVal)
            .longValue())), l_yVal);
        }
      else
        {
        XYSeries l_tempXYS = this.m_xySeriesList.get(l_i);
        if(this.m_xAchsisIsBounded
            && (l_tempXYS.getItemCount()) > this.m_boundedValue)
          {
          l_tempXYS.remove(0);
          }
        l_tempXYS.add(l_xVal, l_yVal);
        }
      }

    }

  /**
   * Returns the string representation of the chart.
   * 
   * @return The name of the line chart.
   */

  @Override
  public String toString()
    {
    return this.m_name;
    }

  /**
   * Returns a sample line chart for a preview.
   * 
   * @return The sample chart.
   */

  public JFreeChart getSampleChart()
    {

    JFreeChart l_chart = ChartFactory.createXYLineChart(this.m_name,
        LanguageSwitcher.getString("chrt_x_achsis"), LanguageSwitcher
            .getString("chrt_y_achsis"), new XYSeriesCollection(),
        PlotOrientation.VERTICAL, true, true, false);
    XYPlot l_plot = l_chart.getXYPlot();

    XYSeriesCollection l_sCollection1 = new XYSeriesCollection();
    XYSeriesCollection l_sCollection2 = new XYSeriesCollection();
    XYSeries l_series1 = randomSeries(LanguageSwitcher
        .getString("chrt_1_dataset"), 10);
    XYSeries l_series2 = randomSeries(LanguageSwitcher
        .getString("chrt_2_dataset"), 10);
    l_sCollection1.addSeries(l_series1);
    l_sCollection2.addSeries(l_series2);

    l_plot.setRangeAxis(0, new NumberAxis(LanguageSwitcher
        .getString("chrt_1_dataset")));
    l_plot.setRangeAxis(1, new NumberAxis(LanguageSwitcher
        .getString("chrt_2_dataset")));
    l_plot.setDataset(0, l_sCollection1);
    l_plot.setDataset(1, l_sCollection2);
    l_plot.mapDatasetToRangeAxis(0, 0);
    l_plot.mapDatasetToRangeAxis(1, 1);
    XYItemRenderer l_renderer1 = new XYLineAndShapeRenderer(true, false);
    XYItemRenderer l_renderer2 = new XYLineAndShapeRenderer(true, false);
    l_renderer1.setPaint(Color.RED);
    l_plot.setRenderer(0, l_renderer1);
    l_renderer2.setPaint(Color.BLUE);
    l_plot.setRenderer(1, l_renderer2);

    return l_chart;

    }

  /**
   * Returns the xy series.
   * 
   * @param p_caption
   *          The name of the series.
   * @param p_count
   *          The count of the value-pairs The maximal value for random
   *          values.
   * @return The series with random data.
   */

  private XYSeries randomSeries(String p_caption, int p_count)
    {
    XYSeries l_series = new XYSeries(p_caption);
    Random l_rand = new Random();
    for(double i = 0; i < p_count; i = i + 0.5)
      {
      l_series.add(i, l_rand.nextDouble());
      }
    return l_series;
    }

  /**
   * Returns the description of this chart.
   * 
   * @return The description of this chart.
   */

  public String getDescription()
    {
    return LanguageSwitcher.getString("xylc_description");
    }

  /**
   * Creates the xy line chart.
   * 
   * @return The xy line chart.
   */

  public JFreeChart createChart()
    {
    if(this.m_isTimeChart)
      {
      this.m_timeDataSet = Collections.create_list(-1);
      this.m_timeSeriesList = Collections.create_list(this.m_yValues
          .size());
      }
    else
      {
      this.m_xyDataSet = Collections.create_list(-1);
      this.m_xySeriesList = Collections.create_list(this.m_yValues.size());
      }

    for(int l_i = 0; l_i < this.m_yValues.size(); ++l_i)
      {
      if(this.m_isTimeChart)
        {
        this.m_timeDataSet.add(new TimeSeriesCollection());
        TimeSeries l_series = new TimeSeries(this.m_yValues.get(l_i)
            .toString(), Second.class);
        this.m_timeSeriesList.add(l_series);
        this.m_timeDataSet.get(l_i).addSeries(l_series);
        }
      else
        {
        this.m_xyDataSet.add(new XYSeriesCollection());
        XYSeries l_series = new XYSeries(this.m_yValues.get(l_i)
            .toString());
        this.m_xySeriesList.add(l_series);
        this.m_xyDataSet.get(l_i).addSeries(l_series);
        }
      }

    // Create the chart
    JFreeChart l_chart;
    if(this.m_isTimeChart)
      {
      l_chart = ChartFactory.createTimeSeriesChart(this.m_name,
          this.m_xValue.toString(), "Y Axis"new TimeSeriesCollection(),
          this.m_showLegend, true, false);
      }
    else
      {
      l_chart = ChartFactory.createXYLineChart(this.m_name, this.m_xValue
          .toString(), "Y Axis"new XYSeriesCollection(),
          PlotOrientation.VERTICAL, this.m_showLegend, true, false);
      }

    // Get the charts plot
    XYPlot l_plot = l_chart.getXYPlot();

    Color[] l_colors = { Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW,
        Color.DARK_GRAY, Color.ORANGE, Color.PINK };

    // Set the data sets
    for(int l_i = 0; l_i < this.m_yValues.size(); ++l_i)
      {

      l_plot.setRangeAxis(l_i, new NumberAxis(this.m_yValues.get(l_i)
          .toString()));
      if(this.m_isTimeChart)
        {
        l_plot.setDataset(l_i, this.m_timeDataSet.get(l_i));
        }
      else
        {
        l_plot.setDataset(l_i, this.m_xyDataSet.get(l_i));
        }
      l_plot.mapDatasetToRangeAxis(l_i, l_i);
      XYItemRenderer l_renderer = new XYLineAndShapeRenderer(
          this.m_lineVisibility, this.m_shapeVisibility);
      l_renderer.setPaint(l_colors[l_i % (l_colors.length)]);
      l_plot.setRenderer(l_i, l_renderer);

      }

    if(this.m_isTimeChart)
      {
      final DateAxis l_axis = (DateAxis) l_plot.getDomainAxis();
      l_axis
          .setDateFormatOverride(new SimpleDateFormat(this.m_timePattern));
      }

    return l_chart;

    }

  /**
   * @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_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_boundedValue
   *          The bounded value of the x achsis.
   */

  public void setBoundedValue(int p_boundedValue)
    {
    this.m_boundedValue = p_boundedValue;
    }

  /**
   * @param p_xAchsisIsBounded
   *          The status, if the x achsis is bounded.
   */

  public void setXAchsisIsBounded(boolean p_xAchsisIsBounded)
    {
    this.m_xAchsisIsBounded = p_xAchsisIsBounded;
    }

  /**
   * @param p_lineVisibility
   *          The status of the line visibility of the chart.
   */

  public void setLineVisibility(boolean p_lineVisibility)
    {
    this.m_lineVisibility = p_lineVisibility;
    }

  /**
   * @param p_shapeVisibility
   *          The status of the shape visibility of the chart.
   */

  public void setShapeVisibility(boolean p_shapeVisibility)
    {
    this.m_shapeVisibility = p_shapeVisibility;
    }

  }

File Information:

file name:XYLineChart.java
package:org.dgpf.gui.charts
qualified name:org.dgpf.gui.charts.XYLineChart.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/dgpf/gui/charts/XYLineChart.java
size:12.165 KB (12457 B)
uploaded: 2015-07-22 04:10:58 GMT+0000
last update: 2006-09-24 23:47:04 GMT+0000
last access: 2017-11-20 02:11:20 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2015-07-22 04:10:53 GMT+0000 served at 2017-11-20 02:11:20 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo