gpt4 book ai didi

java - 使用 Java 和 mysql 添加回归线

转载 作者:行者123 更新时间:2023-11-29 17:15:54 27 4
gpt4 key购买 nike

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

最佳答案

您可以在以下位置找到回归类: JFree API这基本上就是听起来的样子。

更详细的Example显示在此处

'Adding a Regression line to the Graph':

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

关于java - 使用 Java 和 mysql 添加回归线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51628808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com