[GH-ISSUE #163] Question: How can lxw_chart_point be dynamically built? #134

Closed
opened 2026-05-05 11:45:25 -06:00 by gitea-mirror · 3 comments
Owner

Originally created by @TitanicHispanic on GitHub (Apr 19, 2018).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/163

If one had an indeterminate number of Data Points in a Series, can a lxw_chart_point variable be set by looping through a dataset, setting data-point-specific formatting/labeling? Does it behave like an array, or is there different syntax?

I don't have example code; I'm asking if it's feasible.

Originally created by @TitanicHispanic on GitHub (Apr 19, 2018). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/163 If one had an indeterminate number of Data Points in a Series, can a **lxw_chart_point** variable be set by looping through a dataset, setting data-point-specific formatting/labeling? Does it behave like an array, or is there different syntax? I don't have example code; I'm asking if it's feasible.
Author
Owner

@jmcnamara commented on GitHub (Apr 20, 2018):

Hi,

You could build up a dynamic array of points using something like the following:


#include "xlsxwriter.h"

int main() {

    lxw_workbook  *workbook  = new_workbook("chart_points.xlsx");
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
    lxw_chart     *chart     = workbook_add_chart(workbook, LXW_CHART_PIE);


    worksheet_write_number(worksheet, 0, 0, 2, NULL);
    worksheet_write_number(worksheet, 1, 0, 5, NULL);
    worksheet_write_number(worksheet, 2, 0, 4, NULL);

    lxw_chart_series *series = chart_add_series(chart, NULL,
                                                "=Sheet1!$A$1:$A$3");

    /* Some number of known points in the series. */
    uint8_t num_points = 3;

    /* Create an array of points. */
    lxw_chart_point **points = calloc(num_points +1, sizeof(lxw_chart_point *));


    /* Allocate some points with default (NULL) elements. */
    for (int i = 0; i < num_points; i++)
        points[i] = calloc(1, sizeof(lxw_chart_point));

    /* Edit some of the points */
    points[0]->fill = calloc(1, sizeof(lxw_chart_fill));
    points[0]->fill->color = 0xFF0000;

    /* Point 1 (zero indexed) will have default Excel properties. */

    points[2]->fill = calloc(1, sizeof(lxw_chart_fill));
    points[2]->fill->color = 0x00FF00;

    /* Set the points in the chart. */
    chart_series_set_points(series, points);

    worksheet_insert_chart(worksheet, CELL("E9"), chart);


    /* Remember to free everything allocated. */

    return workbook_close(workbook);
}


Hope this helps.

John

<!-- gh-comment-id:383119222 --> @jmcnamara commented on GitHub (Apr 20, 2018): Hi, You could build up a dynamic array of points using something like the following: ```C #include "xlsxwriter.h" int main() { lxw_workbook *workbook = new_workbook("chart_points.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_PIE); worksheet_write_number(worksheet, 0, 0, 2, NULL); worksheet_write_number(worksheet, 1, 0, 5, NULL); worksheet_write_number(worksheet, 2, 0, 4, NULL); lxw_chart_series *series = chart_add_series(chart, NULL, "=Sheet1!$A$1:$A$3"); /* Some number of known points in the series. */ uint8_t num_points = 3; /* Create an array of points. */ lxw_chart_point **points = calloc(num_points +1, sizeof(lxw_chart_point *)); /* Allocate some points with default (NULL) elements. */ for (int i = 0; i < num_points; i++) points[i] = calloc(1, sizeof(lxw_chart_point)); /* Edit some of the points */ points[0]->fill = calloc(1, sizeof(lxw_chart_fill)); points[0]->fill->color = 0xFF0000; /* Point 1 (zero indexed) will have default Excel properties. */ points[2]->fill = calloc(1, sizeof(lxw_chart_fill)); points[2]->fill->color = 0x00FF00; /* Set the points in the chart. */ chart_series_set_points(series, points); worksheet_insert_chart(worksheet, CELL("E9"), chart); /* Remember to free everything allocated. */ return workbook_close(workbook); } ``` Hope this helps. John
Author
Owner

@TitanicHispanic commented on GitHub (Apr 20, 2018):

This looks like exactly what I need. Love it!

Using the code you provided, I am getting this error at the calloc command and cannot compile (P.S. I'm on an x64 PC using VS2017):

void *__cdecl calloc(size_t_Count, size_t_Size)
a value of type “void *” cannot be assigned to an entity of type "lxw_chart_point **"
<!-- gh-comment-id:383241580 --> @TitanicHispanic commented on GitHub (Apr 20, 2018): This looks like exactly what I need. Love it! Using the code you provided, I am getting this error at the **calloc** command and cannot compile (P.S. I'm on an x64 PC using VS2017): ``` void *__cdecl calloc(size_t_Count, size_t_Size) a value of type “void *” cannot be assigned to an entity of type "lxw_chart_point **" ```
Author
Owner

@TitanicHispanic commented on GitHub (Apr 20, 2018):

Figured it out! I just had to do some casting on the calloc's and voila! Silly C++.

Thanks again!

<!-- gh-comment-id:383243771 --> @TitanicHispanic commented on GitHub (Apr 20, 2018): Figured it out! I just had to do some casting on the calloc's and voila! Silly C++. Thanks again!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/libxlsxwriter#134
No description provided.