sfSparklinePlugin - 1.0.1

sfSparklinePlugin provides you with lib to use Sparklines.

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source

sfSparklinePlugin

The sfSparklinePlugin is a symfony plugin that provides abstraction for Sparklines. Creates chart images on the fly basing on array data.

Examples

Back in the dot-com heyday, you might remember that Cisco, EMC, Sun, and Oracle were nicknamed "the four horsemen of the Internet". You might now say: "companies that ride together, slide together." (quoted from http://sparkline.org/)

example sparkline 1

example sparkline 2

example sparkline 3

example sparkline 4

example sparkline 5

example sparkline 6

Installation

Install the plugin via the subversion repository:

$ svn co http://svn.symfony-project.com/plugins/sfSparklinePlugin/trunk plugins/sfSparklinePlugin

from the project root directory or by using the command:

$ ./symfony plugin:install sfSparklinePlugin

sfSparklineExample module

The plugin is provided with a module consisting of sample actions rendering sparklines. To use it, app/config/settings.yml shall have sfSparklineExample module enabled. Then, go to sfSparklinePlugin/config/routing.yml file and uncomment all lines. Finally, run:

$ ./symfony cc

in the command line. Run the example module in your browser:

http://local.site/sfSparklineExample

in your browser. You shall see 3 different demo sparklines.

Usage

Those are several ways you can embed sparkline inside your HTML code:

<?php
 
  sfSparkline::createLine( // create sparkline line style
    '@sparkline_line', // if routing is specified or
    // 'sfSparklineExample/lineGraphData', // if routing unspecified
    250, // width of image
    60, // height of image
    array( // additional html img attributes
      'title' => 'sparkline line style',
      'alt' => 'sparkline alternative text',
    ));
 
  sfSparkline::createBar( // create sparkline bar style
    '@sparkline_bar', // if routing is specified or
    // 'sfSparklineExample/lineGraphData', // if routing unspecified
    7, // single bar width
    3, // bar spacing
    50, // height of image
    array(
      'title' => 'sparkline bar style',
      'alt' => 'sparkline alternative text',
    ));
 
    sfSparkline::createLine(
      'sfSparklineExample/yahooChart', // action
      400, // width of image
      100); // height of image
 
?>

line Sparkline

This is how a bar style Sparkline looks like:

example line

and this is an example code how it can be done:

/**
 * Creates a line graph from random data
 *
 * @param sfRequest $request A request object
 */
public function executeLineGraphData(sfWebRequest $request)
{
  // retrieving size parameters
  $width = $request->getParameter('width');
  $height = $request->getParameter('height');
 
  // initializing object
  $sparkline = new sfSparklineLine();
 
  // setting size
  $sparkline->SetYMin(0);
  $sparkline->SetYMax(25);
 
  // some random data
  for( $i = 0; $i < 100; $i++ )
  {
    $sparkline->SetData($i, rand(1, $i/4));
  }
 
  // additional points on the image
  $sparkline->SetFeaturePoint(1, 1, 'red', 3, 'start', TEXT_TOP, FONT_3);
  $sparkline->SetFeaturePoint(50, 15, 'blue', 3);
 
  // internal sparkline calculation method
  $sparkline->Render($width, $height);
 
  // displaying sparkline
  $sparkline->Output();
 
  return sfView::NONE;
}

bar Sparkline

This is how a bar style Sparkline looks like:

example bar

and this is an example code how it can be done:

/**
 * Creates a bar graph from random data
 *
 * @param sfRequest $request A request object
 */
public function executeBarGraphData()
{
  // Storing the data in an array makes it easy to loop through
  // when adding later, but it can also be added one value at a time
  $data = Array(10,20,30,40,50,20,40,50,40,30,20,10,0,-10,-20,-10,10,20);
 
  // retrieving size parameters
  $bar = $request->getParameter('bar');
  $spacing = $request->getParameter('spacing');
  $height = $request->getParameter('height');
 
  // Next, create a new instance of Sparkline_Bar
  $graph = new sfSparklineBar();
 
  // Set the width of each bar in the graph
  $graph->setBarWidth($bar);
 
  // Set the spacing between the bars
  $graph->setBarSpacing($spacing);
 
  // Add a color called "mygray" to the available color list
  $graph->setColorHtml('background', '#EEEEEE');
 
  // Set the background color to this new color
  // As of 0.2, if you didn't set the color beforehand,
  // your background would be black
  $graph->setColorBackground('background');
 
  // Loop adding the data
  foreach ($data as $key => $value) {
    $graph->setData($key, $value, 'blue');
  }
 
  // Draw all necessary objects for our graph
  $graph->Render($height);
 
  // Displays the graph by sending a 'Content-type: image/png'
  // header then outputting the image data
  echo $graph->Output();
 
  return sfView::NONE;
}

Documentation

The official documentation of Sparkline can be found here.

Notes

  • Multiple inheritance: as for now, the plugin uses the deprecated symfony 1.0 feature called Mixins (sfMixer class from sfPropelPlugin). It's just a temporary solution. Future releases shall provide better ways to achieve multiple inheritance in Symfony.

  • Prerequisite: GD PHP library.