Skip to content

Commit

Permalink
Refs #4041, move Piwik_ViewDataTable_GenerateGraphData logic to new J…
Browse files Browse the repository at this point in the history
…qplotDataGenerator class that does not derive from ViewDataTable and remove recursive API request used to get graph data.
  • Loading branch information
Benaka Moorthi committed Jul 19, 2013
1 parent 53bf5b4 commit 0713062
Show file tree
Hide file tree
Showing 17 changed files with 449 additions and 629 deletions.
21 changes: 1 addition & 20 deletions core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,29 +601,10 @@ protected function setMetricsVariablesView(Piwik_ViewDataTable $view, $defaultMe
$columns = array($firstColumn);
}
// displayed columns
if ($labelDisplayed
&& !($view instanceof Piwik_ViewDataTable_GenerateGraphData)
) {
if ($labelDisplayed) {
array_unshift($columns, 'label');
}
$view->setColumnsToDisplay($columns);


// Continue only for graphs
if (!($view instanceof Piwik_ViewDataTable_GenerateGraphData)) {
return;
}
// do not sort if sorted column was initially "label" or eg. it would make "Visits by Server time" not pretty
if ($view->getSortedColumn() != 'label') {
$view->setSortedColumn($firstColumn);
}
// selectable columns
if (isset($view->period) && $view->period == 'day') {
$selectableColumns = array_merge($metricsForDay, $metricsForAllPeriods);
} else {
$selectableColumns = $metricsForAllPeriods;
}
$view->setSelectableColumns($selectableColumns);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions core/DataTable/Filter/AddSummaryRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ public function __construct($table,
*/
public function filter($table)
{
if ($table->getRowsCount() <= $this->startRowToSummarize + 1) {
return;
}
$table->filter('Sort',
array($this->columnToSortByBeforeTruncating, 'desc'));

if ($table->getRowsCount() <= $this->startRowToSummarize + 1) {
return;
}

$rows = $table->getRows();
$count = $table->getRowsCount();
$newRow = new Piwik_DataTable_Row();
Expand Down
194 changes: 194 additions & 0 deletions core/JqplotDataGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
* @category Piwik
* @package Piwik
*/

/**
* Generates JSON data used to configure and populate JQPlot graphs.
*
* Supports pie graphs, bar graphs and time serieses (aka, evolution graphs).
*/
class Piwik_JqplotDataGenerator
{
/**
* View properties. @see Piwik_ViewDataTable for more info.
*
* @var array
*/
protected $properties;

/**
* This object does most of the work in generating the JQPlot JSON data.
*
* @var Piwik_Visualization
*/
protected $visualization;

/**
* Creates a new JqplotDataGenerator instance for a graph type and view properties.
*
* @param string $type 'pie', 'bar', or 'evolution'
* @param array $properties The view properties.
* @return Piwik_JqplotDataGenerator
*/
public static function factory($type, $properties)
{
switch ($type) {
case 'evolution':
return new Piwik_JqplotDataGenerator_Evolution($properties);
case 'pie':
$visualization = new Piwik_Visualization_Chart_Pie();
return new Piwik_JqplotDataGenerator($visualization, $properties);
case 'bar':
$visualization = new Piwik_Visualization_Chart_VerticalBar();
return new Piwik_JqplotDataGenerator($visualization, $properties);
default:
throw new Exception("Unknown JqplotDataGenerator type '$type'.");
}
}

/**
* Constructor.
*
* @param Piwik_Visualization $visualization
* @param array $properties
*/
public function __construct($visualization, $properties)
{
$this->visualization = $visualization;
$this->properties = $properties;
}

/**
* Generates JSON graph data and returns it.
*
* @param Piwik_DataTable|Piwik_DataTable_Array $dataTable
* @return string
*/
public function generate($dataTable)
{
if (!empty($this->properties['graph_limit'])) {
$offsetStartSummary = $this->properties['graph_limit'] - 1;
$sortColumn = !empty($this->properties['filter_sort_column'])
? $this->properties['filter_sort_column']
: Piwik_Metrics::INDEX_NB_VISITS;

$dataTable->filter(
'AddSummaryRow', array($offsetStartSummary, Piwik_Translate('General_Others'), $sortColumn));
}

if ($dataTable->getRowsCount() > 0) {
// if addTotalRow was called in GenerateGraphHTML, add a row containing totals of
// different metrics
if (!empty($this->properties['add_total_row'])) {
$dataTable->queueFilter('AddSummaryRow', array(0, Piwik_Translate('General_Total'), null, false));
}

$this->initChartObjectData($dataTable);
}

$this->visualization->customizeChartProperties();

return $this->visualization->render();
}

protected function initChartObjectData($dataTable)
{
$dataTable->applyQueuedFilters();

// We apply a filter to the DataTable, decoding the label column (useful for keywords for example)
$dataTable->filter('ColumnCallbackReplace', array('label', 'urldecode'));

$xLabels = $dataTable->getColumn('label');

$columnNames = $this->properties['columns_to_display'];
if (($labelColumnIndex = array_search('label', $columnNames)) !== false) {
unset($columnNames[$labelColumnIndex]);
}

$columnNameToTranslation = $columnNameToValue = array();
foreach ($columnNames as $columnName) {
$columnNameToTranslation[$columnName] = @$this->properties['translations'][$columnName];

$columnNameToValue[$columnName] = $dataTable->getColumn($columnName);
}

$visualization = $this->visualization;
$visualization->setAxisXLabels($xLabels);
$visualization->setAxisYValues($columnNameToValue);
$visualization->setAxisYLabels($columnNameToTranslation);
$visualization->setAxisYUnit($this->properties['y_axis_unit']);
$visualization->setDisplayPercentageInTooltip($this->properties['display_percentage_in_tooltip']);

// show_all_ticks is not real query param, it is set by GenerateGraphHTML.
if ($this->properties['show_all_ticks']) {
$visualization->showAllTicks();
}

$units = $this->getUnitsForColumnsToDisplay();
$visualization->setAxisYUnits($units);

$this->addSeriesPickerToView();
}

protected function getUnitsForColumnsToDisplay()
{
// derive units from column names
$units = $this->deriveUnitsFromRequestedColumnNames();
if (!empty($this->properties['y_axis_unit'])) {
// force unit to the value set via $this->setAxisYUnit()
foreach ($units as &$unit) {
$unit = $this->properties['y_axis_unit'];
}
}

// the bar charts contain the labels a first series
// this series has to be removed from the units
if ($this->visualization instanceof Piwik_Visualization_Chart_VerticalBar) {
array_shift($units);
}

return $units;
}

private function deriveUnitsFromRequestedColumnNames()
{
$idSite = Piwik_Common::getRequestVar('idSite', null, 'int');

$units = array();
foreach ($this->properties['columns_to_display'] as $columnName) {
$derivedUnit = Piwik_Metrics::getUnit($columnName, $idSite);
$units[$columnName] = empty($derivedUnit) ? false : $derivedUnit;
}
return $units;
}

/**
* Used in initChartObjectData to add the series picker config to the view object
* @param bool $multiSelect
*/
protected function addSeriesPickerToView()
{
if (count($this->properties['selectable_columns'])
&& Piwik_Common::getRequestVar('showSeriesPicker', 1) == 1
) {
$selectableColumns = array();
foreach ($this->properties['selectable_columns'] as $column) {
$selectableColumns[] = array(
'column' => $column,
'translation' => @$this->properties['translations'][$column],
'displayed' => in_array($column, $this->properties['columns_to_display'])
);
}

$this->visualization->setSelectableColumns(
$selectableColumns, $this->properties['allow_multi_select_series_picker']);
}
}
}
Loading

0 comments on commit 0713062

Please sign in to comment.