📄 chartproviderimpl.java
字号:
/*
* Copyright (C) 2003 Erik Swenson - eswenson@opensourcesoft.net
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* 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 General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package org.efs.openreports.providers.impl;
import java.awt.Image;
import java.sql.*;
import java.util.*;
import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.interceptor.component.ComponentManager;
import org.apache.log4j.Logger;
import org.efs.openreports.objects.*;
import org.efs.openreports.objects.chart.*;
import org.efs.openreports.providers.*;
import org.efs.openreports.providers.persistence.ChartPersistenceProvider;
import org.efs.openreports.util.*;
import net.sf.jasperreports.engine.design.JRDesignQuery;
import net.sf.jasperreports.engine.util.JRQueryExecuter;
public class ChartProviderImpl
implements ChartProvider, DataSourceProviderAware
{
protected static Logger log =
Logger.getLogger(ChartProviderImpl.class.getName());
private ChartPersistenceProvider chartPersistenceProvider;
private DataSourceProvider dataSourceProvider;
public ChartProviderImpl() throws ProviderException
{
ComponentManager container =
(ComponentManager) ActionContext.getContext().get(
"com.opensymphony.xwork.interceptor.component.ComponentManager");
container.initializeObject(this);
chartPersistenceProvider = new ChartPersistenceProvider();
log.info("Created");
}
public void addCharts(Report report, Map parameters)
throws ProviderException
{
ChartValue[] values =
getChartValues(report.getReportChart(), parameters);
Image chartImage =
ChartCreator.createChart(report.getReportChart(), values);
parameters.put("ChartImage", chartImage);
}
public void setDataSourceProvider(DataSourceProvider dataSourceProvider)
{
this.dataSourceProvider = dataSourceProvider;
}
public ChartValue[] getChartValues(ReportChart reportChart, Map parameters)
throws ProviderException
{
Connection conn = null;
PreparedStatement pStmt = null;
ResultSet rs = null;
try
{
ReportDataSource dataSource = reportChart.getDataSource();
conn = dataSourceProvider.getConnection(dataSource.getId());
// Use JasperReports Query logic to parse parameters in chart
// queries
JRDesignQuery query = new JRDesignQuery();
query.setText(reportChart.getQuery());
// convert parameters to JRDesignParameters so they can be parsed
Map jrParameters = ORUtil.buildJRDesignParameters(parameters);
pStmt =
JRQueryExecuter.getStatement(
query,
jrParameters,
parameters,
conn);
rs = pStmt.executeQuery();
Vector v = new Vector();
switch (reportChart.getChartType())
{
case ReportChart.BAR_CHART :
while (rs.next())
{
CategoryChartValue catValue = new CategoryChartValue();
catValue.setValue(rs.getDouble(1));
catValue.setSeries(rs.getString(2));
catValue.setCategory(rs.getString(3));
v.add(catValue);
}
break;
case ReportChart.PIE_CHART :
while (rs.next())
{
PieChartValue pieValue = new PieChartValue();
pieValue.setValue(rs.getDouble(1));
pieValue.setKey(rs.getString(2));
v.add(pieValue);
}
break;
case ReportChart.XY_CHART :
while (rs.next())
{
XYChartValue xyValue = new XYChartValue();
xyValue.setSeries(rs.getString(1));
xyValue.setValue(rs.getDouble(2));
xyValue.setSecondValue(rs.getDouble(3));
v.add(xyValue);
}
break;
case ReportChart.TIME_CHART :
while (rs.next())
{
TimeChartValue timeValue = new TimeChartValue();
timeValue.setSeries(rs.getString(1));
timeValue.setValue(rs.getDouble(2));
timeValue.setTime(rs.getTimestamp(3));
v.add(timeValue);
}
break;
}
ChartValue[] values = new ChartValue[v.size()];
v.copyInto(values);
return values;
}
catch (Exception e)
{
e.printStackTrace();
throw new ProviderException(LocalStrings
.getString(LocalStrings.ERROR_CHARTQUERY_INVALID)
+ ": " + e.toString());
}
finally
{
try
{
if (rs != null)
rs.close();
if (pStmt != null)
pStmt.close();
if (conn != null)
conn.close();
}
catch (Exception c)
{
log.error("Error closing");
}
}
}
public ReportChart getReportChart(Integer id) throws ProviderException
{
return chartPersistenceProvider.getReportChart(id);
}
public List getReportCharts() throws ProviderException
{
return chartPersistenceProvider.getReportCharts();
}
public ReportChart insertReportChart(ReportChart reportChart)
throws ProviderException
{
return chartPersistenceProvider.insertReportChart(reportChart);
}
public void updateReportChart(ReportChart reportChart)
throws ProviderException
{
chartPersistenceProvider.updateReportChart(reportChart);
}
public void deleteReportChart(ReportChart reportChart)
throws ProviderException
{
chartPersistenceProvider.deleteReportChart(reportChart);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -