Skip to content

Added Line Chart Version 2 code #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 147 additions & 0 deletions src/CarlosIO/Geckoboard/Data/LineChartV2/Series.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
/**
* Created by PhpStorm.
* User: Mischa <mischa@webaction.com.au>
* Date: 24/10/2016
* Time: 9:49 AM
*/

namespace CarlosIO\Geckoboard\Data\LineChartV2;

/**
* Class Series
*
* @package CarlosIO\Geckoboard\Data\LineChartV2
*/
class Series
{

/**
* @var string
*/
protected $label;

/**
* @var
*/
protected $value;

/**
* @var array
*/
public $data;

/**
* @var string
*/
public $name;

/**
* @var
*/
public $incompleteFrom = null;

/**
* @var
* Can be either "main", or "secondary"
* defaults to "main" if not specified.
*/
protected $type = "main";
private $yRequired = false;
private $size = 0;

public function setName($name) {
$this->name = $name;
}

public function setIncompleteFrom($incompleteFrom) {
$this->incompleteFrom = $incompleteFrom;
}

public function addData($x, $y = null) {
if ($y !== null) {
$this->yRequired = true;
$data = array($x, (float) $y);
} else if ($this->yRequired === true) {
throw new Exception("Y Data is required for this series of data as it has been used before");
} else {
$data = $x;
}
$this->data[] = $data;
$this->size++;
return $this;
}

public function length() {
return $this->size;
}

public function getData() {
return $this->data;
}

/**
* @return string
*/
public function getLabel() {
return $this->label;
}
/**
* @param $label
* @return $this
*/
public function setLabel($label) {
$this->label = $label;

return $this;
}

/**
* @return float
*/
public function getValue() {
return $this->value;
}

/**
* @param $value
* @return $this
*/
public function setValue($value) {
$this->value = $value;

return $this;
}

/**
* @return int
*/
public function getPreviousRank() {
return $this->previousRank;
}

/**
* @param $previousRank
* @return $this
*/
public function setPreviousRank($previousRank) {
$this->previousRank = $previousRank;

return $this;
}

/**
* @return array
*/
public function toArray() {
$result = array();
$result['name'] = $this->getName();
$result['data'] = $this->getData();
if($this->incompleteFrom !== null) {
$result['incomplete_from'] = $this->incompleteFrom;
}

return $result;
}

}
128 changes: 128 additions & 0 deletions src/CarlosIO/Geckoboard/Widgets/LineChartV2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
/**
* Created by PhpStorm.
* User: Mischa <mischa@webaction.com.au>
* Date: 24/10/2016
* Time: 9:55 AM
*/

namespace CarlosIO\Geckoboard\Widgets;

use CarlosIO\Geckoboard\Data\LineChartV2\Series;

/**
* Class LineChartV2
*
* @package CarlosIO\Geckoboard\Widgets
*/
class LineChartV2 extends Widget
{

CONST DIMENSION_X = 'x';
CONST DIMENSION_Y = 'y';
CONST DEFAULT_COLOUR = "ff9900";

/**
* @var string $name Line Chart Name
*/
protected $name;

/**
* @var array $data Line Chart Data
*/
protected $data;


/**
* @var array
*/
protected $series = array();

/**
* @var array
*/
protected $axis;

public function addSeries($series) {
$this->series[] = $series;
}


/**
* Set the elements to appear evenly spread along dimension
*
* @param string $dimension The dimension where labels will be displayed
* @param array $labels Labels displayed in this axis
* @return $this
*/
public function setAxis($dimension, $axis) {
$this->axis[$dimension] = $axis;

return $this;
}

/**
* Add a new label to an specific axis
*
* @param string $dimension The dimension where labels will be displayed
* @param mix $label Label displayed in this axis
*/
protected function addLabel($dimension, $label) {
if (!in_array($dimension, array(self::DIMENSION_X, self::DIMENSION_Y))) {
throw new \InvalidArgumentException(sprintf("Value '%s' is not a valid dimension", $dimension));
}

$this->axis[$dimension][] = $label;
}

/**
* Return axises in a 2D array
*/
public function getAxis() {
if (null === $this->axis) {
$this->axis[self::DIMENSION_X] = new Axis(self::DIMENSION_X);
$this->axis[self::DIMENSION_Y] = new Axis(self::DIMENSION_Y);
}

return $this->axis;
}

/**
* Get data in array format.
*
* @return array
*/
public function getData() {
$result = array();

if (!empty($this->axis[self::DIMENSION_X])) {
$result['x_axis'] = $this->axis[self::DIMENSION_X]->toArray();
}

if (!empty($this->axis[self::DIMENSION_Y])) {
$result['y_axis'] = $this->axis[self::DIMENSION_Y]->toArray();
}

foreach ($this->series as $series) {
$result['series'][] = $series;
}
return $result;
}


/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}
}