-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchart_data_tools.bas
212 lines (154 loc) · 6.3 KB
/
chart_data_tools.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
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
/'
* A demo of an various Excel chart data tools that are available via a
* libxlsxwriter chart.
*
* These include Drop Lines and High-Low Lines.
*
* Copyright 2014-2017, John McNamara, [email protected]
*
* 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, 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 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_data_tools.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. Example with High Low Lines.
'/
var chart = workbook_add_chart(workbook, LXW_CHART_LINE)
/' Add a chart title. '/
chart_title_set_name(chart, "Chart with High-Low Lines")
/' Add the first series to the chart. '/
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7")
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7")
/' Add high-low lines to the chart. '/
chart_set_high_low_lines(chart, NULL)
/' Insert the chart into the worksheet. '/
worksheet_insert_chart(worksheet, CELL("E2"), chart)
/'
* Chart 2. Example with Drop Lines.
'/
chart = workbook_add_chart(workbook, LXW_CHART_LINE)
/' Add a chart title. '/
chart_title_set_name(chart, "Chart with Drop Lines")
/' Add the first series to the chart. '/
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7")
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7")
/' Add drop lines to the chart. '/
chart_set_drop_lines(chart, NULL)
/' Insert the chart into the worksheet. '/
worksheet_insert_chart(worksheet, CELL("E18"), chart)
/'
* Chart 3. Example with Up-Down bars.
'/
chart = workbook_add_chart(workbook, LXW_CHART_LINE)
/' Add a chart title. '/
chart_title_set_name(chart, "Chart with Up-Down bars")
/' Add the first series to the chart. '/
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7")
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7")
/' Add Up-Down bars to the chart. '/
chart_set_up_down_bars(chart)
/' Insert the chart into the worksheet. '/
worksheet_insert_chart(worksheet, CELL("E34"), chart)
/'
* Chart 4. Example with Up-Down bars with formatting.
'/
chart = workbook_add_chart(workbook, LXW_CHART_LINE)
/' Add a chart title. '/
chart_title_set_name(chart, "Chart with Up-Down bars")
/' Add the first series to the chart. '/
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7")
chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7")
/' Add Up-Down bars to the chart, with formatting. '/
Dim As lxw_chart_line line_
line_.color = LXW_COLOR_BLACK
Dim As lxw_chart_fill up_fill
up_fill.color = &H00B050
Dim As lxw_chart_fill down_fill
down_fill.color = LXW_COLOR_RED
chart_set_up_down_bars_format(chart, @line_, @up_fill, @line_, @down_fill)
/' Insert the chart into the worksheet. '/
worksheet_insert_chart(worksheet, CELL("E50"), chart)
/'
* Chart 5. Example with Markers and data labels.
'/
chart = workbook_add_chart(workbook, LXW_CHART_LINE)
/' Add a chart title. '/
chart_title_set_name(chart, "Chart with Data Labels and Markers")
/' Add the first series to the chart. '/
var series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7")
chart_add_series( chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7")
/' Add series markers. '/
chart_series_set_marker_type(series, LXW_CHART_MARKER_CIRCLE)
/' Add series data labels. '/
chart_series_set_labels(series)
/' Insert the chart into the worksheet. '/
worksheet_insert_chart(worksheet, CELL("E66"), chart)
/'
* Chart 6. Example with Error Bars.
'/
chart = workbook_add_chart(workbook, LXW_CHART_LINE)
/' Add a chart title. '/
chart_title_set_name(chart, "Chart with Error Bars")
/' Add the first series to the chart. '/
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7")
chart_add_series( chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7")
/' Add error bars to show Standard Error. '/
chart_series_set_error_bars(series->y_error_bars, _
LXW_CHART_ERROR_BAR_TYPE_STD_ERROR, 0)
/' Add series data labels. '/
chart_series_set_labels(series)
/' Insert the chart into the worksheet. '/
worksheet_insert_chart(worksheet, CELL("E82"), chart)
/'
* Chart 7. Example with a trendline
'/
chart = workbook_add_chart(workbook, LXW_CHART_LINE)
/' Add a chart title. '/
chart_title_set_name(chart, "Chart with a Trendline")
/' Add the first series to the chart. '/
series = chart_add_series(chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$B$2:$B$7")
chart_add_series( chart, "=Sheet1!$A$2:$A$7", "=Sheet1!$C$2:$C$7")
/' Add a polynomial trendline. '/
Dim as lxw_chart_line poly_line
poly_line.color = LXW_COLOR_GRAY
poly_line.dash_type = LXW_CHART_LINE_DASH_LONG_DASH
chart_series_set_trendline(series, LXW_CHART_TRENDLINE_TYPE_POLY, 3)
chart_series_set_trendline_line(series, @poly_line)
/' Insert the chart into the worksheet. '/
worksheet_insert_chart(worksheet, CELL("E98"), chart)
return workbook_close(workbook)
End Function
main()