Jump to content
We've recently updated our Privacy Statement, available here ×

Chart not showing data from DatasetRun


donal

Recommended Posts

Hi,

I cannot get a chart to display data from a dataset.

I have a report with no subreports ,1 data set defined and 1 chart. The chart needs to use a java bean data source so I have defined a dataset and I have specified that the chart uses this data set as it's subdataset. (I'm using iReport 3.0.0). The java bean data source is supplied by a report scriptlet. The data set must not be created until the end of the report, so the chart has had it's evaluation time set to 'Report'.

Ive done the following testing: I have printed out statements from within the scriptlet's method for providing the data source: the data source is getting created at the end of the report and all values it requires are available. I have iterated through the data source using next() and all expected values are there. I have provided a chart customizer and retireved the datasetrun from the JRChart and confirmed that the name matches the dataset I provided via iReport.

The chart has the following properties: Eval time: Report, Subdataset: (my data set name), data source expression: ((my scriptlet class name)$P{REPORT_SCRIPTLET}).getChartDataSource(), Dataset reset type: Report, Dataset increment type: None (I have tried various combinations of these)

The datasource is created using: JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(beans);

The dataset has two fields: name, which is the category expression and value, which is the value expression.

Any help would be appreciated,

Thanks

Donal

Link to comment
Share on other sites

  • Replies 8
  • Created
  • Last Reply

Top Posters In This Topic

after doing some more testing I'm wondering if datasets which use a JRDataSource work at all with charts, has anybody gotten this to work?

Is it really a limitation that a query in a dataset definition, can't reference parameters / variables in the same report?

Link to comment
Share on other sites

donal


Wrote:


after doing some more testing I'm wondering if datasets which use a JRDataSource work at all with charts, has anybody gotten this to work?

 

Subdatasets that use JRDataSource objects work fine with charts, see attached example.

 

Is it really a limitation that a query in a dataset definition, can't reference parameters / variables in the same report?

Yes, datasets have their own parameter/variable realm and can't directly use the report's parameters/variables.

Regards,

Lucian

Link to comment
Share on other sites

Hi Lucian,

Thanks for the example. Unfortunantly when I run chart.jrxml I get an empty document. Looking at chart.jrxml I don't see anything unusual, other than for the field definitions in your dataset your provided a description, something I didn't do. I made this change but no luck, (however curirously the field description must match the field name?) My JRDataSource is backed by a bean collection. If you wouldn't mind I've included the bean, dataset and chart definition below, do you see any issues with my implementation?

Using a JRDataSource would have been the most elegant solution to my reporting requirements. A dataset would work, however any sql I need would have to be dependant on report parameters, as they are not available the only approach I have is to generate the sql outside the report and to transform it with a report template so that it's available at compile time, something I've had to do a lot in my use of jasperreports.

If you have an example of using a bean backed JRDataSource as a charts dataset, for a bar chart, using a dtd, I would appreciate it.

Thanks again,

Donal

 

 

 

