[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 770
  • Last Modified:

Bar Charts in java


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
achaljalan
Asked:
achaljalan
1 Solution
 
sudhakar_koundinyaCommented:
0
 
zzynxSoftware engineerCommented:
www.jfree.org/jfreechart is a good third party library
0
 
zzynxSoftware engineerCommented:
Some other open source charting libraries: http://java-source.net/open-source/charting-and-reporting
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Giant2Commented:
There is even CEWolf specified for chart (but I believe the best is JFreeChart).
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I'd also go with JFreeChart :-)
0
 
achaljalanAuthor Commented:
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
 
NaeemgCommented:
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
 
zzynxSoftware engineerCommented:
>> 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
 
zzynxSoftware engineerCommented:
If you unzip the zip file to C:\JF, then the sources can be found in c:\JF\src\org\jfree\chart\demo
0
 
zzynxSoftware engineerCommented:
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
 
achaljalanAuthor Commented:
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
 
n4088832Commented:
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
 
sigmaconCommented:
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
 
FesterWimCommented:
You don't want to write this your own. JFreeChart does a great job and after all it's FREE!
0
 
BwCommented:
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
 
saj_johnCommented:
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
 
NavicertsCommented:
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
 
duane5000Commented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now