Brazil Brasileiro

Well, the strange, wonderful, wild left turns continue to happen in my life. Our Caribbean based business is expanding into Brazil in the next month or so. This is exciting. We will be located in Sao Paulo, which is a place that I haven't been to, but probably will visit with the next month or two.

In addition, we are in prelimary talks to expand to Florida and Texas. It is an exciting time to be in the Bahamas. Usually it is the other way around -- businesses expand to the Bahamas. The Bahamas is not known for exporting businesses.

And as an omnibus salute to my fellow geeks who follow this page, I have some more :

SOURCE CODE FOR JFREECHART

I needed a snazzy dashboard dial to show transactions of yesterday versus today for one of our business lines. I used JFreeChart in an applet, but you can also use it in JSP and put a jpg of the dial once it is made from the mysql data.

Here is a pic of how the dial looks:



This pic is a preliminary pic. I cleaned up the tic labels. Here is the code for the applet:


import java.applet.Applet;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.sql.*;


import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.dial.*;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.StandardGradientPaintTransformer;
import com.mysql.*;


public class transDial extends Applet {

/**
*
*/
private static final long serialVersionUID = 1L;

public void init()
{

int todayTrans =0;
int yesterdayTrans=0;
Connection conn = null;
String url = "jdbc:mysql://100.150.1.27:3306/testtable";

String dbName = "tspayment";
String driver = "com.mysql.jdbc.Driver";
String userName = "myuser";
String password = "password";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
Statement stmt = conn.createStatement();
String myQueryString = "select Count(*) as myCount from testtransaction where Date(TransDate) = CURDATE()";
ResultSet rs = stmt.executeQuery(myQueryString);
todayTrans = rs.getInt("myCount");
}
catch (SQLException e)
{
System.out.println("Error in database --> get today's transactions");
System.out.println(e.toString());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Statement stmt = conn.createStatement();
String myQueryString = "select Count(*) as myCount from testtransaction where Date(TransDate) = Date_SUB(CURDATE(), Interval 1 day)";
ResultSet rs = stmt.executeQuery(myQueryString);
yesterdayTrans = rs.getInt("myCount");
}
catch (SQLException e)
{
System.out.println("Error in database --> get today's transactions");
System.out.println(e.toString());
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}




DefaultValueDataset dataset1 = null;
DefaultValueDataset dataset2 = null;
dataset1 = new DefaultValueDataset(todayTrans);
dataset2 = new DefaultValueDataset(yesterdayTrans);
DialPlot dialplot = new DialPlot();
dialplot.setView(0.0, 0.0, 1.0, 1.0);
dialplot.setDataset(0, dataset1);
dialplot.setDataset(1, dataset2);
StandardDialFrame standarddialframe = new StandardDialFrame();
standarddialframe.setBackgroundPaint(Color.lightGray);
standarddialframe.setForegroundPaint(Color.darkGray);
dialplot.setDialFrame(standarddialframe);
GradientPaint gradientpaint = new GradientPaint(new Point(), new Color(255, 255, 255), new Point(), new Color(170, 170, 220));
DialBackground dialbackground = new DialBackground(gradientpaint);
dialbackground.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));
dialplot.setBackground(dialbackground);
DialTextAnnotation dialtextannotation = new DialTextAnnotation("Transactions");
dialtextannotation.setFont(new Font("Dialog", 1, 14));
dialtextannotation.setRadius(0.69999999999999996);
dialplot.addLayer(dialtextannotation);
DialValueIndicator dialvalueindicator = new DialValueIndicator(0);
dialvalueindicator.setFont(new Font("Dialog", 0, 10));
dialvalueindicator.setOutlinePaint(Color.darkGray);
dialvalueindicator.setRadius(0.59999999999999998);
dialvalueindicator.setAngle(-103);
dialplot.addLayer(dialvalueindicator);
DialValueIndicator dialvalueindicator1 = new DialValueIndicator(1);
dialvalueindicator1.setFont(new Font("Dialog", 0, 10));
dialvalueindicator1.setOutlinePaint(Color.red);
dialvalueindicator1.setRadius(0.59999999999999998D);
dialvalueindicator1.setAngle(-77);
dialplot.addLayer(dialvalueindicator1);
StandardDialScale standarddialscale = new StandardDialScale(0, 150, -120, -300, 10, 4);
standarddialscale.setTickRadius(0.88);
standarddialscale.setTickLabelOffset(0.14999999999999999);
standarddialscale.setTickLabelFont(new Font("Dialog", 0, 10));
dialplot.addScale(0, standarddialscale);
StandardDialScale standarddialscale1 = new StandardDialScale(0, 150, -120, -300, 10, 4);
standarddialscale1.setTickRadius(0.5);
standarddialscale1.setTickLabelOffset(0.14999999999999999);
standarddialscale1.setTickLabelFont(new Font("Dialog", 0, 8));
standarddialscale1.setMajorTickPaint(Color.red);
standarddialscale1.setMinorTickPaint(Color.red);
dialplot.addScale(1, standarddialscale1);
dialplot.mapDatasetToScale(1, 1);
org.jfree.chart.plot.dial.DialPointer.Pin pin = new org.jfree.chart.plot.dial.DialPointer.Pin(1);
pin.setRadius(0.55000000000000004D);
dialplot.addPointer(pin);
org.jfree.chart.plot.dial.DialPointer.Pointer pointer = new org.jfree.chart.plot.dial.DialPointer.Pointer(0);
dialplot.addPointer(pointer);
DialCap dialcap = new DialCap();
dialcap.setRadius(0.10000000000000001D);
dialplot.setCap(dialcap);
JFreeChart jfreechart = new JFreeChart(dialplot);
jfreechart.setTitle("Transactions: Today vs Yesterday");

}

public void start(){
System.out.println("Applet starting.");
showStatus("Dashboard: Loading image file ");
}

public void stop(){
System.out.println("Applet stopping.");
}

public void destroy(){
System.out.println("Destroy method called.");
}
}


Now of course, you could make this a java class or bean and use TomCat to serve it up in jsps.

Here is the code to turn the above into a jpeg:

try {
ChartUtilities.saveChartAsJPEG(new File("webapps/Dashboard/images/arcDial.jpg"), jfreechart1, 400, 400);
} catch (IOException e) {
System.err.println(e.getLocalizedMessage());
}

There you go. You now know how to use the Dial in JFreeChart. There is not a lot of free documentation on this.

No comments: