eu.webtoolkit.jwt.chart
Class WChart2DRenderer

java.lang.Object
  extended by eu.webtoolkit.jwt.chart.WChart2DRenderer

public class WChart2DRenderer
extends java.lang.Object

Helper class for rendering a cartesian chart.

This class is used by WCartesianChart during rendering, and normally, you will not need to use this class directly. You may want to specialize this class if you want to override particular aspects of how the chart is renderered. In that case, you will want to instantiate the specialized class in WCartesianChart#createRenderer().

To simplify the simulatenous handling of Horizontal and Vertical charts, the renderer makes abstraction of the orientation of the chart: regardless of the chart orientation, the getWidth() corresponds to the length along the X axis, and getHeight() corresponds to the length along the Y axis. Similarly, calcChartArea() and getChartArea() return a rectangle where the bottom side corresponds to the lowest displayed Y values, and the left side corresponds to the lowest displayed X values. To map these "chart coordinates" to painter coordinates, use one of the hv() methods.

Note, this class is part of the internal charting API, and may be subject of changes and refactorings.


Nested Class Summary
static class WChart2DRenderer.AxisProperty
          Enumeration that specifies a property of the axes.
 
Field Summary
protected  AxisValue[] location_
          The computed axis locations.
 
Constructor Summary
WChart2DRenderer(WCartesianChart chart, WPainter painter, WRectF rectangle)
          Creates a renderer.
 
Method Summary
 void calcChartArea()
          Calculates the main plotting area rectangle.
 WRectF chartSegmentArea(WAxis yAxis, int xSegment, int ySegment)
          Returns the segment area for a combination of X and Y segments.
protected  int getCalcNumBarGroups()
          Calculates the total number of bar groups.
 WCartesianChart getChart()
          Returns the corresponding chart.
 WRectF getChartArea()
          Returns the main plotting area rectangle.
protected  int getHeight()
          Returns the height along the Y axis (as if orientation is Vertical).
 WPainter getPainter()
          Returns a reference to the painter.
protected  int getSegmentMargin()
          Returns the segment margin.
protected  int getWidth()
          Returns the width along the X axis (as if orientation is Vertical).
 WPointF hv(double x, double y)
          Conversion between chart and painter coordinates.
 WPointF hv(WPointF p)
          Conversion between chart and painter coordinates.
 WRectF hv(WRectF r)
          Conversion between chart and painter coordinates.
 void initLayout()
          Initializes the layout.
protected  void iterateSeries(SeriesIterator iterator)
          Iterates over the series using an iterator.
protected  void iterateSeries(SeriesIterator iterator, boolean reverseStacked)
          Iterates over the series using an iterator.
 WPointF map(double xValue, double yValue)
          Maps a (X, Y) point to chart coordinates.
 WPointF map(double xValue, double yValue, Axis axis)
          Maps a (X, Y) point to chart coordinates.
 WPointF map(double xValue, double yValue, Axis axis, int currentXSegment)
          Maps a (X, Y) point to chart coordinates.
 WPointF map(double xValue, double yValue, Axis axis, int currentXSegment, int currentYSegment)
          Maps a (X, Y) point to chart coordinates.
protected  void prepareAxes()
          Prepares the axes for rendering.
 void render()
          Renders the chart.
protected  void renderAxes(java.util.EnumSet<WChart2DRenderer.AxisProperty> properties)
          Renders one or more properties of the axes.
protected  void renderAxes(WChart2DRenderer.AxisProperty propertie, WChart2DRenderer.AxisProperty... properties)
          Renders one or more properties of the axes.
protected  void renderAxis(WAxis axis, java.util.EnumSet<WChart2DRenderer.AxisProperty> properties)
          Renders properties of one axis.
protected  void renderAxis(WAxis axis, WChart2DRenderer.AxisProperty propertie, WChart2DRenderer.AxisProperty... properties)
          Renders properties of one axis.
protected  void renderBackground()
          Renders the background.
 void renderLabel(java.lang.CharSequence text, WPointF p, WColor color, java.util.EnumSet<AlignmentFlag> flags, double angle, int margin)
          Utility function for rendering text.
protected  void renderLegend()
          Renders the (default) legend and chart titles.
protected  void renderSeries()
          Renders all series data, including value labels.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

location_

protected AxisValue[] location_
The computed axis locations.

See Also:
prepareAxes()
Constructor Detail

WChart2DRenderer

public WChart2DRenderer(WCartesianChart chart,
                        WPainter painter,
                        WRectF rectangle)
Creates a renderer.

Creates a renderer for the cartesian chart chart, for rendering in the specified rectangle of the painter.

Method Detail

getChart

public WCartesianChart getChart()
Returns the corresponding chart.


getPainter

public WPainter getPainter()
Returns a reference to the painter.


getChartArea

public WRectF getChartArea()
Returns the main plotting area rectangle.

This area is calculated and cached by calcChartArea().


calcChartArea

public void calcChartArea()
Calculates the main plotting area rectangle.

This method calculates the main plotting area, and stores it in the member chartArea_. The default implementation simply removes the plot area padding from the entire painting rectangle.

See Also:
WAbstractChart.getPlotAreaPadding(Side side)

initLayout

