It was useful for me to have LineAndShapeRenderer also display in a step formation ( similar to XYStepRenderer). I've modified the code a bit and thought JFreeChart would like to inherit the changes. I'm not sure how to get them to David(?). But here's the code snipit of things I changed, I guess. Let me know if there's an easier way to do this merge, I'm using the cvs repository.
Enjoy.
Snebben
public class LineAndShapeRenderer extends AbstractCategoryItemRenderer {
/** Useful constant for specifying the type of rendering (stepped lines only). */
public static final int STEPS = 4;
/** A flag indicating whether or not stepped lines are drawn between XY points. */
protected boolean plotSteps;
/**
* Constructs a renderer of the specified type.
* <P>
* Use one of the constants SHAPES, LINES, SHAPES_AND_LINES or STEPS
*
* @param type The type of renderer.
* @param labelPosition Location of labels (if shown) relative to the
* data points (TOP, BOTTOM, LEFT, or RIGHT).
*/
public LineAndShapeRenderer(int type, int labelPosition) {
if (type==SHAPES)
this.plotShapes=true;
if (type==LINES)
this.plotLines=true;
if (type==SHAPES_AND_LINES) {
this.plotShapes = true;
this.plotLines = true;
}
if (type==STEPS)
this.plotSteps = true;
this.labelPosition = labelPosition;
}
/**
* Draw a single data item.
*
* @param g2 The graphics device.
* @param dataArea The area in which the data is drawn.
* @param plot The plot.
* @param axis The range axis.
* @param data The data.
* @param series The series number (zero-based index).
* @param category The category.
* @param categoryIndex The category number (zero-based index).
* @param previousCategory The previous category (will be null when the
* first category is drawn).
*/
public void drawCategoryItem(Graphics2D g2, Rectangle2D dataArea,
CategoryPlot plot,
ValueAxis axis,
CategoryDataset data,
int series,
Object category, int categoryIndex, Object previousCategory)
{
// first check the number we are plotting...
Number value = data.getValue(series, category);
if (value!=null) {
// current data point...
double x1 = plot.getCategoryCoordinate(categoryIndex, dataArea);
double y1 = axis
.translateValueToJava2D(value.doubleValue(), dataArea);
g2.setPaint(plot.getSeriesPaint(series));
g2.setStroke(plot.getSeriesStroke(series));
Shape shape = null;
if (this.plotShapes) {
shape = plot.getShape(series, category, x1, y1, shapeScale);
g2.fill(shape);
}
else {
shape = new Rectangle2D.Double(x1-2, y1-2, 4.0, 4.0);
}
if (this.plotLines) {
if (previousCategory!=null) {
Number previousValue =
data.getValue(series, previousCategory);
if (previousValue!=null) {
// previous data point...
double previous = previousValue.doubleValue();
double x0 = plot
.getCategoryCoordinate(categoryIndex-1, dataArea);
double y0 = axis
.translateValueToJava2D(previous, dataArea);
g2.setPaint(plot.getSeriesPaint(series));
g2.setStroke(plot.getSeriesStroke(series));
Line2D line = new Line2D.Double(x0, y0, x1, y1);
g2.draw(line);
}
}
}
if(this.plotSteps){
if (previousCategory!=null) {
Number previousValue =
data.getValue(series, previousCategory);
if (previousValue!=null) {
// previous data point...
double previous = previousValue.doubleValue();
double x0 = plot
.getCategoryCoordinate(categoryIndex-1, dataArea);
double y0 = axis
.translateValueToJava2D(previous, dataArea);
g2.setPaint(plot.getSeriesPaint(series));
g2.setStroke(plot.getSeriesStroke(series));
if( y0 == y1)
{
Line2D line = new Line2D.Double(x0, y0, x1, y1);
g2.draw(line);
}
else
{
Line2D line = new Line2D.Double(x0, y0, x1, y0);
g2.draw(line);
line = new Line2D.Double(x1, y0, x1, y1);
g2.draw(line);
}
}
}
}
if (plot.getLabelsVisible()) {
NumberFormat formatter = plot.getLabelFormatter();
Font labelFont = plot.getLabelFont();
g2.setFont(labelFont);
Paint paint = plot.getLabelPaint();
g2.setPaint(paint);
boolean rotate = plot.getVerticalLabels();
String label = formatter.format(value);
drawLabel(g2, label, x1, y1, labelFont, rotate);
}
// collect entity and tool tip information...
if (this.info!=null) {
EntityCollection entities = this.info.getEntityCollection();
if (entities!=null && shape!=null) {
String tip="";
if (this.toolTipGenerator!=null) {
tip = this.toolTipGenerator.generateToolTip(data,
series, category);
}
String url = null;
if (this.urlGenerator != null)
url = this.urlGenerator.generateURL(data, series, category);
CategoryItemEntity entity = new CategoryItemEntity(shape, tip,
url, series, category, categoryIndex);
entities.addEntity(entity);
}
}
}
}
}
additions to LineAndShapeRenderer.java
Re: additions to LineAndShapeRenderer.java
Thanks for the code. I think it would be good to put this into its own renderer class, LineAndShapeRenderer is already more complex than it should be. I'd sooner see many simple renderers that one big complex one.
Regards,
DG.
Regards,
DG.