Reply To: FireStats – Top 40 php script (now with added charts)

#14693
sonichouse
Participant

@fizze wrote:

Nice chart 😉

Glad you liked it, the latest version has a dependency on libchart.php from http://naku.dohcrew.com/libchart/pages/introduction/

The code is

<?php

if(isset($_GET))
$mode = $_GET;
else
$mode = "Tracks";

try {

if($mode=="Tracks") {
$limit=40;
$Target = "Albums";
} else {
$limit=10;
$Target = "Tracks";
}

$query="
select Artist,Album,title as Track,play_count,track as No
from songs
where play_count >= 1
group by 4,1,2,3,5
order by 4 desc,1 asc,2 asc,5
limit $limit";

$album_query="
select Album,Artist,sum(play_count) as Played
from songs
where play_count > 0 and album''
group by 1,2
having sum(play_count) >= 1
order by 3 desc,1 asc,2 asc
limit $limit";

$dbh = new PDO('sqlite:/opt/var/mt-daapd/songs3.db');
$dbh->exec('SET CHARACTER SET UTF8');

print "nnn";
print "th{font-size: 7pt}td{font-size: 7pt; padding: 2 1em 0 1em;border-right: thin solid; border-bottom: thin dotted;}n";

print "

Firefly - Top $limit $mode

n";
print "nShow $Target";print "

";

if($mode=="Tracks")
{

print "ntnn";

foreach ($dbh->query($query) as $row)
{
print "t";
print "";
print "";
print "";
print "";
print "";
print "n";
}
print "
PlayedArtistAlbumTrack TitleTrack #
$row[play_count]$row[Artist]$row[Album]$row[Track]$row[No]
n";
}

if($mode=="Albums")
{
include "./libchart-1.2/libchart/classes/libchart.php";

$chart = new PieChart(800,300);
$dataSet = new XYDataSet();

print "Pie chart
";
print "
ntnn";

foreach ($dbh->query($album_query) as $row)
{
$n = "$row[Played]";
print "t";
print "";
print "";
print "";
print "n";
$dataSet->addPoint(new Point("$row[Album] - $row[Artist] ( $n )", $n));
}
print "
Tracks PlayedAlbumArtist
$row[Played]$row[Album]$row[Artist]
n";

/**
* Pie chart demonstration
*
*/
$chart->setDataSet($dataSet);

$chart->setTitle("Top $limit $mode");
$chart->render("$mode.png");
}

print "nn";

$dbh = null;
}
catch (PDOException $e)
{
print "Error!: " . $e->getMessage() . "
";
die();
}
?>