Link to home
Start Free TrialLog in
Avatar of Kennywen
Kennywen

asked on

line chart, bar chart and pie chart

What component should i use if i want to draw line chart, bar chart and pie chart. Can anyone give me some example or links about how to draw it. Currently i'm using jdk1.4.2.

thanks
Avatar of Thunder_scream
Thunder_scream

try this free line chart, bar, etc library
www.jfree.org
Avatar of Kennywen

ASKER

is't possible to do it by writing my own code? because i just worry third party library will not have the function that i want.
Avatar of Mayank S
You always can :-)
SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
nothing is impossible..but I'm my self using jfree...as it is open source..as soon as I dont like anything a take it away and edit my chart the way I want it...by the way its a massive and well supported chart.....

Think about that if you do it your self you have to deal with zoom functions, correct range of axies
and as you see it easily gets complicated....and time consuming indeed..if you want to do it your self anyway you want to find out how to use the java2d graphic library possible 3d..

quick tip...as point 0.0 starts in the left upper corner..you must invert the high value x and y coordinates to probably put each drawn point in an array. you must most likely to do it pixel by pixel and write some algorigthm for line interpol....for line chart...

Happy coding.
if i use Jfreechart to draw the chart and sell to customer do i need to pay it?
I'm not 100% sure..you could always read the licence...but as far as i know..you dont...if you leave the jfree source...open in your code...as it was deliverd to you...see what I mean.
any complete code sample of plotting chart by using pure java(no third party library).
>> if i use Jfreechart to draw the chart and sell to customer do i need to pay it?

I guess not.

>> any complete code sample of plotting chart by using pure java

Did you visit the Java Almanac links I posted?
>>Did you visit the Java Almanac links I posted

yes, i do but the code seems like incomplete and it doesn't have line and bar chart.

thanks
You won't get full working code. You will get samples. You will have to write the skeletal code to complete your programs and then use the code posted in those links.

You can see more examples by clicking on the "Related Examples".

For more, look at these:

http://big.faceless.org/products/graph/examples.jsp

http://www.codebeach.com/index.asp?tabID=1&categoryID=4&subcategoryID=4

http://www.hotscripts.com/Detailed/10366.html
can anyone provide some sample source code about plotting the chart in java... (Without using third party software) .....

thanks.

Perhaps you have it on the first link that I posted. Go to that page and click on the "Source" links there to see the code.
the coding like below:
    // Class to hold a value for a slice
    public class PieValue {
        double value;
        Color color;
   
        public PieValue(double value, Color color) {
            this.value = value;
            this.color = color;
        }
    }
   
    // slices is an array of values that represent the size of each slice.
    public void drawPie(Graphics2D g, Rectangle area, PieValue[] slices) {
        // Get total value of all slices
        double total = 0.0D;
        for (int i=0; i<slices.length; i++) {
            total += slices[i].value;
        }
   
        // Draw each pie slice
        double curValue = 0.0D;
        int startAngle = 0;
        for (int i=0; i<slices.length; i++) {
            // Compute the start and stop angles
            startAngle = (int)(curValue * 360 / total);
            int arcAngle = (int)(slices[i].value * 360 / total);
   
            // Ensure that rounding errors do not leave a gap between the first and last slice
            if (i == slices.length-1) {
                arcAngle = 360 - startAngle;
            }
   
            // Set the color and draw a filled arc
            g.setColor(slices[i].color);
            g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);
   
            curValue += slices[i].value;
        }
    }

Here's some code that uses the drawPie() method:
    class MyComponent extends JComponent {
        PieValue[] slices = new PieValue[4];
   
        MyComponent() {
            slices[0] = new PieValue(25, Color.red);
            slices[1] = new PieValue(33, Color.green);
            slices[2] = new PieValue(20, Color.pink);
            slices[3] = new PieValue(15, Color.blue);
        }
   
        // This method is called whenever the contents needs to be painted
        public void paint(Graphics g) {
            // Draw the pie
            drawPie((Graphics2D)g, getBounds(), slices);
        }
    }

    // Show the component in a frame
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MyComponent());
    frame.setSize(300, 200);
    frame.setVisible(true);


