[GH-ISSUE #159] Set Width/Height of Chart #132

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

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

Originally assigned to: @jmcnamara on GitHub.

Having trouble finding this seemingly simple setting, but how does one set the dimensions of a chart in a worksheet?

Originally created by @TitanicHispanic on GitHub (Apr 4, 2018). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/159 Originally assigned to: @jmcnamara on GitHub. Having trouble finding this seemingly simple setting, but how does one set the dimensions of a chart in a worksheet?
gitea-mirror 2026-05-05 11:45:02 -06:00
  • closed this issue
  • added the
    question
    label
Author
Owner

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

See the worksheet_insert_chart_opt() function

<!-- gh-comment-id:378493809 --> @jmcnamara commented on GitHub (Apr 4, 2018): See the [worksheet_insert_chart_opt() function](http://libxlsxwriter.github.io/worksheet_8h.html#a0b05e75e2c2a5c3452374714cdb2b79b)
Author
Owner

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

Thank you for your quick reply. I have tried this option to no success. I even copied the code from an official example to test it. insert_chart works, but replacing that line with insert_chart_opts throws an error at run-time.

Can you please review and provide any info?

	lxw_workbook     *workbook = new_workbook("chart_bar.xlsx");
	lxw_worksheet    *worksheet = workbook_add_worksheet(workbook, NULL);
	lxw_chart_series *series;

	lxw_format *bold = workbook_add_format(workbook);
	format_set_bold(bold);

	write_worksheet_data(worksheet, bold);
	lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_BAR);

	series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
	chart_series_set_name(series, "=Sheet1!$B1$1");

	series = chart_add_series(chart, NULL, NULL);
	chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */
	chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */
	chart_series_set_name_range(series, "Sheet1", 0, 2);       /* "=Sheet1!$C$1"      */
	chart_title_set_name(chart, "Results of sample analysis");
	chart_axis_set_name(chart->x_axis, "Test number");
	chart_axis_set_name(chart->y_axis, "Sample length (mm)");
	chart_set_style(chart, 11);

	lxw_image_options options;
	options.x_offset = 30;
	options.y_offset = 10;
	options.x_scale = 0.5;
	options.y_scale = 0.75;

	worksheet_insert_chart(worksheet, 7, 2, chart);// WORKS FINE
	//worksheet_insert_chart_opt(worksheet, 7, 2, chart, &options);// ERRORS@RUN-TIME

	return workbook_close(workbook);

<!-- gh-comment-id:378755239 --> @TitanicHispanic commented on GitHub (Apr 4, 2018): Thank you for your quick reply. I have tried this option to no success. I even copied the code from an official example to test it. **insert_chart** works, but replacing that line with **insert_chart_opts** throws an error at run-time. Can you please review and provide any info? ```C lxw_workbook *workbook = new_workbook("chart_bar.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_chart_series *series; lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); write_worksheet_data(worksheet, bold); lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_BAR); series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7"); chart_series_set_name(series, "=Sheet1!$B1$1"); series = chart_add_series(chart, NULL, NULL); chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */ chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */ chart_series_set_name_range(series, "Sheet1", 0, 2); /* "=Sheet1!$C$1" */ chart_title_set_name(chart, "Results of sample analysis"); chart_axis_set_name(chart->x_axis, "Test number"); chart_axis_set_name(chart->y_axis, "Sample length (mm)"); chart_set_style(chart, 11); lxw_image_options options; options.x_offset = 30; options.y_offset = 10; options.x_scale = 0.5; options.y_scale = 0.75; worksheet_insert_chart(worksheet, 7, 2, chart);// WORKS FINE //worksheet_insert_chart_opt(worksheet, 7, 2, chart, &options);// ERRORS@RUN-TIME return workbook_close(workbook); ```
Author
Owner

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

but replacing that line with insert_chart_opts throws an error at run-time.

What is the error?

Can you please review and provide any info?

In future, please provide a full compilable example not a snippet.

I don't see any issue. I commented back in the worksheet_insert_chart_opt() function and commented out worksheet_insert_chart() since you cannot insert a chart object more than once and compiled the following code:

#include "xlsxwriter.h"

/*
 * Write some data to the worksheet.
 */
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {

    int row, col;
    uint8_t data[6][3] = {
        /* Three columns of data. */
        {2, 10, 30},
        {3, 40, 60},
        {4, 50, 70},
        {5, 20, 50},
        {6, 10, 40},
        {7, 50, 30}
    };

    worksheet_write_string(worksheet, CELL("A1"), "Number",  bold);
    worksheet_write_string(worksheet, CELL("B1"), "Batch 1", bold);
    worksheet_write_string(worksheet, CELL("C1"), "Batch 2", bold);

    for (row = 0; row < 6; row++)
        for (col = 0; col < 3; col++)
            worksheet_write_number(worksheet, row + 1, col, data[row][col] , NULL);
}


int main() {


    lxw_workbook     *workbook = new_workbook("chart_bar.xlsx");
    lxw_worksheet    *worksheet = workbook_add_worksheet(workbook, NULL);
    lxw_chart_series *series;
    
    lxw_format *bold = workbook_add_format(workbook);
    format_set_bold(bold);
    
    write_worksheet_data(worksheet, bold);
    lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_BAR);
    
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
    chart_series_set_name(series, "=Sheet1!$B1$1");
    
    series = chart_add_series(chart, NULL, NULL);
    chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */
    chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */
    chart_series_set_name_range(series, "Sheet1", 0, 2);       /* "=Sheet1!$C$1"      */
    chart_title_set_name(chart, "Results of sample analysis");
    chart_axis_set_name(chart->x_axis, "Test number");
    chart_axis_set_name(chart->y_axis, "Sample length (mm)");
    chart_set_style(chart, 11);
    
    lxw_image_options options;
    options.x_offset = 30;
    options.y_offset = 10;
    options.x_scale = 0.5;
    options.y_scale = 0.75;
    
    /* worksheet_insert_chart(worksheet, 7, 2, chart);// WORKS FINE */
    worksheet_insert_chart_opt(worksheet, 7, 2, chart, &options);// ERRORS@RUN-TIME
    
    return workbook_close(workbook);
    
}

