• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
});
}
}
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic