WordPress.org

Ready to get started?Download WordPress

Forums

Simple Sparkline graphs (6 posts)

  1. graemep
    Member
    Posted 9 years ago #

    A very simple plugin that allows the creation of simple sparklines (small graphs that fit in the flow of text). It uses James Byers' Sparkline PHP Graphing Library.

    This is my first attempt at a WordPress plug-in, so it is likely to be flawed. However it seems to work OK.

    More details, example sparklines and download.

    I am planning a few improvements. Please let me know if you find it useful.

    I would like to find a way of automatically scaling to the current texst size. I can not figure out a way of doing this, I was hoping that javascript might be able to pick it up, but I have not found anything there.

    Also if anyone if familiar enough with wp-cache to tell me if it would cache the output of this plug-in I would be grateful for the info.

  2. davidchait
    Member
    Posted 9 years ago #

    Very cool idea.

    I don't believe WP-cache will cache an on-the-fly image for you -- you'd need to cache it yourself, and then WP-cache would simply make it so that your dynamic code is only called for the first generation of the page. Note if the data was somehow dynamic, you'd lose that with WP-cache running...

    I could see sparklines being a quick, simple way to generate blog activity graphs (the blogging side or the visitor side) -- though I don't know that your plugin really was targeting that truly dynamic data source, as much as a static data set generating an image.

    If you just add graphic-caching to the plugin (assuming it doesn't), then it'd make for a pretty powerful graph-scripting language within a given post. If you need help, let me know.

    Extending it for dynamic data (somehow) would be the thing I'd want.

    As for finding the text height -- well, you might want to look at sIFR, as I think they are doing some level of dynamic processing (I assume they are looking at the css somehow...).

    -d

  3. graemep
    Member
    Posted 9 years ago #

    Thanks for the pointer to sIFR, they do what I want (or something pretty similar), and it looks like they use javascript to do it. Looks like I will have a bit of work to figure how how it works so I do not expect a quick result there.

    Yes, I am targeting simple static data - at the moment any way. My motivation was to illustrate economic and financial concepts on this site.

    As for dynamic data sources, what sources and displayed where? The simplest solution might be to call James Byer's library from a PHP script that has your dynamic data available, and then call the script from your templates. The library is undocumented but it is not hard to work out enough to use it from the source and the examples.

    Thanks for the offer of help. I will ask if (or perhaps I should say when!) I get stuck.

  4. MarkWubben
    Member
    Posted 9 years ago #

    (Pfew, I found my WordPress account again!)

    sIFR actually checks for element.offsetHeight, and removes the vertical padding from that number. That way you can get near the line-height almost perfectly in virtually all browsers. You have to specify the padding though.

    You can also use getComputedStyle and currentStyle to get at line-height directly (consult Google for more on that).

    If you have any further questions, please mail me (http://novemberborn.net/colophon/contact), as I'm not checking these forums anymore (and I'm not at home now, so I tend to forget about things like this)).

  5. heavysixer
    Member
    Posted 9 years ago #

    This is pretty interesting. I just started working with the idea of a sparkline stats system and am pleased to find there are already people hard at work on the idea.

  6. graemep
    Member
    Posted 9 years ago #

    David and Mark, thanks for the pointers. I should finish the next version, with graph heights automatically adjusted to text size, in the course of this week.

    I am cleaning it up so that the function that generates the graphs can be called directly with the data as parameters, rather than in the text of the post - that should make it reusable by other plugins and from templates. Obviously you can call the the PHP Sparkline library directly instead. My function just provides a simple wrapper plus the adjustment to match text size.

Topic Closed

This topic has been closed to new replies.

About this Topic