Friday, April 19, 2013

Bar Chart data presentation program

The BarChart is a simple program to present frequencies of data in a bar chart. The program allows the user to input the data items (integer numbers) from keyboard, computer frequencies, and draw the bar chart. In this program you will learn how to store a collection of data items in array, read the array elements, and do simple drawings in Java.

Barchart source code:

import java.util.Scanner;
import java.awt.*;
import javax.swing.*;
class Draw extends Canvas{
1          int[] freq;
            Draw(int[] fr){

2                      freq=fr;
3                      setBackground(Color.BLACK);                
            }

4          public void paint(Graphics g){
                        int i;
                        int x=50;
                        int y=80;        
                        int width=10;
                        int height=10;
                        g.setColor(Color.RED);
                        g.drawString("BAR CHART",x-20,y-50);
                        g.setColor(Color.BLUE);
                        for(i=0;i<freq.length;i++){
                        if(freq[i]!=0){
                                    width=freq[i]*50;
                                    g.drawString(""+i,10,y+10);
                                    g.drawString(""+freq[i],width+80,y+10);
                                    g.fillRect(x,y,width,height);
                                    y+=20;
                                    }
                       
                        }
                       
           
           
                       
            }


           
}

5 class BarChart extends JFrame{
           
            public static void main(String[] args)
            {

6                      int[] data=getData();
7                      int[] freq=getFreq(data);
8                      BarChart h=new BarChart();
9                      h.setDefaultCloseOperation(EXIT_ON_CLOSE);
10                    Draw d=new Draw(freq);
11                    h.add(d,BorderLayout.CENTER);
12                    h.setExtendedState(h.getExtendedState() | h.MAXIMIZED_BOTH);
13                    h.setTitle("Bar Chart");
14                    h.setVisible(true);
           
            }

15        public static int[] getData(){
                        int i;
                        Scanner sc=new Scanner(System.in);
                        System.out.print("Enter number of data points:");
                        int n=sc.nextInt();    
                        int[] vals=new int[n];
                        for(i=0;i<n;i++){
                                    System.out.format("Value %d:",i+1);
                                    vals[i]=sc.nextInt();
                        }
                        return vals;
            }

16        public static int[] getFreq(int[] vals){
                        int fsize;
                        int i;    
                        int m=findmax(vals,vals.length);
                        if(m>vals.length) fsize=m+1;
                        else fsize=vals.length+1;
                        int[] freq=new int[fsize];
                        for(i=0;i<fsize;i++)
                                    freq[i]=0;

                        for(i=0;i<vals.length;i++)
                                    freq[vals[i]]++;
                        return freq;
            }

17        public static int findmax(int[] vals,int n){
                        int max=vals[0];
                        int i;
                        for(i=1;i<n;i++)
                                    if(max<vals[i]) max=vals[i];
                        return max;
            }


}

Bar Chart data input

Bar Chart output


Code Explanation:

1 Declare freq array to receive the data set. This data set is used to draw the bar char.

2 Assign array parameter fr to freq array when the Draw object is created.

3 Set the background color of the drawing area (Canvas) to the black color.

4 This is the paint method of the Draw class. This method is inherited from the Canvas class.
In this method, the data set received from the  parameter of the constructor of Draw class is used to construct a bar chart to represent the frequency of each data item in the data set. Drawing a bar is the same as drawing a rectangle shape filled with color. To draw a rectangle filled with color, you will use the fillRect(x,y,width,height) method of the Graphics class. The x and y arguments represent x-axis and y-axis of the shape object on the drawing area. The width and height arguments represent the width and height of the shape object. To display text on the drawing area, you can use  the drawString(string, x,y) method of the Graphics class. 

5 The BarChart class contains the main method to start the program.

6 Invoke the getData method to read data set from the keyboard and store the data set in an array called data.

7 Invoke the getFreq method to compute the frequency of each data item in the data set and store the frequencies set in an array called freq.

8 Create the h BarChart object to load the program window.

9 Allow the program window to close when the user clicks the close button.

10 Create the d Draw object. This object is the drawing area. The freq array is sent to the d Draw object (through constructor) so that the data can be drawn on the drawing area.

11 Add the drawing area to the program window.

12 Open the program window in full screen mode.

13 Set title of the program window.

14 Make sure the program window is visible to the user.

15 This is the code implementation of the getData method to read data from keyboard.

16 This is the code implementation of the getFreq method to compute the frequencies of the data items. The indexes of the freq array are the data items of the data set stored in the vals array. The size of the freq array is determined by the size of the data set or the max item of the data set plus one. This is to make sure that the freq array is big enough to have the data items as its indexes. Computing the frequency of each data item is simple. We need a for loop to iterate through the data set (vals array) and increase the frequency of each data item by one (freq[vals[i]]++).

17 The findmax method contains code to find the max item in the data set. This method is  invoked in the getFreq method.

No comments:

Post a Comment