Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[PHP / JPGraph] - Diagramm erstellen aus MySQL Daten

Empfohlene Antworten

Moin,

ich will nen Liniendiagramm aus Daten erstellen welche von einer MySQL DB geliefert werden.

Die beiden Felder dich ich haben will heissen Datum und Betrag.

Kurzum: Ich will auf der X-Achse das Datum stehen haben und auf der Y-Achse den jeweiligen Betrag (können per Definition ausschliesslcih postivie Beträge sein)

Datum ist DateTime als Typ

Betrag ist integer

Das unten angehängt script liefert mir alelrdings nur ne leere Grafik zurück:


<?php

// JPGraph Library einbinden
include ("includes/dbconnect.inc.php");
include ("includes/jpgraph-2.1.4/src/jpgraph.php");
include ("includes/jpgraph-2.1.4/src/jpgraph_line.php");

// Neuen Graph bilden
$graph = new Graph(350,250,"auto");

//get the db ready
error_reporting(E_ALL);

@mysql_connect($host, $db_user, $db_pass) OR die(mysql_error());
mysql_select_db('darkorbit') OR die(mysql_error());

$sql = "SELECT Datum, Kontostand
FROM kontostand
ORDER BY Datum;";
$result = mysql_query($sql) OR die(mysql_error());

//push the data into $data array to get it plotted

$i=0;
while ($array=mysql_fetch_array($result)) {
$datax[$i]=$array[0];
$datay[$i]=$array[1];
$i++;
};

// Grafik definieren...
$graph->SetScale("int", "textlin");

//Daten an Grafik übergeben
//hier liegt wohl der Fehler, weil $lineplot ja zweimal gesetzt wird
//aber wie zum teufel übergebe ich jpgraph die info
//$datax() ist das array für die daten der x-achse
//$datay() ist das array für die y-achse?
//
//irgendwie check ich die datenübergabe nicht
$lineplot=new LinePlot($datax);
$lineplot=new LinePlot($datay);

$graph->Add($lineplot);

// Graph ausgeben
$graph->Stroke();

?>

[/php]

Hab den fraglichen Bereich mit Komemntaren versehen.

Any Help greatly appreciated :)

  • Autor

ok nach änderung der zeile

$graph->SetScale("int", "textlin");

auf $graph->SetScale("textlin");

hab ich zumindest die x-werte schonmal richtig drin..

nu muss ich nur noch wissen wie ich die y-achse korrekt mit den zu den x-werten gehörigen datumswerte beschrifte ...

  • Autor

also imho ist jetzt allns richtig ...


<?php

// JPGraph Library einbinden
include ("includes/jpgraph-2.1.4/src/jpgraph.php");
include ("includes/jpgraph-2.1.4/src/jpgraph_line.php");

// Neuen Graph bilden
$graph = new Graph(600,400,"auto");

//get the db ready
error_reporting(E_ALL);

@mysql_connect(localhost, root, koo4kej) OR die(mysql_error());
mysql_select_db('darkorbit') OR die(mysql_error());

$sql = "SELECT Datum, Kontostand
FROM kontostand
ORDER BY Datum;";
$result = mysql_query($sql) OR die(mysql_error());

//push the data into $data array to get it plotted

$i=0;
while ($array=mysql_fetch_array($result)) {
$datum[$i]=$array[0];
$betrag[$i]=$array[1];
$i++;
};

// Grafik definieren...
$graph->SetScale("textint");

$lineplot =new LinePlot($betrag, $datum);

$lineplot->value-> Show();

$graph->Add($lineplot);

// Graph ausgeben
$graph->Stroke();

?>
[/php]

Führt aber zur Fehlermeldung

Either X or Y Data contains non-numeric values

...

richtig .. denn das datum aus der sql db hat ja auch das format "YYYY-MM-DD" :rolleyes:

help .. ich komm da echt nimmer weiter :(

  • Autor

schau ich mir auf jeden fall an, aber ich würds scho gern mit jpgraph hinkriegen

bin immerhin schon nen schritt weitergekommen. den rest krieg ich auch noch hin .. und wenn ich den angeblichen string vorher in date umcasten muss :D

edit:

umcasten war ne gute idee.. nu funzt es :)


<?php

// JPGraph Library einbinden
include ("includes/jpgraph-2.1.4/src/jpgraph.php");
include ("includes/jpgraph-2.1.4/src/jpgraph_line.php");
include ("includes/jpgraph-2.1.4/src/jpgraph_date.php");

// Neuen Graph bilden
$graph = new Graph(600,400,"auto");

//get the db ready
error_reporting(E_ALL);

@mysql_connect($host, $dbuser, $dbpass) OR die(mysql_error());
mysql_select_db('darkorbit') OR die(mysql_error());

$sql = "SELECT Datum, (Kontostand/1000000) AS Kontostand
FROM kontostand
ORDER BY Datum;";
$result = mysql_query($sql) OR die(mysql_error());

//push the data into $data array to get it plotted

$i=0;
while ($array=mysql_fetch_array($result)) {
$datum[$i]= strtotime($array[0]);
$betrag[$i]=$array[1];
$i++;
};

// Grafik definieren...
$graph->SetScale("datint");

$lineplot =new LinePlot($betrag, $datum);
$lineplot->SetLegend('Kontostand');
$lineplot->value-> Show();
$graph->xaxis->SetLabelAngle(90);
$graph->SetMargin(40,40,30,130);

$graph->Add($lineplot);

// Graph ausgeben
$graph->Stroke();

?>
[/php]

*freus*

Du willst ein Balkendiargramm mit dem Verlauf eines Kontostandes haben?

Warum schiesst Du mit Kanonen auf Spatzen?

Mal abgesehen von der DB Abfrage sind dass evtl. 30 Zeilen in stinknormalem PHP um die GD Lib für diese Funktion zu nutzen.

Ihr wollt doch alle programmieren, oder?

Dann programmiert auch! :D

  • Autor

1. funktioniert es ja wie gesagt schon

und

2. ich bin netzwerkadmin und das ganze is privatvergnügen

ich will nicht programieren, sondern ich hab nen problem was ich mit dem was ich kann lösen will.

achja:

3. dein (@Eupinkepank) beitrag ist ungefähr so interessant wie der berühmte chinesiche sack reis ;)

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.