使用java和mysql添加回归线

yrdbyhpb  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(363)

这个问题在这里已经有答案了

通过不同的y截距和x值绘制多元回归线(1个答案)
如何从java中的折线图中获取值(1个答案)
两年前关门了。
我是java的初学者。我想问一下,是否可以使用java和mysql在折线图中添加回归线?不管怎样,这是我的代码:

try {

    String sql = "select YEAR(admission.admissiondate) as YEAR, count(admissionID) as StudNum from admission group by YEAR(admissiondate)";
    JDBCXYDataset dataset = new JDBCXYDataset(
            "jdbc:mysql://localhost/zoom", "com.mysql.jdbc.Driver", "root", "");
    dataset.executeQuery(sql);

    final JFreeChart chart = ChartFactory.createXYLineChart ("Number of Students Per Year","YEAR", "Number of Students",
            dataset,PlotOrientation.VERTICAL,true,true,false);
    XYPlot plot =null;
    ChartFrame frame = new ChartFrame("cchart", chart);
    frame.setVisible(true);
    frame.setSize(500, 500);

    chart.setBackgroundPaint(Color.white);

    XYPlot xyPlot = chart.getXYPlot();
    NumberAxis domainAxis = (NumberAxis) xyPlot.getDomainAxis();
    domainAxis.setTickUnit(new NumberTickUnit(1.0));
    domainAxis.setRange(2016,2030);
} catch(Exception e) {
    e.printStackTrace();
}
iyzzxitl

iyzzxitl1#

您可以在jfreeapi中找到一个回归类,它基本上就是这样做的。
下面显示了一个更详细的示例
'向图形添加回归线':

private void drawRegressionLine() {
    // Get the parameters 'a' and 'b' for an equation y = a + b * x,
    // fitted to the inputData using ordinary least squares regression.
    // a - regressionParameters[0], b - regressionParameters[1]
    double regressionParameters[] = Regression.getOLSRegression(inputData,
            0);

    // Prepare a line function using the found parameters
    LineFunction2D linefunction2d = new LineFunction2D(
            regressionParameters[0], regressionParameters[1]);

    // Creates a dataset by taking sample values from the line function
    XYDataset dataset = DatasetUtilities.sampleFunction2D(linefunction2d,
            0D, 300, 100, "Fitted Regression Line");

    // Draw the line dataset
    XYPlot xyplot = chart.getXYPlot();
    xyplot.setDataset(1, dataset);
    XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer(
            true, false);
    xylineandshaperenderer.setSeriesPaint(0, Color.YELLOW);
    xyplot.setRenderer(1, xylineandshaperenderer);
}

相关问题