sparklines, and some tweaks

5 views
Skip to first unread message

Malcolm Wallace

unread,
Jun 12, 2011, 4:21:27 PM6/12/11
to Haskell Chart List
I enclose two patch bundles.

The first adds SparkLines to the Chart package. SparkLines are already available in a package called hsparklines:
http://hackage.haskell.org/package/hsparklines
but that package uses GD as its rendering framework, whereas I needed to render them with Cairo, and integrate with the nice Renderable framework in Chart, and with the Data.Colour package. Hence the re-implementation.

The second bundle contains some more minor tweaks to how things are drawn in Chart. I extended the option to draw background gridlines (where you could already choose to draw at ticks or at labels) to permit, in addition, drawing at major or minor ticks. I added an ability to draw the left/bottom axes in the middle of the diagram (typically at zero, although it is configurable) rather than at the edges. I added a function to help with replacing the axis tick labels completely (which is already possible, but this just makes it easier to accomplish). There is also a new option to rotate the tick labels on the axes (e.g. to avoid overlapping labels, which currently causes some of them to be omitted). The rotation does not yet fully calculate the right space to leave for the labels, but I hope to get to that soon.

Comments and questions welcome.

Regards,
Malcolm

sparklines.dpatch
bigbundle.dpatches

Tim Docker

unread,
Jun 13, 2011, 1:38:41 PM6/13/11
to Malcolm Wallace, haskell...@googlegroups.com
Hi Malcolm.

Thanks for these.

I've applied the simple patches:

Tue Apr 5 08:21:39 CDT 2011 malcolm...@me.com
* Finer control of where gridlines are placed.

Thu Jun 9 07:23:58 CDT 2011 malcolm...@me.com
* Make it easier to override the standard axis labels.

Thu Jun 9 07:26:28 CDT 2011 malcolm...@me.com
* whitespace only

The sparklines patch looks fine, though I'd prefer that it included a
test case. Can you create one?


Some questions:

Tue Apr 5 08:22:03 CDT 2011 malcolm...@me.com
* Allow axes to be drawn in the middle of the plot, e.g. at zeros.
The bottom and left axes can be moved to be drawn at any x and y coords.

1) Many of the functions rendering axes now take an extra parameter
(Range -> Double) which you have often called "zero". What is this param
for? Is there a reason it's an extra parameter rather than being a field
inthe AxisData or AxisT types?

2) The name RectEdge no longer seems appropriate for the type. Given
it's only currently used to specify the location of axes, do you think
it should be called AxisLocation, and moved to Axis.Types ?

3) Rather than shift the bottom/left axes into the middle of the chart
(which feels a bit odd to me), I'm wondering if a different layout type
altogether is appropriate here. I'd always though that there would be
multiple layouts, and Layout1 was just the first of these. Much of the
complexity in Layout1 relates to the fact that it tries to let you have
indepedent left and right axes, something that doesn't really make sense
when the axes are moved into the plot. i imagine:

-- | A Layout2 value is a simple plot area, with a horizontal and
-- vertical axes at a specified origin in the plot area.
data Layout2 x y = Layout2 {

layout2_background_ :: CairoFillStyle,
layout2_plot_background_ :: Maybe CairoFillStyle,

layout2_title_ :: String,
layout2_title_style_ :: CairoFontStyle,

layout2_x_axis_ :: LayoutAxis x,
layout2_y_axis_ :: LayoutAxis y,

layout2_origin :: (x,y),

layout2_margin_ :: Double,
layout2_plots_ :: [(Plot x y)],
layout2_legend_ :: Maybe LegendStyle,

-- | True if the grid is to be rendered on top of the Plots.
layout2_grid_last_ :: Bool
}

4) Again, at least one test plot using the new functionality would be
valuable.


Thu Jun 9 07:25:36 CDT 2011 malcolm...@me.com
* Allow axis labels to be rotated by an arbitary angle.

5) I like the idea of being able to rotate the axes labels. Around what
point in the label is the text rotated? Can you provide a test so I can see.

6) Will it be much effort to get the spacing right?

Thanks again,

Tim

Malcolm Wallace

unread,
Jun 21, 2011, 2:09:26 PM6/21/11
to Haskell Chart List
First, please forgive the tardiness of my response. I only have access to gmail for a couple of days a week. Also, since upgrading to Snow Leopard and ghc-7 on the machine which has gmail access, I can no longer install Chart there. :-( So for various tedious reasons I'm ending up shuttling code between machines on USB sticks...

> The sparklines patch looks fine, though I'd prefer that it included a test case. Can you create one?

OK. In general, I use an extra layer of API on top of the Chart library, so it is rare that I have an example to hand of anything that uses the standard API. This is why my patches rarely include feature demos. Apologies for this. I'll try to be more rigorous in future.

> 1) Many of the functions rendering axes now take an extra parameter (Range -> Double) which you have often called "zero". What is this param for? Is there a reason it's an extra parameter rather than being a field inthe AxisData or AxisT types?

In the patch, the "zero" function is instantiated in layout1PlotAreaToGrid. In essence, the location of the axis line in the middle of the plot area must be determined by application of the axis_viewport function. When I wrote the code, I did not notice that the AxisT contains an AxisData, so all the information is already present for axisMapping to use without needing the extra argument. I'll refactor and revise the patch.

> 2) The name RectEdge no longer seems appropriate for the type. Given it's only currently used to specify the location of axes, do you think it should be called AxisLocation, and moved to Axis.Types ?

Could be, yes.

> 3) Rather than shift the bottom/left axes into the middle of the chart (which feels a bit odd to me), I'm wondering if a different layout type altogether is appropriate here.

It could be, but I'm not keen. In my particular application, I'm interfacing with another language which possesses neither parametric polymorphism nor typeclasses. I already have lots of code wrapper duplication to deal with different axis types (Doubles, Dates, etc). If I had to add more wrappers for different kinds of Layout too, I'd be even sadder.

> Thu Jun 9 07:25:36 CDT 2011 malcolm...@me.com
> * Allow axis labels to be rotated by an arbitary angle.
>
> 5) I like the idea of being able to rotate the axes labels. Around what point in the label is the text rotated? Can you provide a test so I can see.

In general, when a label is angled it is anchored at the tickmark on the appropriate axis, such that the text always lies outside the plot area itself. So on the bottom X axis, when the label is horizontal (0 or 180), it is centred; between 0 and 180, the anchor/tick is at the left edge of the text; and between 180 and 360 at the right edge of the text. Mutatis mutandis for the other axes.

> 6) Will it be much effort to get the spacing right?

No, I fixed it already.

A revised patch bundle is attached, more to follow later.

Regards,
Malcolm

revised.dpatches
Reply all
Reply to author
Forward
0 new messages