libxlsxwriter
Loading...
Searching...
No Matches
chart.c
<< lambda.c chart_area.c >>

An example of creating a simple column chart with 3 data series.

/*
* An example of a simple Excel chart using the libxlsxwriter library.
*
* Copyright 2014-2024, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* Write some data to the worksheet. */
void write_worksheet_data(lxw_worksheet *worksheet) {
uint8_t data[5][3] = {
/* Three columns of data. */
{1, 2, 3},
{2, 4, 6},
{3, 6, 9},
{4, 8, 12},
{5, 10, 15}
};
int row, col;
for (row = 0; row < 5; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row, col, data[row][col], NULL);
}
/* Create a worksheet with a chart. */
int main() {
lxw_workbook *workbook = workbook_new("chart.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Write some data for the chart. */
write_worksheet_data(worksheet);
/* Create a chart object. */
/* Configure the chart. In simplest case we just add some value data
* series. The NULL categories will default to 1 to 5 like in Excel.
*/
chart_add_series(chart, NULL, "Sheet1!$A$1:$A$5");
chart_add_series(chart, NULL, "Sheet1!$B$1:$B$5");
chart_add_series(chart, NULL, "Sheet1!$C$1:$C$5");
chart_title_set_name(chart, "Year End Results");
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("B7"), chart);
return workbook_close(workbook);
}
lxw_chart_series * chart_add_series(lxw_chart *chart, const char *categories, const char *values)
Add a data series to a chart.
@ LXW_CHART_COLUMN
Definition: chart.h:114
void chart_title_set_name(lxw_chart *chart, const char *name)
Set the title of the chart.
void chart_title_set_name_font(lxw_chart *chart, lxw_chart_font *font)
Set the font properties for a chart title.
@ LXW_EXPLICIT_FALSE
Definition: common.h:57
@ LXW_COLOR_BLUE
Definition: format.h:185
Struct to represent a chart font.
Definition: chart.h:700
uint8_t bold
Definition: chart.h:709
Struct to represent an Excel chart.
Definition: chart.h:1091
Struct to represent an Excel workbook.
Definition: workbook.h:293
Struct to represent an Excel worksheet.
Definition: worksheet.h:2115
#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_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_number(lxw_worksheet *worksheet, lxw_row_t row, lxw_col_t col, double number, lxw_format *format)
Write a number to a worksheet cell.