Compilation and execution:

$ make examples V=1
/Applications/Xcode.app/Contents/Developer/usr/bin/make -C examples
cc -I../include -g -Wall -Wextra gh_159.c -o gh_159 ../src/libxlsxwriter.a -lz

$ ./examples/gh_159

Output:

screenshot

Which works as expected.

There are also examples in the test suite that compare against Excel created files, like this one.

<!-- gh-comment-id:378764663 --> @jmcnamara commented on GitHub (Apr 4, 2018): > but replacing that line with insert_chart_opts throws an error at run-time. What is the error? > Can you please review and provide any info? In future, please provide a full compilable example not a snippet. I don't see any issue. I commented back in the `worksheet_insert_chart_opt()` function and commented out `worksheet_insert_chart()` since you cannot insert a chart object more than once and compiled the following code: ```C #include "xlsxwriter.h" /* * Write some data to the worksheet. */ void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) { int row, col; uint8_t data[6][3] = { /* Three columns of data. */ {2, 10, 30}, {3, 40, 60}, {4, 50, 70}, {5, 20, 50}, {6, 10, 40}, {7, 50, 30} }; worksheet_write_string(worksheet, CELL("A1"), "Number", bold); worksheet_write_string(worksheet, CELL("B1"), "Batch 1", bold); worksheet_write_string(worksheet, CELL("C1"), "Batch 2", bold); for (row = 0; row < 6; row++) for (col = 0; col < 3; col++) worksheet_write_number(worksheet, row + 1, col, data[row][col] , NULL); } int main() { lxw_workbook *workbook = new_workbook("chart_bar.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_chart_series *series; lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); write_worksheet_data(worksheet, bold); lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_BAR); series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7"); chart_series_set_name(series, "=Sheet1!$B1$1"); series = chart_add_series(chart, NULL, NULL); chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */ chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */ chart_series_set_name_range(series, "Sheet1", 0, 2); /* "=Sheet1!$C$1" */ chart_title_set_name(chart, "Results of sample analysis"); chart_axis_set_name(chart->x_axis, "Test number"); chart_axis_set_name(chart->y_axis, "Sample length (mm)"); chart_set_style(chart, 11); lxw_image_options options; options.x_offset = 30; options.y_offset = 10; options.x_scale = 0.5; options.y_scale = 0.75; /* worksheet_insert_chart(worksheet, 7, 2, chart);// WORKS FINE */ worksheet_insert_chart_opt(worksheet, 7, 2, chart, &options);// ERRORS@RUN-TIME return workbook_close(workbook); } ``` Compilation and execution: ``` $ make examples V=1 /Applications/Xcode.app/Contents/Developer/usr/bin/make -C examples cc -I../include -g -Wall -Wextra gh_159.c -o gh_159 ../src/libxlsxwriter.a -lz $ ./examples/gh_159 ``` Output: ![screenshot](https://user-images.githubusercontent.com/94267/38337919-381b91e4-385f-11e8-83d5-b9a25abcac96.png) Which works as expected. There are also examples in the test suite that compare against Excel created files, like [this one](https://github.com/jmcnamara/libxlsxwriter/blob/master/test/functional/src/test_chart_size01.c).
Author
Owner

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

