A rudimentory bar chart class : Chart « Advanced Graphics « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Advanced Graphics » ChartScreenshots 
A rudimentory bar chart class
A rudimentory bar chart class

import java.awt.*;
import java.util.*;

import javax.swing.*;

import no.geosoft.cc.geometry.Geometry;
import no.geosoft.cc.graphics.*;



/**
 * G demo program. Demonstrates:
 *
 * <ul>
 * <li>A rudimentory bar chart class
 * <li>Rendering techniques
 * </ul>
 
 @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
 */   
public class Demo17 extends JFrame
{
  public Demo17()
  {
    super ("G Graphics Library - Demo 17");    
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    // Create the graphic canvas
    GWindow window = new GWindow();
    getContentPane().add (window.getCanvas());
    
    // Create scane with default viewport and world extent settings
    GScene scene = new GScene (window);

    GStyle chartStyle = new GStyle();
    chartStyle.setLineStyle (GStyle.LINESTYLE_INVISIBLE);
    chartStyle.setFont (new Font ("Dialog", Font.BOLD, 12));
    chartStyle.setForegroundColor (new Color (000));
    
    BarChart barChart = new BarChart (50450400400);
    barChart.setStyle (chartStyle);

    barChart.addBar ("1998"250, getColor());
    barChart.addBar ("1999"150, getColor());
    barChart.addBar ("2000",  80, getColor());
    barChart.addBar ("2001"174, getColor());
    barChart.addBar ("2002"350, getColor());
    barChart.addBar ("2003",  40, getColor());    
    barChart.addBar ("2004"100, getColor());
    barChart.addBar ("2005"150, getColor());    

    scene.add (barChart);
    
    pack();
    setSize (new Dimension (500500));
    setVisible (true);
  }


  
  private Color getColor()
  {
    return new Color (Color.HSBtoRGB ((float)Math.random()0.2f0.8f));
  }
  
    

  private class BarChart extends GObject
  {
    private int         x_, y_, width_, height_;
    private Collection  bars_;
    
    
    BarChart (int x, int y, int width, int height)
    {
      x_      = x;
      y_      = y;
      width_  = width;
      height_ = height;
      
      bars_ = new ArrayList();
    }


    
    void addBar (String label, int value, Color color)
    {
      bars_.add (new Bar (label, value, color));
    }
    

    
    public void draw()
    {
      final int BAR_WIDTH = 30;
      final int SPACING   = 15;
      final int DEPTH     = 20;
      
      removeSegments();

      int x0 = x_ + 10;

      double angle0 = 0.0;
      for (Iterator i = bars_.iterator(); i.hasNext()) {
        Bar bar = (Bari.next();

        int y0 = y_ - bar.value;
        
        GSegment main = new GSegment();
        addSegment (main);
        GStyle style = new GStyle();
        style.setForegroundColor (bar.color);
        style.setBackgroundColor (bar.color);        
        main.setStyle (style);
        main.setGeometry (Geometry.createRectangle (x0, y0,
                                                    BAR_WIDTH, bar.value));

        GSegment label = new GSegment();
        addSegment (label);
        label.setGeometry (x0 + BAR_WIDTH / 2, y_ + 10);
        GText text = new GText (bar.label);
        label.setText (text);
        
        GSegment top = new GSegment();
        addSegment (top);
        style = new GStyle();
        style.setForegroundColor (bar.color.brighter());
        style.setBackgroundColor (bar.color.brighter());        
        top.setStyle (style);
        int topXy[] {x0, y0,
                       x0 + BAR_WIDTH, y0,
                       x0 + BAR_WIDTH + DEPTH, y0 - DEPTH,
                       x0 + DEPTH, y0 - DEPTH,
                       x0, y0};
        top.setGeometry (topXy);

        GSegment side = new GSegment();
        addSegment (side);
        style = new GStyle();
        style.setForegroundColor (bar.color.darker());
        style.setBackgroundColor (bar.color.darker());        
        side.setStyle (style);
        int[] sideXy = {x0 + BAR_WIDTH, y0,
                        x0 + BAR_WIDTH + DEPTH, y0 - DEPTH,
                        x0 + BAR_WIDTH + DEPTH, y_ - DEPTH,
                        x0 + BAR_WIDTH, y_,
                        x0 + BAR_WIDTH, y0};
        side.setGeometry (sideXy);
        
        x0 += BAR_WIDTH + SPACING;
      }
    }
  }

  

  private class Bar
  {
    public String  label;
    public int     value;
    public Color   color;

    public Bar (String label, int value, Color color)
    {
      this.label = label;
      this.value = value;
      this.color = color;
    }
  }
  


  public static void main (String[] args)
  {
    new Demo17();
  }
}

           
       
G-BarChart.zip( 217 k)
Related examples in the same category
1. Chart based on Graph libraryChart based on Graph library
2. Scroll ChartScroll Chart
3. Animation Line ChartAnimation Line Chart
4. Pie ChartPie Chart
5. A rudimentary chart libraryA rudimentary chart library
w__w___w___.__j__av__a2___s___.___co_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.