libxlsxwriter
chart_fonts.c
<< chart_data_labels.c chart_pattern.c >>

An example of creating a simple chart with different fonts.

/*
* An example of a simple Excel chart with user defined fonts using the
* libxlsxwriter library.
*
* Copyright 2014-2018, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* Create a worksheet with a chart. */
int main() {
lxw_workbook *workbook = workbook_new("chart_fonts.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
/* Write some data for the chart. */
worksheet_write_number(worksheet, 0, 0, 10, NULL);
worksheet_write_number(worksheet, 1, 0, 40, NULL);
worksheet_write_number(worksheet, 2, 0, 50, NULL);
worksheet_write_number(worksheet, 3, 0, 20, NULL);
worksheet_write_number(worksheet, 4, 0, 10, NULL);
worksheet_write_number(worksheet, 5, 0, 50, NULL);
/* Create a chart object. */
/* Configure the chart. */
chart_add_series(chart, NULL, "Sheet1!$A$1:$A$6");
/* Create some fonts to use in the chart. */
lxw_chart_font font1 = {.name = "Calibri", .color = LXW_COLOR_BLUE};
lxw_chart_font font2 = {.name = "Courier", .color = 0x92D050};
lxw_chart_font font3 = {.name = "Arial", .color = 0x00B0F0};
lxw_chart_font font4 = {.name = "Century", .color = LXW_COLOR_RED};
lxw_chart_font font5 = {.rotation = -30};
.italic = LXW_TRUE,
.underline = LXW_TRUE,
.color = 0x7030A0};
/* Write the chart title with a font. */
chart_title_set_name(chart, "Test Results");
chart_title_set_name_font(chart, &font1);
/* Write the Y axis with a font. */
chart_axis_set_name(chart->y_axis, "Units");
/* Write the X axis with a font. */
chart_axis_set_name(chart->x_axis, "Month");
/* Display the chart legend at the bottom of the chart. */
chart_legend_set_font(chart, &font6);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("C1"), chart);
return workbook_close(workbook);
}
workbook_close
lxw_error workbook_close(lxw_workbook *workbook)
Close the Workbook object and write the XLSX file.
chart_legend_set_font
void chart_legend_set_font(lxw_chart *chart, lxw_chart_font *font)
Set the font properties for a chart legend.
chart_axis_set_name
void chart_axis_set_name(lxw_chart_axis *axis, const char *name)
Set the name caption of the an axis.
chart_add_series
lxw_chart_series * chart_add_series(lxw_chart *chart, const char *categories, const char *values)
Add a data series to a chart.
workbook_new
lxw_workbook * workbook_new(const char *filename)
Create a new workbook object.
chart_title_set_name_font
void chart_title_set_name_font(lxw_chart *chart, lxw_chart_font *font)
Set the font properties for a chart title.
lxw_chart_font
Struct to represent a chart font.
Definition: chart.h:699
LXW_CHART_LINE
@ LXW_CHART_LINE
Definition: chart.h:125
lxw_worksheet
Struct to represent an Excel worksheet.
Definition: worksheet.h:1603
chart_axis_set_name_font
void chart_axis_set_name_font(lxw_chart_axis *axis, lxw_chart_font *font)
Set the font properties for a chart axis name.
LXW_TRUE
@ LXW_TRUE
Definition: common.h:53
lxw_chart
Struct to represent an Excel chart.
Definition: chart.h:1086
worksheet_insert_chart
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_chart::y_axis
lxw_chart_axis * y_axis
Definition: chart.h:1107
lxw_chart_font::name
char * name
Definition: chart.h:702
lxw_workbook
Struct to represent an Excel workbook.
Definition: workbook.h:273
lxw_chart::x_axis
lxw_chart_axis * x_axis
Definition: chart.h:1101
lxw_chart_font::rotation
int32_t rotation
Definition: chart.h:720
chart_legend_set_position
void chart_legend_set_position(lxw_chart *chart, uint8_t position)
Set the position of the chart legend.
chart_title_set_name
void chart_title_set_name(lxw_chart *chart, const char *name)
Set the title of the chart.
LXW_COLOR_BLUE
@ LXW_COLOR_BLUE
Definition: format.h:184
lxw_chart_font::bold
uint8_t bold
Definition: chart.h:708
LXW_COLOR_RED
@ LXW_COLOR_RED
Definition: format.h:217
worksheet_write_number
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.
LXW_CHART_LEGEND_BOTTOM
@ LXW_CHART_LEGEND_BOTTOM
Definition: chart.h:179
CELL
#define CELL(cell)
Convert an Excel A1 cell string into a (row, col) pair.
Definition: utility.h:45
workbook_add_worksheet
lxw_worksheet * workbook_add_worksheet(lxw_workbook *workbook, const char *sheetname)
Add a new worksheet to a workbook.
workbook_add_chart
lxw_chart * workbook_add_chart(lxw_workbook *workbook, uint8_t chart_type)
Create a new chart to be added to a worksheet:
chart_axis_set_num_font
void chart_axis_set_num_font(lxw_chart_axis *axis, lxw_chart_font *font)
Set the font properties for the numbers of a chart axis.