No Matches
<< chart_doughnut.c chart_data_table.c >>

Example of creating a clustered Excel chart where there are two levels of category on the X axis.

The categories in clustered charts are 2D ranges, instead of the more normal 1D ranges. The series are shown as formula strings for clarity but you can also use the a list syntax.

* An example of a clustered category chart using the libxlsxwriter library.
* Copyright 2014-2021, John McNamara,
#include "xlsxwriter.h"
* Write some data to the worksheet.
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
worksheet_write_string(worksheet, 0, 0, "Types", bold);
worksheet_write_string(worksheet, 1, 0, "Type 1", NULL);
worksheet_write_string(worksheet, 4, 0, "Type 2", NULL);
worksheet_write_string(worksheet, 0, 1, "Sub Type", bold);
worksheet_write_string(worksheet, 1, 1, "Sub Type A", NULL);
worksheet_write_string(worksheet, 2, 1, "Sub Type B", NULL);
worksheet_write_string(worksheet, 3, 1, "Sub Type C", NULL);
worksheet_write_string(worksheet, 4, 1, "Sub Type D", NULL);
worksheet_write_string(worksheet, 5, 1, "Sub Type E", NULL);
worksheet_write_string(worksheet, 0, 2, "Value 1", bold);
worksheet_write_number(worksheet, 1, 2, 5000, NULL);
worksheet_write_number(worksheet, 2, 2, 2000, NULL);
worksheet_write_number(worksheet, 3, 2, 250, NULL);
worksheet_write_number(worksheet, 4, 2, 6000, NULL);
worksheet_write_number(worksheet, 5, 2, 500, NULL);
worksheet_write_string(worksheet, 0, 3, "Value 2", bold);
worksheet_write_number(worksheet, 1, 3, 8000, NULL);
worksheet_write_number(worksheet, 2, 3, 3000, NULL);
worksheet_write_number(worksheet, 3, 3, 1000, NULL);
worksheet_write_number(worksheet, 4, 3, 6000, NULL);
worksheet_write_number(worksheet, 5, 3, 300, NULL);
worksheet_write_string(worksheet, 0, 4, "Value 3", bold);
worksheet_write_number(worksheet, 1, 4, 6000, NULL);
worksheet_write_number(worksheet, 2, 4, 4000, NULL);
worksheet_write_number(worksheet, 3, 4, 2000, NULL);
worksheet_write_number(worksheet, 4, 4, 6500, NULL);
worksheet_write_number(worksheet, 5, 4, 200, NULL);
* Create a worksheet with examples charts.
int main() {
lxw_workbook *workbook = workbook_new("chart_clustered2.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Add a bold format to use to highlight the header cells. */
lxw_format *bold = workbook_add_format(workbook);
/* Write some data for the chart. */
write_worksheet_data(worksheet, bold);
* Configure the series. Note, that the categories are 2D ranges (from
* column A to column B). This creates the clusters. The series are shown
* as formula strings for clarity but you can also use variables with the
* chart_series_set_categories() and chart_series_set_values()
* functions. See the docs.
/* Set an Excel chart style. */
chart_set_style(chart, 37);
/* Turn off the legend. */
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("G3"), chart);
return workbook_close(workbook);
void chart_set_style(lxw_chart *chart, uint8_t style_id)
Set the chart style type.
lxw_chart_series * chart_add_series(lxw_chart *chart, const char *categories, const char *values)
Add a data series to a chart.
Definition: chart.h:114
Definition: chart.h:168
void chart_legend_set_position(lxw_chart *chart, uint8_t position)
Set the position of the chart legend.
void format_set_bold(lxw_format *format)
Turn on bold for the format font.
Struct to represent an Excel chart.
Definition: chart.h:1091
Struct to represent the formatting properties of an Excel format.
Definition: format.h:359
Struct to represent an Excel workbook.
Definition: workbook.h:293
Struct to represent an Excel worksheet.
Definition: worksheet.h:2108
#define CELL(cell)
Convert an Excel A1 cell string into a (row, col) pair.
Definition: utility.h:46
lxw_chart * workbook_add_chart(lxw_workbook *workbook, uint8_t chart_type)
Create a new chart to be added to a worksheet:
lxw_workbook * workbook_new(const char *filename)
Create a new workbook object.
lxw_format * workbook_add_format(lxw_workbook *workbook)
Create a new Format object to formats cells in worksheets.
lxw_error workbook_close(lxw_workbook *workbook)
Close the Workbook object and write the XLSX file.
lxw_worksheet * workbook_add_worksheet(lxw_workbook *workbook, const char *sheetname)
Add a new worksheet to a workbook.
lxw_error worksheet_insert_chart(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, lxw_chart *chart)
Insert a chart object into a worksheet.
lxw_error worksheet_write_string(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, const char *string, lxw_format *format)
Write a string to a worksheet cell.
lxw_error worksheet_write_number(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, double number, lxw_format *format)
Write a number to a worksheet cell.