Chart Bean : Chart « Swing Components « 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 » Swing Components » ChartScreenshots 
Chart Bean

/**
 @version 1.20 1999-09-28
 @author Cay Horstmann
 */

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.io.Serializable;

import javax.swing.JPanel;

public class ChartBean extends JPanel implements Serializable {
  public void paint(Graphics g) {
    if (values == null || values.length == 0)
      return;
    int i;
    double minValue = 0;
    double maxValue = 0;
    for (i = 0; i < values.length; i++) {
      if (minValue > getValues(i))
        minValue = getValues(i);
      if (maxValue < getValues(i))
        maxValue = getValues(i);
    }
    if (maxValue == minValue)
      return;

    Dimension d = getSize();
    int clientWidth = d.width;
    int clientHeight = d.height;
    int barWidth = clientWidth / values.length;

    g.setColor(inverse ? color : Color.white);
    g.fillRect(00, clientWidth, clientHeight);
    g.setColor(Color.black);

    Font titleFont = new Font("SansSerif", Font.BOLD, 20);
    FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);

    int titleWidth = titleFontMetrics.stringWidth(title);
    int y = titleFontMetrics.getAscent();
    int x;
    if (titlePosition == LEFT)
      x = 0;
    else if (titlePosition == CENTER)
      x = (clientWidth - titleWidth2;
    else
      x = clientWidth - titleWidth;

    g.setFont(titleFont);
    g.drawString(title, x, y);

    int top = titleFontMetrics.getHeight();
    double scale = (clientHeight - top(maxValue - minValue);
    y = clientHeight;

    for (i = 0; i < values.length; i++) {
      int x1 = i * barWidth + 1;
      int y1 = top;
      int height = (int) (getValues(i* scale);
      if (getValues(i>= 0)
        y1 += (int) ((maxValue - getValues(i)) * scale);
      else {
        y1 += (int) (maxValue * scale);
        height = -height;
      }

      g.setColor(inverse ? Color.white : color);
      g.fillRect(x1, y1, barWidth - 2, height);
      g.setColor(Color.black);
      g.drawRect(x1, y1, barWidth - 2, height);
    }
  }

  public void setTitle(String t) {
    title = t;
  }

  public String getTitle() {
    return title;
  }

  public double[] getValues() {
    return values;
  }

  public void setValues(double[] v) {
    values = v;
  }

  public double getValues(int i) {
    if (<= i && i < values.length)
      return values[i];
    return 0;
  }

  public void setValues(int i, double value) {
    if (<= i && i < values.length)
      values[i= value;
  }

  public boolean isInverse() {
    return inverse;
  }

  public void setTitlePosition(int p) {
    titlePosition = p;
  }

  public int getTitlePosition() {
    return titlePosition;
  }

  public void setInverse(boolean b) {
    inverse = b;
  }

  public Dimension getMinimumSize() {
    return new Dimension(MINSIZE, MINSIZE);
  }

  public void setGraphColor(Color c) {
    color = c;
  }

  public Color getGraphColor() {
    return color;
  }

  private static final int LEFT = 0;

  private static final int CENTER = 1;

  private static final int RIGHT = 2;

  private static final int MINSIZE = 50;

  private double[] values = 12};

  private String title = "Title";

  private int titlePosition = CENTER;

  private boolean inverse;

  private Color color = Color.red;
}
           
       
Related examples in the same category
1. The various table charting classesThe various table charting classes
2. Chart Bean 2
ww___w__._ja_v_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.