-
Notifications
You must be signed in to change notification settings - Fork 0
/
DailyDataMeansHtmlFormatter.m
184 lines (107 loc) · 4.33 KB
/
DailyDataMeansHtmlFormatter.m
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
//
// DailyDataMeansHtmlFormatter.m
// Bartender
//
// Created by Tom Houpt on 13/2/16.
//
//
#import "DailyDataMeansHtmlFormatter.h"
@implementation DailyDataMeansHtmlFormatter
-(id)initWithDailyData:(DailyData *)d andTableID:(NSString *)tid; {
self = [super init];
if (self) {
data = d;
tableID = tid;
useVerticalLines = YES;
useAlternatingRowColors = YES;
if (nil != data) {
itemLabels = [data itemLabels];
groupLabels = [data groupLabels];
groupMeans = [data groupMeans];
}
}
return self;
}
- (NSString *) htmlString; {
NSMutableString *htmlString = [[NSMutableString alloc] init];
[self appendHtmlToString:htmlString];
return htmlString;
}
-(void)appendCssToString:(NSMutableString *)buffer; {
NSMutableString *cssString = [[NSMutableString alloc] init];
[cssString appendString: @"<style>\r"];
NSString *prefixString;
if (nil == tableID) prefixString = @".meantable";
else prefixString = [NSString stringWithFormat:@"#%@",tableID];
// specify 12 pt fonts for table
[cssString appendFormat: @"%@ th, td {font-family: Helvetica, sans-serif; font-size:9pt;} \r", prefixString];
// [cssString appendFormat: @"%@ table { border-style: hidden } \r", prefixString];
// // to put vertical rules between columns, set
// td: { border-style: none solid none solid; border-width:thin; }
// or is this better?
// col { border-style: none solid }
//
// // to put rule under header row, set
// th: { border-style: none none solid none; }
// make san-serif
if (useAlternatingRowColors) {
// http://www.w3.org/Style/Examples/007/evenodd.en.html
[cssString appendFormat: @"%@ tr:nth-child(odd) { background-color:#ddd; } \r", prefixString];
[cssString appendFormat: @"%@ tr:nth-child(even) { background-color:#fff; } \r", prefixString];
// be sure to set up the webpreferences of the webview to print backgrounds, e.g.
// [myWebView setPreferencesIdentifier:@"myWebPreferences"];
// WebPreferences *prefs = [myWebView preferences];
// [prefs setShouldPrintBackgrounds:YES];
}
[cssString appendString: @"</style>\r"];
[buffer appendString:cssString];
}
- (void) appendHtmlToString:(NSMutableString *)buffer; {
[self appendCssToString:buffer];
[buffer appendString:@"<BR>\r"];
[buffer appendString:@"<BR>\r"];
[buffer appendString:@"<strong>Group Means</strong>"];
[buffer appendFormat: @"<TABLE "];
if (nil == tableID)[buffer appendString: @"class = \"meantable\" "];
else [buffer appendFormat: @"id = \"%@\" ",tableID];
NSString *rulesString;
if (!useVerticalLines && !useHorizontalLines) {rulesString = @"rules=none ";}
if ( useVerticalLines && !useHorizontalLines) {rulesString = @"rules=cols "; }
if (!useVerticalLines && useHorizontalLines) {rulesString = @"rules=rows "; }
if ( useVerticalLines && useHorizontalLines) {rulesString = @"rules=all "; }
if (nil == tableID)[buffer appendString: rulesString];
[buffer appendString: @">\r"];
[self appendHeadersToString:buffer];
NSUInteger rowIndex;
NSUInteger numRows = [groupLabels count];
for (rowIndex=0;rowIndex<numRows;rowIndex++) {
[self appendRowAtIndex:rowIndex toString:buffer];
}
[buffer appendString: @"</TABLE>\r"];
}
-(void)appendHeadersToString:(NSMutableString *)buffer; {
[buffer appendString: @"\t<TR frame=\"below\">\r"];
// remember to add "Group" label
[buffer appendString: @"\t\t<TH>Group</TH>\r"];
for (NSString *itemName in itemLabels) {
[buffer appendString: @"\t\t<TH>"];
[buffer appendString:itemName];
[buffer appendString: @"</TH>\r"];
}
[buffer appendString: @"\t</TR>\r"];
}
-(void)appendRowAtIndex:(NSUInteger)rowIndex toString:(NSMutableString *)buffer; {
NSArray *rowArray = [groupMeans objectAtIndex:rowIndex];
[buffer appendString: @"\t<TR>\r"];
// put in group name
[buffer appendString: @"\t\t<TD>"];
[buffer appendString: [groupLabels objectAtIndex:rowIndex]];
[buffer appendString: @"</TD>\r"];
for (BCMeanValue *theMean in rowArray) {
[buffer appendString: @"\t\t<TD>"];
[buffer appendString: [theMean mean2Text]];
[buffer appendString: @"</TD>\r"];
}
[buffer appendString: @"\t</TR>\r"];
}
@end