display jfreecharts using servlets
I have a scenario where I need to display charts(generated using jfreecharts) converted it into a 开发者_开发技巧png image and then display them using servlets.
When the chart code extends either ApplicationFrame or Jframe I see the following exception when I display it using servlets :
java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it. at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:159) at java.awt.Window.(Window.java:317) at java.awt.Frame.(Frame.java:419) at javax.swing.JFrame.(JFrame.java:194)
I read through a couple of places and this seems to be because of the ApplicationFrame and JFrame would cause this error.
If i pass just the chart object this throws a
java.lang.IllegalArgumentException: Null 'chart' argument. org.jfree.chart.ChartUtilities.writeChartAsPNG(ChartUtilities.java:181) org.jfree.chart.ChartUtilities.writeChartAsPNG(ChartUtilities.java:136)
Is there any solution for this?
ChartUtilities
is the right choice; streams are supported, too. One approach requires Using Headless Mode in the Java SE Platform, but I've also gotten it to work with VNC.
Addendum: here's a related forum thread.
I will explain you in simple way as I faced the same problem as i was new.
Steps
- Create a servlet
- In doPost create outputstream
- set content type to text/png
- Create datasets
- set values to dataset
- create instance of jfreechart and call createchart(the chart you wanna use) using ChartFqactory.
- finally call WriteChartAsPNG using ChartUtilities and pass the Jfreechart instance,outputstream,width,height.
DONE:
Sample below
package polo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
/**
* Servlet implementation class PieChartDemo1Serv
*/
public class PieServ extends HttpServlet implements useme {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public PieServ() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("image/png");
ServletOutputStream out = response.getOutputStream();
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("In-Network ", .80);
pieDataset.setValue("Out-of-Network ", .20);
JFreeChart chart = ChartFactory.createPieChart("", pieDataset, true, true, false);
ChartUtilities.writeChartAsPNG(out, chart, 202, 182);
System.out.println("done23");
}
}
Now I know you what further.Study Pieplots/Legends to setup border and formatting.You can do a lot further.
精彩评论