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
KennywenAsked:
Who is Participating?
 
Thunder_screamConnect With a Mentor Commented:
>to Thunder_scream, r u study all the source code of the jfreechart???
sorry ...do you mean if I'm familiar with jfreecharts source code...
no ..I dont know it by heart :)

look at this api..
http://www.jfree.org/jfreechart/javadoc/org/jfree/chart/plot/PiePlot.html#setSectionPaint(int,%20java.awt.Paint)

e.g...
private JFreeChart createChart(PieDataset dataset) {
        JFreeChart chart = ChartFactory.createPieChart(
            "Pie Chart Demo 2",  // chart title
            dataset,             // dataset
            true,                // include legend
            true,
            false
        );
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.....<<< and use it here..

Good luck
0
 
Thunder_screamCommented:
try this free line chart, bar, etc library
www.jfree.org
0
 
KennywenAuthor Commented:
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.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Mayank SAssociate Director - Product EngineeringCommented:
You always can :-)
0
 
Thunder_screamCommented:
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.
0
 
KennywenAuthor Commented:
if i use Jfreechart to draw the chart and sell to customer do i need to pay it?
0
 
Thunder_screamCommented:
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.
0
 
KennywenAuthor Commented:
any complete code sample of plotting chart by using pure java(no third party library).
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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?
0
 
KennywenAuthor Commented:
>>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
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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
0
 
WebstormCommented:
0
 
KennywenAuthor Commented:
can anyone provide some sample source code about plotting the chart in java... (Without using third party software) .....

thanks.

0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
KennywenAuthor Commented:
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
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
(I hope you can do that?)
0
 
WebstormCommented:
If you want to learn Java, you can try the Java tutorial :
http://java.sun.com/docs/books/tutorial/
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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);

  }

}
0
 
KennywenAuthor Commented:
>> (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
0
 
KennywenAuthor Commented:
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
0
 
Thunder_screamCommented:
>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..
0
 
KennywenAuthor Commented:
Do u know where can i change the color for the pie chart?

thanks
0
 
KennywenAuthor Commented:
to Thunder_scream, r u study all the source code of the jfreechart???
0
 
KennywenAuthor Commented:
Sorry, do u know how to add button and check box in to the jfreechart???

thanks in advance
0
 
Thunder_screamCommented:
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


0
 
KennywenAuthor Commented:
thanks
0
All Courses

From novice to tech pro — start learning today.