dataFile = $value; } function setDataPoints($value) { $this->chartDataPoints = $value; } function setTitle($value) { $this->chartTitle = $value; } function setSize($value) { $this->chartSize = $value; } function setColors($value) { $this->chartColors = $value; } function setMaxPlays($value) { $this->maxPlays = $value; } function setType($value) { // set the type of chart and some other required / default parameters switch ($value) { case 'pie': $value = 'p'; $type = 'pc'; // pie chart requires percentage values $size = '550x200'; break; case 'bar': $value = 'bhg'; $type = 'num'; // bar chart requires absolute numbers $size = '400x400'; break; default: $value = $value; $type = $type; break; } $this->chartType = $value; $this->chartDataType = $type; $this->setSize($size); } function getData($xml) { // get data from xml // read in xml $music = simplexml_load_file($xml); // initiate some variables $listens = array(); $tot_plays = 0; $counter = 0; // blank labels and values $this->chartLabels = ''; $this->chartValues = ''; // string for encoding - these 62 characters have increasing value $simpleEncoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; // for each artist in the xml foreach ($music->artist as $artist) { // add the artist name to the label string up to the number of data points required if ($counter < $this->chartDataPoints) { $this->chartLabels .= $artist->name . '|'; } // store the number of plays for this artist, add the plays to the total // and increment the counter $plays[$counter] = $artist->playcount; $tot_plays += $artist->playcount; $counter++; } // for each plays value foreach ($plays as $play) { // get a number that works with charts if ($this->chartDataType == 'pc') { $play = $play / $tot_plays * 61; } elseif ($this->chartDataType == 'num') { $play = $play / $plays[0] * 61; } // encode these values $this->chartValues .= substr($simpleEncoding,$play,1); } // trim values to number of data points required $this->chartValues = substr($this->chartValues, 0, $this->chartDataPoints); // trim trailing separator from labels and encode for use in a url $this->chartLabels = substr($this->chartLabels, 0, strlen($this->chartLabels) - 1); $this->chartLabels = urlencode($this->chartLabels); // if this is a bar chart, reverse the chart values if ($this->chartType == 'bhg') { $this->chartValues = strrev($this->chartValues); } // store the highest number of plays $this->setMaxPlays($plays[0]); } function doChart() { // get the data $this->getData($this->dataFile); // start appending to url $this->chartURI .= 'chtt=' . urlencode($this->chartTitle) . '&cht=' . $this->chartType; $this->chartURI .= '&chs=' . $this->chartSize . '&chco=' . $this->chartColors; $this->chartURI .= '&chd=s:' . $this->chartValues; // chart type specifics if ($this->chartType == 'p') { $this->chartURI .= '&chl=' . $this->chartLabels; } elseif ($this->chartType == 'bhg') { $this->chartURI .= '&chxt=x,y&chxl=0:|0|' . $this->maxPlays . '|1:|' . $this->chartLabels; } // output into img tag echo '' . $this->chartTitle . ''; } }