How to enable Gruff Pie Chart to show data values instead of percentages

I am using Gruff Pie chart for displaying a report of number of items brought for various categories.

Now instead of percentages, I want the number of items in each category to be shown against each pie of the chart.

Any clues?



What I did to solve this problem was added a attribute
@y_axis_formatter to the base class that stores a function to reformat
the label. You basically set the attribute to the method you want for
the graph you want to modify. The label is in the file base.rb around
line 473 and is called marker_label. I used the following syntax.

473: vendor/plugins/gruff/lib/base.rb
        marker_label = index * increment + @minimum_value.to_f

        #my changes
        unless @y_axis_formatter.nil?

        @d.fill = @marker_color

I use a standard helper to format the label
  labelFormatter= { |label| number_with_precision(label, 0 ) }

You will need to include the helper in your controller if you do it
this way or write your own formatter method.
And finally you just need to specify your formatter before rendering
the graph in your controller.
      graph.y_axis_formatter= labelFormatter
Using this technique you can fully customize the labels. Hope this

Chris Burnley

Jatinder Singh wrote:

Lines of code in Base.rb seem to be inside draw_line_markers method.
Does the solution given by you work for Pie chart? as I was trying to trace Pie chart instance and it seems draw_line_markers does not get called.


I am not using pie charts. Looking briefly at the code though it looks
like the following code is where you would need to make the change in
pie.rb. ~ line 40

        @d = draw_label(center_x,center_y,
                    ((data_row[1][0] / total_sum) * 100).round.to_s +
'% ')

I would personally move that calculation of the amount variable out of
the call to draw_label to make your changes explicit. You could just as
easily provide a callback before the call to do something special with
the label. It looks like the draw_label method scales the label
appropriately based on what you send it so...

Chris Burnley

After modifying pie.rb it works fine.
But modifying pie.rb would not be the right solution, Is there any alternative?
I was thinking of extending pie.rb and overriding method draw of Pie class.or passing a block to the draw method of pie chart which would calcuate the amount to be shown.

I dont want to change Gruff library’s code, because then there would be additional maintainability involved.

Any clues?