<!DOCTYPE html>
<html>
<head>
<title>Line Chart Example</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<canvas id="myChart"></canvas>
<?php
// Define variables for chart data
$start_age = 20;
$initial_value = 1000;
$yy = 100;
$yy1 = 400;
$yy3 = 50;
// Generate data for chart
$labels = [];
$xx_values = [];
$yy_values = [];
$yy1_values = [];
$yy3_values = [];
$current_age = $start_age;
$current_value = $initial_value;
while ($current_age < 100) {
$labels[] = $current_age;
$xx_values[] = $current_value;
$yy_values[] = $yy;
$yy1_values[] = $yy1;
$yy3_values[] = $yy3;
$current_age += 1;
$current_value += $yy;
if ($current_age % 240 == 0) {
$current_value += $yy1;
}
if ($current_age % 60 == 0) {
$current_value += $yy3;
}
}
// Create chart
echo '<script>';
echo 'var ctx = document.getElementById("myChart").getContext("2d");';
echo 'var myChart = new Chart(ctx, {';
echo ' type: "line",';
echo ' data: {';
echo ' labels: ' . json_encode($labels) . ',';
echo ' datasets: [';
echo ' {';
echo ' label: "$xx",';
echo ' data: ' . json_encode($xx_values) . ',';
echo ' backgroundColor: "rgba(255, 99, 132, 0.2)",';
echo ' borderColor: "rgba(255, 99, 132, 1)",';
echo ' borderWidth: 1';
echo ' },';
echo ' {';
echo ' label: "$yy",';
echo ' data: ' . json_encode($yy_values) . ',';
echo ' backgroundColor: "rgba(54, 162, 235, 0.2)",';
echo ' borderColor: "rgba(54, 162, 235, 1)",';
echo ' borderWidth: 1';
echo ' },';
echo ' {';
echo ' label: "$yy1",';
echo ' data: ' . json_encode($yy1_values) . ',';
echo ' backgroundColor: "rgba(255, 206, 86, 0.2)",';
echo ' borderColor: "rgba(255, 206, 86, 1)",';
echo ' borderWidth: 1';
echo ' },';
echo ' {';
echo ' label: "$yy3",';
echo ' data: ' . json_encode($yy3_values) . ',';
echo ' backgroundColor: "rgba(75, 192, 192, 0.2)",';
echo ' borderColor: "rgba(75, 192, 192, 1)",';
echo ' borderWidth: 1';
echo ' }';
echo ' ]';
echo ' },';
echo ' options: {';
echo ' scales: {';
echo ' yAxes: [{';
echo ' ticks: {';
echo ' beginAtZero: true';
echo ' }';
echo ' }]';
echo ' }';
echo ' }';
echo '});';
echo '</script>';
?>
</body>
</html>
<?php
// Set input parameters
$initial_value = 500;
$age = 25;
$xx_increase_per_year = 50;
$yy_increase_per_year = 10;
$yy1_increase_per_year = 20;
$yy3_increase_per_year = 30;
// Set up data arrays
$labels = [];
$xx_data = [];
$yy_data = [];
$yy1_data = [];
$yy3_data = [];
// Generate data for chart
$current_age = $age;
while ($current_age <= 85) {
$labels[] = $current_age;
$xx_data[] = $initial_value;
$yy_data[] = $current_age <= 65 ? $yy_increase_per_year : 0;
$yy1_data[] = $current_age % 240 == 0 ? $yy1_increase_per_year : 0;
$yy3_data[] = $current_age % 60 == 0 ? $yy3_increase_per_year : 0;
$initial_value += $xx_increase_per_year;
$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
],
[
'label' => '$yy3',
'data' => $yy3_data,
'backgroundColor' => 'rgba(75, 192, 192, 0.2)',
'borderColor' => 'rgba(75, 192, 192, 1)',
'borderWidth' => 1
]
]
];
// Encode data as JSON
$data_json = json_encode($data);
// Output chart HTML and JavaScript
?>
<!DOCTYPE html>
<html>
<head>
<title>Line Chart Example</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<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>
</body>
</html>