rinku singh

Greenhorn
+ Follow
since Apr 22, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by rinku singh

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();
}
});
}
}
20 years ago
Does any one has a code for drawing bar chart . i want to see the code for drawing the chart.

thanks
amit
20 years ago
Craig

what's the logic behind these

1)double dia = Math.min(w,h)*0.85; ( why the value 0.85 ?)
2)cx = w/2 + (dia/2) * Math.cos(theta + phi/2) * 7/12;
cy = h/2 + (dia/2) * Math.sin(theta + phi/2) * 7/12;
// text origin at lower left of box
sx = cx - textWidth/2;
sy = cy + textHeight/2;

Why the value 7/12 ?.
Can you describe this logic. how you come with this to determine the value of x,y co-ordinate to display Value. Is this some sort of standard formula to to calculate the poistion of x,y to display value in pie ?.
20 years ago
thanks Craig . I was looking how to display the value of 100,200,300,400 within the pie , pie representing each item.I am unable to caluclate the value of X,y co-ordinate where is should display the Data. IF you can explain me how to calculate the value for X,Y that will be great.If you know how to calculate can u put that piece and code and also explain how we are calcilating the valeu .

Thanks in advance.

//g2.drawString(s, xp, yp); where S is the value of the Data.
20 years ago
Hi
i have a program i want to show data in each pie for which that that pie is constructed.
thaks
amit
=====================================
import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;

