[GH-ISSUE #92] chart_series_set_line does not work #77

Closed
opened 2026-05-05 11:37:15 -06:00 by gitea-mirror · 4 comments
Owner

Originally created by @Diddlik on GitHub (Jan 20, 2017).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/92

Originally assigned to: @jmcnamara on GitHub.

Hello,

thank you for implementation of additional settings for line-charts. I tried it with minimal example, but it does not work for me ;)

If I set the line properties, in the output file is the line gone...

	lxw_workbook     *workbook = new_workbook("chart_line.xlsx");
	lxw_worksheet    *worksheet = workbook_add_worksheet(workbook, NULL);
	lxw_chart_series *series;
	/* Add a bold format to use to highlight the header cells. */
	lxw_format *bold = workbook_add_format(workbook);
	format_set_bold(bold);
	/* Write some data for the chart. */
	write_worksheet_data(worksheet, bold);
	/*
	* Chart 1. Create a line chart.
	*/
	lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_LINE);
	/* Add the first series to the chart. */
	series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
	/* Set the name for the series instead of the default "Series 1". */
	chart_series_set_name(series, "=Sheet1!$B1$1");
	/* Add a second series but leave the categories and values undefined. They
	* can be defined later using the alternative syntax shown below.  */
	series = chart_add_series(chart, NULL, NULL);
	/* Configure the series using a syntax that is easier to define programmatically. */
	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"      */
															   /* Add a chart title and some axis labels. */
	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)");
	/* Set an Excel chart style. */
	chart_set_style(chart, 10);
	/* Insert the chart into the worksheet. */
	lxw_chart_line line;
	line.color = LXW_COLOR_RED;
	line.width = 0.5;
	line.dash_type = LXW_CHART_LINE_DASH_SQUARE_DOT;

	chart_series_set_line(series, &line);


	worksheet_insert_chart(worksheet, CELL("E2"), chart);
	return workbook_close(workbook);

With line
with_line

Without line
without_lines

Originally created by @Diddlik on GitHub (Jan 20, 2017). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/92 Originally assigned to: @jmcnamara on GitHub. Hello, thank you for implementation of additional settings for line-charts. I tried it with minimal example, but it does not work for me ;) If I set the line properties, in the output file is the line gone... ```C lxw_workbook *workbook = new_workbook("chart_line.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_chart_series *series; /* Add a bold format to use to highlight the header cells. */ lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); /* Write some data for the chart. */ write_worksheet_data(worksheet, bold); /* * Chart 1. Create a line chart. */ lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_LINE); /* Add the first series to the chart. */ series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7"); /* Set the name for the series instead of the default "Series 1". */ chart_series_set_name(series, "=Sheet1!$B1$1"); /* Add a second series but leave the categories and values undefined. They * can be defined later using the alternative syntax shown below. */ series = chart_add_series(chart, NULL, NULL); /* Configure the series using a syntax that is easier to define programmatically. */ 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" */ /* Add a chart title and some axis labels. */ 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)"); /* Set an Excel chart style. */ chart_set_style(chart, 10); /* Insert the chart into the worksheet. */ lxw_chart_line line; line.color = LXW_COLOR_RED; line.width = 0.5; line.dash_type = LXW_CHART_LINE_DASH_SQUARE_DOT; chart_series_set_line(series, &line); worksheet_insert_chart(worksheet, CELL("E2"), chart); return workbook_close(workbook); ``` With line ![with_line](https://cloud.githubusercontent.com/assets/6010713/22145673/b0ea4884-df02-11e6-8650-b77cf248f2b0.png) Without line ![without_lines](https://cloud.githubusercontent.com/assets/6010713/22145674/b0eb20c4-df02-11e6-84d2-9715e595d1e0.png)
gitea-mirror 2026-05-05 11:37:15 -06:00
Author
Owner

@jmcnamara commented on GitHub (Jan 20, 2017):

Thanks for the report.

It would have been nicer as a complete program. ;-)

Anyway, I added the rest of the header code as follows:

/*
 * An example of creating an Excel line chart using the libxlsxwriter library.
 *
 * Copyright 2014-2017, John McNamara, jmcnamara@cpan.org
 *
 */

#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);
}

/*
 * Create a worksheet with examples charts.
 */
int main() {

        lxw_workbook     *workbook = new_workbook("chart_line3.xlsx");
        lxw_worksheet    *worksheet = workbook_add_worksheet(workbook, NULL);
        lxw_chart_series *series;
        /* Add a bold format to use to highlight the header cells. */
        lxw_format *bold = workbook_add_format(workbook);
        format_set_bold(bold);
        /* Write some data for the chart. */
        write_worksheet_data(worksheet, bold);
        /*
        * Chart 1. Create a line chart.
        */
        lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_LINE);
        /* Add the first series to the chart. */
        series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
        /* Set the name for the series instead of the default "Series 1". */
        chart_series_set_name(series, "=Sheet1!$B1$1");
        /* Add a second series but leave the categories and values undefined. They
        * can be defined later using the alternative syntax shown below.  */
        series = chart_add_series(chart, NULL, NULL);
        /* Configure the series using a syntax that is easier to define programmatically. */
        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"      */

        /* Add a chart title and some axis labels. */
        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)");
        /* Set an Excel chart style. */
        chart_set_style(chart, 10);
        /* Insert the chart into the worksheet. */
        lxw_chart_line line;
        line.color = LXW_COLOR_RED;
        line.width = 0.5;
        line.dash_type = LXW_CHART_LINE_DASH_SQUARE_DOT;

        chart_series_set_line(series, &line);


        worksheet_insert_chart(worksheet, CELL("E2"), chart);
        return workbook_close(workbook);
}

