connect_error) {
imageError("Connection failed: " . $conn->connect_error);
exit;
}
// Get vars einlesen
$entity = "DE";
if (isset($_GET['entity']) && strlen($_GET['entity'])> 0)
$entity = $conn->real_escape_string($_GET['entity']);
if ($entity == 'main-tauber-kreis')
$entity = 'DE:BW:TBB';
elseif ($entity == 'wuerzburg')
$entity = 'DE:SONDER:WUE';
elseif ($entity == 'stadt-wuerzburg')
$entity = 'DE:BY:WUE:SK';
elseif ($entity == 'landkreis-wuerzburg')
$entity = 'DE:BY:WUE:LK';
elseif ($entity == 'baden-württemberg')
$entity = 'DE:BW';
elseif ($entity == 'bayern')
$entity = 'DE:BY';
elseif ($entity == 'nrw')
$entity = 'DE:NRW';
elseif ($entity == 'deutschland')
$entity = 'DE';
$entity2 = "";
if (isset($_GET['entity2']) && strlen($_GET['entity2'])> 0)
$entity2 = $conn->real_escape_string($_GET['entity2']);
$entity3 = "";
if (isset($_GET['entity3']) && strlen($_GET['entity3'])> 0)
$entity3 = $conn->real_escape_string($_GET['entity3']);
$entity4 = "";
if (isset($_GET['entity4']) && strlen($_GET['entity4'])> 0)
$entity4 = $conn->real_escape_string($_GET['entity4']);
$entity5 = "";
if (isset($_GET['entity5']) && strlen($_GET['entity5'])> 0)
$entity5 = $conn->real_escape_string($_GET['entity5']);
$entity6 = "";
if (isset($_GET['entity6']) && strlen($_GET['entity6'])> 0)
$entity6 = $conn->real_escape_string($_GET['entity6']);
$plotline = 0;
if (isset($_GET['plotline']) && strlen($_GET['plotline'])> 0)
$plotline = intval($_GET['plotline']);
$width = 1200;
if (isset($_GET['width']) && strlen($_GET['width'])> 0)
$width = intval($_GET['width']);
$height = 628;
if (isset($_GET['height']) && strlen($_GET['height'])> 0)
$height = intval($_GET['height']);
$display_mode = "avg";
if (isset($_GET['mode']) && strlen($_GET['mode'])> 0)
$display_mode = $_GET['mode'];
$daily_data = 0;
if (isset($_GET['daily']) && strlen($_GET['daily'])> 0)
$daily_data = $_GET['daily'];
if (strval($daily_data) == 'mit') {
$daily_data = 1;
} elseif (strval($daily_data) == 'ohne')
$daily_data = 0;
$nbr_days = 10000; /* ~30 yrs, hopefully sufficient for 'all time' */
if (isset($_GET['nbrdays']) && strlen($_GET['nbrdays'])> 0)
$nbr_days = $_GET['nbrdays'];
if ($nbr_days == "alle")
$nbr_days = 10000;
$dateTo = "NOW()";
$dateTo_cache = time();
if (isset($_GET['dateto']) && strlen($_GET['dateto'])> 0) {
$dateTo = "'" . $conn->real_escape_string($_GET['dateto']) . "'";
$dateTo_cache = strtotime($_GET['dateto']);
}
$dateFrom = "2020-01-01";
if (isset($_GET['datefrom']) && strlen($_GET['datefrom'])> 0)
$dateFrom = "'" . $conn->real_escape_string($_GET['datefrom']) . "'";
$dbManuell = 0;
if (isset($_GET['manuell']) && strlen($_GET['manuell'])> 0)
$dbManuell = intval($_GET['manuell']);
$colorDaily = "#BBBBBE"; # note: this is URLencoded %23BBBBBE
if (isset($_GET['colordaily']) && strlen($_GET['colordaily'])> 0)
$colorDaily = $_GET['colordaily'];
$colorEntity = "#0000ff";
if (isset($_GET['colorentity']) && strlen($_GET['colorentity'])> 0)
$colorEntity = $_GET['colorentity'];
$scale = "line-linear"; // "line-log";
if (isset($_GET['scale']))
$scale = $conn->real_escape_string($_GET['scale']);
// --- SET CACHING --- we need params!
//echo "Dateto cache $dateTo_cache, now: " . time();exit;
$seconds_to_cache = 3600;
//echo "Dateto cache $dateTo_cache, now: " . time();echo $dateTo_cache .'
'; echo time() .'
';echo $dateTo_cache + (3 * 86400) .'
'; exit;
if ($dateTo_cache + (3 * 86400) < time()) { // actually 2 days, $dateTo_cache is midnight!
$seconds_to_cache = 30 * 86400; // 30 days
} else {
if(substr($entity, 0, 3) == "VAR") {
$seconds_to_cache = 3600;
} else {
$curr_time = localtime(time(), true);
if($dbManuell == 0) {
if($curr_time['tm_hour'] > 10)
$seconds_to_cache = 3600 * (23 - $curr_time['tm_hour'] + 6);
} else {
if($curr_time['tm_hour'] < 15)
$seconds_to_cache = 3600 * (15 - $curr_time['tm_hour']);
else if($curr_time['tm_hour'] > 18)
$seconds_to_cache = 3600 * (23 - $curr_time['tm_hour'] + 14);
else
$seconds_to_cache = 5*60;
}
}
}
//echo "seconds to cache: $seconds_to_cache"; exit;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");
// ----
// Control Chart stuff
$chartlabelmax = $width / 40;
$chartlinewidth = 3;
if (substr($entity, 3) == 'RKI' || substr($entity2, 3) == 'RKI'
|| substr($entity3, 3) == 'RKI' || substr($entity4, 3) == 'RKI')
$have_RKI = true;
else
$have_RKI = false;
//
$country_clr = "#0000FF";
// Init Helper arrays
$data_confirmed = array();
$data_deaths = array();
$data_recovered = array();
$data_date = array();
$data4 = array();
// Calc Plot values
calcPlotValues();
$rowcount = count($data_date);
$labelsplit = ($rowcount > $chartlabelmax ? $rowcount / $chartlabelmax : 1);
// Setup the graph
$graph = new Graph($width, $height);
if ($scale == "line-log") {
$graph->SetScale("textlog");
} else {
$graph->SetScale("textlin");
}
//$graph->SetYScale(0, 'lin'); for multiple extry y-axis
//$graph->SetScale("textlin", 0, 6500); //$graph->SetScale("textlog");
//$graph->SetScale("textlin"); //$graph->SetScale("textlog");
$theme_class=new UniversalTheme;
$graph->SetTheme($theme_class);
$graph->img->SetAntiAliasing();
if ($display_mode == "avg") {
$title = "Tägliche Neuinfektionen Corona";
if ($nbr_days != 10000)
$title = $title . " letzte $nbr_days Tage";
$graph->subtitle->Set("Berechnet als gleitender Mittelwert über die vorherigen 7 Tage.");
} else {
$title = "Corona Sieben-Tage Inzidenz";
if ($nbr_days != 10000)
$title = $title . " letzte $nbr_days Tage";
$subtitle = "Berechnet auf der Gesamtzahl der jeweils letzten sieben Tage";
if ($scale == "line-log")
$subtitle = "$subtitle - Achtung: LOGARITHMISCHE Skalierung!";
$graph->subtitle->Set($subtitle);
}
$graph->title->Set($title);
$graph->title->SetFont(FF_DV_SANSSERIF, FS_BOLD, 18);
$graph->title->Align("left");
$graph->subtitle->SetFont(FF_DV_SANSSERIF, FS_BOLD, 12);
$graph->subtitle->Align("left");
$graph->SetBox(false);
$graph->SetMargin(100,60,100,80); // Adjust margin area
// Enable X-grid as well
$graph->xgrid->Show();
// Setup X-AXIS
$graph->xaxis->SetLabelAngle(45);
$graph->xaxis->SetLabelMargin(2);
$graph->xaxis->SetTickLabels($data_date);
$graph->xaxis->SetTextLabelInterval($labelsplit);
//$graph->xaxis->SetMajTickPositions($tickPositions, $tickLabels);
// Setup Y-AXIS
if ($scale == "line-log") {
$graph->yaxis->scale->SetAutoMin(0);
$graph->yaxis->scale->ticks->SetLabelLogType(LOGLABELS_PLAIN);
}
//$graph->yaxis->scale->SetGrace(10); // So the value is readable
// add Y Data first
// $graph->AddY2(new LinePlot($data_date));
// Create a bar pot
//$bplot = new BarPlot($datainfiziert);
//$graph->Add($bplot);
//$graph->AddY(0, $bplot);
//$bplot->SetColor('#DDDDDD');
//$bplot->SetFillColor('#DDDDDD');
//$bplot->SetWeight($chartlinewidth);
//$bplot->SetLegend('Infizierte ' . $entity);
$text_Mittelwert = '';
$entityname = get_entity_name($entity);
// Täglicher Zuwachs (nicht geglättet)
if ($daily_data == 1) {
$dayPlot = new LinePlot($data_day);
$graph->Add($dayPlot);
$dayPlot->SetColor($colorDaily);
$dayPlot->SetWeight($chartlinewidth-1);
$dayPlot->SetLegend( 'Tagesdaten ' . $entityname);
}
// plot entity 1
$pPlot = new LinePlot($data);
$graph->Add($pPlot);
//$graph->AddY(0, $pPlot);
$pPlot->SetColor($colorEntity);
$pPlot->SetWeight($chartlinewidth);
$pPlot->SetLegend( $text_Mittelwert . $entityname);
if ($entity2 != "") {
// plot entity 2
$entityname2 = get_entity_name($entity2);
$pPlot2 = new LinePlot($data2);
$graph->Add($pPlot2);
$pPlot2->SetColor('#ff0000');
$pPlot2->SetWeight($chartlinewidth);
$pPlot2->SetLegend( $text_Mittelwert . $entityname2);
}
if ($entity3 != "") {
// plot entity 3
$entityname3 = get_entity_name($entity3);
$pPlot3 = new LinePlot($data3);
$graph->Add($pPlot3);
$pPlot3->SetColor('#00ff00');
$pPlot3->SetWeight($chartlinewidth);
$pPlot3->SetLegend( $text_Mittelwert . $entityname3);
}
if ($entity4 != "") {
// plot entity 4
$entityname4 = get_entity_name($entity4);
$pPlot4 = new LinePlot($data4);
$graph->Add($pPlot4);
$pPlot4->SetColor('#00ffff');
$pPlot4->SetWeight($chartlinewidth);
$pPlot4->SetLegend( $text_Mittelwert . $entityname4);
}
if ($entity5 != "") {
// plot entity 5
$entityname5 = get_entity_name($entity5);
$pPlot5 = new LinePlot($data5);
$graph->Add($pPlot5);
$pPlot5->SetColor('#cccc00');
$pPlot5->SetWeight($chartlinewidth);
$pPlot5->SetLegend( $text_Mittelwert . $entityname5);
}
if ($entity6 != "") {
// plot entity 6
$entityname6 = get_entity_name($entity6);
$pPlot6 = new LinePlot($data6);
$graph->Add($pPlot6);
$pPlot6->SetColor('#bbbbbb');
$pPlot6->SetWeight($chartlinewidth);
$pPlot6->SetLegend( $text_Mittelwert . $entityname6);
}
// Set Legend properties
$graph->legend->SetPos(0.5,0.10,'center','top');
$graph->legend->SetColumns(4);
$graph->legend->SetLayout(LEGEND_HOR);
$graph->legend->SetFrameWeight(2);
$graph->legend->SetShadow('gray@0.4',5);
$graph->legend->SetLineWeight(3);
$graph->legend->SetFont(FF_DV_SANSSERIF, FS_NORMAL, 14);
$graph->legend->SetMarkAbsSize(12);
// Set Graph footer
if ($dbManuell == 0) {
$datenquelle = "RKI";
} else {
$datenquelle = "Main-Tauber Kreis"; // FIXME for different entities, right we only have TBB manually
}
$graph->footer->left->Set ("Grafik: Rainer Gerhards, Daten: $datenquelle (Stand: $datenstand) | www.rainer-gerhards.de");
// Add Marker Line
if ($plotline > 0) {
$pline = new PlotLine(VERTICAL, $plotline, '#ff0000', 2);
$pline->SetLineStyle('dotted');
$pline->SetDirection(HORIZONTAL);
$graph->AddLine($pline, false);
}
/* special, canned, cases */
if ($plotline < 0) {
$pline = new PlotLine(HORIZONTAL, 50, '#ff0000', 2);
$pline->SetLineStyle('dotted');
$graph->AddLine($pline, false);
}
if ($plotline < -1) {
$pline2 = new PlotLine(HORIZONTAL, 35, 'orange', 2);
$pline2->SetLineStyle('dotted');
$graph->AddLine($pline2, false);
}
if ($plotline < -2) {
$pline3 = new PlotLine(HORIZONTAL, 10, 'green', 2);
$pline3->SetLineStyle('dotted');
$graph->AddLine($pline3, false);
}
if ($plotline < -3) {
$pline3 = new PlotLine(HORIZONTAL, 100, 'darkred', 2);
$pline3->SetLineStyle('dotted');
$graph->AddLine($pline3, false);
}
// Output line
$graph->img->SetExpired(0);
$graph->Stroke();
// --- helper functions
function imageError($szErrorMsg) {
global $width, $height;
$image = new Imagick();
$image->newImage($width, $height, new ImagickPixel('black'));
$image->setImageFormat('png');
$image->setFont("fonts/DejaVuSerif.ttf");
$szErrorMsg = chunk_split($szErrorMsg, 50);
$draw = new ImagickDraw();
$draw->setFillColor('yellow');
$draw->setStrokeColor ('black');
$draw->setFontStyle(\Imagick::STYLE_OBLIQUE);
$draw->setFontSize( 30 );
$draw->setFont("fonts/DejaVuSerif.ttf");
$image->annotateImage($draw, 50, 100, 0, $szErrorMsg);
header('Content-type: image/png');
header('Content-Length: ' . $image->getImageLength());
echo $image;
}
function calcPlotValues() {
/* Hinweis zur Berechnung der inzidenz als 7-Tage Differenz. Wir arbeiten
* hier auf der Summe der letzten sieben Tage.
* Sei t_{h-n} der Tageswert n Tage vor heute. Dann ist
* S_v = Summe(t_h-8, .., t_h-1} die Summe der vorherigen 7 Tage seit gestern und
* S_h = Summe(t_h-7, .., t_h} die Summe der vorherigen 7 Tage seit heute.
* Dann ist S_v = t_h-8 + summe(t_h-7, .., t_h-1) und
* S_h = summe(t_h-7, .., t_h-1) + t_h
* Dann ist N = S_h - S_v = t_h - t_h-8 die Anzahl der Neuinfektionen
* seit sieben Tagen der gesuchte Wert.
* Man könnte S_h ohne die Summenbildung berechnen, das würde aber die
* generelle Wiederverwendbarkeit des Codes verschlechtern und wäre auch
* nicht wirklich schneller (da wir dann statt der Summation Array-Shift
* Operationen durchführen müssten). Wir benutzen daher diesen Ansatz,
* und können daher den Code mit Hilfe einiger weniger Parameter auch
* für andere Anwendunsgfälle nutzen, was der Wartung sehr entgegen
* kommt. -- rgerhards, 2020-05-06.
*/
global $data, $data2, $data3, $data4, $data5, $data6, $data_date, $data_day;
global $entity, $entity2, $entity3, $entity4, $entity5, $entity6;
global $conn;
global $datenstand;
global $display_mode, $nbr_days, $dateTo, $dateFrom, $dbManuell;
$days = 7; // How many days to look at for avg?
$ewz_faktor = get_ewz_faktor($entity);
if($entity2 != "")
$ewz_faktor2 = get_ewz_faktor($entity2);
if($entity3 != "")
$ewz_faktor3 = get_ewz_faktor($entity3);
if($entity4 != "")
$ewz_faktor4 = get_ewz_faktor($entity4);
if( $entity6 != "")
$entity_where = "(entity in ('$entity', '$entity2', '$entity3', '$entity4', '$entity5', '$entity6'))";
else if( $entity5 != "")
$entity_where = "(entity in ('$entity', '$entity2', '$entity3', '$entity4', '$entity5'))";
else if( $entity4 != "")
$entity_where = "(entity = '$entity' or entity = '$entity2' or entity = '$entity3' or entity='$entity4')";
else if( $entity3 != "")
$entity_where = "(entity = '$entity' or entity = '$entity2' or entity = '$entity3')";
else if( $entity2 != "")
$entity_where = "(entity = '$entity' or entity = '$entity2')";
else
$entity_where = "(entity = '$entity')";
// datensätze selbst verarbeiten
$strSql = " SELECT date, infizierte, neuinfekt, neuinfekt7, entity
FROM fallzahlen
WHERE date >= $dateFrom and
date BETWEEN DATE_SUB((select max(date) from fallzahlen where entity='$entity' and manuell=$dbManuell), INTERVAL $nbr_days DAY) AND $dateTo and manuell = $dbManuell and $entity_where ORDER BY date, entity";
//echo $strSql; exit;
$result = $conn->query($strSql);
if (!$result) {
imageError("Couldn't find data for: " . $strSql );
exit;
}
$had_cases = 0;
$date_prev = 0;
$idx = -1;
// Loop through data
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$neuinfekt = $row['neuinfekt'];
$neuinfekt7 = $row['neuinfekt7'];
if ($display_mode == "avg" || $display_mode == "inzidenz-avg")
$neuinfekt7 = $neuinfekt7 / 7;
if ($neuinfekt > 0 || $had_cases == 1) {
$had_cases = 1;
if ($date_prev != $row['date']) {
$idx = $idx + 1;
$data_date[$idx] = $row['date'];
$date_prev = $row['date'];
$data[$idx] = NULL;
$data2[$idx] = NULL;
$data3[$idx] = NULL;
$data4[$idx] = NULL;
$data5[$idx] = NULL;
$data6[$idx] = NULL;
//echo "set date: $date_prev
";
}
$data_day[$idx] = $neuinfekt;
if ($row['entity'] == $entity)
$data[$idx] = $neuinfekt7 / $ewz_faktor;
else if ($row['entity'] == $entity2)
$data2[$idx] = $neuinfekt7 / $ewz_faktor2;
else if ($row['entity'] == $entity3)
$data3[$idx] = $neuinfekt7 / $ewz_faktor3;
else if ($row['entity'] == $entity4)
$data4[$idx] = $neuinfekt7 / $ewz_faktor4;
else if ($row['entity'] == $entity5)
$data5[$idx] = $neuinfekt7 / $ewz_faktor4;
else
$data6[$idx] = $neuinfekt7 / $ewz_faktor4;
$datenstand = $row['date'];
//echo "data[$idx]: " . $row['entity'] . " -> $neuinfekt7
";
}
}
//exit;
//print_r ($data); exit;
$result->close();
// ---
}
function get_ewz_faktor($entity) {
global $conn;
global $display_mode;
if (substr($entity, 0, 3) == "RKI")
$srchentity = 'DE' . substr($entity, 3);
else if (substr($entity, 0, 3) == "MUT")
$srchentity = 'DE' . substr($entity, 3);
else if (substr($entity, 0, 5) == "B1351")
$srchentity = 'DE' . substr($entity, 5);
else if (substr($entity, 0, 3) == "VAR")
$srchentity = 'DE';
else
$srchentity = $entity;
if ($display_mode == "inzidenz") {
$result = $conn->query("select ewz from einwohner where entity='$srchentity'");
if (!$result || $result->num_rows == 0) {
imageError("Einwohnerzahl nicht gefunden für: $srchentity (was $entity)");
exit;
}
$row = $result->fetch_array(MYSQLI_ASSOC);
$ewz_faktor = $row['ewz'] / 100000; // Faktor für Angaben je 100.000 Einwohner
$result->close();
} else {
$ewz_faktor = 1;
}
return $ewz_faktor;
}
function get_entity_name($entity) {
global $have_RKI, $text_Mittelwert;
if ($entity == "VAR:A3:OMI")
return "Omikron [berechnet]";
if ($entity == "VAR:A3:DELTA")
return "Delta [berechnet]";
if (substr($entity, 0, 3) == "RKI")
$srchentity = 'DE' . substr($entity, 3);
else
$srchentity = $entity;
if ($srchentity == "DE")
$entname = "Deutschland";
else if ($srchentity == "MUT:BW:TBB")
$entname = "Mutanten Main-Tauber";
else if ($srchentity == "B1351:BW:TBB")
$entname = "B.1.351 Main-Tauber";
else if ($srchentity == "DE:BW:TBB")
$entname = "Main-Tauber Kreis";
else if ($srchentity == "DE:BW")
$entname = "Baden Württemberg";
else if ($srchentity == "DE:NRW")
$entname = "NRW";
else if ($srchentity == "DE:BY")
$entname = "Bayern";
else if ($srchentity == "DE:SONDER:WUE")
$entname = "Würzburg (Stadt- und Landkreis)";
else if ($srchentity == "DE:BY:WUE:SK")
$entname = "Stadt Würzburg";
else if ($srchentity == "DE:BY:WUE:LK")
$entname = "Landkreis Würzburg";
else
$entname = $srchentity;
if (substr($entity, 0, 3) == "RKI")
$entname = $entname . ' [Meldedatum]';
if (substr($entity, -3) == ":A0") {
$entname = "0-4 Jahre";
$text_Mittelwert = '';
}
if (substr($entity, -4) == ":A05") {
$entname = "5-14 Jahre";
$text_Mittelwert = '';
}
if (substr($entity, -4) == ":A15") {
$entname = "15-34 Jahre";
$text_Mittelwert = '';
}
if (substr($entity, -4) == ":A35") {
$entname = "35-59 Jahre";
$text_Mittelwert = '';
}
if (substr($entity, -4) == ":A60") {
$entname = "60-79 Jahre";
$text_Mittelwert = '';
}
if (substr($entity, -4) == ":A80") {
$entname = "80+ Jahre";
$text_Mittelwert = '';
}
if (substr($entity, -5) == ":A-14") {
$entname = "0-14 Jahre";
$text_Mittelwert = '';
}
if (substr($entity, -5) == ":A60-") {
$entname = "60+ Jahre";
$text_Mittelwert = '';
}
return $entname;
}
?>