From: Nathaniel G. A. <nat...@ya...> - 2004-01-07 16:10:51
|
I am not sure off the top of my head how to pass arguments to a JasperReport Scriptlet. You would have to look on their site. --- Sean Yee <sy...@dy...> wrote: > Hi, > > I have recently began to work with jCharts. I was able to go through all of the tutorials and > examples with no problems, generating charts with static data. I am now trying to build a test > to generate charts based on data passed in from the user via web app and dynamic sql statements. > > > This is what I have so far: > > public class JChartsApp > { > . > . > . > public static String renderGMCharts(HttpServletRequest request, HttpServletResponse > response) > { > String chartName = request.getParameter("chartName"); > String reportName = ""; > > try > { > > if(chartName.equals("chart")) > { > reportName = "jChartsReport.jasper"; > } > > ServletContext application = ((ServletContext) > request.getAttribute("servletContext")); > File reportFile = new File(application.getRealPath("/proto/mocuser/reports/" + > reportName)); > > Map parameters = new HashMap(); > > byte[] bytes; > > bytes = > JasperManager.runReportToPdf( > reportFile.getPath(), > parameters, > new JREmptyDataSource() > ); > > response.setContentType("application/pdf"); > response.setContentLength(bytes.length); > > ServletOutputStream ouputStream = response.getOutputStream(); > ouputStream.write(bytes, 0, bytes.length); > ouputStream.flush(); > ouputStream.close(); > } > catch (JRException e) > { > e.printStackTrace(); > System.exit(1); > } > catch (Exception e) > { > e.printStackTrace(); > System.exit(1); > } > return "validCharts"; > } > > > jChartsReport.xml (jChartsReport.jasper) contains a reference to this class to generate the > chart: > > > public class JChartsScriptlet extends JRDefaultScriptlet > { > public void afterReportInit() throws JRScriptletException > { > > try > { > > AreaChartProperties areaChartProperties = new AreaChartProperties(); > > double[][] data = {{10, 15, 30, 53}, {6, 30, 10, 21}, {20, 25, 20, 8} }; > Paint[] paints = {new Color( 0, 255, 0, 100 ), new Color( 255, 0, 0, 100 ), new > Color( 0, 0, 255, 100 )}; > String[] legendLabels = {"Games", "Events", "Players" }; > AxisChartDataSet axisChartDataSet = new AxisChartDataSet( data, legendLabels, > paints, ChartType.AREA, areaChartProperties ); > > String[] axisLabels = {"January", "March", "May", "June"}; > DataSeries dataSeries = new DataSeries( axisLabels, "Months", "People", "Popular > Events" ); > dataSeries.addIAxisPlotDataSet( axisChartDataSet ); > > ChartProperties chartProperties = new ChartProperties(); > AxisProperties axisProperties = new AxisProperties(); > LegendProperties legendProperties = new LegendProperties(); > > AxisChart axisChart = new AxisChart( dataSeries, chartProperties, axisProperties, > legendProperties, 500, 350 ); > > BufferedImage bufferedImage = new BufferedImage( 500, 350, > BufferedImage.TYPE_INT_RGB ); > > axisChart.setGraphics2D( bufferedImage.createGraphics() ); > axisChart.render(); > > super.setVariableValue( "ChartImage", bufferedImage ); > } > catch( ChartDataException chartDataException ) > { > throw new JRScriptletException( chartDataException ); > } > catch( PropertyException propertyException ) > { > throw new JRScriptletException( propertyException ); > } > catch( Exception ex) > { > throw new JRScriptletException( ex ); > } > } > > > The lines in red are to be replaced with data from a database. But if I wish to create dynamic > sql statements via HttpServletRequest which would result in dynamic charts, how can I pass > things like cityId to this SQL statement?? > > (String cityId = request.getParameter("city") - in JChartsApp) > > I would assume that my JDBC connection and query/sql statement reside in JChartsScriptlet class. > Is this correct?? > > > Any help would greatly be appreciated :) > > -sky > __________________________________ Do you Yahoo!? Yahoo! Hotjobs: Enter the "Signing Bonus" Sweepstakes http://hotjobs.sweepstakes.yahoo.com/signingbonus |