I have been searching codes to draw line charts using jfreechart on internetand encountering the constant problem of pack(). When I copy the code I find on net the compiler compiles it. But when I try to use it in my code, the compiler shows error. I don't find its definition in the copied code and I import all the header files from the code to resolve this but it's all useless. Please tell me what I m missing. I need an urgent help.

//My code is:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package income;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Stroke;
import java.sql.*;
import java.text.NumberFormat;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.*;
import org.jfree.data.*;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.plot.XYPlot;
import java.util.Date;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.CategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.ApplicationFrame;

/**
 *
 * @author student
 */

        ResultSet rs;
        Statement stmt;
        Connection con;
        Date kk=new Date();
         final XYSeries series=new XYSeries("Savings");

public SavingChart(final String title){




        final CategoryDataset dataset =createDataset();

        final JFreeChart chart=createChart(dataset);


        final ChartPanel chartPanel = new ChartPanel(chart);
        this.add(chartPanel,BorderLayout.CENTER);

JPanel customPanel = new JPanel();
JLabel lbl = new JLabel("<html><h3>Note : You can add any important note here.</h3></html>");
        customPanel.add(lbl);

    }

    private void add(ChartPanel chartPanel, String CENTER) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

        private CategoryDataset createDataset() {
// create the dataset...
final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con=DriverManager.getConnection("jdbc:odbc:Ek","","");
        stmt=con.createStatement();
        rs=stmt.executeQuery("select * from Saving");



       while(rs.next())
        { 
        dataset.addValue(rs.getFloat(2), "series", "2/2/2013");
        }

}
catch(Exception e){System.out.println("Nahi Hua... :(");}
        return dataset;
    }



     private JFreeChart createChart(final CategoryDataset dataset) {
       final JFreeChart chart =ChartFactory.createLineChart(
               "Your Saving Graphs",  // charttitle
               "Date", // domain(x-//axis) axis label
               "V", // range(y-axis)//axis label
                dataset, // data
                PlotOrientation.VERTICAL, //orientation
                true, // include legend
                true, // tooltips
                false // urls
                );

chart.setBackgroundPaint(Color.white);

final CategoryPlot plot = (CategoryPlot)chart.getPlot();
plot.setBackgroundPaint(new Color(0xffffe0));
plot.setDomainGridlinesVisible(true);
plot.setDomainGridlinePaint(Color.lightGray);
plot.setRangeGridlinePaint(Color.lightGray);

final NumberAxis rangeAxis =(NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
rangeAxis.setVerticalTickLabels(true);

final LineAndShapeRenderer renderer =(LineAndShapeRenderer) plot.getRenderer();

        NumberFormat format =NumberFormat.getNumberInstance();
        format.setMaximumFractionDigits(2);
        CategoryItemLabelGenerator generator =new StandardCategoryItemLabelGenerator(StandardCategoryItemLabelGenerator.DEFAULT_LABEL_FORMAT_STRING,format);
        renderer.setBaseItemLabelGenerator(generator);
        renderer.setBaseItemLabelsVisible(true);


renderer.setBaseShapesFilled(true);
renderer.setBaseShapesVisible(true);

Stroke stroke = new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL);
renderer.setBaseOutlineStroke(stroke);

        return chart;
    }




    public static void main(String[] args){



        SavingChart svc=new SavingChart("Savings");
        svc.pack();
    RefineryUtilities.centerFrameOnScreen(svc);
    svc.setVisible(true);

    }
}

Plz ignore errors.

  • JFreeCharts are based on AWT,

  • calculation of final size (on the screen for all Swings LayoutManagers) is based on PreferredSize

  • childs returns PreferredSize back to contianer, then JFrame.pack(BorderLayout implemented in API) will calculate proper Rectangle for JFrame('s ContentPane)

  • good practicies is to override getPreferredSize for ChartPanel (then will be resizable with its contianer) or (in the case of extreme emergency :-) to hardcode coordinates (height & weight) for ChartPanels constructor, more details to see in API

Edited 3 Years Ago by mKorbel

Hello EkDs
Basically you are making this near impossible for us to help you.
You say "the compiler shows error" but you don't say what error message or which line it refers to.
You post a whole load of code which is onbviously incomplete and cannot possibly be compiled, and tell us to "ignore errors".

Then you ask us "what am I missing?"... How can we possibly tell?

So please post the complete actual code, and post the exact complete text of your error message(s).

This article has been dead for over six months. Start a new discussion instead.