Package org.pentaho.plugin.jfreereport.reportcharts.collectors

Source Code of org.pentaho.plugin.jfreereport.reportcharts.collectors.XYSeriesCollector

/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but 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.
*
* Copyright (c) 2009 Pentaho Corporation.  All rights reserved.
*/

package org.pentaho.plugin.jfreereport.reportcharts.collectors;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.jfree.data.general.Dataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.pentaho.reporting.engine.classic.core.event.ReportEvent;
import org.pentaho.reporting.engine.classic.core.function.Expression;

/**
* Todo: Document me!
* <p/>
* Date: 15.06.2009
* Time: 17:35:17
*
* @author Thomas Morgner.
*/
public class XYSeriesCollector extends AbstractCollectorFunction
{
  private ArrayList xValueColumns;
  private ArrayList yValueColumns;
  private transient HashMap seriesMap;

  public XYSeriesCollector()
  {
    xValueColumns = new ArrayList();
    yValueColumns = new ArrayList();
  }

  protected Dataset createNewDataset()
  {
    seriesMap = new HashMap();
    return new XYSeriesCollection();
  }


  public void setXValueColumn(final int index, final String field)
  {
    if (xValueColumns.size() == index)
    {
      xValueColumns.add(field);
    }
    else
    {
      xValueColumns.set(index, field);
    }
  }

  public String getXValueColumn(final int index)
  {
    return (String) xValueColumns.get(index);
  }

  public int getXValueColumnCount()
  {
    return xValueColumns.size();
  }

  public String[] getXValueColumn()
  {
    return (String[]) xValueColumns.toArray(new String[xValueColumns.size()]);
  }

  public void setXValueColumn(final String[] fields)
  {
    this.xValueColumns.clear();
    this.xValueColumns.addAll(Arrays.asList(fields));
  }

  public void setYValueColumn(final int index, final String field)
  {
    if (yValueColumns.size() == index)
    {
      yValueColumns.add(field);
    }
    else
    {
      yValueColumns.set(index, field);
    }
  }


  public String getYValueColumn(final int index)
  {
    return (String) yValueColumns.get(index);
  }

  public int getYValueColumnCount()
  {
    return yValueColumns.size();
  }

  public String[] getYValueColumn()
  {
    return (String[]) yValueColumns.toArray(new String[yValueColumns.size()]);
  }

  public void setYValueColumn(final String[] fields)
  {
    this.yValueColumns.clear();
    this.yValueColumns.addAll(Arrays.asList(fields));
  }

  /**
   * Return a completly separated copy of this function. The copy does no longer share any changeable objects with the
   * original function.
   *
   * @return a copy of this function.
   */
  public Expression getInstance()
  {
    final XYSeriesCollector expression = (XYSeriesCollector) super.getInstance();
    expression.xValueColumns = (ArrayList) xValueColumns.clone();
    expression.yValueColumns = (ArrayList) yValueColumns.clone();
    expression.seriesMap = null;
    return expression;

  }


  protected void buildDataset()
  {
    final XYSeriesCollection xySeriesDataset = (XYSeriesCollection) getDataSet();
    final List seriesList = xySeriesDataset.getSeries();
    if (seriesMap == null)
    {
      final HashMap seriesMap = new HashMap();
      for (int i = 0; i < seriesList.size(); i++)
      {
        final XYSeries series = (XYSeries) seriesList.get(i);
        seriesMap.put(series.getKey(), series);
      }
      this.seriesMap = seriesMap;
    }

    final int maxIndex = Math.min (this.xValueColumns.size(), this.yValueColumns.size());
    for (int i = 0; i < maxIndex; i++)
    {
      final Comparable seriesName = querySeriesValue(i);
      final Object xValueObject = getDataRow().get((String) xValueColumns.get(i));
      final Object yValueObject = getDataRow().get((String) yValueColumns.get(i));

      if (xValueObject instanceof Number == false)
      {
        continue;
      }
      final Number xValue = (Number) xValueObject;
      final Number yValue = (yValueObject instanceof Number) ? (Number) yValueObject : null;
     
      XYSeries series = (XYSeries) seriesMap.get(seriesName);
      if (series == null)
      {
        series = new XYSeries(seriesName);
        xySeriesDataset.addSeries(series);
        seriesMap.put(seriesName, series);
      }

      series.add(xValue, yValue, false);
    }
  }

  /**
   * Receives notification that report generation has completed, the report footer was printed, no more output is done.
   * This is a helper event to shut down the output service.
   *
   * @param event The event.
   */
  public void reportDone(final ReportEvent event)
  {
    seriesMap = null;
  }
}
TOP

Related Classes of org.pentaho.plugin.jfreereport.reportcharts.collectors.XYSeriesCollector

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.