Sparklines

Summary: Easily embed sparklines inside wiki pages
Version: 2007-08-09
Prerequisites: pmwiki 2.2.0 beta
Status: works
Maintainer: Luigi
Categories: Images

Questions answered by this recipe

How to easily add sparklines into a wiki page.
Dynamically generate and show inline graphics like or and more.

Description

In order to let your wiki pages embed sparklines, just follow these simple steps:

  1. Download spark.phpΔ (Original was by Mark Pursey at http://intepid.com/stuff/sparklines/spark.php, but doesn't appear to have been updated since PHP version 3.) Alternatively, you might try and go to http://bitworking.org/projects/sparklines/ in order to gather the right url for your need.
  2. Type the proper url into your browser, something like
    http://www.example.com/sparklines/spark.php? (You might want to add the relevant parameters.)
    a sparkline should show.
  3. If it does, paste the same url into your wiki page and add "&.png" to the url in order to fool your wiki and let it print the image, and not the url itself.
    http://www.example.com/sparklines/spark.php?&.png
    a sparkline should show now, and not the url any more.

You will also need to be running a version of PHP that supports graphics. This can be checked by looking at phpinfo and verifying there is a status block for gd and that it supports png and possibly other image formats. This can be accomplished by building the extra distributions:

and recompiling PHP with:

    configure \
        --with-jpeg-dir=/usr/local/src/jpeg-6b \
        --with-png-dir=/usr/local/src/libpng-1.2.24 \
        --with-freetype-dir=/usr/local/src/freetype-2.3.5 \
        --with-gd \

In ext/gd/libgd/gd_jpeg.c changed the include location for jpeglib.h and jerror.h to /usr/local/src/jpeg-6b (where src/jpeg-6b is where the installation is located).

Notes

Some more details and live samples are available at http://www.kirpi.it/Technology/Sparklines

To create a built in definition to invoke a sparkline (instead of hardcoding an absolute URL), include the following into your local/config.php file.

    # Define the page variable {*Sparkline} for using the spark.php script
    $UrlSchemeTemp = (@$_SERVER['HTTPS']=='on' || @$_SERVER['SERVER_PORT']==443)
                 ? 'https' : 'http';
    $ScriptUrlTemp = $UrlSchemeTemp.'://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
    $UrlTemp = preg_replace('/pmwiki/', 'cookbook/spark', $ScriptUrlTemp, 1);
    $FmtPV['$Sparkline'] = "'$UrlTemp'";

Then invoke with

    {*$Sparkline}

followed by the "?" and parameters.

For Example

    http://pmwiki.org/sparklines/spark.php?style=2&series=32,26,23,73,37,41,26,41,26&.png

would become

    {*$Sparkline}?style=2&series=32,26,23,73,37,41,26,41,26&.png

Or, you can set in Site.InterMap a shortcut (supposing your spark.php is in /):

  Spark1:  /spark.php?style=1&series= 
  Spark2:  /spark.php?style=2&series= 

and write in the page (&.png required too):

  Spark2:32,26,23,73,37,41,26,41,26&.png

Release Notes

2007-08-09 This is just something put together in order to help solve the matter in minutes. More refined recipes could be developed (not by me), allowing for even better flexibility and integration with pmwiki. In case of any development (or issue) please let me know -- Luigi

Comments

See Also

Contributors

User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.