A rudimentary 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. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Advanced Graphics » ChartScreenshots 
A rudimentary bar chart class
A rudimentary 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
6. bar-graph drawable
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.