Skip to content

Commit

Permalink
added support for donut charts, subset of pie chart
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkhill committed Aug 20, 2013
1 parent be87f06 commit b9d0569
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 8 deletions.
29 changes: 29 additions & 0 deletions controllers/gchart_examples.php
Expand Up @@ -275,6 +275,35 @@ public function pie_chart_basic()
$this->load->view('gcharts/pie_chart_basic');
}

public function donut_chart_basic()
{
$this->gcharts->load('PieChart');

$dataTable = $this->gcharts->DataTable('Foods');

$dataTable->addColumn('string', 'Foods', 'food');
$dataTable->addColumn('string', 'Amount', 'amount');

$p1 = rand(0,50);
$p2 = rand(0,50);
$p3 = rand(0,50);
$p4 = rand(0,50);

$dataTable->addRow(array('Pizza', $p1));
$dataTable->addRow(array('Beer', $p2));
$dataTable->addRow(array('Steak', $p3));
$dataTable->addRow(array('Bacon', $p4));

$config = array(
'title' => 'My Foods',
'pieHole' => .4
);

$this->gcharts->PieChart('Foods')->setConfig($config);

$this->load->view('gcharts/donut_chart_basic');
}

public function pie_chart_advanced()
{
$this->gcharts->load('PieChart');
Expand Down
36 changes: 28 additions & 8 deletions libraries/gcharts/charts/PieChart.php
Expand Up @@ -22,6 +22,7 @@ public function __construct($chartLabel)
$this->defaults = array_merge($this->defaults, array(
'is3D',
// 'slices',
'pieHole',
'pieSliceBorderColor',
'pieSliceText',
'pieSliceTextStyle',
Expand All @@ -44,7 +45,7 @@ public function is3D($is3D)
{
$this->addOption(array('is3D' => $is3D));
} else {
$this->error('Invalid value for is3D, must be type (boolean).');
$this->type_error(__FUNCTION__, 'boolean');
}

return $this;
Expand All @@ -67,7 +68,7 @@ public function pieSliceBorderColor($pieSliceBorderColor)
{
$this->addOption(array('pieSliceBorderColor' => $pieSliceBorderColor));
} else {
$this->error('Invalid value for pieSliceBorderColor, must be type (string).');
$this->type_error(__FUNCTION__, 'string');
}

return $this;
Expand Down Expand Up @@ -97,7 +98,7 @@ public function pieSliceText($pieSliceText)
{
$this->addOption(array('pieSliceText' => $pieSliceText));
} else {
$this->error('Invalid value for pieSliceText, must be type (string) '.$this->_array_string($values));
$this->type_error(__FUNCTION__, 'string', 'with a value of '.$this->_array_string($values));
}

return $this;
Expand All @@ -116,7 +117,26 @@ public function pieSliceTextStyle($textStyle)
{
$this->addOption(array('pieSliceTextStyle' => $textStyle));
} else {
$this->error('Invalid value for textStyle, must be an object type (textStyle).');
$this->type_error(__FUNCTION__, 'textStyle');
}

return $this;
}

/**
* If between 0 and 1, displays a donut chart. The hole with have a radius
* equal to number times the radius of the chart.
*
* @param numeric $pieHole
* @return \PieChart
*/
public function pieHole($pieHole)
{
if(is_numeric($pieHole) && $pieHole > 0 && $pieHole < 1)
{
$this->addOption(array('pieHole' => $pieHole));
} else {
$this->type_error(__FUNCTION__, 'numeric', 'while, 0 < $pieHole < 1 ');
}

return $this;
Expand All @@ -135,7 +155,7 @@ public function reverseCategories($reverseCategories)
{
$this->addOption(array('reverseCategories' => $reverseCategories));
} else {
$this->error('Invalid value for reverseCategories, must be type (boolean).');
$this->type_error(__FUNCTION__, 'boolean');
}
}

Expand All @@ -154,7 +174,7 @@ public function sliceVisibilityThreshold($sliceVisibilityThreshold)
{
$this->addOption(array('sliceVisibilityThreshold' => $sliceVisibilityThreshold));
} else {
$this->error('Invalid value for sliceVisibilityThreshold, must be (numeric).');
$this->type_error(__FUNCTION__, 'numeric');
}
}

Expand All @@ -171,7 +191,7 @@ public function pieResidueSliceColor($pieResidueSliceColor)
{
$this->addOption(array('pieResidueSliceColor' => $pieResidueSliceColor));
} else {
$this->error('Invalid value for pieResidueSliceColor, must be a valid HTML color type (string).');
$this->type_error(__FUNCTION__, 'string', 'representing a valide HTML color');
}
}

Expand All @@ -188,7 +208,7 @@ public function pieResidueSliceLabel($pieResidueSliceLabel)
{
$this->addOption(array('pieResidueSliceLabel' => $pieResidueSliceLabel));
} else {
$this->error('Invalid value for pieResidueSliceLabel, must be type (string).');
$this->type_error(__FUNCTION__, 'string');
}
}

Expand Down
50 changes: 50 additions & 0 deletions views/gcharts/donut_chart_basic.php
@@ -0,0 +1,50 @@
<h1><?php echo anchor('gchart_examples', 'Codeigniter gChart Examples'); ?> \ Basic Donut Chart</h1>
<?php
echo $this->gcharts->PieChart('Foods')->outputInto('food_div');
echo $this->gcharts->div(500,300);

if($this->gcharts->hasErrors())
{
echo $this->gcharts->getErrors();
}
?>

<hr />

<h2>Controller Code</h2>
<pre style="font-family:Courier New, monospaced; font-size:10pt;border:1px solid #000;background-color:#e0e0e0;padding:5px;">
$this->gcharts->load('PieChart');

$dataTable = $this->gcharts->DataTable('Foods');

$dataTable->addColumn('string', 'Foods', 'food');
$dataTable->addColumn('string', 'Amount', 'amount');

$p1 = rand(0,50);
$p2 = rand(0,50);
$p3 = rand(0,50);
$p4 = rand(0,50);

$dataTable->addRow(array('Pizza', $p1));
$dataTable->addRow(array('Beer', $p2));
$dataTable->addRow(array('Steak', $p3));
$dataTable->addRow(array('Bacon', $p4));

$config = array(
'title' => 'My Foods',
'pieHole' => .4
);

$this->gcharts->PieChart('Foods')->setConfig($config);
</pre>

<h2>View Code</h2>
<pre style="font-family:Courier New, monospaced; font-size:10pt;border:1px solid #000;background-color:#e0e0e0;padding:5px;">
echo $this->gcharts->PieChart('Foods')->outputInto('food_div');
echo $this->gcharts->div(500,300);

if($this->gcharts->hasErrors())
{
echo $this->gcharts->getErrors();
}
</pre>
1 change: 1 addition & 0 deletions views/gcharts/index.php
Expand Up @@ -15,6 +15,7 @@
<h2>PieCharts</h2>
<ul>
<li><h3><?php echo anchor('gchart_examples/pie_chart_basic', 'Basic'); ?></h3></li>
<li><h3><?php echo anchor('gchart_examples/donut_chart_basic', 'Donut'); ?></h3></li>
<li><h3><?php echo anchor('gchart_examples/pie_chart_advanced', 'Advanced'); ?></h3></li>
</ul>

Expand Down

0 comments on commit b9d0569

Please sign in to comment.