[GH-ISSUE #161] Chart - Coloring Specific Data Points #133

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

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

Originally assigned to: @jmcnamara on GitHub.

  1. I've rebuilt the DLL using the unchanged download from GitHub (x64/Debug).
  2. I've recompiled the ExampleExe app from GitHub (x64/Debug).
  3. I copied chart_series_set_points code from site.
  4. The .EXE doesn't set the data point color. The chart_series_set_points command doesn't seem to take.

Have I missed a step?

// The example below is take from the libxlsxwriter/examples directory. The
// code can be replaced with code from any of the other example programs in
// thatdirectory. The lib linkage is done via the stdafx.h file.

/*
 * An example of creating Excel column charts using the libxlsxwriter library.
 *
 * Copyright 2014-2016, 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_column.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);


    /*
     * Create a column chart.
     */
    lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);

    /* 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, 11);

	/* ------------------------------------------------------ */
	/* COLOR EACH DATA POINT TEST                             */
	/* ------------------------------------------------------ */
	lxw_chart_fill  red_fill;
	red_fill.color = LXW_COLOR_RED;
	lxw_chart_point red_point;
	red_point.fill = &red_fill;
	lxw_chart_point default_point = { 0, 0, 0 };
	lxw_chart_point *points[] = { &default_point, &default_point, &red_point, NULL };
	chart_series_set_points(series, points);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart(worksheet, CELL("E2"), chart);

    /*
     * Create a stacked column chart.
     */
    chart = workbook_add_chart(workbook, LXW_CHART_COLUMN_STACKED);

    /* 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 the second series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");

    /* Set the name for the series instead of the default "Series 2". */
    chart_series_set_name(series, "=Sheet1!$C1$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, 12);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart(worksheet, CELL("E18"), chart);


    /*
     * Create a percent stacked column chart.
     */
    chart = workbook_add_chart(workbook, LXW_CHART_COLUMN_STACKED_PERCENT);

    /* 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 the second series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");

    /* Set the name for the series instead of the default "Series 2". */
    chart_series_set_name(series, "=Sheet1!$C1$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, 13);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart(worksheet, CELL("E34"), chart);


    return workbook_close(workbook);
}
Originally created by @TitanicHispanic on GitHub (Apr 16, 2018). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/161 Originally assigned to: @jmcnamara on GitHub. 1. I've rebuilt the DLL using the unchanged download from GitHub (x64/Debug). 2. I've recompiled the ExampleExe app from GitHub (x64/Debug). 3. I copied **chart_series_set_points** code from site. 4. The .EXE doesn't set the data point color. The **chart_series_set_points** command doesn't seem to take. Have I missed a step? ```C // The example below is take from the libxlsxwriter/examples directory. The // code can be replaced with code from any of the other example programs in // thatdirectory. The lib linkage is done via the stdafx.h file. /* * An example of creating Excel column charts using the libxlsxwriter library. * * Copyright 2014-2016, 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_column.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); /* * Create a column chart. */ lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_COLUMN); /* 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, 11); /* ------------------------------------------------------ */ /* COLOR EACH DATA POINT TEST */ /* ------------------------------------------------------ */ lxw_chart_fill red_fill; red_fill.color = LXW_COLOR_RED; lxw_chart_point red_point; red_point.fill = &red_fill; lxw_chart_point default_point = { 0, 0, 0 }; lxw_chart_point *points[] = { &default_point, &default_point, &red_point, NULL }; chart_series_set_points(series, points); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("E2"), chart); /* * Create a stacked column chart. */ chart = workbook_add_chart(workbook, LXW_CHART_COLUMN_STACKED); /* 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 the second series to the chart. */ series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7"); /* Set the name for the series instead of the default "Series 2". */ chart_series_set_name(series, "=Sheet1!$C1$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, 12); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("E18"), chart); /* * Create a percent stacked column chart. */ chart = workbook_add_chart(workbook, LXW_CHART_COLUMN_STACKED_PERCENT); /* 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 the second series to the chart. */ series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7"); /* Set the name for the series instead of the default "Series 2". */ chart_series_set_name(series, "=Sheet1!$C1$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, 13); /* Insert the chart into the worksheet. */ worksheet_insert_chart(worksheet, CELL("E34"), chart); return workbook_close(workbook); } ```
gitea-mirror 2026-05-05 11:45:25 -06:00
Author
Owner

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

I ran your example on Linux with the latest code and it looks like it works as expected, i.e., the third point of the series has a red color, as specified in the code:

aa_image

What result did you get?

<!-- gh-comment-id:381893929 --> @jmcnamara commented on GitHub (Apr 17, 2018): I ran your example on Linux with the latest code and it looks like it works as expected, i.e., the third point of the series has a red color, as specified in the code: ![aa_image](https://user-images.githubusercontent.com/94267/38856961-e12190e4-421f-11e8-8f9d-86a3510e81af.png) What result did you get?
Author
Owner

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

Well, I didn't change anything (except for rebooting my PC), but it now magically works!

Thank you for checking the code. Hats off again for this software!

<!-- gh-comment-id:382561824 --> @TitanicHispanic commented on GitHub (Apr 18, 2018): Well, I didn't change anything (except for rebooting my PC), but it now magically works! Thank you for checking the code. Hats off again for this software!
Author
Owner

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

No problem. Thanks for following up. Closing.

<!-- gh-comment-id:382622772 --> @jmcnamara commented on GitHub (Apr 19, 2018): No problem. Thanks for following up. 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#133
No description provided.