File: chart_data_labels.c

package info (click to toggle)
libxlsxwriter 1.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,888 kB
  • sloc: ansic: 65,861; python: 2,106; makefile: 693; perl: 229; sh: 224; xml: 168; cpp: 73; javascript: 5
file content (386 lines) | stat: -rw-r--r-- 13,311 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * A demo of an various Excel chart data label features that are available via
 * a libxlsxwriter chart.
 *
 * Copyright 2014-2026, John McNamara, jmcnamara@cpan.org
 *
 */

#include "xlsxwriter.h"

/*
 * Create a worksheet with examples charts.
 */
int main() {

    lxw_workbook     *workbook  = workbook_new("chart_data_labels.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);
    format_set_bold(bold);

    /* Some chart positioning options. */
    lxw_chart_options options = {.x_offset = 25,  .y_offset = 10};

    /* Write some data for the chart. */
    worksheet_write_string(worksheet, 0, 0, "Number",  bold);
    worksheet_write_number(worksheet, 1, 0, 2,         NULL);
    worksheet_write_number(worksheet, 2, 0, 3,         NULL);
    worksheet_write_number(worksheet, 3, 0, 4,         NULL);
    worksheet_write_number(worksheet, 4, 0, 5,         NULL);
    worksheet_write_number(worksheet, 5, 0, 6,         NULL);
    worksheet_write_number(worksheet, 6, 0, 7,         NULL);

    worksheet_write_string(worksheet, 0, 1, "Data",    bold);
    worksheet_write_number(worksheet, 1, 1, 20,        NULL);
    worksheet_write_number(worksheet, 2, 1, 10,        NULL);
    worksheet_write_number(worksheet, 3, 1, 20,        NULL);
    worksheet_write_number(worksheet, 4, 1, 30,        NULL);
    worksheet_write_number(worksheet, 5, 1, 40,        NULL);
    worksheet_write_number(worksheet, 6, 1, 30,        NULL);

    worksheet_write_string(worksheet, 0, 2, "Text",    bold);
    worksheet_write_string(worksheet, 1, 2, "Jan",     NULL);
    worksheet_write_string(worksheet, 2, 2, "Feb",     NULL);
    worksheet_write_string(worksheet, 3, 2, "Mar",     NULL);
    worksheet_write_string(worksheet, 4, 2, "Apr",     NULL);
    worksheet_write_string(worksheet, 5, 2, "May",     NULL);
    worksheet_write_string(worksheet, 6, 2, "Jun",     NULL);


    /*
     * Chart 1. Example with standard data labels.
     */
    lxw_chart *chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);

    /* Add a chart title. */
    chart_title_set_name(chart, "Chart with standard data labels");

    /* Add a data series to the chart. */
    lxw_chart_series *series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
                                                       "=Sheet1!$B$2:$B$7");

    /* Add the series data labels. */
    chart_series_set_labels(series);

    /* Turn off the legend. */
    chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart_opt(worksheet, CELL("D2"), chart, &options);


    /*
     * Chart 2. Example with value and category data labels.
     */
    chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);

    /* Add a chart title. */
    chart_title_set_name(chart, "Category and Value data labels");

    /* Add a data series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
                                     "=Sheet1!$B$2:$B$7");

    /* Add the series data labels. */
    chart_series_set_labels(series);

    /* Turn on Value and Category labels. */
    chart_series_set_labels_options(series, LXW_FALSE, LXW_TRUE, LXW_TRUE);

    /* Turn off the legend. */
    chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart_opt(worksheet, CELL("D18"), chart, &options);


    /*
     * Chart 3. Example with standard data labels with different font.
     */
    chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);

    /* Add a chart title. */
    chart_title_set_name(chart, "Data labels with user defined font");

    /* Add a data series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
                                     "=Sheet1!$B$2:$B$7");

    /* Add the series data labels. */
    chart_series_set_labels(series);

    lxw_chart_font font1 = {.bold = LXW_TRUE, .color = LXW_COLOR_RED, .rotation = -30};
    chart_series_set_labels_font(series, &font1);

    /* Turn off the legend. */
    chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart_opt(worksheet, CELL("D34"), chart, &options);


    /*
     * Chart 4. Example with standard data labels and formatting.
     */
    chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);

    /* Add a chart title. */
    chart_title_set_name(chart, "Data labels with formatting");

    /* Add a data series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
                                     "=Sheet1!$B$2:$B$7");

    /* Add the series data labels. */
    chart_series_set_labels(series);

    /* Set the border/line and fill for the data labels. */
    lxw_chart_line line1 = {.color = LXW_COLOR_RED};
    lxw_chart_fill fill1 = {.color = LXW_COLOR_YELLOW};

    chart_series_set_labels_line(series, &line1);
    chart_series_set_labels_fill(series, &fill1);

    /* Turn off the legend. */
    chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart_opt(worksheet, CELL("D50"), chart, &options);


    /*
     * Chart 5.Example with custom string data labels.
     */
    chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);

    /* Add a chart title. */
    chart_title_set_name(chart, "Chart with custom string data labels");

    /* Add a data series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
                                     "=Sheet1!$B$2:$B$7");

    /* Add the series data labels. */
    chart_series_set_labels(series);

    /* Create some custom labels. */
    lxw_chart_data_label data_label5_1 = {.value = "Amy"};
    lxw_chart_data_label data_label5_2 = {.value = "Bea"};
    lxw_chart_data_label data_label5_3 = {.value = "Eva"};
    lxw_chart_data_label data_label5_4 = {.value = "Fay"};
    lxw_chart_data_label data_label5_5 = {.value = "Liv"};
    lxw_chart_data_label data_label5_6 = {.value = "Una"};

    /* Create an array of label pointers. NULL indicates the end of the array. */
    lxw_chart_data_label *data_labels5[] = {
        &data_label5_1,
        &data_label5_2,
        &data_label5_3,
        &data_label5_4,
        &data_label5_5,
        &data_label5_6,
        NULL
    };

    /* Set the custom labels. */
    chart_series_set_labels_custom(series, data_labels5);

    /* Turn off the legend. */
    chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart_opt(worksheet, CELL("D66"), chart, &options);


    /*
     * Chart 6. Example with custom data labels from cells.
     */
    chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);

    /* Add a chart title. */
    chart_title_set_name(chart, "Chart with custom data labels from cells");

    /* Add a data series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
                                     "=Sheet1!$B$2:$B$7");

    /* Add the series data labels. */
    chart_series_set_labels(series);

    /* Create some custom labels. */
    lxw_chart_data_label data_label6_1 = {.value = "=Sheet1!$C$2"};
    lxw_chart_data_label data_label6_2 = {.value = "=Sheet1!$C$3"};
    lxw_chart_data_label data_label6_3 = {.value = "=Sheet1!$C$4"};
    lxw_chart_data_label data_label6_4 = {.value = "=Sheet1!$C$5"};
    lxw_chart_data_label data_label6_5 = {.value = "=Sheet1!$C$6"};
    lxw_chart_data_label data_label6_6 = {.value = "=Sheet1!$C$7"};

    /* Create an array of label pointers. NULL indicates the end of the array. */
    lxw_chart_data_label *data_labels6[] = {
        &data_label6_1,
        &data_label6_2,
        &data_label6_3,
        &data_label6_4,
        &data_label6_5,
        &data_label6_6,
        NULL
    };

    /* Set the custom labels. */
    chart_series_set_labels_custom(series, data_labels6);

    /* Turn off the legend. */
    chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart_opt(worksheet, CELL("D82"), chart, &options);


    /*
     * Chart 7. Example with custom and default data labels.
     */
    chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);

    /* Add a chart title. */
    chart_title_set_name(chart, "Mixed custom and default data labels");

    /* Add a data series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
                                     "=Sheet1!$B$2:$B$7");

    lxw_chart_font font2 = {.color = LXW_COLOR_RED};

    /* Add the series data labels. */
    chart_series_set_labels(series);

    /* Create some custom labels. */

    /* The following is used to get a mix of default and custom labels. The
     * items initialized with '{0}' and items without a custom label (points 5
     * and 6 which come after NULL) will get the default value. We also set a
     * font for the custom items as an extra example.
     */
    lxw_chart_data_label data_label7_1 = {.value = "=Sheet1!$C$2", .font = &font2};
    lxw_chart_data_label data_label7_2 = {0};
    lxw_chart_data_label data_label7_3 = {.value = "=Sheet1!$C$4", .font = &font2};
    lxw_chart_data_label data_label7_4 = {.value = "=Sheet1!$C$5", .font = &font2};

    /* Create an array of label pointers. NULL indicates the end of the array. */
    lxw_chart_data_label *data_labels7[] = {
        &data_label7_1,
        &data_label7_2,
        &data_label7_3,
        &data_label7_4,
        NULL
    };

    /* Set the custom labels. */
    chart_series_set_labels_custom(series, data_labels7);

    /* Turn off the legend. */
    chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart_opt(worksheet, CELL("D98"), chart, &options);


    /*
     * Chart 8. Example with deleted/hidden custom data labels.
     */
    chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);

    /* Add a chart title. */
    chart_title_set_name(chart, "Chart with deleted data labels");

    /* Add a data series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
                                     "=Sheet1!$B$2:$B$7");

    /* Add the series data labels. */
    chart_series_set_labels(series);

    /* Create some custom labels. */
    lxw_chart_data_label hide = {.hide = LXW_TRUE};
    lxw_chart_data_label keep = {.hide = LXW_FALSE};

    /* An initialized struct like this would also work: */
    /* lxw_chart_data_label keep = {0}; */

    /* Create an array of label pointers. NULL indicates the end of the array. */
    lxw_chart_data_label *data_labels8[] = {
        &hide,
        &keep,
        &hide,
        &hide,
        &keep,
        &hide,
        NULL
    };

    /* Set the custom labels. */
    chart_series_set_labels_custom(series, data_labels8);

    /* Turn off the legend. */
    chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart_opt(worksheet, CELL("D114"), chart, &options);


    /*
     * Chart 9.Example with custom string data labels and formatting.
     */
    chart = workbook_add_chart(workbook, LXW_CHART_COLUMN);

    /* Add a chart title. */
    chart_title_set_name(chart, "Chart with custom labels and formatting");

    /* Add a data series to the chart. */
    series = chart_add_series(chart, "=Sheet1!$A$2:$A$7",
                                     "=Sheet1!$B$2:$B$7");

    /* Add the series data labels. */
    chart_series_set_labels(series);

    /* Set the border/line and fill for the data labels. */
    lxw_chart_line line2 = {.color = LXW_COLOR_RED};
    lxw_chart_fill fill2 = {.color = LXW_COLOR_YELLOW};
    lxw_chart_line line3 = {.color = LXW_COLOR_BLUE};
    lxw_chart_fill fill3 = {.color = LXW_COLOR_GREEN};

    /* Create some custom labels. */
    lxw_chart_data_label data_label9_1 = {.value = "Amy", .line = &line3};
    lxw_chart_data_label data_label9_2 = {.value = "Bea"};
    lxw_chart_data_label data_label9_3 = {.value = "Eva"};
    lxw_chart_data_label data_label9_4 = {.value = "Fay"};
    lxw_chart_data_label data_label9_5 = {.value = "Liv"};
    lxw_chart_data_label data_label9_6 = {.value = "Una", .fill = &fill3};

    /* Set the default formatting for the data labels in the series. */
    chart_series_set_labels_line(series, &line2);
    chart_series_set_labels_fill(series, &fill2);

    /* Create an array of label pointers. NULL indicates the end of the array. */
    lxw_chart_data_label *data_labels9[] = {
        &data_label9_1,
        &data_label9_2,
        &data_label9_3,
        &data_label9_4,
        &data_label9_5,
        &data_label9_6,
        NULL
    };

    /* Set the custom labels. */
    chart_series_set_labels_custom(series, data_labels9);

    /* Turn off the legend. */
    chart_legend_set_position(chart, LXW_CHART_LEGEND_NONE);

    /* Insert the chart into the worksheet. */
    worksheet_insert_chart_opt(worksheet, CELL("D130"), chart, &options);

    return workbook_close(workbook);
}