From: Sean Y. <sy...@dy...> - 2004-01-06 16:00:28
|
Hi, =20 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. =20 =20 This is what I have so far: =20 public class JChartsApp { . . . =20 public static String renderGMCharts(HttpServletRequest request, = HttpServletResponse response)=20 { String chartName =3D request.getParameter("chartName"); String reportName =3D ""; =20 try { =20 if(chartName.equals("chart")) { reportName =3D "jChartsReport.jasper"; } =20 ServletContext application =3D ((ServletContext) = request.getAttribute("servletContext")); File reportFile =3D new = File(application.getRealPath("/proto/mocuser/reports/" + reportName)); =20 Map parameters =3D new HashMap(); =20 byte[] bytes; =20 bytes =3D JasperManager.runReportToPdf( reportFile.getPath(), parameters, new JREmptyDataSource() ); =20 response.setContentType("application/pdf"); response.setContentLength(bytes.length); =20 ServletOutputStream ouputStream =3D = response.getOutputStream(); ouputStream.write(bytes, 0, bytes.length); ouputStream.flush(); ouputStream.close(); =20 } catch (JRException e) { e.printStackTrace(); System.exit(1); } catch (Exception e) { e.printStackTrace(); System.exit(1); } return "validCharts"; } =20 =20 jChartsReport.xml (jChartsReport.jasper) contains a reference to this = class to generate the chart: =20 =20 public class JChartsScriptlet extends JRDefaultScriptlet=20 { public void afterReportInit() throws JRScriptletException=20 { =20 try=20 { =20 AreaChartProperties areaChartProperties =3D new = AreaChartProperties(); =20 double[][] data =3D {{10, 15, 30, 53}, {6, 30, 10, 21}, {20, = 25, 20, 8} }; Paint[] paints =3D {new Color( 0, 255, 0, 100 ), new Color( = 255, 0, 0, 100 ), new Color( 0, 0, 255, 100 )}; String[] legendLabels =3D {"Games", "Events", "Players" }; AxisChartDataSet axisChartDataSet =3D new AxisChartDataSet( = data, legendLabels, paints, ChartType.AREA, areaChartProperties ); =20 String[] axisLabels =3D {"January", "March", "May", "June"}; DataSeries dataSeries =3D new DataSeries( axisLabels, = "Months", "People", "Popular Events" ); dataSeries.addIAxisPlotDataSet( axisChartDataSet );=20 =20 ChartProperties chartProperties =3D new ChartProperties(); AxisProperties axisProperties =3D new AxisProperties(); LegendProperties legendProperties =3D new = LegendProperties(); =20 AxisChart axisChart =3D new AxisChart( dataSeries, = chartProperties, axisProperties, legendProperties, 500, 350 ); =20 BufferedImage bufferedImage =3D new BufferedImage( 500, 350, = BufferedImage.TYPE_INT_RGB ); =20 axisChart.setGraphics2D( bufferedImage.createGraphics() ); axisChart.render(); =20 super.setVariableValue( "ChartImage", bufferedImage ); } catch( ChartDataException chartDataException )=20 { throw new JRScriptletException( chartDataException ); } catch( PropertyException propertyException )=20 { throw new JRScriptletException( propertyException ); } catch( Exception ex)=20 { throw new JRScriptletException( ex ); } } =20 =20 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?? =20 (String cityId =3D request.getParameter("city") - in JChartsApp) =20 I would assume that my JDBC connection and query/sql statement reside in = JChartsScriptlet class. Is this correct?? =20 =20 Any help would greatly be appreciated :) =20 -sky |