codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  bar graphs  masterchief at 15:30 on Thursday, February 15, 2007
 

Hey everyone,

Having a little problem creating some bar graphs.

What I am suppose to do is be able to create some bar for a bar graph, at different heights. With this when you click on it, the bar is select and you are able to change the height. Also you can add more bars to the bar graph. This is using a gui interface.

Can anyone help??


-Master Chief will save us all-

  Re: bar graphs  crwood at 03:20 on Friday, February 16, 2007
 


import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import javax.swing.event.*;

public class BarGraph extends JPanel {
JSpinner heightSpinner;
int[] data = { 19, 47, 32 };
Rectangle2D.Double[] cells;
AffineTransform at;
int selectedBar = -1;
final int PAD = 20;

public BarGraph() {
addMouseListener(selector);
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
double w = getWidth();
double h = getHeight();
if(at == null)
initTransform();
// show selection
if(selectedBar != -1) {
g2.setPaint(new Color(240,220,240));
g2.fill(cells[selectedBar]);
}
g2.setPaint(new Color(51,51,51));
// ordinate
g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
// abcissa
g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
// plot data
g2.setPaint(Color.blue);
double xInc = (w-2*PAD)/data.length;
double x = PAD, lastY;
double zero = modelToView(0);
for(int j = 0; j < data.length; j++) {
double y = modelToView(data[j]);
g2.draw(new Line2D.Double(x, y, x+xInc, y));
if(j > 0) {
if(data[j-1] < data[j])
g2.draw(new Line2D.Double(x, y, x, zero));
if(j < data.length-1 && data[j+1] <= data[j])
g2.draw(new Line2D.Double(x+xInc, y, x+xInc, zero));
} else if(j == 0 && data[0] >= data[1]) {
g2.draw(new Line2D.Double(x+xInc, y, x+xInc, zero));
}
if(j == data.length-1) {
// last bar, close
g2.draw(new Line2D.Double(x+xInc, y, x+xInc, zero));
}
x += xInc;
lastY = y;
}
}

public Dimension getPreferredSize() {
return new Dimension(400,400);
}

private double modelToView(int y) {
Point2D.Double dest = new Point2D.Double();
Point2D.Double src = new Point2D.Double(0, y);
at.transform(src, dest);
return dest.y;
}

private void initTransform() {
double w = getWidth();
double h = getHeight();
int maxY = getDataMax();
double xScale = (w-2*PAD)/data.length;
double yScale = (h-2*PAD)/maxY;
// move to origin
double x = PAD;
double y = h-PAD;
at = AffineTransform.getTranslateInstance(x,y);
// scale and flip(-) about the abcissa
at.scale(xScale, -yScale);
if(cells == null)
configureCells();
}

private int getDataMax() {
int max = Integer.MIN_VALUE;
for(int j = 0; j < data.length; j++) {
if(data[j] > max)
max = data[j];
}
return max;
}

private void configureCells() {
cells = new Rectangle2D.Double[data.length];
double w = getWidth();
double h = getHeight();
double xInc = (w-2*PAD)/data.length;
double x = PAD;
double zero = modelToView(0);
for(int j = 0; j < data.length; j++) {
double y = modelToView(data[j]);
cells[j] = new Rectangle2D.Double(x, y, xInc, zero-y);
x += xInc;
}
}

private Box getUIPanel() {
SpinnerNumberModel heightModel = new SpinnerNumberModel(50,10,100,1);
heightSpinner = new JSpinner(heightModel);
SpinnerNumberModel sizeModel = new SpinnerNumberModel(3,1,5,1);
final JSpinner sizeSpinner = new JSpinner(sizeModel);
ChangeListener cl = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSpinner spinner = (JSpinner)e.getSource();
int value = ((Number)spinner.getValue()).intValue();
if(spinner == heightSpinner) {
data[selectedBar] = value;
if(value == getDataMax())
initTransform();
}
if(spinner == sizeSpinner) {
int height = ((Number)heightSpinner.getValue()).intValue();
resetData(value, height);
}
configureCells();
repaint();
}
};
heightSpinner.addChangeListener(cl);
sizeSpinner.addChangeListener(cl);
Box box = Box.createHorizontalBox();
box.add(Box.createHorizontalGlue());
box.add(new JLabel("cell height"));
box.add(heightSpinner);
box.add(Box.createHorizontalGlue());
box.add(new JLabel("bars"));
box.add(sizeSpinner);
box.add(Box.createHorizontalGlue());
return box;
}

private void resetData(int size, int height) {
if(size != data.length) {
int[] temp = new int[size];
int length = (size > data.length) ? data.length : size;
System.arraycopy(data, 0, temp, 0, length);
if(size > data.length)
temp[length] = height;
data = temp;
}
}

public static void main(String[] args) {
BarGraph barGraph = new BarGraph();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(barGraph);
f.getContentPane().add(barGraph.getUIPanel(), "Last");
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

private MouseListener selector = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
Point p = e.getPoint();
for(int j = 0; j < cells.length; j++) {
if(cells[j].contains(p)) {
selectedBar = (selectedBar == j) ? -1 : j;
if(selectedBar != -1)
heightSpinner.setValue(data[selectedBar]);
repaint();
break;
}
}
}
};
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  perl cgi and html links
•  Re: bar graphs
•  Perl Developers Guide
•  Submitting a parent form from a child window in IE 7
•  combo box
•  Problem in comiling
•  New to C++ need help
•  Re: Auto Mail Notification
•  Re: array copy


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007