diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/Controller.php | 2 | ||||
-rw-r--r-- | core/DataTable.php | 16 | ||||
-rw-r--r-- | core/DataTable/Filter/Sort.php | 2 | ||||
-rw-r--r-- | core/Period.php | 8 | ||||
-rw-r--r-- | core/Period/Range.php | 2 | ||||
-rw-r--r-- | core/SmartyPlugins/function.sparkline.php | 2 | ||||
-rw-r--r-- | core/ViewDataTable.php | 37 | ||||
-rw-r--r-- | core/ViewDataTable/Cloud.php | 40 | ||||
-rw-r--r-- | core/ViewDataTable/GenerateGraphData.php | 27 | ||||
-rw-r--r-- | core/ViewDataTable/GenerateGraphData/ChartEvolution.php | 34 | ||||
-rw-r--r-- | core/ViewDataTable/GenerateGraphHTML/ChartEvolution.php | 2 | ||||
-rw-r--r-- | core/ViewDataTable/HtmlTable.php | 68 | ||||
-rw-r--r-- | core/ViewDataTable/Sparkline.php | 50 | ||||
-rw-r--r-- | core/Visualization/Chart.php | 25 | ||||
-rw-r--r-- | core/Visualization/Chart/Evolution.php | 15 | ||||
-rw-r--r-- | core/Visualization/Chart/Pie.php | 3 | ||||
-rw-r--r-- | core/Visualization/Chart/VerticalBar.php | 6 | ||||
-rw-r--r-- | core/Visualization/Cloud.php | 126 |
18 files changed, 215 insertions, 250 deletions
diff --git a/core/Controller.php b/core/Controller.php index ad5b4a5653..36c56852a0 100644 --- a/core/Controller.php +++ b/core/Controller.php @@ -92,7 +92,7 @@ abstract class Piwik_Controller * @param bool $fetch * @return string|void */ - protected function renderView( Piwik_ViewDataTable $view, $fetch) + protected function renderView( Piwik_ViewDataTable $view, $fetch = false) { Piwik_PostEvent( 'Controller.renderView', $this, diff --git a/core/DataTable.php b/core/DataTable.php index ee4d6beb6b..b4f100b7b9 100644 --- a/core/DataTable.php +++ b/core/DataTable.php @@ -1082,13 +1082,23 @@ class Piwik_DataTable * LABEL => array(col1 => X, col2 => Y), * LABEL2 => array(col1 => X, col2 => Y), * ) - * * to the structure * array ( * array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL, col1 => X, col2 => Y)), * array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL2, col1 => X, col2 => Y)), * ) * + * It also works with array having only one value per row, eg. + * array ( + * LABEL => X, + * LABEL2 => Y, + * ) + * would be converted to the structure + * array ( + * array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL, 'value' => X)), + * array( Piwik_DataTable_Row::COLUMNS => array('label' => LABEL2, 'value' => Y)), + * ) + * * The optional parameter $subtablePerLabel is an array of subTable associated to the rows of the $array * For example if $subtablePerLabel is given * array( @@ -1116,6 +1126,10 @@ class Piwik_DataTable $cleanRow = array(); foreach($array as $label => $row) { + if(!is_array($row)) + { + $row = array('value' => $row); + } $cleanRow[Piwik_DataTable_Row::DATATABLE_ASSOCIATED] = null; // we put the 'label' column first as it looks prettier in API results $cleanRow[Piwik_DataTable_Row::COLUMNS] = array('label' => $label) + $row; diff --git a/core/DataTable/Filter/Sort.php b/core/DataTable/Filter/Sort.php index 077a09cbca..0b80a322d8 100644 --- a/core/DataTable/Filter/Sort.php +++ b/core/DataTable/Filter/Sort.php @@ -20,7 +20,7 @@ class Piwik_DataTable_Filter_Sort extends Piwik_DataTable_Filter protected $columnToSort; protected $order; - public function __construct( $table, $columnToSort, $order = 'desc', $naturalSort = false, $recursiveSort = false ) + public function __construct( $table, $columnToSort, $order = 'desc', $naturalSort = true, $recursiveSort = false ) { parent::__construct($table); if(empty($columnToSort)) diff --git a/core/Period.php b/core/Period.php index b5fbd33e0e..29323e6a45 100644 --- a/core/Period.php +++ b/core/Period.php @@ -40,6 +40,11 @@ abstract class Piwik_Period $this->date = clone $date; } + /** + * @param $strPeriod "day", "week", "month", "year" + * @param $date Piwik_Date object + * @return Piwik_Period + */ static public function factory($strPeriod, $date) { switch ($strPeriod) { @@ -129,7 +134,6 @@ abstract class Piwik_Period } /** - * * @return Piwik_Date */ protected function getDate() @@ -163,7 +167,7 @@ abstract class Piwik_Period * Returns Period_Day for a period made of days (week, month), * Period_Month for a period made of months (year) * - * @return array + * @return array Piwik_Period */ public function getSubperiods() { diff --git a/core/Period/Range.php b/core/Period/Range.php index dea4ed73ad..1d695ba971 100644 --- a/core/Period/Range.php +++ b/core/Period/Range.php @@ -101,9 +101,7 @@ class Piwik_Period_Range extends Piwik_Period if(ereg('(last|previous)([0-9]*)', $this->strDate, $regs)) { $lastN = $regs[2]; - $lastOrPrevious = $regs[1]; - if(!is_null($this->defaultEndDate)) { $defaultEndDate = $this->defaultEndDate; diff --git a/core/SmartyPlugins/function.sparkline.php b/core/SmartyPlugins/function.sparkline.php index 096bb1a05e..e96f34a4a8 100644 --- a/core/SmartyPlugins/function.sparkline.php +++ b/core/SmartyPlugins/function.sparkline.php @@ -15,7 +15,7 @@ require_once "Visualization/Sparkline.php"; * @param string $url * @return string IMG HTML tag */ -function smarty_function_sparkline($params, &$smarty) +function smarty_function_sparkline($params, &$smarty = false) { $src = $params['src']; $width = Piwik_Visualization_Sparkline::getWidth(); diff --git a/core/ViewDataTable.php b/core/ViewDataTable.php index c30cb185d6..0321171ba9 100644 --- a/core/ViewDataTable.php +++ b/core/ViewDataTable.php @@ -116,6 +116,9 @@ abstract class Piwik_ViewDataTable */ protected $columnsTranslations = array(); + + protected $columnsToDisplay = array(); + /** * Method to be implemented by the ViewDataTable_*. * This method should create and initialize a $this->view object @see Piwik_iView @@ -433,7 +436,7 @@ abstract class Piwik_ViewDataTable else { // the $uniqIdTable variable is used as the DIV ID in the rendered HTML - // we use the current Controller action name as it is supposed to be unique in the rendered page + // we use the current Controller action name as it is supposed to be unique in the rendered page $uniqIdTable = $this->currentControllerName . $this->currentControllerAction; } return $uniqIdTable; @@ -648,7 +651,7 @@ abstract class Piwik_ViewDataTable * When this method is called, the output will not contain the template datatable_footer.tpl * @return void */ - public function doNotShowFooter() + public function disableFooter() { $this->viewProperties['show_footer'] = false; } @@ -790,6 +793,36 @@ abstract class Piwik_ViewDataTable return $columnName; } } + + /** + * Sets the columns that will be displayed in the HTML output + * By default all columns are displayed ($columnsNames = array() will display all columns) + * + * @param array $columnsNames Array of column names eg. array('nb_visits','nb_hits') + */ + public function setColumnsToDisplay( $columnsNames ) + { + if(!is_array($columnsNames)) + { + $columnsNames = array($columnsNames); + } + $this->columnsToDisplay = $columnsNames; + } + + /** + * Returns columns names to display, in order. + * If no columns were specified to be displayed, return all columns found in the first row. + * @param array PHP array conversion of the data table + * @return array + */ + public function getColumnsToDisplay() + { + if(empty($this->columnsToDisplay)) + { + return array_keys($this->dataTable->getFirstRow()->getColumns()); + } + return $this->columnsToDisplay; + } /** * Sets columns translations array. diff --git a/core/ViewDataTable/Cloud.php b/core/ViewDataTable/Cloud.php index 50078fc169..c5fc45a67e 100644 --- a/core/ViewDataTable/Cloud.php +++ b/core/ViewDataTable/Cloud.php @@ -22,6 +22,11 @@ class Piwik_ViewDataTable_Cloud extends Piwik_ViewDataTable { protected $displayLogoInsteadOfLabel = false; + public function displayLogoInTagCloud() + { + $this->displayLogoInsteadOfLabel = true; + } + protected function getViewDataTableId() { return 'cloud'; @@ -59,45 +64,46 @@ class Piwik_ViewDataTable_Cloud extends Piwik_ViewDataTable $this->view = $this->buildView(); } + function getColumnToDisplay() + { + $columns = parent::getColumnsToDisplay(); + // not label, but the first numeric column + return $columns[1]; + } + protected function buildView() { $view = new Piwik_View($this->dataTableTemplate); - $words = $labelMetadata = array(); + $columnToDisplay = $this->getColumnToDisplay(); + $columnTranslation = $this->getColumnTranslation($columnToDisplay); + $values = $this->dataTable->getColumn($columnToDisplay); + $labels = $this->dataTable->getColumn('label'); foreach($this->dataTable->getRows() as $row) { - $label = $row->getColumn('label'); - $value = $row->getColumn('nb_uniq_visitors'); - - // case no unique visitors - if($value === false) - { - $value = $row->getColumn('nb_visits'); - } - $words[$label] = $value; - $logo = false; if($this->displayLogoInsteadOfLabel) { $logo = $row->getMetadata('logo'); } - - $labelMetadata[$label] = array( + $labelMetadata[$row->getColumn('label')] = array( 'logo' => $logo, 'url' => $row->getMetadata('url'), - 'hits' => $value ); } - $cloud = new Piwik_Visualization_Cloud($words); + $cloud = new Piwik_Visualization_Cloud(); + foreach($labels as $i => $label) + { + $cloud->addWord($label, $values[$i]); + } $cloudValues = $cloud->render('array'); - foreach($cloudValues as &$value) { $value['logoWidth'] = round(max(16, $value['percent'])); } + $view->columnTranslation = $columnTranslation; $view->labelMetadata = $labelMetadata; $view->cloudValues = $cloudValues; - $view->javascriptVariablesToSet = $this->getJavascriptVariablesToSet(); $view->properties = $this->getViewProperties(); return $view; diff --git a/core/ViewDataTable/GenerateGraphData.php b/core/ViewDataTable/GenerateGraphData.php index bf5285cca9..af61510bac 100644 --- a/core/ViewDataTable/GenerateGraphData.php +++ b/core/ViewDataTable/GenerateGraphData.php @@ -36,17 +36,11 @@ abstract class Piwik_ViewDataTable_GenerateGraphData extends Piwik_ViewDataTable * @var int */ protected $graphLimit = null; + protected $yAxisUnit = ''; - protected $columnsToDisplay = array(); - - public function setColumnsToDisplay($columns) - { - $this->columnsToDisplay = $columns; - } - - public function getColumnsToDisplay() + public function setAxisYUnit($unit) { - return $this->columnsToDisplay; + $this->yAxisUnit = $unit; } /** @@ -80,6 +74,7 @@ abstract class Piwik_ViewDataTable_GenerateGraphData extends Piwik_ViewDataTable // the queued filters will be manually applied later. This is to ensure that filtering using search // will be done on the table before the labels are enhanced (see ReplaceColumnNames) + $this->disableGenericFilters(); $this->disableQueuedFilters(); $this->loadDataTableFromAPI(); @@ -114,21 +109,25 @@ abstract class Piwik_ViewDataTable_GenerateGraphData extends Piwik_ViewDataTable $this->dataTable->applyQueuedFilters(); // We apply a filter to the DataTable, decoding the label column (useful for keywords for example) - $this->dataTable->filter('ColumnCallbackReplace', array('label','urldecode') ); + $this->dataTable->filter('ColumnCallbackReplace', array('label','urldecode')); $xLabels = $this->dataTable->getColumn('label'); - $columnNames = array_keys($this->dataTable->getFirstRow()->getColumns()); - unset($columnNames[array_search('label',$columnNames)]); + $columnNames = parent::getColumnsToDisplay(); + if(($labelColumnFound = array_search('label',$columnNames)) !== false) + { + unset($columnNames[$labelColumnFound]); + } - $columnNameToTranslation = $columnNameToValue = array(); + $columnNameToTranslation = $columnNameToValue = $columnNameToUnit = array(); foreach($columnNames as $columnName) { $columnNameToTranslation[$columnName] = $this->getColumnTranslation($columnName); $columnNameToValue[$columnName] = $this->dataTable->getColumn($columnName); + $columnNameToUnit[$columnName] = $this->yAxisUnit; } $this->view->setAxisXLabels($xLabels); $this->view->setAxisYValues($columnNameToValue); $this->view->setAxisYLabels($columnNameToTranslation); + $this->view->setAxisYUnits($columnNameToUnit); } } - diff --git a/core/ViewDataTable/GenerateGraphData/ChartEvolution.php b/core/ViewDataTable/GenerateGraphData/ChartEvolution.php index 30ee37fd1f..dc4eab30c0 100644 --- a/core/ViewDataTable/GenerateGraphData/ChartEvolution.php +++ b/core/ViewDataTable/GenerateGraphData/ChartEvolution.php @@ -21,6 +21,13 @@ class Piwik_ViewDataTable_GenerateGraphData_ChartEvolution extends Piwik_ViewDat protected function generateDataFromDataTable() { + // if the loaded datatable is a simple DataTable, it is most likely a plugin plotting some custom data + // we don't expect plugin developers to return a well defined Piwik_DataTable_Array + if($this->dataTable instanceof Piwik_DataTable) + { + return parent::generateDataFromDataTable(); + } + $this->dataTable->applyQueuedFilters(); if(!($this->dataTable instanceof Piwik_DataTable_Array)) { @@ -56,7 +63,6 @@ class Piwik_ViewDataTable_GenerateGraphData_ChartEvolution extends Piwik_ViewDat } } - // make sure all column values are set (at least zero) in order for all unique idDataTable $columnNameToValueCleaned = array(); foreach($uniqueIdsDataTable as $uniqueIdDataTable) @@ -76,28 +82,38 @@ class Piwik_ViewDataTable_GenerateGraphData_ChartEvolution extends Piwik_ViewDat } $columnNames = array_keys($columnNameToValueCleaned); $columnNameToTranslation = array(); - $columnNameToType = array(); - $nameToType = array( + $columnNameToUnit = array(); + $nameToUnit = array( '_rate' => '%', '_revenue' => Piwik::getCurrency(), ); foreach($columnNames as $columnName) { $columnNameToTranslation[$columnName] = $this->getColumnTranslation($columnName); - $columnNameToType[$columnName] = false; - foreach($nameToType as $pattern => $type) + + $columnNameToUnit[$columnName] = false; + // if the unit was specified, we use it + if(!empty($this->yAxisUnit)) { - if(strpos($columnName, $pattern) !== false) + $columnNameToUnit[$columnName] = $this->yAxisUnit; + } + // otherwise we guess the unit from the column name + else + { + foreach($nameToUnit as $pattern => $type) { - $columnNameToType[$columnName] = $type; - break; + if(strpos($columnName, $pattern) !== false) + { + $columnNameToUnit[$columnName] = $type; + break; + } } } } $this->view->setAxisXLabels($xLabels); $this->view->setAxisYValues($columnNameToValueCleaned); $this->view->setAxisYLabels($columnNameToTranslation); - $this->view->setAxisYValuesTypes($columnNameToType); + $this->view->setAxisYUnits($columnNameToUnit); $firstDatatable = reset($this->dataTable->metadata); $period = $firstDatatable['period']; diff --git a/core/ViewDataTable/GenerateGraphHTML/ChartEvolution.php b/core/ViewDataTable/GenerateGraphHTML/ChartEvolution.php index c169cdcf26..77082e99f9 100644 --- a/core/ViewDataTable/GenerateGraphHTML/ChartEvolution.php +++ b/core/ViewDataTable/GenerateGraphHTML/ChartEvolution.php @@ -35,7 +35,7 @@ class Piwik_ViewDataTable_GenerateGraphHTML_ChartEvolution extends Piwik_ViewDat $apiMethodToRequestDataTable ); $this->setParametersToModify(array('date' => Piwik_Common::getRequestVar('date', 'last30', 'string'))); - $this->doNotShowFooter(); + $this->disableFooter(); } /** diff --git a/core/ViewDataTable/HtmlTable.php b/core/ViewDataTable/HtmlTable.php index ac6d56ca44..cfd079da48 100644 --- a/core/ViewDataTable/HtmlTable.php +++ b/core/ViewDataTable/HtmlTable.php @@ -20,13 +20,6 @@ class Piwik_ViewDataTable_HtmlTable extends Piwik_ViewDataTable { /** - * Array of columns names to display - * - * @var array - */ - protected $columnsToDisplay = array(); - - /** * Set to true when the DataTable must be loaded along with all its children subtables * Useful when searching for a pattern in the DataTable Actions (we display the full hierarchy) * @@ -93,8 +86,12 @@ class Piwik_ViewDataTable_HtmlTable extends Piwik_ViewDataTable */ protected function buildView() { - $phpArray = $this->getPHPArrayFromDataTable(); - $columns = $this->getColumnsToDisplay($phpArray); + $columns = $this->getColumnsToDisplay(); + $columnTranslations = array(); + foreach($columns as $columnName) + { + $columnTranslations[$columnName] = $this->getColumnTranslation($columnName); + } $nbColumns = count($columns); // case no data in the array we use the number of columns set to be displayed if($nbColumns == 0) @@ -103,8 +100,9 @@ class Piwik_ViewDataTable_HtmlTable extends Piwik_ViewDataTable } $view = new Piwik_View($this->dataTableTemplate); - $view->arrayDataTable = $phpArray; + $view->arrayDataTable = $this->getPHPArrayFromDataTable(); $view->dataTableColumns = $columns; + $view->columnTranslations = $columnTranslations; $view->nbColumns = $nbColumns; $view->javascriptVariablesToSet = $this->getJavascriptVariablesToSet(); $view->properties = $this->getViewProperties(); @@ -140,17 +138,7 @@ class Piwik_ViewDataTable_HtmlTable extends Piwik_ViewDataTable $phpArray = $renderer->originalRender(); return $phpArray; } - - /** - * Sets the columns that will be displayed in the HTML output - * By default all columns are displayed ($columnsNames = array() will display all columns) - * - * @param array $columnsNames Array of column names eg. array('nb_visits','nb_hits') - */ - public function setColumnsToDisplay( $columnsNames) - { - $this->columnsToDisplay = $columnsNames; - } + /** * Adds a column to the list of columns to be displayed @@ -161,44 +149,6 @@ class Piwik_ViewDataTable_HtmlTable extends Piwik_ViewDataTable { $this->columnsToDisplay[] = $columnName; } - - /** - * Returns array( - * array('name' => 'nb_visits', 'displayName' => 'Visits'), - * array('name' => 'nb_uniq_visitors', 'displayName' => 'Unique Visitors'), - * - * @param array PHP array conversion of the data table - * @return array - */ - protected function getColumnsToDisplay($phpArray) - { - $metadataColumnToDisplay = array(); - if(count($phpArray) > 0) - { - // we show the columns in order specified in the setColumnsToDisplay - // each column has a string name; - // this name will for example be used to specify the sorting column - $columnsToDisplay = $this->columnsToDisplay; - - if(count($columnsToDisplay) == 0) - { - $columnsToDisplay = array_keys($phpArray[0]['columns']); - } - - $columnsToDisplay = array_unique($columnsToDisplay); - foreach($columnsToDisplay as $columnToDisplay) - { - if(!empty($columnToDisplay)) - { - $metadataColumnToDisplay[] = array( - 'name' => $columnToDisplay, - 'displayName' => $this->getColumnTranslation($columnToDisplay) - ); - } - } - } - return $metadataColumnToDisplay ; - } /** * Sets the columns in the HTML table as not sortable (they are not clickable) diff --git a/core/ViewDataTable/Sparkline.php b/core/ViewDataTable/Sparkline.php index 1982cba555..4a2d0c1514 100644 --- a/core/ViewDataTable/Sparkline.php +++ b/core/ViewDataTable/Sparkline.php @@ -23,18 +23,6 @@ class Piwik_ViewDataTable_Sparkline extends Piwik_ViewDataTable return 'sparkline'; } - protected $columnsToDisplay = array(); - - public function setColumnsToDisplay($columns) - { - $this->columnsToDisplay = $columns; - } - - public function getColumnsToDisplay() - { - return $this->columnsToDisplay; - } - /** * @see Piwik_ViewDataTable::main() */ @@ -46,7 +34,7 @@ class Piwik_ViewDataTable_Sparkline extends Piwik_ViewDataTable } $this->mainAlreadyExecuted = true; - // we load the data with the filters applied + $this->disableGenericFilters(); $this->loadDataTableFromAPI(); $this->isDataAvailable = $this->dataTable->getRowsCount() != 0; @@ -55,7 +43,6 @@ class Piwik_ViewDataTable_Sparkline extends Piwik_ViewDataTable throw new Exception(Piwik_Translate('General_NoDataForGraph')); } $values = $this->getValuesFromDataTable($this->dataTable); - $graph = new Piwik_Visualization_Sparkline; $graph->setValues($values); $graph->main(); @@ -63,24 +50,17 @@ class Piwik_ViewDataTable_Sparkline extends Piwik_ViewDataTable $this->view = $graph; } - protected function getValuesFromDataTable( Piwik_DataTable_Array $dataTableArray) + protected function getValuesFromDataTableArray( $dataTableArray, $columnToPlot ) { $dataTableArray->applyQueuedFilters(); - - $columns = $this->getColumnsToDisplay(); - $columnToPlot = false; - if(!empty($columns)) - { - $columnToPlot = $columns[0]; - } $values = array(); - foreach($dataTableArray->getArray() as $keyName => $table) + foreach($dataTableArray->getArray() as $table) { - $value = 0; if($table->getRowsCount() > 1) { throw new Exception("Expecting only one row per DataTable"); } + $value = 0; $onlyRow = $table->getFirstRow(); if($onlyRow !== false) { @@ -100,4 +80,26 @@ class Piwik_ViewDataTable_Sparkline extends Piwik_ViewDataTable } return $values; } + + protected function getValuesFromDataTable( $dataTable ) + { + $columns = $this->getColumnsToDisplay(); + $columnToPlot = false; + if(!empty($columns)) + { + $columnToPlot = $columns[0]; + } + + // a Piwik_DataTable_Array is returned when using the normal code path to request data from Archives, in all core plugins + // however plugins can also return simple datatable, hence why the sparkline can accept both data types + if($this->dataTable instanceof Piwik_DataTable_Array) + { + $values = $this->getValuesFromDataTableArray($dataTable, $columnToPlot); + } + elseif($this->dataTable instanceof Piwik_DataTable) + { + $values = $this->dataTable->getColumn($columnToPlot); + } + return $values; + } } diff --git a/core/Visualization/Chart.php b/core/Visualization/Chart.php index 2f12137255..9a1ca5ebc2 100644 --- a/core/Visualization/Chart.php +++ b/core/Visualization/Chart.php @@ -29,6 +29,7 @@ abstract class Piwik_Visualization_Chart implements Piwik_iView protected $yLabels = array(); protected $yValues = array(); + protected $yUnits = array(); function __construct() { @@ -39,21 +40,27 @@ abstract class Piwik_Visualization_Chart implements Piwik_iView { $this->xLabels = $xLabels; } + + public function setAxisXOnClick($onClick) + { + $this->xOnClick = $onClick; + } public function setAxisYValues($values) { $this->yValues = $values; } + + function setAxisYUnits($yUnits) + { + $this->yUnits = $yUnits; + } public function setAxisYLabels($labels) { $this->yLabels = $labels; } - public function setAxisXOnClick($onClick) - { - $this->xOnClick = $onClick; - } //TODO call + make sure matches beginning of period? (hard..) // day -> every 7 days @@ -153,6 +160,16 @@ abstract class Piwik_Visualization_Chart implements Piwik_iView $maxValue = 1; } $this->y->set_range( $minValue, $maxValue, $stepsEveryNLabel); + $dataSetsToDisplay = $this->getDataSetsToDisplay(); + if($dataSetsToDisplay != false) + { + $dataSetToDisplay = current($dataSetsToDisplay); + if(isset($this->yUnits[$dataSetToDisplay])) + { + $unit = $this->yUnits[$dataSetToDisplay]; + $this->y->set_label_text("#val#$unit"); + } + } // Tooltip $this->tooltip = new tooltip(); diff --git a/core/Visualization/Chart/Evolution.php b/core/Visualization/Chart/Evolution.php index 6cef02f6e0..314acbffd9 100644 --- a/core/Visualization/Chart/Evolution.php +++ b/core/Visualization/Chart/Evolution.php @@ -18,12 +18,6 @@ require_once "Visualization/Chart.php"; */ class Piwik_Visualization_Chart_Evolution extends Piwik_Visualization_Chart { - protected $yValuesType = array(); - function setAxisYValuesTypes($yValuesTypes) - { - $this->yValuesTypes = $yValuesTypes; - } - function customizeGraph() { parent::customizeGraph(); @@ -71,13 +65,10 @@ class Piwik_Visualization_Chart_Evolution extends Piwik_Visualization_Chart $lineValue = new hollow_dot($value); $unit = ''; - if(!empty($this->yValuesTypes[$dataSetToDisplay])) + if(!empty($this->yUnits[$dataSetToDisplay])) { - $unit = $this->yValuesTypes[$dataSetToDisplay]; + $unit = $this->yUnits[$dataSetToDisplay]; } - // set the Y Label to display the right unit - $this->y->set_label_text("#val#$unit"); - $lineValue->tooltip("$label<br>$value$unit $labelName"); if(!empty($this->xOnClick)) { @@ -97,7 +88,7 @@ class Piwik_Visualization_Chart_Evolution extends Piwik_Visualization_Chart // if one column is a percentage we set the grid accordingly // note: it is invalid to plot a percentage dataset along with a numeric dataset //TODO only if the max was 100!! - if(array_search('%', $this->yValuesTypes) !== false) + if(array_search('%', $this->yUnits) !== false) { $this->y->set_range( 0, 100, 50); } diff --git a/core/Visualization/Chart/Pie.php b/core/Visualization/Chart/Pie.php index 8ea8e7e2ec..12effb7f6c 100644 --- a/core/Visualization/Chart/Pie.php +++ b/core/Visualization/Chart/Pie.php @@ -49,6 +49,7 @@ class Piwik_Visualization_Chart_Pie extends Piwik_Visualization_Chart // create the Pie values $yValues = $this->yValues[$dataSetToDisplay]; $labelName = $this->yLabels[$dataSetToDisplay]; + $unit = @$this->yUnits[$dataSetToDisplay]; $sum = array_sum($yValues); $pieValues = array(); $i = 0; @@ -61,7 +62,7 @@ class Piwik_Visualization_Chart_Pie extends Piwik_Visualization_Chart } $pieValue = new pie_value($value, $label); $percentage = round(100 * $value / $sum); - $pieValue->set_tooltip("$label <br>$percentage% ($value $labelName)"); + $pieValue->set_tooltip("$label<br>$percentage% ($value$unit $labelName)"); $pieValues[] = $pieValue; } $pie->set_values($pieValues); diff --git a/core/Visualization/Chart/VerticalBar.php b/core/Visualization/Chart/VerticalBar.php index a87487bb51..c9794f4f00 100644 --- a/core/Visualization/Chart/VerticalBar.php +++ b/core/Visualization/Chart/VerticalBar.php @@ -52,19 +52,17 @@ class Piwik_Visualization_Chart_VerticalBar extends Piwik_Visualization_Chart // create the bar values $yValues = $this->yValues[$dataSetToDisplay]; $labelName = $this->yLabels[$dataSetToDisplay]; - $sum = array_sum($yValues); + $unit = @$this->yUnits[$dataSetToDisplay]; $barValues = array(); $i = 0; foreach($this->xLabels as $label) { $value = $yValues[$i]; $barValue = new bar_value($value); - $percentage = round(100 * $value / $sum); - $barValue->set_tooltip("$label <br>$value $labelName ($percentage%)"); + $barValue->set_tooltip("$label<br>$value$unit $labelName"); $barValues[] = $barValue; $i++; } $bar->set_values($barValues); $this->chart->add_element($bar); } - } diff --git a/core/Visualization/Cloud.php b/core/Visualization/Cloud.php index 46ca66208d..287c57d86c 100644 --- a/core/Visualization/Cloud.php +++ b/core/Visualization/Cloud.php @@ -21,32 +21,15 @@ class Piwik_Visualization_Cloud { protected $wordsArray = array(); - public $truncatingLimit = 30; - /** - * @param array array( word => 10, word2 => 50, word3 => 1) - */ - function __construct($words = false) - { - if ($words !== false && is_array($words)) - { - foreach ($words as $word => $value) - { - $this->addWord($word, $value); - } - } - } - /* * Assign word to array - * * @param string $word * @return string */ function addWord($word, $value = 1) { - // $word = strtolower($word); if (isset($this->wordsArray[$word])) { $this->wordsArray[$word] += $value; @@ -56,11 +39,38 @@ class Piwik_Visualization_Cloud $this->wordsArray[$word] = $value; } } - + + public function render() + { + $this->shuffleCloud(); + $return = array(); + $maxValue = max($this->wordsArray); + foreach ($this->wordsArray as $word => $popularity) + { + $wordTruncated = $word; + if(strlen($word) > $this->truncatingLimit) + { + $wordTruncated = substr($word, 0, $this->truncatingLimit - 3).'...'; + } + $percent = ($popularity / $maxValue) * 100; + // CSS style value + $sizeRange = $this->getClassFromPercent($percent); + + $return[$word] = array( + 'word' => $word, + 'wordTruncated' => $wordTruncated, + 'value' => $popularity, + 'size' => $sizeRange, + 'percent' => $percent, + ); + } + return $return; + } + /* * Shuffle associated names in array */ - function shuffleCloud() + protected function shuffleCloud() { $keys = array_keys($this->wordsArray); @@ -76,30 +86,13 @@ class Piwik_Visualization_Cloud } /* - * Calculate size of words array - */ - - function getCloudSize() - { - return array_sum($this->wordsArray); - } - - /* * Get the class range using a percentage * * @returns int $class */ - function getClassFromPercent($percent) + protected function getClassFromPercent($percent) { - $mapping = array( - 95, - 70, - 50, - 30, - 15, - 5, - 0 - ); + $mapping = array(95, 70, 50, 30, 15, 5, 0); foreach($mapping as $key => $value) { if($percent >= $value) @@ -108,62 +101,5 @@ class Piwik_Visualization_Cloud } } } - - /* - * Create the HTML code for each word and apply font size. - * - * @returns string $spans - */ - - function render($returnType = "html") - { - $this->shuffleCloud(); - - if($returnType == "html") - { - $return = ''; - } - else - { - $return = array(); - } - - if (count($this->wordsArray) > 0) - { - $this->max = max($this->wordsArray); - - $return = ($returnType == "html" ? "" : ($returnType == "array" ? array() : "")); - foreach ($this->wordsArray as $word => $popularity) - { - - // truncating the word - $wordTruncated = $word; - if(strlen($word) > $this->truncatingLimit) - { - $wordTruncated = substr($word, 0, $this->truncatingLimit - 3).'...'; - } - - // computing the percentage - $percent = ($popularity / $this->max) * 100; - - // and the CSS style value - $sizeRange = $this->getClassFromPercent($percent); - - if ($returnType == "array") - { - $return[$word]['word'] = $word; - $return[$word]['wordTruncated'] = $wordTruncated; - $return[$word]['size'] = $sizeRange; - $return[$word]['percent'] = $percent; - } - else if ($returnType == "html") - { - $return .= "\n<span title='".$word."' class='word size{$sizeRange}'> {$wordTruncated} </span>"; - } - // print( $word ."=".$percent."<br>"); - } - } - return $return; - } } |