libxlsxwriter
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
chart_fonts.c
<< chart_data_tools.c chart_pattern.c >>

An example of creating a simple chart with different fonts.

chart_fonts.png
/*
* An example of a simple Excel chart with user defined fonts using the
* libxlsxwriter library.
*
* Copyright 2014-2017, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* Create a worksheet with a chart. */
int main() {
lxw_workbook *workbook = new_workbook("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);
}