JFreeChart能生成“四象限图”吗?
怎么弄啊?
最好给个例子。有相关资料更好。

解决方案 »

  1.   

     我用JFreeChar做过饼图,柱状图,还有折线图,不知道你说的图想要的是什么效果。
    我给你一个有以上三种效果的例子吧。package com.zzw.dao;import java.awt.Color;
    import java.awt.RenderingHints;
    import java.io.IOException;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;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.ChartRenderingInfo;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.entity.StandardEntityCollection;
    import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
    import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.PiePlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.renderer.category.BarRenderer3D;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.data.general.DefaultPieDataset;import com.zzw.dao.impl.IJfreeCharImpl;
    public class RksjLhtjServlet extends HttpServlet {
    public void destroy() {
    super.destroy();
    }public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        this.doPost(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String cyear=request.getParameter("cyear");
    String cmonth=request.getParameter("cmonth");
    String byear=request.getParameter("byear");
    String bmonth=request.getParameter("bmonth");
    String typeid=request.getParameter("typeid");
    System.out.println("cyear  "+cyear+"  "+"cmonth  "+cmonth+"byear  "+byear+"bmonth  "+bmonth); response.setContentType("image/jpeg"); IJfreeChar jfree=new IJfreeCharImpl();
    int count1=jfree.getCountByYearAndMonth(cyear, cmonth);
    int count2=jfree.getCountByYearAndMonth(byear, bmonth);
    if (typeid.equals("1")) {    //typeid为1表示画饼图
    DefaultPieDataset dataset=new  DefaultPieDataset();

    dataset.setValue(cyear+"~"+cmonth+"物品数目",count1);
    dataset.setValue(byear+"~"+bmonth+"物品数目",count2);
     
    JFreeChart chart=ChartFactory.createPieChart("按入库时间统计("+cyear+"~"+cmonth+"和"+byear+"~"+bmonth+")", dataset, true, true, true);
    //通过JFreeChart获得对象
    PiePlot pieplot=(PiePlot)chart.getPlot();
    //当没有数据时显示的内容
    pieplot.setNoDataMessage("无数据可显示");
    //("{0}:({1},{2})")是生成的格式,{0}表示section名,{1}表示section的值,{2}表示百分比。可以自定义。而new DecimalFormat("0.00%")表示小数点后保留两位 
    pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(("{0}:({2})"),NumberFormat.getNumberInstance(),new DecimalFormat("0.0%")));
    ChartUtilities.writeChartAsJPEG(response.getOutputStream(), chart, 650, 400);
      
    }else if(typeid.equals("2")){   //typeid为2表示画柱状图
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    int num=0;
    dataset.addValue(count1, "99999",cyear+"~"+cmonth+"物品数目");
    dataset.addValue(count2, "99999",byear+"~"+bmonth+"物品数目");
            ServletOutputStream out = response.getOutputStream();
    JFreeChart chart = ChartFactory.createBarChart3D("按入库时间统计("+cyear+"~"+cmonth+"和"+byear+"~"+bmonth+")", "物品数据柱状图","纵向值", dataset, PlotOrientation.VERTICAL, false, false, false);
    // 设定plot参数
    CategoryPlot plot = chart.getCategoryPlot();
    BarRenderer3D rend = new BarRenderer3D();
    plot.setNoDataMessage("无数据可显示");
    // 柱子的宽度
    rend.setMaximumBarWidth(0.2);
    // 柱间距
    rend.setItemMargin(0.1);
    rend.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    // 是否在柱状上显示数值
    rend.setItemLabelsVisible(true);
    plot.setRenderer(rend);
    // 柱子的透明度
    plot.setForegroundAlpha(0.9f);
    try {
    ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 300, chart, 600, 350, null);
    } finally {
    try {
    out.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }else if(typeid.equals("3")){   //画折线图
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        int num=0;
        dataset.addValue(count1, "1",cyear+"~"+cmonth+"物品数目");
        dataset.addValue(count2, "1",byear+"~"+bmonth+"物品数目");
        JFreeChart chart=ChartFactory.createLineChart("按入库时间统计("+cyear+"~"+cmonth+"和"+byear+"~"+bmonth+")", "物品数目折线图", "纵向值", dataset,
    PlotOrientation.VERTICAL, true, false,false);   chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
      RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); 
      //设置整个图表背景色
      chart.setBackgroundPaint(Color.pink);
      //设定plot参数 
      CategoryPlot plot=chart.getCategoryPlot(); 
      //设置详细图表的显示细节部分的背景色
      //plot.setBackgroundPaint(Color.red); 
      //设置垂直网格线的颜色
      plot.setDomainGridlinePaint(Color.black); 
      //设置是否显示网格线
      plot.setDomainGridlinesVisible(true);
      //5,设置水平网格线颜色
      plot.setRangeGridlinePaint(Color.blue);
      //6,设置是否显示水平网格线
      plot.setRangeGridlinesVisible(true); 
      
      plot.setNoDataMessage("没有相关统计数据");
      
      try{ 
      ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); 
      ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 100, chart,600, 350, null); 
      }catch(IOException e){ 
      e.printStackTrace(); 
      }
    }

    }
    public void init() throws ServletException {
    // Put your code here
    }}
      

  2.   

    楼上,知道用rcp能否实现动态实时图片更新?
      

  3.   

    我要的是“象限图”。
    一横,一竖把平面分成四块然后有很多的散列点形似:I象限           |              II象限
                    |
                    |
    ----------------|----------------
                    |
                    |
    VI象限          |              III象限
      

  4.   

    在JFreeChart的包里面有些Demo,其中有个叫“散列图”的和那“象限图”个很像。
    贴出来和大家分享一下。
    也希望有更好的解决方法。ScatterPlotDemo2.java
    import java.awt.Dimension;
    import javax.swing.JPanel;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.XYDotRenderer;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;public class ScatterPlotDemo2 extends ApplicationFrame
    {
      public ScatterPlotDemo2(String paramString)
      {
        super(paramString);
        JPanel localJPanel = createDemoPanel();
        localJPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(localJPanel);
      }  private static JFreeChart createChart(XYDataset paramXYDataset)
      {
        JFreeChart localJFreeChart = ChartFactory.createScatterPlot("Scatter Plot Demo 2", "X", "Y", paramXYDataset, PlotOrientation.VERTICAL, true, true, false);
        XYPlot localXYPlot = (XYPlot)localJFreeChart.getPlot();
        localXYPlot.setDomainCrosshairVisible(true);
        localXYPlot.setDomainCrosshairLockedOnData(true);
        localXYPlot.setRangeCrosshairVisible(true);
        localXYPlot.setRangeCrosshairLockedOnData(true);
        localXYPlot.setDomainZeroBaselineVisible(true);
        localXYPlot.setRangeZeroBaselineVisible(true);
        XYDotRenderer localXYDotRenderer = new XYDotRenderer();
        localXYDotRenderer.setDotWidth(2);
        localXYDotRenderer.setDotHeight(2);
        localXYPlot.setRenderer(localXYDotRenderer);
        NumberAxis localNumberAxis = (NumberAxis)localXYPlot.getDomainAxis();
        localNumberAxis.setAutoRangeIncludesZero(false);
        return localJFreeChart;
      }  public static JPanel createDemoPanel()
      {
        JFreeChart localJFreeChart = createChart(new SampleXYDataset2());
        ChartPanel localChartPanel = new ChartPanel(localJFreeChart);
        localChartPanel.setMouseWheelEnabled(true);
        return localChartPanel;
      }  public static void main(String[] paramArrayOfString)
      {
        ScatterPlotDemo2 localScatterPlotDemo2 = new ScatterPlotDemo2("JFreeChart: ScatterPlotDemo2.java");
        localScatterPlotDemo2.pack();
        RefineryUtilities.centerFrameOnScreen(localScatterPlotDemo2);
        localScatterPlotDemo2.setVisible(true);
      }
    }
    SampleXYDataset2.javaimport org.jfree.data.DomainInfo;
    import org.jfree.data.Range;
    import org.jfree.data.RangeInfo;
    import org.jfree.data.xy.AbstractXYDataset;
    import org.jfree.data.xy.XYDataset;public class SampleXYDataset2 extends AbstractXYDataset
      implements XYDataset, DomainInfo, RangeInfo
    {
      private static final int DEFAULT_SERIES_COUNT = 4;
      private static final int DEFAULT_ITEM_COUNT = 40;
      private static final double DEFAULT_RANGE = 200.0D;
      private Double[][] xValues;
      private Double[][] yValues;
      private int seriesCount;
      private int itemCount;
      private Number domainMin;
      private Number domainMax;
      private Number rangeMin;
      private Number rangeMax;
      private Range domainRange;
      private Range range;  public SampleXYDataset2()
      {
        this(2, 1);
      }  public SampleXYDataset2(int paramInt1, int paramInt2)
      {
        this.xValues = new Double[paramInt1][paramInt2];
        this.yValues = new Double[paramInt1][paramInt2];
        this.seriesCount = paramInt1;
        this.itemCount = paramInt2;
        double d1 = (1.0D / 0.0D);
        double d2 = (-1.0D / 0.0D);
        double d3 = (1.0D / 0.0D);
        double d4 = (-1.0D / 0.0D);
        for (int i = 0; i < paramInt1; i++)
          for (int j = 0; j < paramInt2; j++)
          {
            double d5 = (Math.random() - 0.5D) * 200.0D;
            this.xValues[i][j] = new Double(d5);
            if (d5 < d1)
              d1 = d5;
            if (d5 > d2)
              d2 = d5;
            double d6 = (Math.random() + 0.5D) * 6.0D * d5 + d5;
            this.yValues[i][j] = new Double(d6);
            if (d6 < d3)
              d3 = d6;
            if (d6 <= d4)
              continue;
            d4 = d6;
          }
        this.domainMin = new Double(d1);
        this.domainMax = new Double(d2);
        this.domainRange = new Range(d1, d2);
        this.rangeMin = new Double(d3);
        this.rangeMax = new Double(d4);
        this.range = new Range(d3, d4);
      }  public Number getX(int paramInt1, int paramInt2)
      {
        return this.xValues[paramInt1][paramInt2];
      }  public Number getY(int paramInt1, int paramInt2)
      {
        return this.yValues[paramInt1][paramInt2];
      }  public int getSeriesCount()
      {
        return this.seriesCount;
      }  public Comparable getSeriesKey(int paramInt)
      {
        return "例 " + paramInt;
      }  public int getItemCount(int paramInt)
      {
        return this.itemCount;
      }  public double getDomainLowerBound()
      {
        return this.domainMin.doubleValue();
      }  public double getDomainLowerBound(boolean paramBoolean)
      {
        return this.domainMin.doubleValue();
      }  public double getDomainUpperBound()
      {
        return this.domainMax.doubleValue();
      }  public double getDomainUpperBound(boolean paramBoolean)
      {
        return this.domainMax.doubleValue();
      }  public Range getDomainBounds()
      {
        return this.domainRange;
      }  public Range getDomainBounds(boolean paramBoolean)
      {
        return this.domainRange;
      }  public Range getDomainRange()
      {
        return this.domainRange;
      }  public double getRangeLowerBound()
      {
        return this.rangeMin.doubleValue();
      }  public double getRangeLowerBound(boolean paramBoolean)
      {
        return this.rangeMin.doubleValue();
      }  public double getRangeUpperBound()
      {
        return this.rangeMax.doubleValue();
      }  public double getRangeUpperBound(boolean paramBoolean)
      {
        return this.rangeMax.doubleValue();
      }  public Range getRangeBounds(boolean paramBoolean)
      {
        return this.range;
      }  public Range getValueRange()
      {
        return this.range;
      }  public Number getMinimumDomainValue()
      {
        return this.domainMin;
      }  public Number getMaximumDomainValue()
      {
        return this.domainMax;
      }  public Number getMinimumRangeValue()
      {
        return this.domainMin;
      }  public Number getMaximumRangeValue()
      {
        return this.domainMax;
      }
    }
      

  5.   

    是线图吧 ,  我这有jfreechart生成的柱状图,病状图,线形图。。 可以的话我传给你 qq369485270