Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Creating a Bar Graph In Java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 05-03-2010
Member
 
Join Date: Jan 2009
Posts: 61
Creating a Bar Graph In Java
  

Hello, I am searching for the code which will help me to get the Source Code for the Bar Graph with the help of Java programming Language. If you are having Program for this then please provide that program for me. Or if you are having any source information for it then also provide details about it.

Reply With Quote
  #2  
Old 05-03-2010
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Creating a Bar Graph In Java

Hello, I have created the code below for you. You can simply make use of it and get the bar graph in java:
Code:
import javax.swing.*;
public class barGraph 
{
   public static void main(String[] args)
   {
      JFrame fr = new JFrame();
       final int width = 300;
       final int height = 400;
       fr.setSize(width, height);
       fr.setTitle("Grade Bar Graph");
       fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       fr.setVisible(true);
       String str = JOptionPane.showInputDialog("What is the String file name?");
        barGraphComponent component = new barGraphComponent();
        fr.add(component);
       fr.setVisible(true);
   }

}
Reply With Quote
  #3  
Old 05-03-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: Creating a Bar Graph In Java

Hello, for creating a Bar Graph in java you must need to make use of the Graphics class which is having following methods and that can be help you while creating Bar Graph:
  • clearRect(int num1, int num2, int width, int height)
  • clipRect(int num1, int num2, int width, int height)
  • copyArea(int num1, int num2, int width, int height, int dnum1, int dnum2)
  • create()
  • dispose()
  • draw3DRect(int num1, int num2, int width, int height, boolean raised)
  • drawArc(int num1, int num2, int width, int height, int startAngle, int arcAngle)
  • drawBnum2tes(byte[] data, int offset, int length, int num1, int num2)
  • drawChars(char[] data, int offset, int length, int num1, int num2)
  • drawImage(Image img, int num1, int num2, Color bgcolor, ImageObserver observer)
  • drawLine(int num11, int num21, int num12, int num22)
  • drawOval(int num1, int num2, int width, int height)
  • drawPolygon(Polygon p)
  • drawRect(int num1, int num2, int width, int height)
  • drawString(String str, int num1, int num2)
  • finalize()
  • getClip()
  • getColor()
  • getFont()
Reply With Quote
  #4  
Old 05-03-2010
Member
 
Join Date: May 2008
Posts: 2,293
Re: Creating a Bar Graph In Java

If you want to create a Histogram in your java then you must need to make use of the following steps:
  • Start your program in java.
  • Now you must need to make use of the "PlanarImage" and "Create" functions for loading picture on your storage device. Then you will need to make use of the following code of line at first:
    Code:
    PlanarImage picname JAI.create("fileload",picname.Getabsolutepath())
  • Now just create an array of double having range of 0-256.
  • Now add the image to the array. Now you will need to make use of the counting through the line of color using the add command.
  • Now you can simply use the feature of "GetProperty" for producing the Histogram of the image. For that you can make use of the line of code below:
    Code:
    operator.Getproperty("histogram")
  • Now just compile and run the program.
  • Done.
Reply With Quote
  #5  
Old 05-03-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Creating a Bar Graph In Java