Compiling it in the examples dir and running this gives the following, expected output in Excel 2013:

$ make clean
$ make examples

$ ./examples/chart_bug  
Libxlsxwriter version = 0.6.3

Output:
aa_image

Also there is a test case in the test suite that compares against an Excel generated file that is very similar to this and it passes: https://github.com/jmcnamara/libxlsxwriter/blob/master/test/functional/src/test_chart_format09.c

So, double check the version you are using and your test program/output.

John

<!-- gh-comment-id:274049965 --> @jmcnamara commented on GitHub (Jan 20, 2017): Thanks for the report. It would have been nicer as a complete program. ;-) Anyway, I added the rest of the header code as follows: ```C /* * An example of creating an Excel line chart using the libxlsxwriter library. * * Copyright 2014-2017, John McNamara, jmcnamara@cpan.org * */ #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); } /* * Create a worksheet with examples charts. */ int main() { lxw_workbook *workbook = new_workbook("chart_line3.xlsx"); lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); lxw_chart_series *series; /* Add a bold format to use to highlight the header cells. */ lxw_format *bold = workbook_add_format(workbook); format_set_bold(bold); /* Write some data for the chart. */ write_worksheet_data(worksheet, bold); /* * Chart 1. Create a line chart. */ lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_LINE); /* Add the first series to the chart. */ series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7"); /* Set the name for the series instead of the default "Series 1". */ chart_series_set_name(series, "=Sheet1!$B1$1"); /* Add a second series but leave the categories and values undefined. They * can be defined later using the alternative syntax shown below. */ series = chart_add_series(chart, NULL, NULL); /* Configure the series using a syntax that is easier to define programmatically. */ 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" */ /* Add a chart title and some axis labels. */ 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)"); /* Set an Excel chart style. */ chart_set_style(chart, 10); /* Insert the chart into the worksheet. */ lxw_chart_line line; line.color = LXW_COLOR_RED; line.width = 0.5; line.dash_type = LXW_CHART_LINE_DASH_SQUARE_DOT; chart_series_set_line(series, &line); worksheet_insert_chart(worksheet, CELL("E2"), chart); return workbook_close(workbook); } ``` Compiling it in the examples dir and running this gives the following, expected output in Excel 2013: ```bash $ make clean $ make examples $ ./examples/chart_bug Libxlsxwriter version = 0.6.3 ``` Output: ![aa_image](https://cloud.githubusercontent.com/assets/94267/22147746/0a06e770-df02-11e6-90f3-4ace5316aa9e.png) Also there is a test case in the test suite that compares against an Excel generated file that is very similar to this and it passes: https://github.com/jmcnamara/libxlsxwriter/blob/master/test/functional/src/test_chart_format09.c So, double check the version you are using and your test program/output. John
Author
Owner

@jmcnamara commented on GitHub (Jan 20, 2017):

Maybe the issue is that this struct isn't initialised:

        lxw_chart_line line;
        line.color = LXW_COLOR_RED;
        line.width = 0.5;
        line.dash_type = LXW_CHART_LINE_DASH_SQUARE_DOT;

Try initialise it to zero first and then set the members that you want.

        lxw_chart_line line = {0}; // Or whatever keeps your compiler happy.
        line.color = LXW_COLOR_RED;
        line.width = 0.5;
        line.dash_type = LXW_CHART_LINE_DASH_SQUARE_DOT;
<!-- gh-comment-id:274052797 --> @jmcnamara commented on GitHub (Jan 20, 2017): Maybe the issue is that this struct isn't initialised: ```C lxw_chart_line line; line.color = LXW_COLOR_RED; line.width = 0.5; line.dash_type = LXW_CHART_LINE_DASH_SQUARE_DOT; ``` Try initialise it to zero first and then set the members that you want. ```C lxw_chart_line line = {0}; // Or whatever keeps your compiler happy. line.color = LXW_COLOR_RED; line.width = 0.5; line.dash_type = LXW_CHART_LINE_DASH_SQUARE_DOT; ```
Author
Owner

@Diddlik commented on GitHub (Jan 20, 2017):

It works with initialization!!! You are the best!
Thank a lot for a great job!

<!-- gh-comment-id:274055285 --> @Diddlik commented on GitHub (Jan 20, 2017): It works with initialization!!! You are the best! Thank a lot for a great job!
Author
Owner

@jmcnamara commented on GitHub (Jan 20, 2017):

Ok. Good stuff. Closing.

<!-- gh-comment-id:274057644 --> @jmcnamara commented on GitHub (Jan 20, 2017): Ok. Good stuff. Closing.
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#77
No description provided.