public void initLayout()
Initializes the layout.

This computes the chart plotting area dimensions, and intializes the axes so that they provide a suitable mapping from logical coordinates to device coordinates.


render

public void render()
Renders the chart.

This method renders the chart. The default implementation does the following:

 calcChartArea();           // sets chartArea_
    prepareAxes();             // provides logical dimensions to the axes
    
    renderBackground();        // render the background
    renderAxes(Grid);          // render the grid
    renderSeries();            // render the data series
    renderAxes(AxisProperty.Line, AxisProperty.Labels); // render the axes (lines & labels) 
    renderLegend();            // render legend and titles
   
 

You may want to reimplement this method to change the sequence of steps for rendering the chart.


map

public WPointF map(double xValue,
                   double yValue,
                   Axis axis,
                   int currentXSegment,
                   int currentYSegment)
Maps a (X, Y) point to chart coordinates.

This method maps the point with given (xValue, yValue) to chart coordinates. The y value is mapped by one of the Y axes indicated by axis.

Note that chart coordinates may not be the same as painter coordinates, because of the chart orientation. To map from chart coordinates to painter coordinates, use hv().

The currentXSegment and currentYSegment specify the axis segments in which you wish to map the point.


map

public final WPointF map(double xValue,
                         double yValue)
Maps a (X, Y) point to chart coordinates.

Returns map(xValue, yValue, Axis.OrdinateAxis, 0, 0)


map

public final WPointF map(double xValue,
                         double yValue,
                         Axis axis)
Maps a (X, Y) point to chart coordinates.

Returns map(xValue, yValue, axis, 0, 0)


map

public final WPointF map(double xValue,
                         double yValue,
                         Axis axis,
                         int currentXSegment)
Maps a (X, Y) point to chart coordinates.

Returns map(xValue, yValue, axis, currentXSegment, 0)


renderLabel

public void renderLabel(java.lang.CharSequence text,
                        WPointF p,
                        WColor color,
                        java.util.EnumSet<AlignmentFlag> flags,
                        double angle,
                        int margin)
Utility function for rendering text.

This method renders text on the chart position pos, with a particular alignment flags. These are both specified in chart coordinates. The position is converted to painter coordinates using hv(), and the alignment flags are changed accordingly. The rotation, indicated by angle is specified in painter coordinates and thus an angle of 0 always indicates horizontal text, regardless of the chart orientation.


hv

public WPointF hv(double x,
                  double y)
Conversion between chart and painter coordinates.

Converts from chart coordinates to painter coordinates, taking into account the chart orientation.


hv

public WPointF hv(WPointF p)
Conversion between chart and painter coordinates.

Converts from chart coordinates to painter coordinates, taking into account the chart orientation.


hv

public WRectF hv(WRectF r)
Conversion between chart and painter coordinates.

Converts from chart coordinates to painter coordinates, taking into account the chart orientation.


chartSegmentArea

public WRectF chartSegmentArea(WAxis yAxis,
                               int xSegment,
                               int ySegment)
Returns the segment area for a combination of X and Y segments.

This segment area is used for clipping when rendering in a particular segment.


prepareAxes

protected void prepareAxes()
Prepares the axes for rendering.

Computes axis properties such as the range (if not manually specified), label interval (if not manually specified) and axis locations. These properties are stored within the axes (we may want to change that later to allow for reentrant rendering by multiple renderers ?).


renderBackground

protected void renderBackground()
Renders the background.


renderAxes

protected void renderAxes(java.util.EnumSet<WChart2DRenderer.AxisProperty> properties)
Renders one or more properties of the axes.


renderAxes

protected final void renderAxes(WChart2DRenderer.AxisProperty propertie,
                                WChart2DRenderer.AxisProperty... properties)
Renders one or more properties of the axes.

Calls renderAxes(EnumSet.of(propertie, properties))


renderSeries

protected void renderSeries()
Renders all series data, including value labels.


renderLegend

protected void renderLegend()
Renders the (default) legend and chart titles.


getWidth

protected int getWidth()
Returns the width along the X axis (as if orientation is Vertical).


getHeight

protected int getHeight()
Returns the height along the Y axis (as if orientation is Vertical).


getSegmentMargin

protected int getSegmentMargin()
Returns the segment margin.

This is the separation between segments, and defaults to 40 pixels.


renderAxis

protected void renderAxis(WAxis axis,
                          java.util.EnumSet<WChart2DRenderer.AxisProperty> properties)
Renders properties of one axis.

See Also:
renderAxes(EnumSet properties)

renderAxis

protected final void renderAxis(WAxis axis,
                                WChart2DRenderer.AxisProperty propertie,
                                WChart2DRenderer.AxisProperty... properties)
Renders properties of one axis.

Calls renderAxis(axis, EnumSet.of(propertie, properties))


getCalcNumBarGroups

protected int getCalcNumBarGroups()
Calculates the total number of bar groups.


iterateSeries

protected void iterateSeries(SeriesIterator iterator,
                             boolean reverseStacked)
Iterates over the series using an iterator.


iterateSeries

protected final void iterateSeries(SeriesIterator iterator)
Iterates over the series using an iterator.

Calls iterateSeries(iterator, false)