Solved

Bar Charts in java

Posted on 2004-09-28
21
756 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
Comment Utility
0
 
LVL 37

Expert Comment

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

Expert Comment

by:zzynx
Comment Utility
Some other open source charting libraries: http://java-source.net/open-source/charting-and-reporting
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
0
 
LVL 12

Expert Comment

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

Expert Comment

by:mayankeagle
Comment Utility
I'd also go with JFreeChart :-)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 

Author Comment

by:achaljalan
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

771 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

11 Experts available now in Live!

Get 1:1 Help Now