libxlsxwriter
chart_scatter.c
<< chart_line.c chart_radar.c >>

Example of creating Excel Scatter charts. Five types of scatter chart are shown.

The default scatter chart:

Straight scatter chart with markers:

Straight scatter chart:

Smooth scatter chart with markers:

Smooth scatter chart:

/*
* An example of creating Excel scatter charts using the libxlsxwriter library.
*
* Copyright 2014-2018, John McNamara, jmcnamara@cpan.org
*
*/
#include "xlsxwriter.h"
/*
* Write some data to the worksheet.
*/
void write_worksheet_data(lxw_worksheet *worksheet, lxw_format *bold) {
int row, col;
uint8_t data[6][3] = {
/* Three columns of data. */
{2, 10, 30},
{3, 40, 60},
{4, 50, 70},
{5, 20, 50},
{6, 10, 40},
{7, 50, 30}
};
worksheet_write_string(worksheet, CELL("A1"), "Number", bold);
worksheet_write_string(worksheet, CELL("B1"), "Batch 1", bold);
worksheet_write_string(worksheet, CELL("C1"), "Batch 2", bold);
for (row = 0; row < 6; row++)
for (col = 0; col < 3; col++)
worksheet_write_number(worksheet, row + 1, col, data[row][col] , NULL);
}
/*
* Create a worksheet with examples charts.
*/
int main() {
lxw_workbook *workbook = workbook_new("chart_scatter.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);
/*
* Chart 1. Create a scatter chart, the default shows points only.
*/
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add a second series but leave the categories and values undefined. They
* can be defined later using the alternative syntax shown below. */
series = chart_add_series(chart, NULL, NULL);
/* Configure the series using a syntax that is easier to define programmatically. */
chart_series_set_categories(series, "Sheet1", 1, 0, 6, 0); /* "=Sheet1!$A$2:$A$7" */
chart_series_set_values(series, "Sheet1", 1, 2, 6, 2); /* "=Sheet1!$C$2:$C$7" */
chart_series_set_name_range(series, "Sheet1", 0, 2); /* "=Sheet1!$C$1" */
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 11);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E2"), chart);
/*
* Chart 2. Create a scatter chart with straight lines and markers
* connecting the points.
*/
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 12);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E18"), chart);
/*
* Chart 3. Create a scatter chart with straight lines connecting the
* points.
*/
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 13);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E34"), chart);
/*
* Chart 4. Create a scatter chart with smooth lines and markers
* connecting the points.
*/
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 14);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E50"), chart);
/*
* Chart 5. Create a scatter chart with smooth lines connecting the
* points.
*/
/* Add the first series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7");
/* Set the name for the series instead of the default "Series 1". */
chart_series_set_name(series, "=Sheet1!$B$1");
/* Add the second series to the chart. */
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7");
/* Set the name for the series instead of the default "Series 2". */
chart_series_set_name(series, "=Sheet1!$C$1");
/* Add a chart title and some axis labels. */
chart_title_set_name(chart, "Results of sample analysis");
chart_axis_set_name(chart->x_axis, "Test number");
chart_axis_set_name(chart->y_axis, "Sample length (mm)");
/* Set an Excel chart style. */
chart_set_style(chart, 15);
/* Insert the chart into the worksheet. */
worksheet_insert_chart(worksheet, CELL("E66"), chart);
return workbook_close(workbook);
}
chart_set_style
void chart_set_style(lxw_chart *chart, uint8_t style_id)
Set the chart style type.
workbook_close
lxw_error workbook_close(lxw_workbook *workbook)
Close the Workbook object and write the XLSX file.
LXW_CHART_SCATTER_STRAIGHT
@ LXW_CHART_SCATTER_STRAIGHT
Definition: chart.h:140
chart_series_set_name
void chart_series_set_name(lxw_chart_series *series, const char *name)
Set the name of a chart series range.
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_series_set_categories
void chart_series_set_categories(lxw_chart_series *series, const char *sheetname, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col)
Set a series "categories" range using row and column values.
LXW_CHART_SCATTER_SMOOTH_WITH_MARKERS
@ LXW_CHART_SCATTER_SMOOTH_WITH_MARKERS
Definition: chart.h:149
format_set_bold
void format_set_bold(lxw_format *format)
Turn on bold for the format font.
lxw_worksheet
Struct to represent an Excel worksheet.
Definition: worksheet.h:1603
LXW_CHART_SCATTER_STRAIGHT_WITH_MARKERS
@ LXW_CHART_SCATTER_STRAIGHT_WITH_MARKERS
Definition: chart.h:143
lxw_format
Struct to represent the formatting properties of an Excel format.
Definition: format.h:358
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_series
Struct to represent an Excel chart data series.
Definition: chart.h:957
lxw_chart::y_axis
lxw_chart_axis * y_axis
Definition: chart.h:1107
LXW_CHART_SCATTER_SMOOTH
@ LXW_CHART_SCATTER_SMOOTH
Definition: chart.h:146
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
chart_title_set_name
void chart_title_set_name(lxw_chart *chart, const char *name)
Set the title of the chart.
worksheet_write_string
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.
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.
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.
chart_series_set_values
void chart_series_set_values(lxw_chart_series *series, const char *sheetname, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col)
Set a series "values" range using row and column values.
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:
LXW_CHART_SCATTER
@ LXW_CHART_SCATTER
Definition: chart.h:137
workbook_add_format
lxw_format * workbook_add_format(lxw_workbook *workbook)
Create a new Format object to formats cells in worksheets.
chart_series_set_name_range
void chart_series_set_name_range(lxw_chart_series *series, const char *sheetname, lxw_row_t row, lxw_col_t col)
Set a series name formula using row and column values.