How to add data labels using axlsx
Pergunta
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
Solução
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