0

I'm a beginner in Java. I would like to ask if is it possible to add a regression line in line chart using java and mysql? anyway this is my code:

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();
}
Ashot Karakhanyan
  • 2,804
  • 3
  • 23
  • 28

1 Answers1

0

You can find a Regression Class in the JFree API which basically does what it sounds like.

A more detailed Example is shown here under

'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);
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
r0qs3t
  • 1
  • 2
  • 1
    Thank you for this link, which might provide some limited, immediate help. An answer [should include sufficient context around the link](https://meta.stackexchange.com/q/225370/206345) so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, to make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Tim Diekmann Aug 01 '18 at 09:07