You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Jeff LaMarche <je...@mac.com> on 2004/07/11 04:32:32 UTC

Re: JFreeChart

I stumbled across @ServiceLink mere moments after sending that 
e-mail... sorry to have bothered you.. =)


On Jul 9, 2004, at 3:20 PM, Shawn Church wrote:

> Jeff,
>
> IChartProvider is a minimal interface which looks like this:
>
> import org.jfree.chart.JFreeChart;
>
> /**
>  *  An object which can provide a {@link org.jfree.chart.JFreeChart}
> (to the {@link ChartService}).
>  **/
>
> public interface IChartProvider
> {
>     public JFreeChart getChart();
> }
>
>
> The class I have which implements IChartProvider is called ChartPage,
> and its implementation of getChart() creates and returns a JFreeChart.
> Below is my abbreviated code to create the chart, although there is
> nothing Tapestry-specific about the getChart() method itself.  I've
> stripped out a bunch of code since I'm doing some funny things with the
> chart image (we wanted the chart to have an image of one of our
> machines climbing up the line ;), but maybe it will be useful to you.
>
> The getChartImageAsset() is important however, since it provides the
> IAsset needed by the @Image component:
>
> public abstract class ChartPage extends BasePage
>    implements PageRenderListener, IChartProvider
> {
>    ...
>
>    public IAsset getChartImageAsset()
>    {
>       if (chartImageAsset == null)
>          chartImageAsset = new ChartAsset(getRequestCycle(), this);
>
>       return chartImageAsset;
>    }
>
>    public JFreeChart getChart()
>    {
>
>      String[] xAxisLabels = {"Sep", "Oct", "Nov", "Dec", "Jan",
>                               "Feb", "Mar", "Apr", "May", "Jun", "Jul",
> "Aug"};
>
>      ArrayList sections = getSections();
>      Iterator sectionIter = sections.iterator();
>      DefaultTableXYDataset dataset = new DefaultTableXYDataset();
>      int lastval = 0;
>      while (sectionIter.hasNext())
>      {
>         String section = (String) sectionIter.next();
>         XYSeries dataSeries = new XYSeries(section, true, false);
>
>         while (true)
>         {
>            lastval = 0;
>            double val = _salesData.getSepytd().doubleValue();
>            if (val <= 0)
>               break;
>            dataSeries.add(lastval++, val);
> 			
> 			...
>
>         }
>
>         dataset.addSeries(dataSeries);
>      }
>
>      //  Create the X-Axis
>      SymbolicAxis xAxis = new SymbolicAxis(null, xAxisLabels);
>      xAxis.setRange(0, 12);
>      xAxis.setLowerMargin(0.0);
>      xAxis.setUpperMargin(0.0);
>
>      //  Create the Y-Axis
>      NumberAxis yAxis = new NumberAxis(null);
>      yAxis.setRange(0, 55000000);
>      yAxis.setLabel("Sales");
>
>      //  Create the renderer
>      StackedXYAreaRenderer renderer =
>         new StackedXYAreaRenderer(XYAreaRenderer.AREA_AND_SHAPES);
>
>      renderer.setShapePaint(Color.gray);
>      renderer.setShapeStroke(new BasicStroke(0.5f));
>      renderer.setShape(new Ellipse2D.Double(-3, -3, 6, 6));
>      renderer.setOutline(true);
>
>      //  Create the plot
>      XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
>      plot.setForegroundAlpha(0.65f);
>
>      //  Reconfigure Y-Axis so the auto-range knows that the data is
> stacked
>      yAxis.configure();
>
> 	...
>
>      // Create the chart
>      JFreeChart chart = new JFreeChart(null,
> JFreeChart.DEFAULT_TITLE_FONT, plot, true);
>      chart.setBackgroundPaint(java.awt.Color.white);
>
>      return chart;
>    }
>
>
> Here is my ChartAsset (an IAsset is needed by the @Image Tapestry
> component):
>
> public class ChartAsset extends AbstractAsset
> {
>    private IEngineService _chartService;
>    private IComponent _chartProvider;
>
>    public ChartAsset(IRequestCycle cycle, IComponent chartProvider)
>    {
>       super(null, null);
>
>       IEngine engine = cycle.getEngine();
>
>       _chartService = engine.getService(ChartService.SERVICE_NAME);
>       _chartProvider = chartProvider;
>    }
>
>    public String buildURL(IRequestCycle cycle)
>    {
>       ILink l = _chartService.getLink(cycle, _chartProvider, null);
>       return l.getURL();
>    }
>
>    public InputStream getResourceAsStream(IRequestCycle cycle)
>    {
>       return null;
>    }
>
>    public InputStream getResourceAsStream(IRequestCycle cycle, Locale
> locale)
>    {
>       return null;
>    }
>
> }
>
> Finally, to have my page render and use the actual chart image, I use
> the @Image component in my ChartPage.html:
>
> <img jwcid="@Image" image="ognl:chartImageAsset"/>
>
>
> By the way, I've used a similar method to create PDF documents and
> other types of files where the bytestream is written to the service()
> method's ResponseOutputStream.  Hopefully this helps.
>
> Shawn
>
>
> Quoting Jeff LaMarche <je...@mac.com>:
>
>> Shawn:
>>
>> Thanks for posting your ChartService code - it's got me pointed in
>> the
>> right direction on a similar problem. One thing, though: Your code
>> references an interface called IChartProvider. I don't see it in your
>>
>> import statements, so I'm assuming it's also your code - any chance
>> you
>> could tell me a little more about it?
>>
>>
>> Thanks.
>> Jeff
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail:
>> tapestry-user-help@jakarta.apache.org
>>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org