Size of bars in HighLowRenderer

Discussion about JFreeChart related to stockmarket charts.
Locked
Bartholodeus
Posts: 2
Joined: Tue May 27, 2008 4:07 pm

Size of bars in HighLowRenderer

Post by Bartholodeus » Tue May 27, 2008 4:27 pm

Hello together,

I'm new to JFreeChart and unfamiliar with the customization possibilities of a HighLowRenderer. So far I need 2 things to customize which I could not figure out myself:

1.) Instead of lines I would like bars: basically thicker lines. I tried using a different Stroke size but then the open- and close ticks are getting too fat either (They should become larger, too, but not at the same rate).

Also I would like the size of the bars to adjust (getting larger/smaller) when zooming in or out otherwise they will overlap at some point.

2.) On opening the chart it shows the y-axis (whicht is a number-axis) from 0 to the maxValue even if there is nothing in the bottom 3/4.
I guess there is some sort of function opens the chart zoomed in so that only the part with data on it (with a margin of course) is shown.

I hope there are some easy solutions ^^

Thank you for your help,
Bartholodeus

RoyW
Posts: 93
Joined: Wed Apr 23, 2008 7:42 pm
Contact:

Post by RoyW » Thu May 29, 2008 3:20 pm

There is one easy solution and one difficult solution

for #2

Code: Select all

        rangeAxis.setAutoRangeIncludesZero(false);
for #1 you will have to extend the HighLowRenderer and implement the functionality you require.
Below is a quick example created by altering the "magic" numbers in the original renderer. You would have to look at other renderers (probably CandlestickRenderer) to figure out how to scale the width of the bars.

Code: Select all

    public class MyHighLowRenderer extends HighLowRenderer{
        public void drawItem(Graphics2D g2,
                             XYItemRendererState state,
                             Rectangle2D dataArea,
                             PlotRenderingInfo info,
                             XYPlot plot,
                             ValueAxis domainAxis,
                             ValueAxis rangeAxis,
                             XYDataset dataset,
                             int series,
                             int item,
                             CrosshairState crosshairState,
                             int pass) {

            double x = dataset.getXValue(series, item);
            if (!domainAxis.getRange().contains(x)) {
                return;    // the x value is not within the axis range
            }
            double xx = domainAxis.valueToJava2D(x, dataArea,
                    plot.getDomainAxisEdge());

            // setup for collecting optional entity info...
            Shape entityArea = null;
            EntityCollection entities = null;
            if (info != null) {
                entities = info.getOwner().getEntityCollection();
            }

            PlotOrientation orientation = plot.getOrientation();
            RectangleEdge location = plot.getRangeAxisEdge();

            Paint itemPaint = getItemPaint(series, item);
            Stroke itemStroke = getItemStroke(series, item);
            g2.setPaint(itemPaint);
            g2.setStroke(itemStroke);

            if (dataset instanceof OHLCDataset) {
                OHLCDataset hld = (OHLCDataset) dataset;

                double yHigh = hld.getHighValue(series, item);
                double yLow = hld.getLowValue(series, item);
                if (!Double.isNaN(yHigh) && !Double.isNaN(yLow)) {
                    double yyHigh = rangeAxis.valueToJava2D(yHigh, dataArea,
                            location);
                    double yyLow = rangeAxis.valueToJava2D(yLow, dataArea,
                            location);
                    if (orientation == PlotOrientation.HORIZONTAL) {
//                        g2.draw(new Line2D.Double(yyLow, xx, yyHigh, xx));
                        entityArea = new Rectangle2D.Double(Math.min(yyLow, yyHigh),
                                xx - 2.0, Math.abs(yyHigh - yyLow), 4.0);
                        g2.fill(entityArea);
                    }
                    else if (orientation == PlotOrientation.VERTICAL) {
//                        g2.draw(new Line2D.Double(xx, yyLow, xx, yyHigh));
                        entityArea = new Rectangle2D.Double(xx - 2.0,
                                Math.min(yyLow, yyHigh), 4.0,
                                Math.abs(yyHigh - yyLow));
                        g2.fill(entityArea);
                    }
                }

                double delta = 8.0;
                if (domainAxis.isInverted()) {
                    delta = -delta;
                }
                if (getDrawOpenTicks()) {
                    double yOpen = hld.getOpenValue(series, item);
                    if (!Double.isNaN(yOpen)) {
                        double yyOpen = rangeAxis.valueToJava2D(yOpen, dataArea,
                                location);
                        if (getOpenTickPaint() != null) {
                            g2.setPaint(getOpenTickPaint());
                        }
                        else {
                            g2.setPaint(itemPaint);
                        }
                        if (orientation == PlotOrientation.HORIZONTAL) {
                            g2.draw(new Line2D.Double(yyOpen, xx + delta, yyOpen,
                                    xx));
                        }
                        else if (orientation == PlotOrientation.VERTICAL) {
                            g2.draw(new Line2D.Double(xx - delta, yyOpen, xx,
                                    yyOpen));
                        }
                    }
                }

                if (getDrawCloseTicks()) {
                    double yClose = hld.getCloseValue(series, item);
                    if (!Double.isNaN(yClose)) {
                        double yyClose = rangeAxis.valueToJava2D(
                            yClose, dataArea, location);
                        if (getCloseTickPaint() != null) {
                            g2.setPaint(getCloseTickPaint());
                        }
                        else {
                            g2.setPaint(itemPaint);
                        }
                        if (orientation == PlotOrientation.HORIZONTAL) {
                            g2.draw(new Line2D.Double(yyClose, xx, yyClose,
                                    xx - delta));
                        }
                        else if (orientation == PlotOrientation.VERTICAL) {
                            g2.draw(new Line2D.Double(xx, yyClose, xx + delta,
                                    yyClose));
                        }
                    }
                }

            }
            else {
                // not a HighLowDataset, so just draw a line connecting this point
                // with the previous point...
                if (item > 0) {
                    double x0 = dataset.getXValue(series, item - 1);
                    double y0 = dataset.getYValue(series, item - 1);
                    double y = dataset.getYValue(series, item);
                    if (Double.isNaN(x0) || Double.isNaN(y0) || Double.isNaN(y)) {
                        return;
                    }
                    double xx0 = domainAxis.valueToJava2D(x0, dataArea,
                            plot.getDomainAxisEdge());
                    double yy0 = rangeAxis.valueToJava2D(y0, dataArea, location);
                    double yy = rangeAxis.valueToJava2D(y, dataArea, location);
                    if (orientation == PlotOrientation.HORIZONTAL) {
                        g2.draw(new Line2D.Double(yy0, xx0, yy, xx));
                    }
                    else if (orientation == PlotOrientation.VERTICAL) {
                        g2.draw(new Line2D.Double(xx0, yy0, xx, yy));
                    }
                }
            }

            // add an entity for the item...
            if (entities != null) {
                String tip = null;
                XYToolTipGenerator generator = getToolTipGenerator(series, item);
                if (generator != null) {
                    tip = generator.generateToolTip(dataset, series, item);
                }
                String url = null;
                if (getURLGenerator() != null) {
                    url = getURLGenerator().generateURL(dataset, series, item);
                }
                XYItemEntity entity = new XYItemEntity(entityArea, dataset,
                        series, item, tip, url);
                entities.add(entity);
            }

        }
    }

The answer does not come from thinking outside the box, rather the answer comes from realizing the truth; There is no Box. my js site

Locked