Hey, I have got the code below which will simply provide you solution for your Bar Graph in java:
Code:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import org.jfree.fchart.ChartFactory;
import org.jfree.fchart.ChartPanel;
import org.jfree.fchart.JFreeChart;
import org.jfree.fchart.axis.NumberAxis;
import org.jfree.fchart.cPlot.CategoryPlot;
import org.jfree.fchart.cPlot.PlotOrientation;
import org.jfree.fchart.lsrend.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class fchart extends ApplicationFrame 
{
    public fchart(final String title) {
        super(title);
        final CategoryDataset cdset = createDataset();
        final JFreeChart fchart = createChart(cdset);
        final ChartPanel cpanel = new ChartPanel(fchart);
        cpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(cpanel);
    }
    private CategoryDataset createDataset() {
        final String Start1 = "First";
        final String Start2 = "Second";
        final String Start3 = "Third";
        final String variation1 = "Type 1";
        final String variation2 = "Type 2";
        final String variation3 = "Type 3";
        final String variation4 = "Type 4";
        final String variation5 = "Type 5";
        final String variation6 = "Type 6";
        final String variation7 = "Type 7";
        final String variation8 = "Type 8";
        final DefaultCategoryDataset cdset = new DefaultCategoryDataset();
        cdset.addValue(1.0, Start1, variation1);
        cdset.addValue(4.0, Start1, variation2);
        cdset.addValue(3.0, Start1, variation3);
        cdset.addValue(5.0, Start1, variation4);
        cdset.addValue(5.0, Start1, variation5);
        cdset.addValue(7.0, Start1, variation6);
        cdset.addValue(7.0, Start1, variation7);
        cdset.addValue(8.0, Start1, variation8);
        cdset.addValue(5.0, Start2, variation1);
        cdset.addValue(7.0, Start2, variation2);
        cdset.addValue(6.0, Start2, variation3);
        cdset.addValue(8.0, Start2, variation4);
        cdset.addValue(4.0, Start2, variation5);
        cdset.addValue(4.0, Start2, variation6);
        cdset.addValue(2.0, Start2, variation7);
        cdset.addValue(1.0, Start2, variation8);
        cdset.addValue(4.0, Start3, variation1);
        cdset.addValue(3.0, Start3, variation2);
        cdset.addValue(2.0, Start3, variation3);
        cdset.addValue(3.0, Start3, variation4);
        cdset.addValue(6.0, Start3, variation5);
        cdset.addValue(3.0, Start3, variation6);
        cdset.addValue(4.0, Start3, variation7);
        cdset.addValue(3.0, Start3, variation8);
        return cdset;               
    }
    private JFreeChart createChart(final CategoryDataset cdset) {
        final JFreeChart fchart = ChartFactory.createLineChart("Line Chart Demo 1", "Type","Value",cdset, PlotOrientation.VERTICAL,true, true, false);
        fchart.setBackgroundPaint(Color.white);
        final CategoryPlot cPlot = (CategoryPlot) fchart.getPlot();
        cPlot.setBackgroundPaint(Color.lightGray);
        cPlot.setRangeGridlinePaint(Color.white);
        final NumberAxis NAxis = (NumberAxis) cPlot.getRangeAxis();
        NAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        NAxis.setAutoRangeIncludesZero(true);
        final LineAndShapeRenderer lsrend = (LineAndShapeRenderer) cPlot.getRenderer();
        lsrend.setSeriesStroke(
            0, new BasicStroke(
                2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                1.0f, new float[] {10.0f, 6.0f}, 0.0f
            )
        );
        lsrend.setSeriesStroke(
            1, new BasicStroke(
                2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                1.0f, new float[] {6.0f, 6.0f}, 0.0f
            )
        );
        lsrend.setSeriesStroke(
            2, new BasicStroke(
                2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
                1.0f, new float[] {2.0f, 6.0f}, 0.0f
            )
        );
        return fchart;
    }
    public static void main(final String[] args) {
        final fchart obj = new fchart("Line Chart Demo");
        obj.pack();
        RefineryUtilities.centerFrameOnScreen(obj);
        obj.setVisible(true);
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Creating a Bar Graph In Java"
Thread Thread Starter Forum Replies Last Post
Creating a counter in java Beverly Archer Software Development 5 10-03-2010 01:49 PM
Creating a New Policy File in java technika Software Development 4 18-02-2010 10:28 PM
Creating a Custom Event in Java Jagdish Gada Software Development 5 16-02-2010 08:16 PM
Creating an operating system (OS) in Java Shaan12 Software Development 3 29-10-2009 11:51 PM
Creating Tables in Java RockOn Software Development 3 09-04-2009 10:57 PM


All times are GMT +5.5. The time now is 02:26 AM.