Random bug with XYDifferenceRenderer

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Higeath
Posts: 9
Joined: Sun Oct 23, 2016 3:44 pm
antibot: No, of course not.

Random bug with XYDifferenceRenderer

Post by Higeath » Mon Nov 21, 2016 11:40 pm

I have a graph that displays normal distribution from jfreechart and I shade it using XYDifferenceRenderer to show the user inputted area. For some reason when I input the mean:2 standard deviation:3 and want to shade the area outside of -2 and 6 a weird visual bug occurs even though the values for XYDifferenceRenderer seem to be correct (there are attached images below).

Code: Select all

    private final XYPlot plot;
    double mean = 0.0, sd = 1.0;

    XYSeries area = new XYSeries("area");
    XYDifferenceRenderer areaRenderer = new XYDifferenceRenderer();
    
    Color none = new Color(0, 0, 0, 0);
    Color teal = new Color(0, 128, 128, 255);
    
    XYDataset dataset = initDataset();
    NumberAxis domain;

    public JFreeChartPanel() {
        JFreeChart chart = ChartFactory.createXYLineChart(
                "Normal Distribution",
                "X",
                "PDF",
                dataset,
                PlotOrientation.VERTICAL,
                false,
                false,
                false
        );

        plot = chart.getXYPlot();
        domain = (NumberAxis) plot.getDomainAxis();
        domain.setAutoRangeStickyZero(false); //Fixes the margin issue with 0
        domain.setTickUnit(new NumberTickUnit(sd)); //Spacing on X-axis should be standard deviation + mean   

        areaRenderer.setNegativePaint(none);//hide the area where the values for the second series are higher
        areaRenderer.setSeriesPaint(1, none);//hide the outline of the "difference area"
        areaRenderer.setPositivePaint(teal);
        plot.setRenderer(areaRenderer);

        plot.setDomainAxis(domain);
        final ChartPanel chartPanel = new ChartPanel(chart);
        setLayout(new BorderLayout());
        add(chartPanel);
    }

    private XYDataset initDataset() {
        double minX = mean - (4 * sd), maxX = mean + (4 * sd);  //Minimum and Maximum values on X-axis (4 deviations)
        Function2D normal = new NormalDistributionFunction2D(mean, sd);
        XYDataset dataset = DatasetUtilities.sampleFunction2D(normal, minX, maxX, 100, "Normal");
        area.clear();
        ((XYSeriesCollection) dataset).addSeries(area);
        return dataset;
    }
Methods that I use to shade and clear the area

Code: Select all

    public void clearArea() {
        area.clear();
    }

    public void shadeArea(double x, boolean isInfinity) {
        area.clear();
        if (isInfinity) { //Above infinity
            area.add(x, 0);
            area.add(mean + sd * 4, 0);
        } else { //Below infinity
            area.add(x, 0);
            area.add(mean - sd * 4, 0);
        }

    }

    public void shadeArea(double x1, double x2, boolean isBetween) {
        area.clear();
        if (isBetween) {
            double PDF = dataset.getYValue(0, dataset.getItemCount(0) / 2); //Value that is rounded up to 2 decimal places for the mean PDF value
            area.add(mean - sd * 4.0+0.2, 0);
            area.add(x1, 0);
            area.add(x1, PDF);
            area.add(x2, PDF);
            area.add(x2, 0);
            area.add(mean + sd * 4.0 +0.2, 0);
        } else {
            area.add(x1, 0);
            area.add(x2, 0);
        }
    }
I use XYDifferenceRenderer to shade area on my Normal distribution graph which seems to be working fine:

Image

But it bugs out when mean is set to 2, standard deviation to 3 and i want to shade values outside of -2 and 6 (all other values shade correctly). This bug also occurs when I multiply the values mean 4, sd 6 values outside of -4 and 12

Image

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Random bug with XYDifferenceRenderer

Post by paradoxoff » Tue Nov 22, 2016 2:06 pm

I played with the code that I wrote for your earlier post, and I found some visual bugs as well, though they were different from what you observe.
I "solved" it by increasing the number of data points for the NormalDistributionFunction from 100 to 120.

Higeath
Posts: 9
Joined: Sun Oct 23, 2016 3:44 pm
antibot: No, of course not.

Re: Random bug with XYDifferenceRenderer

Post by Higeath » Tue Nov 22, 2016 2:37 pm

Paradoxoff cheers mate I owe you yet again. Should I increase it more dramatically than 120? to like 200 just for the safety net?

Locked