import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
public class Charts2D extends JFrame
{

public Charts2D() {
super("2D Charts");
setSize(720, 280);
getContentPane().setLayout(new GridLayout(1, 3, 10, 0));
getContentPane().setBackground(Color.white);
//int nData = 10;
int[] nData = {102,87,18,95 };
int[] xData = nData;//new int[nData];
int[] yData = nData;//new int[nData];
for (int k=0; k<nData.length; k++) {
xData[k] = k;
yData[k] = (int)(Math.random()*100);

if (k > 0)
yData[k] = (yData[k-1] + yData[k])/2;
}

System.out.println("####### nData "+nData+" xData "+xData+" yData "+yData);
System.out.println("Math.random()*100) "+(int)(Math.random()*100));




JChart2D chart = new JChart2D(JChart2D.CHART_PIE, nData, xData,yData, "Pie Chart");
ImageIcon icon = new ImageIcon("hubble.gif");
chart.setEffectIndex(JChart2D.EFFECT_IMAGE);
chart.enterData(nData);
getContentPane().add(chart);



WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};

addWindowListener(wndCloser);
setVisible(true);
}
public static void main(String argv[]) {
new Charts2D();
}

}
class JChart2D extends JPanel
{
int[] nData, percents;
int dataTotal;
final int PAD = 25, R_PAD = 5;
public static final int CHART_LINE = 0;
public static final int CHART_COLUMN = 1;
public static final int CHART_PIE = 2;
public static final int EFFECT_PLAIN = 0;
public static final int EFFECT_GRADIENT = 2;
public static final int EFFECT_IMAGE = 3;
protected int m_chartType = CHART_LINE;
protected JLabel m_title;
protected ChartPanel m_chart;
protected int[] m_nData;
protected int[] m_xData;
protected int[] m_yData;
protected int m_xMin;
protected int m_xMax;
protected int m_yMin;
protected int m_yMax;
protected double[] m_pieData;
protected int m_effectIndex = EFFECT_PLAIN;
protected Stroke m_stroke;
protected GradientPaint m_gradient;
protected Image m_foregroundImage;
protected Color m_lineColor = Color.red;
protected Color m_columnColor = Color.yellow;
protected int m_columnWidth = 15;
protected boolean m_drawShadow = false;

public JChart2D(int type, int[] nData,int[] yData, String text)
{
this(type, nData, null, yData, text);
}

public JChart2D(int type, int[] nData, int[] xData,int[] yData, String text)
{
super(new BorderLayout());

setBackground(Color.white);

m_title = new JLabel(text, JLabel.CENTER);
add(m_title, BorderLayout.NORTH);

m_chartType = type;
if (xData==null)
{
//xData = new int[nData];
for (int k=0; k<nData.length; k++)
xData[k] = k;
}

if (yData == null)
throw new IllegalArgumentException("yData can't be null");
if (nData.length > yData.length)
throw new IllegalArgumentException("Insufficient yData length");
if (nData.length > xData.length)
throw new IllegalArgumentException("Insufficient xData length");

m_nData = nData;
m_xData = xData;
m_yData = yData;
m_xMin = m_xMax = 0; // To include 0 into the interval
m_yMin = m_yMax = 0;


for (int k=0; k<m_nData.length; k++)
{
m_xMin = Math.min(m_xMin, m_xData[k]);
m_xMax = Math.max(m_xMax, m_xData[k]);
m_yMin = Math.min(m_yMin, m_yData[k]);
m_yMax = Math.max(m_yMax, m_yData[k]);
}

if (m_xMin == m_xMax)
m_xMax++;

if (m_yMin == m_yMax)
m_yMax++;

if (m_chartType == CHART_PIE)
{
double sum = 0;
for (int k=0; k<m_nData.length; k++)
{
m_yData[k] = Math.max(m_yData[k], 0);
sum += m_yData[k];
}

m_pieData = new double[m_nData.length];

for (int k=0; k<m_nData.length; k++)
{
m_pieData[k] = m_yData[k]*360.0/sum;
System.out.println("m_pieData[k]"+m_pieData[k]);
}

}
m_chart = new ChartPanel();
add(m_chart, BorderLayout.CENTER);

}


public void setEffectIndex(int effectIndex) {
m_effectIndex = effectIndex;
repaint();
}

public int getEffectIndex() { return m_effectIndex; }

public void setStroke(Stroke stroke) {
m_stroke = stroke;
m_chart.repaint();
}



public Stroke getStroke() { return m_stroke; }

public void setGradient(GradientPaint gradient) {
m_gradient = gradient;
repaint();
}
public GradientPaint getGradient() { return m_gradient; }


class ChartPanel extends JComponent
{
int m_xMargin = 5;
int m_yMargin = 5;
int m_pieGap = 10;
int m_x;
int m_y;
int m_w;
int m_h;
ChartPanel() {
enableEvents(ComponentEvent.COMPONENT_RESIZED);
}
protected void processComponentEvent(ComponentEvent e) {
calcDimensions();
}
public void calcDimensions() {

Dimension d = getSize();
m_x = m_xMargin;
m_y = m_yMargin;
m_w = d.width-2*m_xMargin;
m_h = d.height-2*m_yMargin;


}

//////////////////////////////////////////
public void paintComponent(Graphics g) {

int width = getWidth();
int height = getHeight();
int xp,yp;
String s;
int dataWidth, dataHeight;
int cx = width/2;
int cy = height/2;



Graphics2D g2 = (Graphics2D) g;
int x0 = 0;
int y0 = 0;
Font font = new Font("lucida sans unicode", Font.PLAIN, 16);
g2.setFont(font);

FontRenderContext frc = g2.getFontRenderContext();



g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
if (m_stroke != null)
g2.setStroke(m_stroke);
GeneralPath path = new GeneralPath();


///////////////////// chart /////////////////////////
switch (m_chartType) {

case CHART_PIE:

double start = 0.0;
double finish = 0.0;
int ww = m_w - 2*m_pieGap;
int hh = m_h - 2*m_pieGap;

int rcolor =30;
int gcolor =20;
int bcolor =10;

for (int k=0; k<m_nData.length; k++)
{

finish = start+m_pieData[k];

s = String.valueOf(percents[k]) + "%";

double f1 = Math.min(90-start, 90-finish);
double f2 = Math.max(90-start, 90-finish);
System.out.println("%%%%%%%%%%%%% finish "+finish+" "+f1+" "+f2);

Shape shp = new Arc2D.Double(m_x, m_y, ww, hh,f1, f2-f1, Arc2D.PIE);

//this is where i want to show data( 102,87,18,95) in each pie. can someone write the code for this.
double f = (f1 + f2)/2*Math.PI/180;
AffineTransform s1 = AffineTransform.getTranslateInstance(m_pieGap*Math.cos(f),-m_pieGap*Math.sin(f));
g2.setColor(new Color(0, 140, 180));

s1.translate(m_pieGap, m_pieGap);
Shape piece = s1.createTransformedShape(shp);
path.append(piece, false);
g2.fill(piece);
start = finish;


}

if (m_effectIndex==EFFECT_GRADIENT && m_gradient != null)
{
g2.setPaint(m_gradient);
g2.fill(path);
}
else if (m_effectIndex==EFFECT_IMAGE &&m_foregroundImage != null)
{
fillByImage(g2, path, 0);
}
else {
g2.setColor(m_columnColor);
g2.fill(path);
}



g2.setColor(m_lineColor);
g2.draw(path);
break;
}
}

protected void fillByImage(Graphics2D g2,Shape shape, int xOffset) {

if (m_foregroundImage == null)
return;

int wImg = m_foregroundImage.getWidth(this);
int hImg = m_foregroundImage.getHeight(this);

if (wImg <=0 || hImg <= 0)
return;
//g2.setClip(shape);
Rectangle bounds = shape.getBounds();
System.out.println("bounds "+bounds);


for (int xx = bounds.x+xOffset;xx < bounds.x+bounds.width; xx += wImg)
for (int yy = bounds.y; yy < bounds.y+bounds.height;yy += hImg)
g2.drawString("x", xx, yy);

}


}
public void prepareData()
{
for(int i = 0; i < nData.length; i++)
dataTotal += nData[i];
percents = new int[nData.length];
int dataPlus = 0;

for(int i = 0; i < nData.length; i++){
dataPlus += nData[i];
percents[i] = Math.round(100 * dataPlus/dataTotal);
}
}

public void enterData(int[] nData)
{
this.nData = nData;
prepareData();
repaint();
}
}
[EMAIL][/EMAIL]
20 years ago
i have set the classptah and my sample java connection program is running . i am able to log into database. but when i am using the same thing in a EJB and deploying the bean on JBoss and trying to get access to Data base . i am having problem loading driver.
I have alredy set the jboss.jcml
==
<mbean code="org.jboss.jdbc.JdbcProvider" name="DefaultDomain:service=JdbcProvider">
<attribute name="Drivers">org.hsqldb.jdbcDriver,com.sybase.jdbc.SybDriver</attribute>
</mbean>
===
jboss.property file
==
jdbc.drivers=org.hsql.jdbcDriver,org.enhydra.instantdb.jdbc.idbDriver,com.sybase.jdbc.SybDriver
==
put the jDTS.jar in lb\ext of JBoss
but stil getting the problem.
Any suggestion please
21 years ago
hi i am using JBOSS ans syabse as database. i am trying to connect the database throught Entity Bean
================== CODE==========================
try {
Class.forName ("com.sybase.jdbc.SybDriver" ).newInstance();
}catch(ClassNotFoundException ex){
System.out.println("Error"+ex);
}
catch(Exception e) {
System.err.println("Error loading driver: " + e);
}
================================================
when i run my client to access the bean deployed on JBOSS. i get error "NO SUITABLE driver". i have modified . jboss.xml file by adding
<mbean code="org.jboss.jdbc.JdbcProvider" name="DefaultDomain:service=JdbcProvider">
<attribute name="Drivers">com.sybase.jdbc.SybDriver</attribute>
</mbean>
as my default driver and add the jDTS.jar in the lib
ext. i donot know understand why i am getting this error and how to remove it. pls help
21 years ago