-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchart_area.bas
146 lines (108 loc) · 4.94 KB
/
chart_area.bas
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
/'
* An example of creating Excel area charts using the libxlsxwriter library.
*
* Copyright 2014-2017, John McNamara, jmcnamara@cpan.org
*
* translated by Lee June by using https://github.com/retsyo/libxlsxwriter_freebasic
'/
#include "auto_xlsxwriter.bi"
/'
* Write some data to the worksheet.
'/
sub write_worksheet_data(byref worksheet as lxw_worksheet ptr , byref bold as lxw_format ptr)
Dim As integer row, col
Dim As uinteger data_(0 to 5, 0 to 2) = { _
/' Three columns of data. '/ _
{2, 40, 30}, _
{3, 40, 25}, _
{4, 50, 30}, _
{5, 30, 10}, _
{6, 25, 5}, _
{7, 50, 10} _
}
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 to 5
for col = 0 to 2
worksheet_write_number(worksheet, row + 1, col, data_(row, col) , NULL)
next
next
end sub
/'
* Create a worksheet with examples charts.
'/
function main() as Integer
var workbook = new_workbook("chart_area.xlsx")
var worksheet = workbook_add_worksheet(workbook, NULL)
/' Add a bold format to use to highlight the header cells. '/
var bold = workbook_add_format(workbook)
format_set_bold(bold)
/' Write some data for the chart. '/
write_worksheet_data(worksheet, bold)
/'
* Chart 1. Create a area chart.
'/
var chart = workbook_add_chart(workbook, LXW_CHART_AREA)
/' Add the first series to the chart. '/
var 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!$B1$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 stacked area chart.
'/
chart = workbook_add_chart(workbook, LXW_CHART_AREA_STACKED)
/' 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!$B1$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!$C1$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 percent stacked area chart.
'/
chart = workbook_add_chart(workbook, LXW_CHART_AREA_STACKED_PERCENT)
/' 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!$B1$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!$C1$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)
return workbook_close(workbook)
End Function
main()