Skip to content

Commit

Permalink
Refs #4077, fix several bugs in treemap visualization, and show evolu…
Browse files Browse the repository at this point in the history
…tion in tooltip.
  • Loading branch information
Benaka Moorthi committed Aug 25, 2013
1 parent e009d47 commit b196fb1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
21 changes: 19 additions & 2 deletions plugins/TreemapVisualization/API.php
Expand Up @@ -11,6 +11,7 @@
namespace Piwik\Plugins\TreemapVisualization;

use Piwik\Common;
use Piwik\Period\Range;
use Piwik\API\Request;

class API
Expand All @@ -33,17 +34,33 @@ public static function getInstance()
* to data usable by the treemap visualization. E.g. 'Actions.getPageUrls'.
* @param string $column The column to generate metric data for. If more than one column is supplied,
* the first is used and the rest discarded.
* @param string $period
* @param string $date
* @param int|bool $show_evolution_values Whether to calculate evolution values for each row or not.
* @return array
*/
public function getTreemapData($apiMethod, $column)
public function getTreemapData($apiMethod, $column, $period, $date, $show_evolution_values = false)
{
$dataTable = Request::processRequest("$apiMethod");
if ($period == 'range') {
$show_evolution_values = false;
}

$params = array();
if ($show_evolution_values) {
list($previousDate, $ignore) = Range::getLastDate($date, $period);
$params['date'] = $previousDate . ',' . $date;
}

$dataTable = Request::processRequest("$apiMethod", $params);

$columns = explode(',', $column);
$column = reset($columns);

$generator = new TreemapDataGenerator($column);
$generator->setInitialRowOffset(Common::getRequestVar('filter_offset', 0, 'int'));
if ($show_evolution_values) {
$generator->showEvolutionValues();
}
return $generator->generate($dataTable);
}
}
7 changes: 5 additions & 2 deletions plugins/TreemapVisualization/Treemap.php
Expand Up @@ -14,6 +14,7 @@
use Piwik\Common;
use Piwik\View;
use Piwik\Period\Range;
use Piwik\DataTable\Map;
use Piwik\Visualization\Graph;

/**
Expand Down Expand Up @@ -111,7 +112,7 @@ public static function getDefaultPropertyValues()
*/
public function isThereDataToDisplay($dataTable, $view)
{
if ($view->visualization_properties->show_evolution_values) {
if ($dataTable instanceof Map) { // will be true if calculating evolution values
$childTables = $dataTable->getArray();
$dataTable = end($childTables);
}
Expand All @@ -124,7 +125,9 @@ private function getGraphData($dataTable, $properties)
$generator = new TreemapDataGenerator($this->getMetricToGraph($properties['columns_to_display']));
$generator->setRootNodeName($properties['title']);
$generator->setInitialRowOffset($properties['filter_offset'] ?: 0);
if ($properties['visualization_properties']->show_evolution_values) {
if ($properties['visualization_properties']->show_evolution_values
&& Common::getRequestVar('period') != 'range'
) {
$generator->showEvolutionValues();
}

Expand Down
9 changes: 5 additions & 4 deletions plugins/TreemapVisualization/TreemapDataGenerator.php
Expand Up @@ -149,12 +149,13 @@ private function makeNodeFromRow($tableId, $rowId, $row, $pastRow)
}

// add evolution
if ($pastRow !== false) {
$pastValue = $pastRow->getColumn($this->metricToGraph) ?: 0;

if ($rowId !== DataTable::ID_SUMMARY_ROW
&& $this->showEvolutionValues
) {
if ($pastRow === false) {
$data['evolution'] = 0;
$data['evolution'] = 100;
} else {
$pastValue = $pastRow->getColumn($this->metricToGraph) ?: 0;
$data['evolution'] = CalculateEvolutionFilter::calculate(
$columnValue, $pastValue, $quotientPrecision = 0, $appendPercentSign = false);
}
Expand Down
13 changes: 10 additions & 3 deletions plugins/TreemapVisualization/javascripts/treemapViz.js
Expand Up @@ -128,6 +128,10 @@
} else {
var tooltip = node.name;
}
if (node.data.evolution) {
var greaterOrLess = node.data.evolution > 0 ? '>' : '<';
tooltip += ' ' + greaterOrLess + ' ' + Math.abs(node.data.evolution) + '%';
}
$nodeElement.attr('title', tooltip);

// set label color
Expand Down Expand Up @@ -193,7 +197,7 @@
} else if (evolution > 0) {
maxEvolution = Math.max(maxEvolution, evolution);
}
});
}, root);

// color each node
var self = this,
Expand All @@ -216,7 +220,7 @@
}

node.data.$color = color;
});
}, root);

this.labelColor = colors.label;
},
Expand Down Expand Up @@ -355,7 +359,10 @@
format: 'json',
column: this.param.columns,
filter_truncate: this.props.max_graph_elements - 1,
filter_limit: -1
filter_limit: -1,
expanded: 1,
depth: this.props.depth || 1,
show_evolution_values: this.props.show_evolution_values || 0
});

// make sure parallel load data requests aren't made
Expand Down

0 comments on commit b196fb1

Please sign in to comment.