Print Friendly and PDF

Program:

package com.hmkcode.mongodb;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import no.geosoft.cc.geometry.Geometry;
import no.geosoft.cc.graphics.*;
public class HistogramChart extends JFrame
{
public HistogramChart(int[] arrayInt, String[] arrayString)
{
super ("G Graphics Library - Demo 17"); 
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// Create the graphic canvas
GWindow window = new GWindow();
getContentPane().add (window.getCanvas());
// Create scane with default viewport and world extent settings
GScene scene = new GScene (window);
GStyle chartStyle = new GStyle();
chartStyle.setLineStyle (GStyle.LINESTYLE_INVISIBLE);
chartStyle.setFont (new Font ("Dialog", Font.BOLD, 9));
chartStyle.setForegroundColor (new Color (0, 0, 0));
BarChart barChart = new BarChart (70, 450, 400, 400);
barChart.setStyle (chartStyle);
//sorting the arraies
int minval = arrayInt[0];
int temp=0;
String tempString="";
int i;
for( i = 0; i< arrayInt.length; i++)
{ 
for(int j = 0; j< arrayInt.length-1; j++)
{
if(arrayInt[j+1]<arrayInt[j])
{
temp=arrayInt[j+1];
tempString = arrayString[j+1];
arrayInt[j+1]=arrayInt[j];
arrayString[j+1]= arrayString[j];
arrayInt[j]=temp;
arrayString[j] = tempString;
}
}
}
for (i=arrayInt.length-1; i>=0 ;i--)
{
barChart.addBar(arrayString[i]+"\n"+"aba",arrayInt[i], getColor());
}
/*barChart.addBar ("1998", 250, getColor());
barChart.addBar ("1999", 150, getColor());
barChart.addBar ("2000", 80, getColor());
barChart.addBar ("2001", 174, getColor());
barChart.addBar ("2002", 350, getColor());
barChart.addBar ("2003", 40, getColor()); 
barChart.addBar ("2004", 100, getColor());
barChart.addBar ("2005", 150, getColor());*/ 
scene.add (barChart);
pack();
setSize (new Dimension (1000, 1500));
setVisible (true);
}
private Color getColor()
{
return new Color (Color.HSBtoRGB ((float)Math.random(), 0.2f, 0.8f));
}
private class BarChart extends GObject
{
private int x_, y_, width_, height_;
private Collection bars_;
BarChart (int x, int y, int width, int height)
{
x_ = x;
y_ = y;
width_ = width;
height_ = height;
bars_ = new ArrayList();
} 
void addBar (String label, int value, Color color)
{
bars_.add (new Bar (label, value, color));
}
public void draw()
{
final int BAR_WIDTH = 40;
final int SPACING = 25;
final int DEPTH = 30;
removeSegments();
int x0 = x_ + 10;
double angle0 = 0.0;
for (Iterator i = bars_.iterator(); i.hasNext(); ) 
{
Bar bar = (Bar) i.next();
int y0 = y_ - bar.value;
GSegment main = new GSegment();
addSegment (main);
GStyle style = new GStyle();
style.setForegroundColor (bar.color);
style.setBackgroundColor (bar.color); 
main.setStyle (style);
main.setGeometry (Geometry.createRectangle (x0, y0,BAR_WIDTH, bar.value));
GSegment label = new GSegment();
addSegment (label);
label.setGeometry (x0 + BAR_WIDTH / 2, y_ + 10);
GText text = new GText (bar.label);
label.setText (text);
GSegment top = new GSegment();
addSegment (top);
style = new GStyle();
style.setForegroundColor (bar.color.brighter());
style.setBackgroundColor (bar.color.brighter()); 
top.setStyle (style);
int topXy[] = {x0, y0,
x0 + BAR_WIDTH, y0,
x0 + BAR_WIDTH + DEPTH, y0 - DEPTH,
x0 + DEPTH, y0 - DEPTH,
x0, y0
};
top.setGeometry (topXy);
GSegment side = new GSegment();
addSegment (side);
style = new GStyle();
style.setForegroundColor (bar.color.darker());
style.setBackgroundColor (bar.color.darker()); 
side.setStyle (style);
int[] sideXy = {x0 + BAR_WIDTH, y0,
x0 + BAR_WIDTH + DEPTH, y0 - DEPTH,
x0 + BAR_WIDTH + DEPTH, y_ - DEPTH,
x0 + BAR_WIDTH, y_,
x0 + BAR_WIDTH, y0};
side.setGeometry (sideXy);
x0 += BAR_WIDTH + SPACING;
}
}
}
private class Bar
{
public String label;
public int value;
public Color color;
public Bar (String label, int value, Color color)
{
this.label = label;
this.value = value;
this.color = color;
}
}
public static void main (String[] args)
{
//new HistogramChart();
}
}
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: