This chart combines two scatter charts, where the first chart's data is fetched from a file and created by function "rgraph_chart::createScatterData".
Data for the second scatter chart is built by php-RChart's utility function "calculate_Regression". This function does a basic regression analysis and may use a user provided regression formula to do the calculation. Together with this regression formula also the calculated coefficient of determination as "R" is displayed. The coefficient of determination is the proportion of variability in the data that is accounted for by the calculated formula and it will range from "0" (no correlation) to "1" (perfect correlation).
<?php

include_once "environment.php";
use phpRGraph\rgraph_chart;
use phpRGraph\rgraph_options;
include_once $utility;

// rgraph charts
$template = "combine_two.php";
$id = "cvs";
$draw_option = "draw();";
$width = "600";
$height = "350";

$data = rgraph_chart::getDataFromFile("scatterdata1.json");
$x_values = $data["x_values"];
$y_values = $data["y_values"];

$chart1 = new rgraph_chart("cvs", null, "Scatter");
$data = $chart1->createScatterData($x_values, $y_values);
$chart1->set_data($data);

$options1 = new rgraph_options("default.ini");
$options1->set_option("marginLeft", 70);
$options1->set_option("xaxisLabels", $options1->xlabelsteps($x_values, 9));
$options1->set_option("backgroundGridAutofit", false);
$options1->set_option("xaxisScaleMax", 100);
$options1->set_option("xaxisTickmarksCount", 20);
$options1->set_option("xaxisTitle","Spendings");
$options1->set_option("yaxisTitle","Earnings");
$options1->set_option("yaxisTitlePos",0.20);

$chart1->set_options($options1);
$chart1_json = $chart1->toString();

$event1_script = "";
$chart1_script = "";

// chart2 caculated Regression line with own formula
$chart2 = new rgraph_chart("cvs", null, "Scatter");

$result = calculate_Regression($x_values, $y_values, "y=-0.8*x^3+100*x^2", "en", 3);
$data = $result['values'];
$formula = $result['formula'];
$chart2->set_data($data);

$options2 = new rgraph_options("default.ini");
$options2->set_option("marginLeft", 70);
$options2->set_option("title", "Law of deminishing Returns\n(Regression: $formula)\n\n");
$options2->set_option("xaxisScaleMax", 100);
$options2->set_option("line",true);
$options2->set_option("lineLinewidth", 1.5);
$options2->set_option("backgroundGrid", false);
$options2->set_option("ylabels", false);
$options2->set_option("yaxisLabelsCount", 0);
$options2->set_option("xaxis", false);
$options2->set_option("yaxis", false);
$options2->set_option("yaxisTickmarks",false);

$chart2_json = $chart2->toString();

$event2_script = "";
$chart2_script = "";

include_once ($templates . $template);

?>