Standard Deviation domain axis ticks start with mean value

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.

Standard Deviation domain axis ticks start with mean value

Post by Higeath » Wed Oct 26, 2016 8:37 pm

I am trying to have my ticks start in the middle with whatever is set to mean value and then the next tick would be mean + sd

e.g. mean: 5 sd: 2 - -1 1 3 5 7 9 11
mean: 1 sd: 1 - -3 -2 -1 0 1 2 3 4
mean: 3 sd: 3 - -6 -3 0 3 6 9 12

Example image - red numbers on X-axis is how it should look like
Image

Code: Select all

public class JFreeChartPanel extends JPanel {
    private final XYPlot plot;
    double mean = 3.0, sd = 2.0;
    XYDataset dataset = initDataset();
    NumberAxis domain = new NumberAxis("X") {
    @Override
    protected double calculateLowestVisibleTickValue() {
        double lowTickValue = super.calculateLowestVisibleTickValue();
        if (mean % 2 == 1) {
            return lowTickValue + 1;
        } else {
            return lowTickValue;
        }
    }
};
    public JFreeChartPanel(){
        JFreeChart chart = ChartFactory.createXYLineChart(
            "Normal Distribution",
            "X", 
            "PDF", 
            dataset,
            PlotOrientation.VERTICAL,
            false,
            false,
            false
        );
        plot=chart.getXYPlot();
        domain.setAutoRangeStickyZero(false); //Fixes the margin issue with 0
        domain.setTickUnit(new NumberTickUnit(sd)); //Spacing on X-axis should be standard deviation + mean   
        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");
        return dataset;
    }

    public double getMean() {
        return mean;
    }

    public void setMean(double mean) {
       this.mean = mean;
       plot.setDataset(initDataset());
    }

    public double getSd() {
        return sd;
    }

    public void setSd(double sd) {
        this.sd = sd;
        domain.setTickUnit(new NumberTickUnit(sd));
        plot.setDataset(initDataset());
    }
}

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Standard Deviation domain axis ticks start with mean val

Post by John Matthews » Thu Oct 27, 2016 11:53 am

Cross-posted here.

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Standard Deviation domain axis ticks start with mean val

Post by david.gilbert » Thu Oct 27, 2016 5:11 pm

I don't know a reliable way to get the result you want by just tweaking the existing algorithm. My recommendation here would be to override the refreshTicks() method in the NumberAxis and take over control of the ticks that get generated.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked