Solved

Bar Charts in java

Posted on 2004-09-28
21
762 Views
Last Modified: 2012-05-05

I need to create bar charts that look good and cant seem to find anythin in the standard JDK. Is there anyway that someone could give me some sample code on how I can do this.

Thanks a bunch
Achal
0
Comment
Question by:achaljalan
21 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12170107
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12170111
www.jfree.org/jfreechart is a good third party library
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12170122
Some other open source charting libraries: http://java-source.net/open-source/charting-and-reporting
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12170151
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12170204
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12170237
There is even CEWolf specified for chart (but I believe the best is JFreeChart).
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 12170411
I'd also go with JFreeChart :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12175058
0
 

Author Comment

by:achaljalan
ID: 12177744
I have been looking at the JFreeChart and can't seem to find any code samples on creating bar charts... would anyone know where I could find something...
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 12177938
download the demo api of all chart types from
http://www.java4less.com/charts_e.htm

then i'll give u an example class, its easy to use and embed with application, i've personally used.

Naeem Shehzad Ghuman
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12177986
>> I have been looking at the JFreeChart and can't seem to find any code samples on creating bar charts
The samples you see on http://www.jfree.org/jfreechart/samples.html are included in the download
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12178021
If you unzip the zip file to C:\JF, then the sources can be found in c:\JF\src\org\jfree\chart\demo
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12178043
About the last release (0.9.21) I even read:
This release includes (...) a new "umbrella" demo that makes it easier to browse the numerous JFreeChart demo applications.
0
 

Author Comment

by:achaljalan
ID: 12178263
Hey Naeem

i have downloaded 'RChart' from http://www.java4less.com/download.htm#RChart
please do advise me on what the next step would be

Do you use msn?

Thanks
Achal


download the demo api of all chart types from
http://www.java4less.com/charts_e.htm

then i'll give u an example class, its easy to use and embed with application, i've personally used.

Naeem Shehzad Ghuman
0
 

Accepted Solution

by:
n4088832 earned 350 total points
ID: 12364392
If you wanted to write your own, and you need only simple charting capabilities, something like this might be a good starting point (It's very basic, but it should be fairly easy to add axes, labels, and any other bits you want).  Also have a look at http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html and http://java.sun.com/docs/books/tutorial/uiswing/14painting/  for more info on custom painting.

public class ChartPanel extends JPanel {
   
   
    private int barSpacing = 10;
    private int chartHeight = 120, chartWidth = 400;
    private Bar[] bars;
   
   
    class Bar{
        private int barWidth = 25, barHeight = 100;
        private Color barColour = Color.BLUE;
       
        public Color getBarColour() {
            return barColour;
        }
        public void setBarColour(Color barColour) {
            this.barColour = barColour;
        }
        public int getBarHeight() {
            return barHeight;
        }
        public void setBarHeight(int barHeight) {
            this.barHeight = barHeight;
        }
        public int getBarWidth() {
            return barWidth;
        }
        public void setBarWidth(int barWidth) {
            this.barWidth = barWidth;
        }
    }
   
   
   
   
    protected void paintComponent(Graphics g){
       
        for(int i = 0; i < bars.length; i++){
            int height = bars[i].getBarHeight();
            int width = bars[i].getBarWidth();
            Color color = bars[i].getBarColour();
           
            g.setColor(color);
            g.fillRect((width + barSpacing) * i, chartHeight-height, width, height);
        }
       
       
       
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Test ChartPanel");
        ChartPanel chart = new ChartPanel();
        Bar[] bars = new Bar[5];
        for(int i = 0; i < bars.length; i++ ){
            bars[i] = chart.new Bar();
            if(i%2 == 0){
                bars[i].setBarColour(Color.RED);
                bars[i].setBarHeight(120);
            }else{
                bars[i].setBarColour(Color.BLUE);
                bars[i].setBarHeight(80);
            }
        }
       
        chart.setBars(bars);
       
        frame.setSize(500, 500);
        chart.setSize(500, 500);
        frame.getContentPane().add(chart);
        frame.show();
       
    }
   
   
    public Bar[] getBars() {
        return bars;
    }
    public void setBars(Bar[] bars) {
        this.bars = bars;
    }
    public int getBarSpacing() {
        return barSpacing;
    }
    public void setBarSpacing(int barSpacing) {
        this.barSpacing = barSpacing;
    }
    public int getChartHeight() {
        return chartHeight;
    }
    public void setChartHeight(int chartHeight) {
        this.chartHeight = chartHeight;
    }
    public int getChartWidth() {
        return chartWidth;
    }
    public void setChartWidth(int chartWidth) {
        this.chartWidth = chartWidth;
    }
}

0
 
LVL 8

Expert Comment

by:sigmacon
ID: 12366082
Do you want to paint the charts in a client Java application or publish them to the web? Do they need to be generated in real time based on user input? Although I have used JFreeChart with success in the past, if you want a more detailed code sample as starting point for what you're trying to do, some more information would be helpful. Thanks.
0
 
LVL 1

Expert Comment

by:FesterWim
ID: 12367028
You don't want to write this your own. JFreeChart does a great job and after all it's FREE!
0
 

Expert Comment

by:Bw
ID: 12367426
Hi,

I think you shoud consider using SVG in order to get graphs.
http://java.sun.com/developer/technicalArticles/GUI/svg/

It is alsost as strong as frash except it is an xml code! Very handy and strong.

Combine it with
http://www.svgopen.org/2004/papers/ClinicalStudyToSVG/
and
https://svgsalamander.dev.java.net/

Cheers
0
 

Expert Comment

by:saj_john
ID: 12368330
if you looking for some standard stuff that will take care of the chart,color bgroung everything and also to show some data out on side etc.

go to http://www.corda.com/ and findout is that the one you looking for.

see their examples at http://www.corda.com/examples/go/

if you need live examples on this, ask me. For some reasons I cannot publish our url here . Hope this helps

0
 
LVL 7

Expert Comment

by:Navicerts
ID: 12368895
www.ve.com

this is VERY easy, and it works really great.  The software is called KavaChart.  Use the wizard to create an applet and then just feed your data in through hardcoding or other means to the applet.  It will do bar charts bubble charts line charts, all sorts of things.  Check it out!!

Good Luck

-Navicerts
0
 

Expert Comment

by:duane5000
ID: 12370296
I would like to endorse KavaChart also, but have a few words of warning: I used a paid-for version of this in a large corporate setting, even went as far as modifying the source to use a properties file to set chart-size/font-size attributes and dynamically resize the legend font/legend position when the item-number went over a set amount.. this was a bug, as the legend text would creep outside the viewable area with larger fonts, but readability was an issue and larger text prefered when item numbers were few.

KavaChart had a dispute with a license holder of the gif generator that it used(3 years ago). The license holder was sueing for royalties, and our company scrambled to replace the charting utility with another product, just to stay out of the turmoil. I'm sure this issue is resolved since, but doesn't hurt to watch your back.

Also, I hope I've hinted plainly enough that before you become locked into any product, you'll want to test the legend issue we encountered for your own purposes. Good luck!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 88
web application structure 18 76
use lov values 2 49
What's wrong with this code? 4 22
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

776 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