• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2948
  • Last Modified:

JFreeChart Help

Hi all,

Does anyone have any good tutorials on JFreeChart, specifically creating thermometers, 'meters' al la speedometer, etc?

I'm pretty new to all this, and am having a nightmare job creating charts. I've created an instance of ThermometerPlot, but don't really know exactly what to do with it.

I just want to create some components to add to a Panel to view them.

The more info the better, please.
0
Shemmie
Asked:
Shemmie
  • 5
  • 2
2 Solutions
 
InteractiveMindCommented:
Hey Shemmie,

I once tried JFreeChart; it was rubbish -- I never really got on with it.. So, I decided to learn how to draw graphics myself, and can now create my own Graphs for my applications.

Have you considered learning just the basics of Java Graphics? It's actually very easy! :)

Regards;
0
 
InteractiveMindCommented:
...and if you're not too bad at Mathematics, then that helps a lot :) Alternatively though, you can ask for Math help at this site.
Other than that, JFreeChart don't seem to have many (if any) tutorials that cover what you're trying to achieve... Perhaps use this one to get the feel of JFreeChart (http://www.cdf.utoronto.ca/~csc408h/winter/project/contrib/Megha_Kumar_JFreeChart_Tutorial.html), then from there, just improvise :) And figure the rest out yourself...? You may also find the JFreeChart spec useful:
http://www.jfree.org/jfreechart/javadoc/overview-summary.html

But like I said, definitely consider learning to create your own Graphs. :)
0
 
ashok3sepCommented:
Here are some links for the JFreeChart tutorials

http://www.javaworld.com/javaworld/jw-12-2002/jw-1227-opensourceprofile.html

http://sourceforge.net/projects/jfreechart/

http://cewolf.sourceforge.net/

http://www.jspin.com/home/apps/graphics
http://www.jspin.com/home/beans/graphics
http://www.utdallas.edu/~ntl021000/ASE/tutorials/netbeans_tutorial.htm

Here is a simple example for the Pie Chart

import com.jrefinery.data.DefaultPieDataset;
import com.jrefinery.chart.ChartFactory;
import com.jrefinery.chart.JFreeChart;
import com.jrefinery.chart.ChartFrame;
public class Test {
public static void main(String[] args) {

// create a dataset...
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("Category 1", new Double(43.2));
data.setValue("Category 2", new Double(27.9));
data.setValue("Category 3", new Double(79.5));

// create a chart...
JFreeChart chart = ChartFactory.createPieChart("Sample Pie Chart",
data,true); // legend

// create and display a frame...
ChartFrame frame = new ChartFrame("Test", chart);
frame.pack();
frame.setVisible(true);
}
}

Please use the following to do this...this is available from version 0.9.6
                 Pieplot.setPaint(int, ChartColor)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ShemmieAuthor Commented:
ashok3sep:

Thanks for the reply. I've already got the two homepage links for JFreeChart, there's just very little details they provide, unless I'm mistaken?

I specifically want to use it for a Java application, cewolf seems to be geared towards JSP.

http://www.utdallas.edu/~ntl021000/ASE/tutorials/netbeans_tutorial.htm leads to a server error.

Having had a look through, none of them lead to tutorials on the thermometer or 'speedometer' type chart, please correct me if I'm wrong. Thank you for your input none the less.

InteractiveMind :

Thanks a lot for the replies.

Hehe, rubbish? Urgh... everywhere was singing its praises, but with the very little in the way of tutorials on the net, beyond pie charts and bar charts, I'd certainly be inclined to believe you.

http://www.cdf.utoronto.ca/~csc408h/winter/project/contrib/Megha_Kumar_JFreeChart_Tutorial.html

That provided at least some idea of how datasets work in relation to chart, but it's a so n so trying to apply it to thermometer and speedometer.

Hmm... a thermometer's just a circle with a barchart on top... drawable. You might be onto something with the DIY. Could I be cheeky, and ask if you'd be willing to share a snippet of basic DIY bar chart that you've done?
0
 
InteractiveMindCommented:
hehe :-)

> Could I be cheeky, and ask if you'd be willing to share a snippet of basic DIY bar chart that you've done?

Sure, here's one that I created for a Business Management system (for my own uses). I needed to display data in line graphs, and also have the ability to extrapolate the given data (e.g. I'm given the data for January, February and March, I then need to use this data to estimate the values for November.. well this graph does it for you!)  :-)


//------ LineGraph.java
   
    package darkins.visual.graphs;
   
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.border.*;
   
    public class LineGraph extends JLabel
    {
       
        private GraphDimension  gd          = null;
       
        private Color           lineColor   = Color.BLACK;
        private Color           gridColor   = new Color(200, 200, 200);
        private Color           axisColor   = Color.BLACK;
        private Color           bgColor     = Color.WHITE;
        private Color           borderColor = Color.BLACK;
        private Color           labelColor  = Color.RED;
        private Color           extrapColor = Color.BLUE;
       
        private int     []      data        = null;
        private String  []      xKey        = null;
       
        private final   int     margin      = 15;    // px
        private final   int     Y_SHIFT     = 4;     //
        private final   int     X_SHIFT     = 3;     //
       
        private boolean         extrapolate = false;
        private int             noOfData    = 0;
       
        private boolean         extrapPos   = false;
       
       
        public LineGraph( int width, int height )
        {
            gd = new GraphDimension(width, height);
           
            this.setPreferredSize(new Dimension(gd.getWidth(), gd.getHeight()));
           
            Border b = BorderFactory.createLineBorder(this.borderColor);
            this.setBorder(b);
        }
       
        public void setElement( int element, int value )
        {
            data[element] = value;
        }
       
        public int getElement( int element )
        {
            return data[element];
        }
       
        public void setLineColor( Color c )
        {
            this.lineColor = c;
        }
       
        public void setGridColor( Color c )
        {
            this.gridColor = c;
        }
       
        public void setBgColor( Color c )
        {
            this.bgColor = c;
        }
       
        public void setExtrapolationColor( Color c )
        {
            this.extrapColor = c;
        }
       
        public void setAxisColor( Color c )
        {
            this.axisColor = c;
        }
       
        public void setLabelColor( Color c )
        {
            this.labelColor = c;
        }
       
        public void setBorderColor( Color c )
        {
            this.borderColor = c;
           
            Border b = BorderFactory.createLineBorder(this.borderColor);
            this.setBorder(b);
        }
       
        public void createData( int [] data )
        {
            this.data = data;
        }
       
        public void setXkey( String [] xKey )
        {
            this.xKey = xKey;
        }
       
        public void setExtrapolate( boolean e )
        {
            this.extrapolate = e;
        }
       
        public boolean getExtrapolate()
        {
            return this.extrapolate;
        }
       
        public boolean extrapPositive()
        {
            return this.extrapPos;
        }
       
        public void setNoOfData( int d )
        {
            if ( d > 1 )
                this.noOfData = d;
        }
       
        public int getNoOfData()
        {
            return this.noOfData;
        }
       
        public void paintComponent( Graphics g )
        {
            g.setColor(bgColor);                            // Draw Background
            g.fillRect(0,0,gd.getWidth(),gd.getHeight());   //
           
            int heightOfYAxis = data[getMaxElement()];                                  // Draw on text for Y-Axis
            g.setColor(labelColor);                                                     //
            g.setFont(new Font("arial", Font.PLAIN, 9));                                //
            g.drawString(" 0", X_SHIFT, gd.getHeight()-margin+Y_SHIFT);                 //
            g.drawString(""+heightOfYAxis/2, X_SHIFT, (gd.getHeight()/2)+Y_SHIFT);      //
            g.drawString(""+heightOfYAxis, X_SHIFT, margin+Y_SHIFT);                    //
           
            int Y_Width = gd.getWidth()-(margin*2);                                     // Draw on text for X-Axis
            int G_Dist  = Y_Width / xKey.length;                                        //
            int E_Dist  = G_Dist/2;                                                     //
                                                                                        //
            for ( int  i = 0; i < xKey.length; i++ )                                    //
            {                                                                           //
                g.drawString( xKey[i],                                                  //
                              margin+(G_Dist*i)+E_Dist,                                 //
                              (gd.getHeight()-margin)+12 );                             //
            }                                                                           //
           
            g.setColor(this.gridColor);
           
            for ( int i = 0; i < xKey.length; i++ )
            {
                g.drawLine(margin+(i*G_Dist)+E_Dist+3, margin, margin+(i*G_Dist)+E_Dist+3, gd.getHeight()-margin);
            }
           
            g.drawLine(margin, margin, gd.getWidth()-margin, margin);
            g.drawLine(margin, gd.getHeight()/2, gd.getWidth()-margin, gd.getHeight()/2);
           
            g.setColor(axisColor);                                      // Draw Y-Axis
            g.drawLine(margin, margin, margin, gd.getHeight()-margin);  //
           
            g.drawLine(margin, gd.getHeight()-margin, gd.getWidth()-margin, gd.getHeight()-margin); // Draw X-Axis
           
            g.setColor(this.lineColor);
            for ( int i = 0; i < data.length; i++ )
            {
                if ( i < (data.length-1) )
                {
                   
                    double da   = data[i];
                    double di   = heightOfYAxis;
                    double res1 = da/di;
                   
                    da   = data[i+1];
                    double res2 = da/di;
                   
                    double temp = (((double)gd.getHeight())-((double)margin*2));
                   
                    double temp2 = temp*res1;
                    int iR1 = (int ) temp2;
                   
                    temp2 = temp*res2;
                    int iR2 = (int ) temp2;
                   
                    g.drawLine( (margin+3+(i*G_Dist)+E_Dist),
                                (gd.getHeight()-margin- iR1),
                                (margin+3+((1+i)*G_Dist)+E_Dist),
                                (gd.getHeight()-margin- iR2) );
                   
                }
            }
           
            if ( extrapolate )
            {
               
                if ( noOfData <= 0 )
                    noOfData = data.length;
               
                int avX = 0;
                int avY = 0;
               
                for ( int i = 0; i < noOfData; i++ )
                {
                    avX += i / noOfData;
                    avY += data[i] / noOfData;
                }
               
                int m1 = 0;
                int m2 = 0;
               
                for ( int i = 0; i < noOfData; i++ )
                {
                    m1 += (i-avX)*(data[i]-avY);
                    m2 += (i-avX)*(i-avX);
                }
               
                int m = m1 / m2;
                int c = avY - m * avX;
               
                // y = mx + c
               
                double graphHeight = gd.getHeight() - (margin*2);
                double Y1 = m*0 + c;
                double Y2 = m*data.length + c;
                double dR1 = (Y1 / heightOfYAxis) * graphHeight;
                double dR2 = (Y2 / heightOfYAxis) * graphHeight;
               
                int iR1 = (int ) dR1;
                int iR2 = (int ) dR2;
               
                if ( iR1 < iR2 )
                    extrapPos = true;
               
                int pX1 = margin + E_Dist + 2;
                int pY1 = gd.getHeight() - margin - iR1;
                int pX2 = margin + data.length*G_Dist - E_Dist + 2;
                int pY2 = gd.getHeight() - margin - iR2;
               
                g.setColor(this.extrapColor);
                g.drawLine( pX1, pY1, pX2, pY2 );
               
            }
        }
       
        private int getMaxElement()
        {
            int maxV = 0;
            int maxE = 0;
           
            for ( int i = 0; i < data.length; i++ )
            {
                if ( data[i] > maxV )
                {
                    maxV = data[i];
                    maxE = i;
                }
            }
           
            return maxE;
        }
       
        public void update()
        {
            this.repaint();
        }
       
        class GraphDimension
        {
            private int     w           = 0;
            private int     h           = 0;
           
            public GraphDimension(){}
           
            public GraphDimension( int w, int h )
            {
                this.w = w;
                this.h = h;
            }
           
            public void setWidth( int w )
            {
                this.w = w;
            }
           
            public void setHeight( int h )
            {
                this.h = h;
            }
           
            public int getWidth()
            {
                return this.w;
            }
           
            public int getHeight()
            {
                return this.h;
            }
        }
       
    }
   
