libxlsxwriter/examples/chart_pattern.c
Nick fc0c7bd382 - Added support for secondary Y axis in charts. Series can be assigned to a
secondary Y axis using an optional fourth parameter to `chart_add_series()`.
  Secondary axes are accessible via the `chart->x2_axis` and `chart->y2_axis`
  pointers, and all existing `chart_axis_set_*()` functions work with these
  secondary axis pointers. The API is backward compatible with existing code.

- Added support for combined charts via `chart_combine()`. Combined charts
  allow overlaying two different chart types (e.g., Column + Line, Bar + Line,
  Area + Column) on the same plot area. For combined charts with secondary
  axes, set `chart->is_secondary = LXW_TRUE` on the secondary chart.

- Added `chart_set_series_gap_y2()` and `chart_set_series_overlap_y2()` to set
  the gap and overlap for series on a secondary Y axis in Bar/Column charts.

- Added support for Stock charts via `LXW_CHART_STOCK`. Stock charts display
  High-Low-Close (HLC) or Open-High-Low-Close (OHLC) data. Default series
  formatting is applied automatically (line with no fill, appropriate markers).
  Stock charts support hi-low lines, drop lines, and up-down bars.

- Added `LXW_CHART_MARKER_DOT` marker type used by Stock charts.

- Added support for gradient fills in chart series, plotarea, and chartarea.
  New functions: `chart_series_set_gradient()`, `chart_plotarea_set_gradient()`,
  and `chart_chartarea_set_gradient()`. Supports linear, radial, rectangular,
  and path gradient types with 2-10 color stops.

- Added support for textboxes via `worksheet_insert_textbox()` and
  `worksheet_insert_textbox_opt()`. Textboxes allow inserting text with a
  rectangular shape into a worksheet. The optional `lxw_textbox_options`
  struct allows setting width, height, offsets, scale, and description.

- Added new chart examples:
  - `chart_stock.c`: Demonstrates stock chart creation with High-Low-Close data.
  - `chart_pareto.c`: Shows how to create a Pareto chart using combined Column
    and Line charts with a secondary Y axis.
  - `chart_gauge.c`: Demonstrates creating a gauge chart by combining Doughnut
    and Pie charts with custom point colors.
  - `chart_gradient.c`: Shows how to apply gradient fills to chart series
    and the plot area.

- Added `textbox.c` example demonstrating textbox insertion.

- Added support for sparklines via `worksheet_add_sparkline()`. Sparklines are
  mini-charts that fit in a single cell to show data trends at a glance. The
  `lxw_sparkline_options` struct allows configuring:
  - Sparkline type: line, column, or win/loss
  - Predefined styles (0-35)
  - Point markers: high, low, negative, first, last, and all markers
  - Custom colors for series, markers, and axis
  - Axis options: min, max, date axis, reverse, show hidden data
  Added new example:
  - `sparklines.c`: Demonstrates creating different sparkline types with
    various options and custom colors.

- Added support for chart date axis via `chart_axis_set_date_axis()`. This
  enables date-based X axis on charts with automatic date formatting. Related
  functions for configuring time units:
  - `chart_axis_set_major_unit_type()`: Set major time unit (days/months/years)
  - `chart_axis_set_minor_unit_type()`: Set minor time unit (days/months/years)

- Added autofit helper utility functions for calculating column widths based
  on string content:
  - `lxw_pixel_width()`: Calculate pixel width of a string (Calibri 11 font)
  - `lxw_autofit_width()`: Calculate column width suitable for
    `worksheet_set_column()`
  Note: Excel does not store autofit information in files; these functions
  help users calculate appropriate column widths programmatically.

- Added support for inserting checkboxes in worksheet cells via
  `worksheet_insert_checkbox()`. Checkboxes are a feature of Excel 365
  from 2024 onwards. They are boolean values displayed as interactive
  checkboxes in cells. Added new example:
  - `checkbox.c`: Demonstrates inserting checkboxes with different states.
2026-01-02 22:30:12 -06:00

77 lines
2.7 KiB
C

/*
* An example of a simple Excel chart with patterns using the libxlsxwriter
* library.
*
* Copyright 2014-2025, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/* Create a worksheet with a chart. */
int main() {
lxw_workbook *workbook = workbook_new("chart_pattern.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
lxw_chart *chart;
/* 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. */
worksheet_write_string(worksheet, 0, 0, "Shingle", bold);
worksheet_write_number(worksheet, 1, 0, 105, NULL);
worksheet_write_number(worksheet, 2, 0, 150, NULL);
worksheet_write_number(worksheet, 3, 0, 130, NULL);
worksheet_write_number(worksheet, 4, 0, 90, NULL);
worksheet_write_string(worksheet, 0, 1, "Brick", bold);
worksheet_write_number(worksheet, 1, 1, 50, NULL);
worksheet_write_number(worksheet, 2, 1, 120, NULL);
worksheet_write_number(worksheet, 3, 1, 100, NULL);
worksheet_write_number(worksheet, 4, 1, 110, NULL);
/* Create a chart object. */
chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);
/* Configure the chart. */
lxw_chart_series *series1 = chart_add_series(chart, NULL, "Sheet1!$A$2:$A$5", 0);
lxw_chart_series *series2 = chart_add_series(chart, NULL, "Sheet1!$B$2:$B$5", 0);
chart_series_set_name(series1, "=Sheet1!$A$1");
chart_series_set_name(series2, "=Sheet1!$B$1");
chart_title_set_name(chart, "Cladding types");
chart_axis_set_name(chart->x_axis, "Region");
chart_axis_set_name(chart->y_axis, "Number of houses");
/* Configure an add the chart series patterns. */
lxw_chart_pattern pattern1 = {.type = LXW_CHART_PATTERN_SHINGLE,
.fg_color = 0x804000,
.bg_color = 0XC68C53};
lxw_chart_pattern pattern2 = {.type = LXW_CHART_PATTERN_HORIZONTAL_BRICK,
.fg_color = 0XB30000,
.bg_color = 0XFF6666};
chart_series_set_pattern(series1, &pattern1);
chart_series_set_pattern(series2, &pattern2);
/* Configure and set the chart series borders. */
lxw_chart_line line1 = {.color = 0x804000};
lxw_chart_line line2 = {.color = 0xb30000};
chart_series_set_line(series1, &line1);
chart_series_set_line(series2, &line2);
/* Widen the gap between the series/categories. */
chart_set_series_gap(chart, 70);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("D2"), chart);
return workbook_close(workbook);
}