*
The moose likes Swing / AWT / SWT and the fly likes Please tell me how to display value on Y axis in this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Please tell me how to display value on Y axis in this code" Watch "Please tell me how to display value on Y axis in this code" New topic
Author

Please tell me how to display value on Y axis in this code

rinku singh
Greenhorn

Joined: Apr 22, 2003
Posts: 7
package com.Charts;

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
public class ChartPanel extends JPanel{
private int[] values;
private String[] names, namesy;
private String title;
private int barWidth;
public ChartPanel(int[] v, String[] n,String[] m ,String t) {
names = n;
namesy = m;
values = v;
title = t;

}
public void paint(Graphics g)
{
super.paint(g);System.out.println("******");

Graphics2D g2 = (Graphics2D)g.create();
// compute the minimum and maximum values
if (values == null) return;
int minValue = 0;
int maxValue = 0;
int total =0;
for (int i = 0; i < values.length; i++)
{
if (minValue > values[i])
minValue = values[i];
if (maxValue < values[i])
maxValue = values[i];
total = values[i]+total;
}
if (maxValue == minValue)
return;
int panelWidth = getWidth();
int panelHeight = getHeight();
Font titleFont = new Font("SansSerif", Font.BOLD, 20);
Font labelFont = new Font("SansSerif", Font.PLAIN, 15);
// compute the extent of the title

FontRenderContext context = g2.getFontRenderContext();
Rectangle2D titleBounds = titleFont.getStringBounds(title, context);
int titleWidth = (int)titleBounds.getWidth();
int top =(int) titleBounds.getHeight(); // draw the title
int y =(int) -titleBounds.getY(); // ascent
int x = (panelWidth - titleWidth) / 2;
g2.setFont(titleFont);
g2.drawString(title, (float)x, (float)y);

// compute the extent of the bar labels
LineMetrics labelMetrics = labelFont.getLineMetrics("", context);
int bottom = (int)labelMetrics.getHeight();
y = (int)(panelHeight - labelMetrics.getDescent());
g2.setFont(labelFont);
// get the scale factor and width for the bars
int scale = (panelHeight - top - bottom) / (maxValue - minValue);
barWidth = 40;//y axis
g2.draw(new Line2D.Double(10, top+maxValue*scale , 10, top-20));//x axis
g2.draw(new Line2D.Double(10, top+maxValue*scale , (values.length)*(barWidth+10), top+maxValue*scale));

// draw the bars
for (int i = 0; i < values.length; i++)
{
// get the coordinates of the bar rectangle
int x1 = i * barWidth + 20;
int y1 = top;
int height = values[i] * scale;
if (values[i] >= 0)
y1 += (maxValue - values[i]) * scale;
else
{
y1 += maxValue * scale;
height = -height;
}
// fill the bar and draw the bar outline
Rectangle2D rect = new Rectangle2D.Double(x1, y1, barWidth - 20, height);
g2.setPaint(Color.red);
g2.fill(rect);
g2.setPaint(Color.black);
g2.draw(rect);

// draw the centered label below the bar
Rectangle2D labelBounds = labelFont.getStringBounds(names[i], context);
int labelWidth =(int) labelBounds.getWidth();
x = i * barWidth + (barWidth - labelWidth) / 2 +15;
g2.drawString(names[i], (float)x, (float)y-20);

// draw the label above



}


g2.dispose();
}

public Dimension getPreferredSize() {
int width = (barWidth * values.length);
int height = getSize().height;
return new Dimension(width, height);
}

public static void main(String[] args) {
int[] v = { 10, 40, 25, 70 };
String[] n = { "Jan", "Feb", "Mar", "Apr","JUM" };
String[] m= { "1", "2", "3", "5","6" };
ChartPanel panel = new ChartPanel(v, n,m,"");
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(panel);
f.setSize(300, 300);
f.setLocation(300,300);
f.setVisible(true); }
}
======================= Class calling this Chartpanel============


import com.Charts.ChartPanel;
import javax.swing.*;
import java.awt.*;

public class tryDraw extends JPanel{
private static void drawWindow()
{JFrame frame=new JFrame("Bar chart");
/* main container */
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /* exits when window closed */
Container contentPane=frame.getContentPane(); /* not really reqd, all visible components */JPanel pane=new JPanel(new GridLayout(0,1)); /* add all components in JPanel */
pane.setOpaque(true);
JPanel subPane=new JPanel(); /* add all components in JPanel */
//ScrollPane subPane = new ScrollPane();
JLabel lbl=new JLabel("Hello World!!!");
lbl.setBorder(BorderFactory.createEtchedBorder()); /* add border using BorderFactory class */subPane.add(lbl); /* add lbl to pane*/
subPane.setBorder(BorderFactory.createEmptyBorder(100,100,100,100));/* t,l,b,r */
pane.add(subPane); /* add pane to content pane */
ScrollPane subPane1= new ScrollPane(); /* add all components in JPanel */
subPane1.setSize(300, 300);
subPane1.add(new ChartPanel(new int[]{ 10, 40, 25, 70,100 },new String[]{ "1", "2", "3", "4", "5" }, new String[]{ "10", "20", "30", "40", "50" },""));
pane.add(subPane1); /* add pane to content pane */
contentPane.add(pane); /* add pane to content pane */
frame.pack();
frame.setVisible(true); /* make frame visible */
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater
(new Runnable()
{public void run()
{drawWindow();
}
});
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Please tell me how to display value on Y axis in this code