<form method="post" action="">
<input type="hidden" name="x" id="hiddenX" value="500">
<input type="hidden" name="y" id="hiddenY" value="699">
<input type="hidden" name="age" id="hiddenAge" value="">
<button type="submit" name="submit">Calculate</button>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$x = $_POST['x'];
$y = $_POST['y'];
$age = $_POST['age'];
$xx = number_format($x);
$yy = ($y * 1);
$yy1 = ($y * 3);
$yy3 = ($y * 2);
$yy6 = ($y * 4);
echo "<p>Selected age: $age</p>";
$initial_yy_value = 1000;
$initial_yy1_value = 1000;
$initial_value = 1000;
$xx_increase_per_year = $xx;
$yy_increase_per_year = $yy;
$yy1_increase_per_year = $yy1;
// Set up data arrays
$labels = [];
$xx_data = [];
$yy_data = [];
$yy1_data = [];
// Generate data for chart
$current_age = $age;
$max_age = min($age + 20, 100);
while ($current_age <= $max_age) {
$labels[] = $current_age;
$xx_data[] = $initial_value;
$initial_value += $xx_increase_per_year;
$yy_data[] = $current_age <= 65 ? $yy_increase_per_year : 0;
$initial_yy1_value += $yy1_increase_per_year;
$yy1_data[] = $initial_yy1_value;
$current_age++;
}
// Set up data object for chart.js
$data = [
'labels' => $labels,
'datasets' => [
[
'label' => '$xx',
'data' => $xx_data,
'backgroundColor' => 'rgba(255, 99, 132, 0.2)',
'borderColor' => 'rgba(255, 99, 132, 1)',
'borderWidth' => 1
],
[
'label' => '$yy',
'data' => $yy_data,
'backgroundColor' => 'rgba(54, 162, 235, 0.2)',
'borderColor' => 'rgba(54, 162, 235, 1)',
'borderWidth' => 1
],
[
'label' => '$yy1',
'data' => $yy1_data,
'backgroundColor' => 'rgba(255, 206, 86, 0.2)',
'borderColor' => 'rgba(255, 206, 86, 1)',
'borderWidth' => 1
]
]
];
$data_json = json_encode($data);
}
?>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<canvas id="myChart"></canvas>
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'line',
data: <?php echo $data_json; ?>,
options: {}
});
</script>
แบบ ใส่ค่าก่อน
<form method="post" action="">
<input type="text" name="x" id="inputX" value="">
<input type="text" name="y" id="inputY" value="">
<input type="text" name="age" id="inputAge" value="">
<button type="submit" name="submit">Calculate</button>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$x = $_POST['x'];
$y = $_POST['y'];
$age = $_POST['age'];
$xx = number_format($x);
$yy = ($y * 1);
$yy1 = ($y * 3);
$yy3 = ($y * 2);
$yy6 = ($y * 4);
echo "<p>Selected age: $age</p>";
$initial_yy_value = 1000;
$initial_yy1_value = 1000;
$initial_value = 1000;
$xx_increase_per_year = $xx;
$yy_increase_per_year = $yy;
$yy1_increase_per_year = $yy1;
// Set up data arrays
$labels = [];
$xx_data = [];
$yy_data = [];
$yy1_data = [];
// Generate data for chart
$current_age = $age;
$max_age = min($age + 20, 100);
while ($current_age <= $max_age) {
$labels[] = $current_age;
$xx_data[] = $initial_value;
$initial_value += $xx_increase_per_year;
$yy_data[] = $current_age <= 65 ? $yy_increase_per_year : 0;
$initial_yy1_value += $yy1_increase_per_year;
$yy1_data[] = $initial_yy1_value;
$current_age++;
}
// Set up data object for chart.js
$data = [
'labels' => $labels,
'datasets' => [
[
'label' => '$xx',
'data' => $xx_data,
'backgroundColor' => 'rgba(255, 99, 132, 0.2)',
'borderColor' => 'rgba(255, 99, 132, 1)',
'borderWidth' => 1
],
[
'label' => '$yy',
'data' => $yy_data,
'backgroundColor' => 'rgba(54, 162, 235, 0.2)',
'borderColor' => 'rgba(54, 162, 235, 1)',
'borderWidth' => 1
],
[
'label' => '$yy1',
'data' => $yy1_data,
'backgroundColor' => 'rgba(255, 206, 86, 0.2)',
'borderColor' => 'rgba(255, 206, 86, 1)',
'borderWidth' => 1
]
]
];
$data_json = json_encode($data);
}
?>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<canvas id="myChart"></canvas>
<script>
var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
type: 'line',
data: <?php echo $data_json; ?>,
options: {}
});
</script>