Posted on August 6, 2007 by Subin
I have attached a sample code snippet for creating a Pie Chart using JFreeChart with custom colors. There is a PieReneder (a static inner class) which can be used to achieve the same. The PieRenderer takes an array of java.awt.Color in its constructor.
package subin.rnd.chart;
import java.awt.Color;
import java.io.File;
import java.util.List;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
public class PieChartWithCustomColours
{
public [...]
Filed under: Code Snippets, Graphics | Tagged: JFreeChart | No Comments »
Posted on July 10, 2007 by Subin
This code snippet will tell you how to create a chart (using JFreeChart) on the fly and displaying it in a webpage.
getChart.jsp
<%@ page import=“java.io.*” %>
<%@ page import=“org.jfree.chart.JFreeChart” %>
<%@ page import=“org.jfree.chart.ChartUtilities” %>
<%
try
{
File image = File.createTempFile(“image”, “tmp”);
// Assume that we have the chart
ChartUtilities.saveChartAsPNG(image, chart, 500, 300);
FileInputStream fileInStream = new FileInputStream(image);
OutputStream outStream = response.getOutputStream();
long fileLength;
byte[] byteStream;
fileLength [...]
Filed under: Code Snippets, Graphics, JSP, Tips, Web | Tagged: JFreeChart | No Comments »
Posted on July 4, 2007 by Subin
This can be achieved by extending the BarRenderer. BarRenderer is responsible for all kind of UI related things like setting the font, setting the colors, setting the alignment etc etc. Rather we can say everything. The same can be achieved by extending corresponding renderer.
Here the getItemPaint(row, col) has been overridden to achieve the custom colors.
CustomRenderer:
import [...]
Filed under: Code Snippets, Graphics | Tagged: JFreeChart | No Comments »
Posted on July 4, 2007 by Subin
The given code snippet can create a 3D Bar Chart. The given Bar Chart compares 2 aspects of each customer. The generated chart will have two bars for each customer for both Profit1 and Profit 2 (see the dataset).
Profit 1 can can be total annual profit and Profit 2 can be profit for a [...]
Filed under: Code Snippets, Graphics | Tagged: JFreeChart | 2 Comments »
Posted on June 28, 2007 by Subin
I have attached a code snippet which can give you a basic idea on creating a time-series chart using JFreeChart. This is a part of JFreeChart samples which come along with the free distribution of JFreeChart.
// Creating a dataset
TimeSeries s1 = new TimeSeries(”Name of the Time series”, Month.class);
s1.add(new Month(2, 2001), 181.8);
s1.add(new Month(3, 2001), 167.3);
s1.add(new Month(4, 2001), [...]
Filed under: Code Snippets, Graphics | Tagged: JFreeChart | No Comments »