Linear fitting with Jfreechart

Source: Internet
Author: User
Tags pack java jframe

Effect drawing source Code reference

For some discrete points, the linear fitting effect diagram of the discrete point is performed using the least squares method.


Source Code

Import Java.awt.Color;
Import Java.io.File;
Import java.io.IOException;

Import java.util.ArrayList;
Import Org.jfree.chart.ChartFactory;
Import Org.jfree.chart.ChartFrame;
Import org.jfree.chart.ChartUtilities;
Import Org.jfree.chart.JFreeChart;
Import Org.jfree.chart.labels.StandardXYToolTipGenerator;
Import Org.jfree.chart.plot.DatasetRenderingOrder;
Import org.jfree.chart.plot.PlotOrientation;
Import Org.jfree.chart.plot.XYPlot;
Import Org.jfree.chart.renderer.xy.XYDotRenderer;
Import Org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

Import Org.jfree.data.xy.DefaultXYDataset;
        public class Lineop {public static void Draw () {//Data I can not divulge, the paper should be used to drop.
        Float[] pmv_x = {/*data*/};
        Float[] pmv_y = {/*data*/};
        Float[] mts_x = {/*data*/};

        Float[] mts_y = {/*data*/};
        arraylist<float> x_pmv = new arraylist<float> ();
        arraylist<float> y_pmv = new arraylist<float> (); arraylist<float> X_mts = newArraylist<float> ();

        arraylist<float> Y_mts = new arraylist<float> ();
                for (int i = 0; i < mts_y.length; i++) {if (mts_y[i]! = 0) {x_mts.add (mts_x[i]);
            Y_mts.add (Mts_y[i]);
                } if (Pmv_y[i]! = 0) {x_pmv.add (pmv_x[i]);
            Y_pmv.add (Pmv_y[i]);
        }} int pmv_point = X_pmv.size ();
        int mts_point = X_mts.size ();
        float SumX = 0;
        float SumX2 = 0;
            for (int i = 0; i < Pmv_point; i++) {SumX + = X_pmv.get (i);
        SumX2 + = (X_pmv.get (i) * X_pmv.get (i));
        } float SumY = 0;
        for (int i = 0; i < Pmv_point; i++) {SumY + = Y_pmv.get (i);
        } float sumxy = 0;
        for (int i = 0; i < Pmv_point; i++) {Sumxy + = (X_pmv.get (i) * Y_pmv.get (i)); } Float b_pmv = ((SumX2 * sumy-sumx * sumxy)/(Pmv_point * SumX2 -SumX * SumX));

        float K_PMV = ((Pmv_point * sumxy-sumx * SumY)/(Pmv_point * sumx2-sumx * SumX));
        Cold sense linear slope, intercept SumX = 0;
        SumX2 = 0;
            for (int i = 0; i < Mts_point; i++) {SumX + = X_mts.get (i);
        SumX2 + = (X_mts.get (i) * X_mts.get (i));
        } SumY = 0;
        for (int i = 0; i < Mts_point; i++) {SumY + = Y_mts.get (i);
        } sumxy = 0;
        for (int i = 0; i < Mts_point; i++) {Sumxy + = (X_mts.get (i) * Y_mts.get (i));
        } Float B_mts = ((SumX2 * sumy-sumx * sumxy)/(Mts_point * sumx2-sumx * SumX));

        float K_mts = ((Mts_point * sumxy-sumx * SumY)/(Mts_point * sumx2-sumx * SumX));
        Construction Diagram double[][] scatterdatas_pmv = new Double[2][pmv_point];
        double[][] linedatas_pmv = new double[2][2]; double[][] Scatterdatas_mts = new Double[2][mts_Point];

        double[][] Linedatas_mts = new double[2][2];
        Defaultxydataset linedataset_pmv = new Defaultxydataset ();
        Defaultxydataset scatterdataset_pmv = new Defaultxydataset ();
        Defaultxydataset Linedataset_mts = new Defaultxydataset ();

        Defaultxydataset Scatterdataset_mts = new Defaultxydataset ();
            for (int i = 0; i < Mts_point; i++) {Scatterdatas_mts[0][i] = X_mts.get (i);
        Scatterdatas_mts[1][i] = Y_mts.get (i);
            } for (int i = 0; i < Pmv_point; i++) {Scatterdatas_pmv[0][i] = X_pmv.get (i);
        Scatterdatas_pmv[1][i] = Y_pmv.get (i);
        } Float linexmax_pmv = x_pmv.get (0);
        float linexmin_pmv = x_pmv.get (0);
        float Linexmax_mts = x_mts.get (0);
        float Linexmin_mts = x_mts.get (0); for (int i = 0; i < Pmv_point; i++) {if (Linexmax_pmv < X_pmv.get (i)) LINEXMAX_PMV = X_
            Pmv.get (i); if (linexmin_pMV > X_pmv.get (i)) linexmin_pmv = X_pmv.get (i);

        } Float lineymax_pmv = linexmax_pmv * k_pmv + b_pmv; for (int i = 0; i < Mts_point; i++) {if (Linexmax_mts < X_mts.get (i)) Linexmax_mts = X_
            Mts.get (i);
        if (Linexmin_mts > X_mts.get (i)) Linexmin_mts = X_mts.get (i);
        } Float Lineymax_mts = Linexmax_mts * K_mts + b_mts;
        Maximum point//calculated minimum float LINEYMIN_PMV = linexmin_pmv * k_pmv + b_pmv;

        float Lineymin_mts = linexmin_mts * K_mts + b_mts;
        Linedatas_pmv[0][0] = LINEXMIN_PMV;
        Linedatas_pmv[1][0] = LINEYMIN_PMV;
        LINEDATAS_PMV[0][1] = LINEXMAX_PMV;

        LINEDATAS_PMV[1][1] = LINEYMAX_PMV;
        Linedatas_mts[0][0] = Linexmin_mts;
        Linedatas_mts[1][0] = Lineymin_mts;
        LINEDATAS_MTS[0][1] = Linexmax_mts;

        LINEDATAS_MTS[1][1] = Lineymax_mts; Load Data Set Scatterdataset_pmv.addseries ("AAAAA", SCATTERDATAS_PMV);
        Scatterdataset_mts.addseries ("bbbbb", SCATTERDATAS_MTS);
        Linedataset_pmv.addseries ("Linear (AAAAA)", LINEDATAS_PMV);

        Linedataset_mts.addseries ("Linear (AAAAA)", LINEDATAS_MTS); PMV Jfreechart chart = chartfactory.createscatterplot ("Ababababab", "QQQQQQQQQ", "AAAAA", Scatt
        ERDATASET_PMV, Plotorientation.vertical, True, false, false);
        Xyplot Xyplot = Chart.getxyplot ();//diagram itself Xyplot.setdataset (1, SCATTERDATASET_PMV);
        Xyplot.setdataset (2, LINEDATASET_PMV);//Line chart Data//Plot.getrenderer (). Setseriespaint (0, Color.Black);
        Xydotrenderer xydotrenderer1 = new Xydotrenderer ();
        Xydotrenderer1. Setbasetooltipgenerator (New Standardxytooltipgenerator ());
        Xyplot.setrenderer (1, xydotrenderer1);
        Xylineandshaperenderer xylineandshaperenderer1 = new Xylineandshaperenderer (); Xylineandshaperenderer1. SetbasetooltIpgenerator (New Standardxytooltipgenerator ());
        Xylineandshaperenderer1.setseriespaint (0, Color.Blue);
        Xyplot.getrenderer (0). Setseriespaint (0, Color.Blue);//Set point color Xyplot.setrenderer (2, Xylineandshaperenderer1);

        Xyplot.setdatasetrenderingorder (Datasetrenderingorder.forward);
        Chartframe chartframe = new Chartframe ("AAAAA", chart);
        The chart is placed in the Java container component and Chartframe inherits from the Java JFrame class.
        The data for the first parameter is placed in the upper-left corner of the window, not in the middle of the title. Chartframe.pack (); Display graphics Chartframe.setvisible (true) at the appropriate size;//graphics visible//Based on MTS drawing Jfreechart Chartmts = Chartfactory . Createscatterplot ("Ababababab", "QQQQQQQQQ", "bbbbb", Scatterdataset_mts, Plotorientatio
        N.vertical, True, false, false);
        Xyplot Xyplotmts = Chartmts.getxyplot ();//diagram itself Xyplotmts.setdataset (1, SCATTERDATASET_MTS);

        Xyplotmts.setdataset (2, LINEDATASET_MTS);
     Xydotrenderer xydotrenderermts1 = new Xydotrenderer ();   Xydotrenderermts1. Setbasetooltipgenerator (New Standardxytooltipgenerator ());
        Xyplotmts.setrenderer (1, xydotrenderermts1);
        Xylineandshaperenderer xylineandshaperenderermts1 = new Xylineandshaperenderer ();
        Xylineandshaperenderermts1. Setbasetooltipgenerator (New Standardxytooltipgenerator ());
        Xylineandshaperenderermts1.setseriespaint (0, color.red);

        Xyplotmts.setrenderer (2, xylineandshaperenderermts1);
        Xyplotmts.setdatasetrenderingorder (Datasetrenderingorder.forward);
        Chartframe Chartframemts = new Chartframe ("bbbbb", CHARTMTS);
        The chart is placed in the Java container component and Chartframe inherits from the Java JFrame class.
        The data for the first parameter is placed in the upper-left corner of the window, not in the middle of the title. Chartframemts.pack (); Display the graphic chartframemts.setvisible (true) at the appropriate size;//Whether the graphic is visible try {chartutilities.savechartaspng (NE
            W File ("E:/chart/chart1.png"), chart, 800, 500); Chartutilities.savechartaspng (The New File ("E:/chart/chart2.png "), Chartmts, 800, 500);
        } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }
    }
}
References

Using Jfreechart to draw smooth curve (b) Simple realization
Jfreechart setting the color of a point
Jfree API
Jfreechart Line chart polyline size, color settings, icon size settings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.