/** * Creates and returns the JSON encoded chart details, options and data * from the injected Chart, generally called by the view layer * @param Chart $chart Instance of Phighchart\Chart * @return String $ret The JSON string to display the complete chart */ public function render(Chart $chart) { if ($chart->getData()->getCounts()) { // prepare the data $series = array(); foreach ($chart->getData()->getCounts() as $key => $count) { // prepare the series item $seriesItem = new \StdClass(); $seriesItem->name = $key; $seriesItem->y = $count; // add the sticky colour if present if ($colour = $this->_getStickyColour($chart, $key)) { $seriesItem->color = $colour; } // add to the series $series[] = $seriesItem; } // make it a pie chart and pass back in the prepared data $chartSeriesOptions = $chart->getOptionsType('series', new Container('series')); $chartSeriesOptions->setType('pie'); $chartSeriesOptions->setData($series); // get all the existing options $options = $chart->getOptionsForOutput(); // add the series as a nested array $options['series'] = array($chartSeriesOptions->getOptions()); // send back the prepared chart JS return $this->outputJavaScript($chart, $options); } return ''; }
/** * Returns sticky colour for the given key if the extended options are set * and a sticky colour is defined for the given key. * @param Chart $chart Chart instances * @param string $key Key to get the sticky colour for * @return bool FALSE if extended options not set or the sticky colour not * found string sticky colour if found for the given key * @author Shahrukh Omar <*****@*****.**> */ protected function _getStickyColour(Chart $chart, $key) { //return False if the extended options are not set if (!$chart->getExtendedOptions()) { return FALSE; } //return the sticky colour if it is set for the given key if ($colour = $chart->getExtendedOptions()->getStickyColour($key)) { return $colour; } return FALSE; }
/** * Tests the xAxis format options for the linear type */ public function testFormatOptions() { $data = new Data(); $data->addSeries('Test', array('01-08-2012' => 18, '02-08-2012' => 4, '03-08-2012' => 12)); $chart = new Chart(); $chart->setData($data); $chart->setRenderer(new Area()); $linear = new Linear(); $formatOptions = $linear->getFormatOptions($chart); $this->assertInstanceOf('Phighchart\\Options\\Container', $formatOptions); $expected = new \StdClass(); //for linear format the xAxis labels are set as categories $expected->categories = array('01-08-2012', '02-08-2012', '03-08-2012'); $this->assertEquals($expected, $formatOptions->getOptions()); $this->assertSame('xAxis', $formatOptions->getOptionsType()); }
public function render(Chart $chart) { //set the chart type to area $chartSection = $chart->getOptionsType('chart', new Container('chart')); $chartSection->setType('area'); $chart->addOptions($chartSection); //prepare series array $series = array(); //formatter for rendering this chart $format = $chart->getFormat(); foreach ($chart->getData()->getSeries() as $key => $seriesData) { $member = new \StdClass(); $member->name = $key; $member->data = $format->getFormattedChartData($seriesData); //check if a sticky colour is defined for the key of this member if ($colour = $this->_getStickyColour($chart, $key)) { $member->color = $colour; } //commit member to the series $series[] = $member; } if ($formatOptions = $format->getFormatOptions($chart)) { $chart->addOptions($formatOptions); } //return the series data $options = $chart->getOptionsForOutput(); $options['series'] = $series; return $this->outputJavaScript($chart, $options); }
/** * Creates and returns the JSON encoded chart details, options and data * from the injected Chart, generally called by the view layer * @param Chart $chart Instance of Phighchart\Chart * @return String $ret The JSON string to display the complete chart */ public function render(Chart $chart) { // make it a line chart $chartOptions = $chart->getOptionsType('chart', new Container('chart')); $chartOptions->setType('column'); $chart->addOptions($chartOptions); // prepare the data $series = array(); //formatter for rendering this chart $format = $chart->getFormat(); // for each series of data (a series is a line) foreach ($chart->getData()->getSeries() as $key => $seriesData) { // add each one to the chart along with a sticky colour if present $seriesItem = new \StdClass(); $seriesItem->name = $key; $seriesItem->data = $format->getFormattedChartData($seriesData); if ($colour = $this->_getStickyColour($chart, $key)) { $seriesItem->color = $colour; } $series[] = $seriesItem; } if ($formatOptions = $format->getFormatOptions($chart)) { $chart->addOptions($formatOptions); } // get all the existing options $options = $chart->getOptionsForOutput(); // add the series data $options['series'] = $series; // send back the prepared chart JS return $this->outputJavaScript($chart, $options); }
public function testRenderWithNoData() { $options = new OptionsContainer('chart'); $options->setRenderTo('chart_example_59'); $options->setMarginRight(130); $options->setMarginBottom(25); $data = new Data(); // put it all together $chart = new Chart(); $chart->addOptions($options); $chart->setData($data); $chart->setRenderer(new Pie()); // test the full expected output $this->assertSame('', $chart->render()); $this->assertSame('<div id="chart_example_59"></div>', $chart->renderContainer()); }
/** * Sets the xAxis type as datetime and returns it in a Phighchart\Container * @param Chart $chart chart instance * @return Phighchart\Container */ public function getFormatOptions(Chart $chart) { $xAxis = $chart->getOptionsType('xAxis', new Container('xAxis')); $xAxis->setType('datetime'); return $xAxis; }
public function testSetRenderContainer() { $chart = new Chart(); $options = new Container('chart'); $chart->setRenderer(new Pie()); $options->setRenderTo('chart_123'); $chart->addOptions($options); $this->assertEquals($chart->renderContainer(), '<div id="chart_123"></div>'); $this->assertEquals($chart->renderContainer('span'), '<span id="chart_123"></span>'); }
public function testAreaRenderWithCustomDateTimeFormat() { $options = new OptionsContainer('chart'); $options->setRenderTo('chart_example_59'); $options->setMarginRight(130); $options->setMarginBottom(25); $titleOptions = new OptionsContainer('title'); $titleOptions->setText('Monthly Details'); $titleOptions->setX(-20); $data = new Data(); $data->addSeries('Apples', array('1st May, 2012 00:00:00' => 12, '2nd May, 2012 00:00:00' => 3, '3rd May, 2012 00:00:00' => 33))->addSeries('Oranges', array('1st May, 2012 00:00:00' => 32, '2nd May, 2012 00:00:00' => 36, '3rd May, 2012 00:00:00' => 18)); $format = new DatetimeFormat(); $format->setDateTimeFormat('jS F, Y H:i:s'); // put it all together $chart = new Chart(); $chart->addOptions($options); $chart->addOptions($titleOptions); $chart->setData($data); $chart->setFormat($format); $chart->setRenderer(new Area()); // test the full expected output $this->assertSame('var chart_example_59; chart_example_59 = new Highcharts.Chart({"chart":{"renderTo":"chart_example_59","marginRight":130,"marginBottom":25,"type":"area"},"title":{"text":"Monthly Details","x":-20},"xAxis":{"type":"datetime"},"series":[{"name":"Apples","data":[[Date.UTC(2012,4,01,00,00,00),12],[Date.UTC(2012,4,02,00,00,00),3],[Date.UTC(2012,4,03,00,00,00),33]]},{"name":"Oranges","data":[[Date.UTC(2012,4,01,00,00,00),32],[Date.UTC(2012,4,02,00,00,00),36],[Date.UTC(2012,4,03,00,00,00),18]]}]});', $chart->render()); $this->assertSame('<div id="chart_example_59"></div>', $chart->renderContainer()); }
$loader = new UniversalClassLoader(); $loader->register(); $loader->registerNamespaces(array('Phighchart' => __DIR__ . '/../classes')); use Phighchart\Chart; use Phighchart\Options\Container as OptionsContainer; use Phighchart\Options\ExtendedContainer as ExtendedOptionsContainer; use Phighchart\Data; use Phighchart\Renderer\Pie; use Phighchart\Renderer\Line; $extOptions = new ExtendedOptionsContainer(); $extOptions->setStickyColour('apples', '#629632'); $extOptions->setStickyColour('oranges', '#CD3700'); $options = new OptionsContainer('chart'); $options->setRenderTo('chart_example_59'); $options->setMarginRight(130); $options->setMarginBottom(25); $titleOptions = new OptionsContainer('title'); $titleOptions->setText('Monthly Details'); $titleOptions->setX(-20); $data = new Data(); $data->addCount('Apples', 32)->addCount('Oranges', 68)->addSeries('Apples', array('2012-05-01' => 12, '2012-05-02' => 3, '2012-05-03' => 33))->addSeries('Oranges', array('2012-05-01' => 32, '2012-05-02' => 36, '2012-05-03' => 18)); // put it all together $chart = new Chart(); $chart->addOptions($options)->addOptions($titleOptions)->addOptions($extOptions)->setData($data)->setRenderer(new Pie()); // a line chart is similiar, and our data container holds series data for this $lineChart = clone $chart; $options = new OptionsContainer('chart'); $options->setRenderTo('chart_example_60'); $options->setMarginRight(130); $options->setMarginBottom(25); $lineChart->addOptions($options)->setRenderer(new Line());