【问题标题】:Add Regression Line using Java and mysql [duplicate]使用Java和mysql添加回归线[重复]
【发布时间】:2018-08-01 08:36:27
【问题描述】:

我是 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();
}

【问题讨论】:

    标签: java mysql linear-regression jfreechart


    【解决方案1】:

    您可以在 JFree API 基本上就是它听起来的样子。

    更详细的Example 显示在下面

    '向图表添加回归线':

    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);
    }
    

    【讨论】:

    • 感谢您提供此链接,它可能会提供一些有限的即时帮助。一个答案should include sufficient context around the link,这样您的其他用户就会知道它是什么以及它为什么存在。始终引用重要链接中最相关的部分,以使其对有其他类似问题的未来读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
    猜你喜欢
    • 2013-11-11
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 2016-02-24
    • 2013-03-16
    相关资源
    最近更新 更多