001: /*
002: * Created on 02/08/2005
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2004 Igor Regis da Silva Sim�es
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023: package br.com.gfp.reports;
024:
025: import java.awt.Color;
026: import java.text.DecimalFormat;
027: import java.text.SimpleDateFormat;
028:
029: import net.sf.jasperreports.engine.JRDefaultScriptlet;
030: import net.sf.jasperreports.engine.JRScriptletException;
031:
032: import org.jfree.chart.ChartFactory;
033: import org.jfree.chart.JFreeChart;
034: import org.jfree.chart.axis.DateAxis;
035: import org.jfree.chart.labels.StandardXYToolTipGenerator;
036: import org.jfree.chart.plot.XYPlot;
037: import org.jfree.chart.renderer.xy.XYDifferenceRenderer;
038: import org.jfree.chart.renderer.xy.XYItemRenderer;
039: import org.jfree.data.time.Month;
040: import org.jfree.data.time.TimeSeries;
041: import org.jfree.data.time.TimeSeriesCollection;
042: import org.jfree.ui.RectangleInsets;
043:
044: import br.com.gfp.internationalization.ReportsMessages;
045:
046: /**
047: *
048: * @author Igor Regis da Silva Simoes
049: * @since 02/08/2005 07:58:34
050: */
051: public class GrafDespesasXReceitasMensal extends JRDefaultScriptlet {
052: private TimeSeries despesas = new TimeSeries(ReportsMessages
053: .getMessages().getString("Despesas"), Month.class);
054:
055: private TimeSeries receitas = new TimeSeries(ReportsMessages
056: .getMessages().getString("Receitas"), Month.class);
057:
058: /**
059: *
060: */
061: @Override
062: public void afterReportInit() throws JRScriptletException {
063: TimeSeriesCollection timeseriesCollection = new TimeSeriesCollection();
064: timeseriesCollection.addSeries(despesas);
065: timeseriesCollection.addSeries(receitas);
066:
067: JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(
068: ReportsMessages.getMessages().getString(
069: "GrafDespesasXReceitasMensalTitle"),
070: ReportsMessages.getMessages().getString("Mes"),
071: ReportsMessages.getMessages().getString("Value"),
072: timeseriesCollection, true, true, false);
073:
074: jfreechart.setBackgroundPaint(Color.WHITE);
075: XYPlot xyplot = jfreechart.getXYPlot();
076: xyplot.setRenderer(new XYDifferenceRenderer(Color.RED,
077: Color.GREEN, false));
078: xyplot.setBackgroundPaint(Color.LIGHT_GRAY);
079: xyplot.setDomainGridlinePaint(Color.WHITE);
080: xyplot.setRangeGridlinePaint(Color.WHITE);
081: xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
082: DateAxis dateaxis = new DateAxis(ReportsMessages.getMessages()
083: .getString("Mes"));
084: dateaxis.setLowerMargin(0.0D);
085: dateaxis.setUpperMargin(0.0D);
086: xyplot.setDomainAxis(dateaxis);
087: xyplot.setForegroundAlpha(0.5F);
088:
089: XYItemRenderer xyitemrenderer = jfreechart.getXYPlot()
090: .getRenderer();
091: StandardXYToolTipGenerator standardxytooltipgenerator = new StandardXYToolTipGenerator(
092: "{0}: ({1}, {2})", new SimpleDateFormat("MM-yyyy"),
093: new DecimalFormat("0.00"));
094: xyitemrenderer.setToolTipGenerator(standardxytooltipgenerator);
095:
096: this .setVariableValue("Chart", new JCommonDrawableRenderer(
097: jfreechart));
098: }
099:
100: /**
101: * Aqui n�s pegamos os resultados do SQL e colocamos nas vari�veis que vamos manipular
102: * @see net.sf.jasperreports.engine.JRDefaultScriptlet#afterDetailEval()
103: */
104: @Override
105: public void afterDetailEval() throws JRScriptletException {
106: despesas.add(
107: new Month(((Integer) this .getFieldValue("mesDespesa"))
108: .intValue(), ((Integer) this
109: .getFieldValue("anoDespesa")).intValue()),
110: new Double(
111: ((Double) this .getFieldValue("valorDespesa"))
112: .doubleValue()
113: * -1));
114: receitas.add(
115: new Month(((Integer) this .getFieldValue("mesDespesa"))
116: .intValue(), ((Integer) this
117: .getFieldValue("anoDespesa")).intValue()),
118: new Double(
119: ((Double) this .getFieldValue("valorReceita"))
120: .doubleValue()));
121: }
122: }
|