1<?php 2/** 3 * Matomo - free/libre analytics platform 4 * 5 * @link https://matomo.org 6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later 7 * 8 */ 9namespace Piwik\Plugins\PagePerformance\JqplotDataGenerator; 10 11use Piwik\Common; 12use Piwik\ProxyHttp; 13 14/** 15 * 16 */ 17class Chart extends \Piwik\Plugins\CoreVisualizations\JqplotDataGenerator\Chart 18{ 19 // the data kept here conforms to the jqplot data layout 20 // @see http://www.jqplot.com/docs/files/jqPlotOptions-txt.html 21 protected $series = array(); 22 protected $data = array(); 23 protected $axes = array(); 24 25 // temporary 26 public $properties; 27 28 public function setAxisXLabels($xLabels, $xTicks = null, $index = 0) 29 { 30 $axisName = $this->getXAxis($index); 31 32 $xSteps = $this->properties['x_axis_step_size']; 33 $showAllTicks = $this->properties['show_all_ticks']; 34 35 $this->axes[$axisName]['labels'] = array_values($xLabels); 36 37 $ticks = array_values($xTicks ?: $xLabels); 38 39 if (!$showAllTicks) { 40 // unset labels so there are $xSteps number of blank ticks between labels 41 foreach ($ticks as $i => &$label) { 42 if ($i % $xSteps != 0) { 43 $label = ' '; 44 } 45 } 46 } 47 $this->axes[$axisName]['ticks'] = $ticks; 48 } 49 50 public function setAxisXOnClick(&$onClick) 51 { 52 $this->axes['xaxis']['onclick'] = & $onClick; 53 } 54 55 public function setAxisYValues(&$values, $seriesLabels = null) 56 { 57 $this->series = $seriesLabels; 58 array_walk_recursive($values, function (&$v) { 59 $v = (float) Common::forceDotAsSeparatorForDecimalPoint($v); 60 }); 61 $this->data = &$values; 62 } 63 64 public function setAxisYUnits($yUnits) 65 { 66 $yUnits = array_values(array_map('strval', $yUnits)); 67 68 // generate axis IDs for each unique y unit 69 $axesIds = array(); 70 foreach ($yUnits as $idx => $unit) { 71 if (!isset($axesIds[$unit])) { 72 // handle axes ids: first y[]axis, then y[2]axis, y[3]axis... 73 $nextAxisId = empty($axesIds) ? '' : count($axesIds) + 1; 74 75 $axesIds[$unit] = 'y' . $nextAxisId . 'axis'; 76 } 77 } 78 79 // generate jqplot axes config 80 foreach ($axesIds as $unit => $axisId) { 81 $this->axes[$axisId]['tickOptions']['formatString'] = '%s' . $unit; 82 } 83 84 // map each series to appropriate yaxis 85 foreach ($yUnits as $idx => $unit) { 86 $this->series[$idx]['yaxis'] = $axesIds[$unit]; 87 } 88 } 89 90 public function setAxisYLabels($labels) 91 { 92 foreach ($this->series as &$series) { 93 $label = $series['internalLabel']; 94 if (isset($labels[$label])) { 95 $series['label'] = $labels[$label]; 96 } 97 } 98 } 99 100 public function render() 101 { 102 ProxyHttp::overrideCacheControlHeaders(); 103 104 // See http://www.jqplot.com/docs/files/jqPlotOptions-txt.html 105 $data = array( 106 'params' => array( 107 'axes' => &$this->axes, 108 'series' => &$this->series 109 ), 110 'data' => &$this->data 111 ); 112 113 return $data; 114 } 115 116 public function setAxisXLabelsMultiple($xLabels, $seriesToXAxis, $ticks = null) 117 { 118 foreach ($xLabels as $index => $labels) { 119 $this->setAxisXLabels($labels, $ticks === null ? null : $ticks[$index], $index); 120 } 121 122 foreach ($seriesToXAxis as $seriesIndex => $xAxisIndex) { 123 $axisName = $this->getXAxis($xAxisIndex); 124 125 // don't actually set xaxis otherwise jqplot will show too many axes. however, we need the xaxis labels, so we add them 126 // to the jqplot config 127 $this->series[$seriesIndex]['_xaxis'] = $axisName; 128 } 129 } 130 131 private function getXAxis($index) 132 { 133 $axisName = 'xaxis'; 134 if ($index != 0) { 135 $axisName = 'x' . ($index + 1) . 'axis'; 136 } 137 return $axisName; 138 } 139} 140