使用JFreeChart绘制简单图表
import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.jfree.chart.ChartFactory;import org.jfree.chart.ChartFrame;import org.jfree.chart.ChartUtilities;import org.jfree.chart.JFreeChart;import org.jfree.chart.plot.PiePlot;import org.jfree.data.general.DefaultPieDataset;public class ShowChart extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// 建立一个默认的饼图DefaultPieDataset dp = new DefaultPieDataset();// 输入数据dp.setValue("fangbingbing", 90);dp.setValue("liuyifei", 100);dp.setValue("lingzhiling", 95);dp.setValue("liushishi", 85);//创建一个饼图JFreeChart chart = ChartFactory.createPieChart("Beauty", dp);PiePlot p3d = (PiePlot) chart.getPlot();p3d.setForegroundAlpha(0.5f);ChartFrame chartFrame = new ChartFrame("Beauty", chart);//chartFrame.pack(); // 以合适的大小展现图形//chartFrame.setVisible(true);// 图形是否可见response.setContentType("image/png");ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, 400, 300);}}?我直接建立了一个Servlet像web网页进行图表的输出,在前端web网页上,你可以直接调用这个Servlet或者使用<img src="你的servlet">标签 展示你的图表。