Question

I am using JFreeChart library to create Chart on website (library integrated with my application according to this tutorial). Everything looks great except one thing: for some reason, for some data line chart is not completly visible (please see screen).

Not fully visible chart

I don't know why is it happening. I'm posting code responsible for configuration:

public JFreeChart createChart()
    {
        NumberAxis numberaxis = new NumberAxis("X");
        numberaxis.setAutoRangeIncludesZero(false);
        NumberAxis numberaxis1 = new NumberAxis("Y");
        numberaxis1.setAutoRangeIncludesZero(false);
        XYSplineRenderer xysplinerenderer = new XYSplineRenderer();

        XYPlot xyplot = new XYPlot(createSampleData(), numberaxis, numberaxis1, xysplinerenderer);
        xyplot.setBackgroundPaint(new Color(238, 242, 250));//
        xyplot.setDomainGridlinePaint(new Color(238, 242, 250));
        xyplot.setRangeGridlinePaint(new Color(238, 242, 250));
        xyplot.getRenderer().setSeriesPaint(0, Color.BLUE);
        xyplot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) xyplot.getRenderer();
        renderer.setSeriesShapesVisible(0, true);//FIXME Dots

        xyplot.getDomainAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        JFreeChart jfreechart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
        jfreechart.setBackgroundPaint(Color.white);
        return jfreechart;

    }
Was it helpful?

Solution

The link provided by Richard describes the problem well; but, ultimately, it looks like you need to manually set the upper bound of your range axis to account for the upper curve of the spline produced by XYSplineRenderer. For your example above, this might be:

xyplot.getRangeAxis().setUpperBound(22.5);

For practical purposes, you would probably want to calculate the maximum Y value, and either add a percentage to it, or more complicated, calculate a ceiling based on its surrounding points. I would start by adding 10% and see how that goes:

// Iterate data values; use Math.max() to determine maxYValue; then:
xyplot.getRangeAxis().setUpperBound( maxYValue + maxYValue * 0.1);

It's at patch, but it should provide the result you want, depending on the nature of your data and the curves produced connecting data points.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top