jmcnamara / rust_xlsxwriter

A Rust library for creating Excel XLSX files.
https://crates.io/crates/rust_xlsxwriter
Apache License 2.0
316 stars 25 forks source link

feature request: add support for chart series data labels #28

Closed jmcnamara closed 1 year ago

jmcnamara commented 1 year ago

Feature Request

Add support for chart series data labels, like this: https://xlsxwriter.readthedocs.io/working_with_charts.html#chart-series-option-data-labels-1

jmcnamara commented 1 year ago

@adriandelgado

I've added initial chart series data label support to main, You can now access those percentages you were looking for:

use rust_xlsxwriter::{Chart, ChartDataLabel, ChartType, Workbook, XlsxError};

fn main() -> Result<(), XlsxError> {
    let mut workbook = Workbook::new();
    let worksheet = workbook.add_worksheet();

    // Add some data for the chart.
    worksheet.write(0, 0, 15)?;
    worksheet.write(1, 0, 15)?;
    worksheet.write(2, 0, 30)?;

    // Create a new chart.
    let mut chart = Chart::new(ChartType::Pie);

    // Add a data series with formatting.
    chart
        .add_series()
        .set_values("Sheet1!$A$1:$A$3")
        .set_data_label(ChartDataLabel::new().show_percentage());

    // Add the chart to the worksheet.
    worksheet.insert_chart(0, 2, &chart)?;

    // Save the file.
    workbook.save("chart.xlsx")?;

    Ok(())
}

Output:

screenshot

adriandelgado commented 1 year ago

Awesome!

jmcnamara commented 1 year ago

Added in version v0.35.0.