4

I'm trying to generate a pie chart using axlsx for ruby. Everything is working fine but I can't seem to get data labels to appear for each "pie slice". What I'm shooting for is more or less like this image. (ie it should have the data labels and leader lines). It would also be fine to have the labels just have the numerical value and include a legend with the color mapping as usual.

Anyone know how to do this?

Thanks

gdelfino
  • 11,053
  • 6
  • 44
  • 48
jmichels
  • 43
  • 3
  • Welcome to Stack Overflow. I'm not an axlsx expert, but could you please post you code ? Take a look at http://stackoverflow.com/faq#questions and http://stackoverflow.com/questions/how-to-ask if you want to improve your post – Jean-Rémy Revy Jul 18 '12 at 07:26

1 Answers1

8

This is randym, the author of axlsx. The short version is that this has not been implemented yet, so you are not going to be able to get labels into the chart as per your image.

The biggest missing piece from the spec is (ECMA-376 - Part 1)

21.2.2.49 dLbls (Data Labels)

Which needs to be plugged into either series, chart, or both as it appears to be a common element of all three.

I will have sometime later this week to have a deeper look, but if you are up for contributing to axlsx - grab me on freenode (#axlsx) or post an issue to the repo https://github.com/randym/axlsx

UPDATE

Turns out this was much easier than I anticipated.

There is now a working version on master (https://github.com/randym/axlsx) for pie charts. I'll update the rest of the supported chart types over the next few days and shoot for an official release next week.

Thanks for letting me know that you needed this.

Here is a simple example of how to use it:

require 'axlsx'
p = Axlsx::Package.new
wb = p.workbook

# Pie Chart
wb.add_worksheet(:name => "Pie Chart") do |sheet|
  sheet.add_row ["First", "Second", "Third", "Fourth"]
  sheet.add_row [1, 2, 3, 4]
  sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,2], :end_at => [5, 15], :title=> 'dark corner here') do |chart|
    chart.add_series :data => sheet["A2:D2"], :labels => sheet["A1:D1"]
    chart.d_lbls.show_val = true
    chart.d_lbls.show_percent = true
    chart.d_lbls.d_lbl_pos = :outEnd
    chart.d_lbls.show_leader_lines = true
  end
end

p.serialize 'pie_chart_with_data_labels.xlsx'

Available attributes for d_lbls are:

lbl_pos Must be one of :bestFit, :b, :ctr, :inBase, :inEnd, :l, :outEnd, :r, :t By default axlsx will use :bestFit

and the following boolean attributes:

show_legend_key

show_val

show_cat_name

show_ser_name

show_percent

show_bubble_size

show_leader_lines

randym
  • 2,430
  • 1
  • 19
  • 18
  • Hi @randym, could you please have a look at this question:http://stackoverflow.com/questions/23986981/uninitialized-constant-zipdostime-error-when-using-axlsx-gem-in-rails-app I could not find any good documentation on the serialize method of package in axlsx. I am facing some difficulties while trying to use axlsx in my Rails app but its working perfectly in a separate ruby program. Would appreciate any kind of help in this regard. Thank you! – K M Rakibul Islam Jun 02 '14 at 15:18
  • Best kind of answer is the one that changes the api to magically solve your problem hahaha – SparK Jun 16 '15 at 20:18