请问哪个高手会JFreeChart插件制作散点图,请帮帮忙,现在急用,请给个小例子或者给解释一下都可以,请高手指点一下,谢谢……

解决方案 »

  1.   


    我这有个 可参考下。package com;import org.jfree.ui.ApplicationFrame;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.text.NumberFormat;
    import javax.swing.JPanel;
    import org.jfree.chart.*;
    import org.jfree.chart.axis.*;
    import org.jfree.chart.plot.*;
    import org.jfree.chart.renderer.category.LineAndShapeRenderer;
    import org.jfree.chart.title.TextTitle;
    import org.jfree.data.DataUtilities;
    import org.jfree.data.DefaultKeyedValues;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.general.DatasetUtilities;
    import org.jfree.ui.RefineryUtilities;
    import org.jfree.util.SortOrder;public class ParetoChartDemo1 extends ApplicationFrame
    {    public ParetoChartDemo1(String s)
        {
            super(s);
            JPanel jpanel = createDemoPanel();
            jpanel.setPreferredSize(new Dimension(550, 270));
            setContentPane(jpanel);
        }    public static JFreeChart createChart(CategoryDataset acategorydataset[])
        {
            JFreeChart jfreechart = ChartFactory.createBarChart("Freshmeat Software Projects", "Language", "Projects", acategorydataset[0], PlotOrientation.VERTICAL, true, true, false);
            jfreechart.addSubtitle(new TextTitle("By Programming Language"));
            jfreechart.addSubtitle(new TextTitle("As at 5 March 2003"));
            jfreechart.setBackgroundPaint(Color.white);
            CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
            categoryplot.setBackgroundPaint(Color.lightGray);
            categoryplot.setRangeGridlinePaint(Color.white);
            CategoryAxis categoryaxis = categoryplot.getDomainAxis();
            categoryaxis.setLowerMargin(0.02D);
            categoryaxis.setUpperMargin(0.02D);
            categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);
            NumberAxis numberaxis = (NumberAxis)categoryplot.getRangeAxis();
            numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            LineAndShapeRenderer lineandshaperenderer = new LineAndShapeRenderer();
            NumberAxis numberaxis1 = new NumberAxis("Percent");
            numberaxis1.setNumberFormatOverride(NumberFormat.getPercentInstance());
            categoryplot.setRangeAxis(1, numberaxis1);
            categoryplot.setDataset(1, acategorydataset[1]);
            categoryplot.setRenderer(1, lineandshaperenderer);
            categoryplot.mapDatasetToRangeAxis(1, 1);
            categoryplot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
            return jfreechart;
        }    public static CategoryDataset[] createDatasets()
        {
            DefaultKeyedValues defaultkeyedvalues = new DefaultKeyedValues();
            defaultkeyedvalues.addValue("C", new Integer(4843));
            defaultkeyedvalues.addValue("C++", new Integer(2098));
            defaultkeyedvalues.addValue("C#", new Integer(26));
            defaultkeyedvalues.addValue("Java", new Integer(1901));
            defaultkeyedvalues.addValue("Perl", new Integer(2507));
            defaultkeyedvalues.addValue("PHP", new Integer(1689));
            defaultkeyedvalues.addValue("Python", new Integer(948));
            defaultkeyedvalues.addValue("Ruby", new Integer(100));
            defaultkeyedvalues.addValue("SQL", new Integer(263));
            defaultkeyedvalues.addValue("Unix Shell", new Integer(485));
            defaultkeyedvalues.sortByValues(SortOrder.DESCENDING);
            org.jfree.data.KeyedValues keyedvalues = DataUtilities.getCumulativePercentages(defaultkeyedvalues);
            CategoryDataset categorydataset = DatasetUtilities.createCategoryDataset("Languages", defaultkeyedvalues);
            CategoryDataset categorydataset1 = DatasetUtilities.createCategoryDataset("Cumulative", keyedvalues);
            return (new CategoryDataset[] {
                categorydataset, categorydataset1
            });
        }    public static JPanel createDemoPanel()
        {
            CategoryDataset acategorydataset[] = createDatasets();
            JFreeChart jfreechart = createChart(acategorydataset);
            return new ChartPanel(jfreechart);
        }    public static void main(String args[])
        {
            ParetoChartDemo1 paretochartdemo1 = new ParetoChartDemo1("Pareto Chart Demo 1");
            paretochartdemo1.pack();
            RefineryUtilities.centerFrameOnScreen(paretochartdemo1);
            paretochartdemo1.setVisible(true);
        }
    }
      

  2.   

    http://www.javaeye.com/topic/544104
      

  3.   

    这是快速散点图,float的data存储数据。定义两个轴为XY。调用时传入标题,X轴上的数据,Y轴上的数据。package analysis;import org.jfree.chart.ChartFrame;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.DateAxis;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.plot.FastScatterPlot;
    import org.jfree.ui.RefineryUtilities;    public class ScatterPlot
        {
        public static void data(String title,String[] a,String[] b)
        {
        float[][] data=new float[2][a.length];
        for(int i=0;i<a.length;i++)
        {
         data[0][i]=Float.parseFloat(a[i]);
         data[1][i]=Float.parseFloat(b[i]);
        }
            final DateAxis  domainAxis = new DateAxis("X");
            //domainAxis.setAutoRangeIncludesZero(false);        final NumberAxis rangeAxis = new NumberAxis("Y");
            rangeAxis.setAutoRangeIncludesZero(false);
            final FastScatterPlot plot = new FastScatterPlot(data, domainAxis, rangeAxis);
           // FastScatterPlot plot1=ChartFactory.createScatterPlot(title, title, title, dataset, orientation, legend, tooltips, urls);        final JFreeChart chart = new JFreeChart(title, plot);
            ChartFrame frame = new ChartFrame(title,chart);
            frame.pack();
            RefineryUtilities.centerFrameOnScreen(frame);
            frame.setVisible(true);
        }    }
      

  4.   

    java代码
    /**
     * 
     * @param xydatalist
     * @param bloods
     * @return
     */
    public static XYDataset createxydataset(List<PressureBean> xydatalist,
    String bloods) {
    DefaultXYDataset xydataset = new DefaultXYDataset(); int size = xydatalist.size();
    double[][] datas = new double[2][size];
    for (int i = 0; i < size; i++) {
    PressureBean pres = xydatalist.get(i);
    int sys = pres.getSyspress();//收缩压
    int dia = pres.getDiapress();//舒张压 datas[0][i] = sys;
    datas[1][i] = dia;
    } xydataset.addSeries(bloods, datas); return xydataset; }public static JFreeChart createChart(XYDataset xydataset,
    String bloodcattile, String shou, String shu, String nobloodData,
    String bloods, String nomal, String fore, String one, String two,
    List<PressureBean> list, Log log) { // 有可能用户在后面的版本中故意输入不正常数值,但是为了保证图片画图的完整,这里先计算
    // 用户血压值的最大值。
    int maxpress = 160;
    int addmax = 20; if (list != null && list.size() > 0) { Iterator<PressureBean> it = list.iterator();
    while (it.hasNext()) {
    PressureBean pres = it.next();

    if (maxpress < pres.getDiapress()) {
    maxpress = pres.getDiapress();
    } if (maxpress < pres.getSyspress()) {
    maxpress = pres.getSyspress();
    }
    } maxpress += addmax;
    log.info("high press value is =" + maxpress); } JFreeChart jfreechart = ChartFactory.createScatterPlot(bloodcattile,
    shou, shu, xydataset, PlotOrientation.VERTICAL, true, false,
    false);
    jfreechart.setBackgroundPaint(Color.white);
    jfreechart.setBorderPaint(Color.GREEN);
    jfreechart.setBorderStroke(new BasicStroke(1.5f));
    XYPlot xyplot = (XYPlot) jfreechart.getPlot();
    xyplot.setNoDataMessage(nobloodData);
    xyplot.setNoDataMessageFont(new Font("", Font.BOLD, 14));
    xyplot.setNoDataMessagePaint(new Color(87, 149, 117)); xyplot.setBackgroundPaint(new Color(255, 253, 246));
    ValueAxis vaaxis = xyplot.getDomainAxis();
    vaaxis.setAxisLineStroke(new BasicStroke(1.5f)); ValueAxis va = xyplot.getDomainAxis(0);
    va.setAxisLineStroke(new BasicStroke(1.5f)); va.setAxisLineStroke(new BasicStroke(1.5f)); // 坐标轴粗细
    va.setAxisLinePaint(new Color(215, 215, 215)); // 坐标轴颜色
    xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 边框粗细
    va.setLabelPaint(new Color(10, 10, 10)); // 坐标轴标题颜色
    va.setTickLabelPaint(new Color(102, 102, 102)); // 坐标轴标尺值颜色
    ValueAxis axis = xyplot.getRangeAxis();
    axis.setAxisLineStroke(new BasicStroke(1.5f)); XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot
    .getRenderer();
    xylineandshaperenderer.setSeriesOutlinePaint(0, Color.WHITE);
    xylineandshaperenderer.setUseOutlinePaint(true);
    NumberAxis numberaxis = (NumberAxis) xyplot.getDomainAxis();
    numberaxis.setAutoRangeIncludesZero(false);
    numberaxis.setTickMarkInsideLength(2.0F);
    numberaxis.setTickMarkOutsideLength(0.0F);
    numberaxis.setAxisLineStroke(new BasicStroke(1.5f));
    numberaxis.setUpperBound(maxpress);
    numberaxis.setLowerBound(60);//最小值设置为60
    NumberAxis numberaxis1 = (NumberAxis) xyplot.getRangeAxis();
    numberaxis1.setTickMarkInsideLength(2.0F);
    numberaxis1.setTickMarkOutsideLength(0.0F);
    numberaxis1.setUpperBound(105d);
    numberaxis1.setLowerBound(35);
    numberaxis1.setAxisLineStroke(new BasicStroke(1.5f)); // if (xydataset != null) {
    XYBoxAnnotation box = new XYBoxAnnotation(0, 0, 89, 59); //正常血压所在区域内边界
    XYBoxAnnotation box1 = new XYBoxAnnotation(0, 0, 119, 79);//高血压前期所在区域内边界
    XYBoxAnnotation box2 = new XYBoxAnnotation(0, 0, 139, 89);//高血压一期所在区域内边界
    XYBoxAnnotation box3 = new XYBoxAnnotation(0, 0, 159, 99);//高血压二期所在区域内边界
    XYTextAnnotation text1 = new XYTextAnnotation(nomal, 70, 62.5);//标识“正常”
    XYTextAnnotation text = new XYTextAnnotation(fore, 70, 82.5);//“高血压前期”
    XYTextAnnotation text2 = new XYTextAnnotation(one, 70, 91.5);//“高血压一期”
    XYTextAnnotation text3 = new XYTextAnnotation(two, 70, 101.5);//“高血压二期”
    //将上面的边界线条,说明文字加入到xyplot中。
    xyplot.addAnnotation(box);
    xyplot.addAnnotation(box1);
    xyplot.addAnnotation(box2);
    xyplot.addAnnotation(box3); xyplot.addAnnotation(text);
    xyplot.addAnnotation(text1);
    xyplot.addAnnotation(text2);
    xyplot.addAnnotation(text3);
    // }
    return jfreechart;
    }
    public static void drawScatterChart(IrisIoInterface io, Log log,
    XYDataset xydataSet, String title, String shou, String shu,
    String nodata, String boolds, String nomal, String fore,
    String one, String two, List<PressureBean> list) { JFreeChart chart = createChart(xydataSet, title, shou, shu, nodata,
    boolds, nomal, fore, one, two, list, log); HttpServletRequest request = io.getRequest();
    String filename = "";
    String graphURL = "";
    try {
    filename = ServletUtilities.saveChartAsPNG(chart, 400, 300, null,
    io.getSession());
    graphURL = request.getContextPath() + "/displayChart?filename="
    + filename;
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    log.error(e);
    } io.setData("filename", filename, BeanShare.BEAN_SHARE_REQUEST);
    io.setData("scatterurl", graphURL, BeanShare.BEAN_SHARE_REQUEST); }