I believe we're getting somewhere. I just copied and pasted your example, and it fails. I'm using v0.7.2 and am going to try using a more recent release.

<!-- gh-comment-id:378767992 --> @TitanicHispanic commented on GitHub (Apr 4, 2018): I believe we're getting somewhere. I just copied and pasted your example, and it fails. I'm using v0.7.2 and am going to try using a more recent release.
Author
Owner

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

I just copied and pasted your example, and it fails.

Fails how? What is the error message?

Also what compiler and OS are you using?

<!-- gh-comment-id:378769087 --> @jmcnamara commented on GitHub (Apr 4, 2018): > I just copied and pasted your example, and it fails. Fails how? What is the error message? Also what compiler and OS are you using?
Author
Owner

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

I rebuilt my DLL & project from scratch and this functionality now works.

Having said that, are there plans to provide more control over sizing of a chart? scale isn't very conducive; perhaps parameters for endRow/endCol?

<!-- gh-comment-id:378812281 --> @TitanicHispanic commented on GitHub (Apr 5, 2018): I rebuilt my DLL & project from scratch and this functionality now works. Having said that, are there plans to provide more control over sizing of a chart? **scale** isn't very conducive; perhaps parameters for endRow/endCol?
Author
Owner

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

I rebuilt my DLL & project from scratch and this functionality now works.

Great. Thanks for letting me know.

are there plans to provide more control over sizing of a chart?

No. That isn't planned.

<!-- gh-comment-id:378863441 --> @jmcnamara commented on GitHub (Apr 5, 2018): > I rebuilt my DLL & project from scratch and this functionality now works. Great. Thanks for letting me know. > are there plans to provide more control over sizing of a chart? No. That isn't planned.
Author
Owner

@znakeeye commented on GitHub (Oct 21, 2024):

Wouldn't it be pretty easy to add width and height to lxw_chart_options? I mean, why hard-code the chart size like this?

worksheet.c(11081)

object_props->width = 480;
object_props->height = 288;

😢

<!-- gh-comment-id:2426128650 --> @znakeeye commented on GitHub (Oct 21, 2024): Wouldn't it be pretty easy to add `width` and `height` to `lxw_chart_options`? I mean, why hard-code the chart size like this? [worksheet.c(11081)](https://github.com/jmcnamara/libxlsxwriter/blob/d9633436dcfee1d5018218e7b980241ccd612245/src/worksheet.c#L11081) ```c object_props->width = 480; object_props->height = 288; ``` 😢
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#132
No description provided.