Solved

line chart, bar chart and pie chart

Posted on 2004-03-30
28
2,013 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:Kennywen
  • 12
  • 8
  • 6
  • +1
28 Comments
 
LVL 2

Expert Comment

by:Thunder_scream
ID: 10721348
try this free line chart, bar, etc library
www.jfree.org
0
 

Author Comment

by:Kennywen
ID: 10721400
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721455
You always can :-)
0
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 100 total points
ID: 10721463
0
 
LVL 2

Expert Comment

by:Thunder_scream
ID: 10721507
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
 

Author Comment

by:Kennywen
ID: 10721594
if i use Jfreechart to draw the chart and sell to customer do i need to pay it?
0
 
LVL 2

Expert Comment

by:Thunder_scream
ID: 10721615
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
 

Author Comment

by:Kennywen
ID: 10721667
any complete code sample of plotting chart by using pure java(no third party library).
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721779
>> 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
 

Author Comment

by:Kennywen
ID: 10721801
>>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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10721854
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
 
LVL 13

Expert Comment

by:Webstorm
ID: 10722358
0
 

Author Comment

by:Kennywen
ID: 10722784
can anyone provide some sample source code about plotting the chart in java... (Without using third party software) .....

thanks.

0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722801
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Kennywen
ID: 10722844
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722938
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
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10722947
(I hope you can do that?)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 10723036
If you want to learn Java, you can try the Java tutorial :
http://java.sun.com/docs/books/tutorial/
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10723143
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
 

Author Comment

by:Kennywen
ID: 10728966
>> (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
 

Author Comment

by:Kennywen
ID: 10729291
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
 
LVL 2

Expert Comment

by:Thunder_scream
ID: 10729854
>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
 

Author Comment

by:Kennywen
ID: 10730521
Do u know where can i change the color for the pie chart?

thanks
0
 

Author Comment

by:Kennywen
ID: 10730665
to Thunder_scream, r u study all the source code of the jfreechart???
0
 
LVL 2

Accepted Solution

by:
Thunder_scream earned 150 total points
ID: 10730698
>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
 

Author Comment

by:Kennywen
ID: 10756078
Sorry, do u know how to add button and check box in to the jfreechart???

thanks in advance
0
 
LVL 2

Expert Comment

by:Thunder_scream
ID: 10766388
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
 

Author Comment

by:Kennywen
ID: 10771494
thanks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 59
parentbit challenge 3 52
nestparen challenge 4 56
wordappend challenge 8 84
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now