JFreeChart Tutorial
Using JFreeChart to create chart images
On my recent project I've been on the lookout for a good charting tool. After evaluating many i realized that JFreeChart is the one . It is completely open source and available free.There is a great book about how to use that and in the true open source spirit i recommend that you buy the book if you need to learn more about JFreeChart and show your support for the folks behind this.
I needed a quick solution to put clickable image chart on my web application. If you too have the same requirement , this might help.
The java of the code is quite wrong, non static methods etc etc. but this is the initial working code that i wrote as a start ..so excuse the lack of elegance :-)..
The resulting chart is on the left
You could use this in a servlet as follows public class EventChart extends HttpServlet { private static final Logger LOGGER = Logger .getLogger(EventChart.class.getCanonicalName()); @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { SiteEventChart eventChart = new UsageChart(); eventChart.writeChar(response.getOutputStream()); } }
In the jsp use the code .....
package com.mobchannel.usagettracker.charts;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.TickUnits;
import org.jfree.chart.imagemap.ImageMapUtilities;
import org.jfree.chart.imagemap.StandardToolTipTagFragmentGenerator;
import org.jfree.chart.imagemap.StandardURLTagFragmentGenerator;
import org.jfree.chart.imagemap.URLTagFragmentGenerator;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.labels.XYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.urls.StandardXYURLGenerator;
import org.jfree.chart.urls.XYURLGenerator;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Week;
public class UsageChart{
private static final Logger LOGGER
= Logger
.getLogger(usagettrackerChart.class.getCanonicalName());
public enum EventType
{
VIEW,
EDIT, DELETE, ALERT
}
public TimeSeries getTimeSeries(EventType type) {
final TimeSeries series = new TimeSeries(type.name().toUpperCase(),Week.class);
RegularTimePeriod
period = new Week();
for (int i
= 0; i < 5; i++) {
series.add(period,
(type.ordinal()+1)* 5);
period = period.next();
}
return series;
}
public String getImageMap() throws
IOException{
ChartRenderingInfo info = new ChartRenderingInfo();
generateChart(new ByteArrayOutputStream(),
info);
String map = ImageMapUtilities.getImageMap("CHART",
info,new StandardToolTipTagFragmentGenerator
(),new StandardURLTagFragmentGenerator());
LOGGER.log(Level.INFO,map);
return map;
}
public void writeChar(OutputStream outStream) throws IOException{
generateChart(outStream, new ChartRenderingInfo());
}
public void generateChart(OutputStream outStream,ChartRenderingInfo
info) throws IOException{
final TimeSeriesCollection dataSet = new TimeSeriesCollection();
dataSet.addSeries(getTimeSeries(EventType.VIEW));
dataSet.addSeries(getTimeSeries(EventType.ALERT));
dataSet.addSeries(getTimeSeries(EventType.EDIT));
dataSet.addSeries(getTimeSeries(EventType.DELETE));
final JFreeChart chart = ChartFactory.createXYLineChart(
"Site
Event Tracker", "Day", "Event", dataSet,
PlotOrientation.VERTICAL, true, true, false);
XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(0, true);
renderer.setSeriesShapesVisible(0, true);
renderer.setSeriesLinesVisible(1, true);
renderer.setSeriesShapesVisible(1, true);
renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
renderer.setURLGenerator(new StandardXYURLGenerator());
plot.setRenderer(renderer);
plot.setDomainAxis(new DateAxis());
final DateAxis axis = (DateAxis) plot.getDomainAxis();
final TickUnits standardUnits = new TickUnits();
standardUnits.add(new DateTickUnit(DateTickUnit.DAY, 1,
new
SimpleDateFormat("MMM dd ''yy")));
standardUnits.add(new DateTickUnit(DateTickUnit.DAY, 7,
new
SimpleDateFormat("MMM dd ''yy")));
standardUnits.add(new DateTickUnit(DateTickUnit.MONTH, 1,
new
SimpleDateFormat("MMM ''yy")));
axis.setStandardTickUnits(standardUnits);
ChartUtilities.writeChartAsJPEG(outStream, chart, 400,
300,info);
}
}
Last but not the least , here is the web.xml to do the necessary configurations...
Cloud Computing - A platform Study..
7th , 9th no, You got it wrong. This is the geek question, if you aren't aware of what a cloud is you might as well lose your scalability expert credentials if you are one.
Clouds offer you an elastic solution to your application needs. When you are a start up all you would need would be a shared hosted environment , but how about if your app becomes the biggest hit on the planet. Addressing this issue has been the nightmare of all web entrepreneurs, what if i become hugely successful with my first launch.
Obviously you can't build an array of virtualized servers expecting that you are going to achieve sustainable growth.Out of a thousand web start ups only a handful survive the first 2 years and only a few are even venture funded .And out of those only a few survive after 5 years.For example I've been in business for close to 2 years now , but i haven't seen traffic yet that justifies my dedicated server sitting on an optic fiber from verizon. I don't even need that, but i may become successful by the end of this year and might have a billion users. How can i plan for that event. I cannot be building arrays of virtualized servers when i should be building my app. Nor have i got the skills necessary to maintain an array.I have to hire the experts and can i afford that now?. Even if i can , should i . Do i have an alternative?
Here is where a cloud can help me.The cloud removes you from the hassle of scalability planning. There aren't a quite a large number of providers, Amazon EC2 service and S3 environments offer completely owned virtual elastic environments. It allows you to host your own AMI(Amazon Machine Image) which is almost similar to VMWare's mechanism of bootstrapping images.But one obvious disadvantage is if you have to frequently change your image ,then you could be in trouble as each image is to be reinstalled and ec2 does not allow you to just change/upgrade your environment. I am not yet sure how you would manage an app upgrade without reinstalling an AMI.Few other cloud environments exist , google's App Engine allows you to run Python application which is good if you are a python programmer, but what if you want to do a project on ruby on rails , Engine Yard is there for you.What if you want to do java or .NET.
Hmm.. i wouldn't want to deploy my app on a platform which forces me to use a particular framework.I would wait for a solution which allows you to install applications but frees you from the task of installing and maintaining images. May be a new provider will come who offers virtualized linux servers in a cloud with elastic scalability and that solution would take away trophy of the best cloud platform . Till that i would give my vote for the best cloud platform to Amazon's S3 unless you are decided on python or rails as your app framework.