i dun know how to test this program.... so i need a sample of complete code.....

sorry, i'm new to JAVA......

thanks
Just add a main () method and add:

>> // Show the component in a frame
>> JFrame frame = new JFrame();
>> frame.getContentPane().add(new MyComponent());
>> frame.setSize(300, 200);
>> frame.setVisible(true);

- to the main () method.
(I hope you can do that?)
If you want to learn Java, you can try the Java tutorial :
http://java.sun.com/docs/books/tutorial/
Oh, bother. Anyway, it would just be something like:

public class Test
{
  public static void main ( String args[] )
  {
    // the same four lines
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MyComponent());
    frame.setSize(300, 200);
    frame.setVisible(true);

  }

}
>> (I hope you can do that?)

yup, i can do it... but how can add label to the pie chart???E.g. percent of the pie chart and the legend...

thanks
To Thunder_scream,

can i use the jfreechart to develop my own line chart, bar chart and pie chart?

if yes, then where should i put the java file and what jar file should i include in the classpath?

actually i try to modify 1 of the demo program and i successful to compile it but when i try to run it it prompt me an error:
Exception in thread "main" java.lang.NoClassDefFoundError: src/org/jfree/chart/demo/PieChartDemo1

in command prompt i type:
C:\GUI\Graph\jfreechart-0.9.17>java -classpath lib/jcommon-0.9.2.jar;jfreechart-0.9.17.jar;lib/log4j-1.2.8.jar src.org.jfree.chart.demo.PieChartDemo1

thanks
>jfreechart to develop my own line chart, bar chart and pie chart?

I'm not sure what you mean by "my own".
jfreechart has all those charts finished, i.e the methods are there for you to create for instance line chart, picechart ,bar chart...etc...you just need to add you dataset and use the methods to create your charts....how to do this..can be seen by browsing through some of the demos given in the package but you can custimze them in anyway you'd like...(doesnt need to look like the demos)

Going furher if you dont like the implementation, it is possible to change it as it is open souce as I mentioned earlier..but you need to know what you are doing...(and I dont see why you would like to this..)
To recompile the JFreeChart classes, you can use the Ant build.xml file included
in the distribution. Change to the ant directory and type:
ant compile
This will recompile all the necessary source files and recreate the JFreeChart
run-time jar file.
To run the script requires that you have Ant 1.5.1 (or later) installed on your
system, to find out more about Ant visit: http://ant.apache.org/

>if yes, then where should i put the java file and what jar file should i include in the classpath?
those are the necessary jar files...and it does not matter where you put them(your java files) as long as you give correct path.

see these links for further guidence

Installation guide..quite useful.
http://prdownloads.sourceforge.net/jfreechart/jfreechart-0.9.17-install.pdf

http://forum.java.sun.com/thread.jsp?forum=20&thread=359321&tstart=0&trange=15


and also when you compile drop the "src"...try this...
C:\GUI\Graph\jfreechart-0.9.17>java -classpath lib/jcommon-0.9.2.jar;jfreechart-0.9.17.jar;lib/log4j-1.2.8.jar org.jfree.chart.demo.PieChartDemo1

I would suggest you use some sort of compiler if you are using Windows..such as jcreator..it would make life easier..
Do u know where can i change the color for the pie chart?

thanks
to Thunder_scream, r u study all the source code of the jfreechart???
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, do u know how to add button and check box in to the jfreechart???

thanks in advance
you can add the chart to a pane and add as much as buttons as you like..

check in your souce there is plenty of examples
src\org\jfree\chart\demo

see e.g src\org\jfree\chart\demo\DynamicDataDemo2

good luck


thanks