Code:
//bean definitionpublic class ReportTotalBean{    private String name;    private Long value;    public ReportTotalBean(String name, Long value) {        this.name = name;        this.value = value;    }    public String getName() {        return name;    }    public Long getValue() {        return value;    }    public void setName(String name) {        this.name = name;    }    public void setValue(Long value) {        this.value = value;    }}//create the bean and put into parameter map for report        List<ReportTotalBean> beans = new ArrayList<ReportTotalBean>();        for(int i = 0; i < 2; i++){            ReportTotalBean bean = new ReportTotalBean("Bean"+i, Long.valueOf(100+i));            beans.add(bean);        }        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(beans);        parameters.put("TEST_DATA_SOURCE", dataSource);//subdataset definition	<subDataset name="SubDataset1" >		<field name="name" class="java.lang.String">			<fieldDescription><![CDATA[name]]></fieldDescription>		</field>		<field name="value" class="java.lang.Long">			<fieldDescription><![CDATA[value]]></fieldDescription>		</field>	</subDataset>//chart definition				<barChart>					<chart evaluationTime="Report"  hyperlinkTarget="Self"  customizerClass="com.l7tech.server.ems.standardreports.UsageSummaryChartCustomizer"  renderType="image" >					<reportElement						x="0"						y="0"						width="820"						height="130"						key="element-1"/>					<box></box>						<chartLegend textColor="#000000" backgroundColor="#FFFFFF" >					</chartLegend>					</chart>					<categoryDataset>						<dataset>							<datasetRun subDataset="SubDataset1">					<dataSourceExpression><![CDATA[$P{TEST_DATA_SOURCE}]]></dataSourceExpression>							</datasetRun>						</dataset>						<categorySeries>							<seriesExpression><![CDATA["# Successful Requests"]]></seriesExpression>							<categoryExpression><![CDATA[$F{name}]]></categoryExpression>							<valueExpression><![CDATA[$F{value}]]></valueExpression>				<itemHyperlink >				</itemHyperlink>						</categorySeries>					</categoryDataset>					<barPlot isShowLabels="true" >						<plot orientation="Horizontal" />						<categoryAxisFormat>							<axisFormat >							</axisFormat>						</categoryAxisFormat>						<valueAxisFormat>							<axisFormat >							</axisFormat>						</valueAxisFormat>					</barPlot>				</barChart>
Link to comment
Share on other sites

I forgot to mention that you would need to run my example with an empty data source (new JREmptyDataSource() or, in iReport, via the Execute with empty data source button).  Alternatively, you can set whenNoDataType="AllSectionsNoDetail" for the report.

My fields have description because this is required by the XML data source implementation.

Subdatasets can have parameters of their own, and if you want to use a report parameter in a subdataset query you would declare the same parameter in the subdataset, and transfer the value to the subdataset via <datasetParameter> elements.

I don't have an example of using a bean backed JRDataSource as a charts dataset, for a bar chart, using a dtd, but your code worked fine for me.  You must be doing something different..

Regards,

Lucian

Link to comment
Share on other sites

Hi Lucian,