//------ LineGraph.java


Take note of the "paintComponent()" method. That's where the drawing goes on ;)

I haven't used it in a while, and I can't seem to find that Management system I was on about (to get an example from), but from what I can see, this is how to use it:


int width  = 500;  // pixels
int height = 300;  //

LineGraph lg = new LineGraph( width, height );

String [] x-axis = { "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec" };
lg.setXkey( x-axis };

int [] y-values = { 2, 7, 5, 9, 5, 0 };    // Only up until June
lg.createData( y-values );

lg.setExtrapolate( true );

getContentPane().add( lg );       // Just add it to your JPanel

// You can then change the content of a value at any time, and it will be instantly updated on the display:
lg.setElement( 1, 5 );    // Change February data to 5

// You can also change the colours of the grid, axis, data lines, extrapolation lines, etc..


I hope that helps :-)

Regards;
0
 
InteractiveMindCommented:
By the way: my example is a Line Graph, as I'm sure you can tell ;)
I don't have a Bar chart example, but it would require a very similar method in order to create one!
0
 
ShemmieAuthor Commented:
Many, many thanks for all that code! I'll give it a good going over and see where I stand with it. :o)

Much appreciated InteractiveMind, cheers.
0
 
InteractiveMindCommented:
Sure. Good luck with it  :-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now