-2

I have generated a few charts, as per an assignment, and for data analysis I would like the domain axis to scale to my specifications, rather than to automatically fit the data in each series.

I found a solution here, which seemed very simple to follow: JFReeChart x axis scale

However, after I create my plot, with the code below, it seems like the method does not exist.

Is the method setDomainAxis() one that is included in the libraries or do I have to create it myself? Or have I violated some fundamental rule of java that has nothing to do with JFreeChart? I am a beginner java programmer, so please inform your answer accordingly. Thanks!

Here is my XYPlot class:

public class XYPlot extends JFrame {

    private String title;
    private String xAxis;
    private String yAxis; 
    private XYSeriesCollection dataset;

    public XYPlot(String header, String graphTitle, String xax, String yax) {
        super(header);
        xAxis = xax;
        yAxis = yax;
        title = graphTitle;
        XYPlot myPlot;
       // dataset = dat;

    } 

    public void setTit(String newTitle){
        title = newTitle;
    }

    public void setXAxis(String X){
        xAxis = X;
    }

    public void setYAxis(String Y){
        xAxis = Y;
    }

    public void passData(XYSeriesCollection data){
        dataset = data;
    }

    public void createChart(XYSeriesCollection dataIn){
        final JFreeChart chart = ChartFactory.createXYLineChart(
                title,
                xAxis,
                yAxis, 
                dataIn       
        );

        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(1200, 800));
        setContentPane(chartPanel);      


    }

And here is the implementation of the plot in my main program:

public static void seriesPlotter(XYSeriesCollection dataset, String title, String header, String xAxis, String yAxis) {

        XYPlot myPlot = new XYPlot(header, title, xAxis, yAxis);
        myPlot.createChart(dataset);
        NumberAxis domain = new NumberAxis();
        domain.setTickUnit(1);
        myPlot.setDomainAxis(domain);
        myPlot.pack();
        myPlot.setVisible(true);        


    }
Community
  • 1
  • 1
rocksNwaves
  • 5,331
  • 4
  • 38
  • 77
  • If the method doesn't exists, you will get an compiler error. But the method is in the XYPlot class. Maybe try to set first the setDomainAxis() and then createChart() ... – PeterMmm Oct 06 '16 at 19:54
  • @PeterMmm Ok! I'll give it a go. I wrote the XYPlot class based on the tutorials found JFreeChart's forums and elsewhere. Nowhere can I find a sample of how such a method might be created, so I kinda assumed it was part of the library download. – rocksNwaves Oct 06 '16 at 20:22

1 Answers1

1

Your fragment's use of XYPlot is inconsistent with the API. In particular, there is no such constructor, and there is no method named createChart(). Verify that you are using the current version, 1.0.19, available here.

The methods of ChartFactory are an excellent guide to creating a chart from individual subcomponents. As a concrete example, ChartFactory.createXYLineChart() is recapitulated here. Note how the axes are passed to the XYPlot constructor, obviating the need to invoke setDomainAxis() explicitly. In outline,

// axes
NumberAxis domain = new NumberAxis(xAxis);
NumberAxis range = new NumberAxis(yAxis);
// renderer
XYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
// plot
XYPlot plot = new XYPlot(dataset, domain, range, renderer);
// chart
JFreeChart chart = new JFreeChart(
    title", JFreeChart.DEFAULT_TITLE_FONT, plot, false);

Maybe you could take a second look after I edit to add the additional code.

  • Your XYPlot is a JFrame which has no setDomainAxis() method; rename your class to avoid the conflict, e.g. MyXYPlot.

  • Let your renamed class have a member variable to hold a reference to the org.jfree.chart.plot.XYPlot and add a method to update the name.

    XYPlot myPlot;
    …
    public void setDomainAxisName(String name){
        myPlot.getDomainAxis().setLabel(name);
    }
    
  • There's no reason to extend JFrame.

My technical vocabulary is nil.

Experimenting with JFreeChart is an excellent opportunity to learn.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks @trashgod. I will add the code where I created that method myself (as seen somewhere else on the internet). I am not advanced enough to understand the API, as my technical vocabulary is nil. Maybe you could take a second look after I edit to add the additional code. – rocksNwaves Oct 07 '16 at 15:27
  • The update is helpful. I wait to click the check mark until I feel that the question has been legitimately answered, and also to give time to others to provide me other, unique approaches. I am well aware of what the checkmark does, but you have answered several of my questions about JFreeChart by providing information having nothing to do with the question asked, leading me to feel like you simply want to rack up internet cool points. I respect your knowledge, which is obviously extensive and vastly greater than my own, but I'm not here to validate that for you. – rocksNwaves Oct 08 '16 at 11:09
  • Your inference is unwarranted; my goal is to create useful answers to interesting questions; your update illustrates several common design flaws; I'm pleased to address them in addition to answering your question; you should only accept an answer that best represents your adopted solution; feel free to leave the question open against a better answer. – trashgod Oct 08 '16 at 18:31