Thanks a lot for your help on this, I really appreciate it. Yes I am doing something different, I had made a change to allow subreport return variables to be used in a master reports chart (http://jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=48456&res=41) and as a result datasets were no longer being calculated properly. I updated my other post with the additional changes required to keep data sets working.

Regards,

Donal

Link to comment
Share on other sites

  • 4 weeks later...
  • 4 years later...

I need to create chart

I have one subdataset  "ChartDataset" which has parameters Actual_cost,Actual_sale.Cost,Sale and month. Data to ChartDataset whould come from maindataset parameter "listOfCostSales"  which is a array list from java.  In chart data i gave subdataset as ChartDataset and added parameter as listOfCostSales and Expression as $P{listOfCostSales} and Category expression as $P{month} and Value expression as $P{Cost } and Connection/Data Source Exp. in Dataset Run as $P{REPORT_CONNECTION}.

 

My piece of jave code

private JasperPrint getProjectReportByActual(int projectId,String year,int period,String viewtype,String reportUser,HttpServletRequest request, String reportType) throws JRException, APSEException {  

//to resolve connection leak by huirong on 05/01/2012
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
 
 
 
try {
JasperPrint jasperPrint = null;
ServletContext application=getServletConfig().getServletContext();
 
//JRXML file processing
JasperCompileManager.compileReportToFile(application.getRealPath("/reports/project_consolidated_report_actual.jrxml"));
File reportFile = new File(application.getRealPath("/reports/project_consolidated_report_actual.jasper"));
if (!reportFile.exists())
throw new JRRuntimeException("File project_consolidated_report_actual.jasper not found."+
"The report design must be compiled first.");
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
 
//getting data
String url = accessdatabaseURL;
Properties props = new Properties();
props.setProperty("user",accessdatabaseUser);
props.setProperty("password",accessdatabasePwd);
conn = DriverManager.getConnection(url, props);
stmt = conn.createStatement();
 
Projects project=new ProjectsBiz().getProjectsById(projectId);
String proName=project.getProjectCode()+" - "+project.getProjectName();
 
//Give the datasourse to read data from AD DB...
rs = stmt.executeQuery(getQueryString(project.getProjectCode(),year,period)); 
JRResultSetDataSource dataSource = new JRResultSetDataSource(rs);
 
if(reportType.equals(PROJECT_BY_ACTUAL_APSE_AD_DB)){
 
break;
}
}
} else {
 
}
 
//TODO: to get Budget and Actual data for drawing, refer to TestHibernate to populate data
Map> monthMap = new ProjectCodeBiz().getProjectBudgetAndActualToMonth(projectId, year, period);,>
 
AccountingCalendar ojbAccountingCalendar = new AccountingCalendarBiz().getAccountingPeriod(year,period);
String fYrPeriod = period + "  " + ojbBMTAccountingCalendar.getPeriodDescription();
 
//Report's parameters
Map parameters = new HashMap();
parameters.put(REPORT_TITLE, title);
parameters.put(PROJECT_NAME, proName);
parameters.put(PERIOD_NO, fYrPeriod);
parameters.put(REPORT_GENERATOR, reportUser);
 
parameters.put(BUDGET_LABOUR_HOUR, labourHour);
parameters.put(BUDGET_LABOUR_COST, labourCost);
parameters.put(BUDGET_ICO_COST, icoCost);
parameters.put(BUDGET_SUBCON_COST, subconCost);
parameters.put(BUDGET_OTH_PURCHASE_COST, othPhurCost);
parameters.put(BUDGET_SALE, sale);
int count=0;
List   listOfCostSales  = new ArrayList();
 
if (monthMap != null) {
Iterator monthIterator = monthMap.keySet().iterator();
Integer monthKey;
Double actual_Cost =0.0;Double actual_Sale =0.0;
Double cost =0.0; Double sale1=0.0;
Map sumMap;,>
Iterator sumIterator;
String sumKey;
 
while(monthIterator.hasNext()){
monthKey = (Integer) monthIterator.next();
parameters.put("months1", monthKey);
sumMap = monthMap.get(monthKey);
logger.debug("monthKey"+monthKey);
sumIterator = sumMap.keySet().iterator();
 
while(sumIterator.hasNext()){
sumKey = (String) sumIterator.next();
count = count + 1;
BigDecimal value = (BigDecimal)sumMap.get(sumKey);
 
if (sumKey.equals("Actual_Cost"))
actual_Cost = ObjectUtils.round(value).doubleValue();  
if (sumKey.equals("Actual_Sale"))
actual_Sale = ObjectUtils.round(value).doubleValue();  
if (sumKey.equals("Cost"))
cost = ObjectUtils.round(value).doubleValue();  
if (sumKey.equals("Sale"))
sale1 = ObjectUtils.round(value).doubleValue();  
 
}
 
listOfCostSales.add(new CostSales(actual_Cost, actual_Sale,cost,sale1,monthKey));
}
 
}
 
// JRBeanCollectionDataSource beanToWrap = new JRBeanCollectionDataSource(listOfCostSales);
//
// logger.debug("listOfCostSales"+beanToWrap);
parameters.put("listOfCostSales", listOfCostSales);
}
 
 
   
jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,dataSource);
return jasperPrint;
 
and JRXML code
 
 
 
 
 
 
 
 
 
..................................
.....................
 
$P{REPORT_PARAMETERS_MAP}.get( "Actual_Cost" )
$P{REPORT_PARAMETERS_MAP}.get( "Actual_Sale" )
$P{REPORT_PARAMETERS_MAP}.get( "Cost" )
$P{REPORT_PARAMETERS_MAP}.get( "Sale" )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
<>

 

 

Link to comment
Share on other sites

MYJRXML code

 

 
 
$P{REPORT_PARAMETERS_MAP}.get( "Actual_Cost" )
$P{REPORT_PARAMETERS_MAP}.get( "Actual_Sale" )
$P{REPORT_PARAMETERS_MAP}.get( "Cost" )
$P{REPORT_PARAMETERS_MAP}.get( "Sale" )
 
 
 
........
.........
x="105"
y="258"
width="439"
height="208"
key="element-1"/>
$P{listOfCostSales}
$P{REPORT_CONNECTION}
"a"
$P{month}
$P{Cost}
$P{month}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...