1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * Rappture 2.0 Plot Object Source |
---|
4 | * |
---|
5 | * ====================================================================== |
---|
6 | * AUTHOR: Derrick Kearney, Purdue University |
---|
7 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
8 | * |
---|
9 | * See the file "license.terms" for information on usage and |
---|
10 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
11 | * ====================================================================== |
---|
12 | */ |
---|
13 | |
---|
14 | #include "RpPlot.h" |
---|
15 | |
---|
16 | using namespace Rappture; |
---|
17 | |
---|
18 | const char Plot::format[] = "RAPPTURE::PLOT::FORMAT"; |
---|
19 | const char Plot::id[] = "RAPPTURE::PLOT::ID"; |
---|
20 | const char Plot::xaxis[] = "xaxis"; |
---|
21 | const char Plot::yaxis[] = "yaxis"; |
---|
22 | const char Plot::creator[] = "RAPPTURE::PLOT::CREATOR"; |
---|
23 | |
---|
24 | /* |
---|
25 | const char *Plot::creator[] = { |
---|
26 | "plot", |
---|
27 | "user" |
---|
28 | }; |
---|
29 | */ |
---|
30 | |
---|
31 | Plot::Plot () |
---|
32 | : Object (), |
---|
33 | _curveList (NULL) |
---|
34 | { |
---|
35 | // this->path(autopath()); |
---|
36 | this->name(""); |
---|
37 | this->path("run"); |
---|
38 | this->label(""); |
---|
39 | this->desc(""); |
---|
40 | } |
---|
41 | |
---|
42 | // copy constructor |
---|
43 | Plot::Plot ( const Plot& o ) |
---|
44 | : Object(o) |
---|
45 | { |
---|
46 | _curveList = Rp_ChainCreate(); |
---|
47 | Rp_ChainCopy(this->_curveList,o._curveList,&__curveCopyFxn); |
---|
48 | // need to copy _curveList |
---|
49 | } |
---|
50 | |
---|
51 | // default destructor |
---|
52 | Plot::~Plot () |
---|
53 | { |
---|
54 | // remove the properties plot put in each curve |
---|
55 | // unallocate _curveList |
---|
56 | |
---|
57 | Rp_ChainLink *l = Rp_ChainFirstLink(_curveList); |
---|
58 | while (l != NULL) { |
---|
59 | Curve * c = (Curve *) Rp_ChainGetValue(l); |
---|
60 | c->propremove(Plot::format); |
---|
61 | c->propremove(Plot::id); |
---|
62 | delete c; |
---|
63 | c = NULL; |
---|
64 | l = Rp_ChainNextLink(l); |
---|
65 | } |
---|
66 | |
---|
67 | Rp_ChainDestroy(_curveList); |
---|
68 | } |
---|
69 | |
---|
70 | /**********************************************************************/ |
---|
71 | // METHOD: add() |
---|
72 | /// Add an xy curve to the object |
---|
73 | /** |
---|
74 | * Add an xy curve to the object. |
---|
75 | * returns curve id |
---|
76 | */ |
---|
77 | |
---|
78 | Plot& |
---|
79 | Plot::add( |
---|
80 | size_t nPts, |
---|
81 | double *x, |
---|
82 | double *y, |
---|
83 | const char *fmt, |
---|
84 | const char *name) |
---|
85 | { |
---|
86 | // Curve *c = Curve(autopath(),"","",__group()); |
---|
87 | Curve *c = new Curve("","","",""); |
---|
88 | |
---|
89 | if (c == NULL) { |
---|
90 | // raise memory error and exit |
---|
91 | return *this; |
---|
92 | } |
---|
93 | |
---|
94 | c->name(name); |
---|
95 | |
---|
96 | // can't use "xaxis" kinda strings here have to allocate it forreal |
---|
97 | c->axis(Plot::xaxis,"","","","",x,nPts); |
---|
98 | c->axis(Plot::yaxis,"","","","",y,nPts); |
---|
99 | c->propstr(Plot::format,fmt); |
---|
100 | c->propstr(Plot::creator,"plot"); |
---|
101 | |
---|
102 | if (_curveList == NULL) { |
---|
103 | _curveList = Rp_ChainCreate(); |
---|
104 | if (_curveList == NULL) { |
---|
105 | // raise error and exit |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | Rp_ChainAppend(_curveList,c); |
---|
110 | |
---|
111 | return *this; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | /**********************************************************************/ |
---|
116 | // METHOD: add() |
---|
117 | /// Add an xy curve to the object |
---|
118 | /** |
---|
119 | * Add an xy curve to the object. |
---|
120 | * returns curve id |
---|
121 | */ |
---|
122 | |
---|
123 | Plot& |
---|
124 | Plot::add( |
---|
125 | Curve *c, |
---|
126 | const char *name) |
---|
127 | { |
---|
128 | if (c == NULL) { |
---|
129 | // raise memory error and exit |
---|
130 | return *this; |
---|
131 | } |
---|
132 | |
---|
133 | c->propstr(Plot::id,name); |
---|
134 | |
---|
135 | if (_curveList == NULL) { |
---|
136 | _curveList = Rp_ChainCreate(); |
---|
137 | if (_curveList == NULL) { |
---|
138 | // raise error and exit |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | Rp_ChainAppend(_curveList,c); |
---|
143 | |
---|
144 | return *this; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | /**********************************************************************/ |
---|
149 | // METHOD: count() |
---|
150 | /// return number of curves in this object |
---|
151 | /** |
---|
152 | * count of the number of curves contained in this object |
---|
153 | * returns curve count |
---|
154 | */ |
---|
155 | |
---|
156 | size_t |
---|
157 | Plot::count() const |
---|
158 | { |
---|
159 | // return (size_t) Rp_ChainGetLength(_curveList); |
---|
160 | return Rp_ChainGetLength(_curveList); |
---|
161 | } |
---|
162 | |
---|
163 | /**********************************************************************/ |
---|
164 | // METHOD: curve() |
---|
165 | /// Retrieve an xy curve from the object |
---|
166 | /** |
---|
167 | * Get a named xy curve. |
---|
168 | * returns pointer to Rappture::Curve |
---|
169 | */ |
---|
170 | |
---|
171 | Curve * |
---|
172 | Plot::curve( |
---|
173 | const char *name) const |
---|
174 | { |
---|
175 | Curve *c = NULL; |
---|
176 | Rp_ChainLink *l = NULL; |
---|
177 | |
---|
178 | l = __searchCurveList(name); |
---|
179 | if (l != NULL) { |
---|
180 | c = (Curve *) Rp_ChainGetValue(l); |
---|
181 | } |
---|
182 | return c; |
---|
183 | } |
---|
184 | |
---|
185 | /**********************************************************************/ |
---|
186 | // METHOD: getNthCurve() |
---|
187 | /// Return the Nth Curve object |
---|
188 | /** |
---|
189 | * Return the Nth Curve |
---|
190 | */ |
---|
191 | |
---|
192 | Curve * |
---|
193 | Plot::getNthCurve(size_t n) const |
---|
194 | { |
---|
195 | Rp_ChainLink *l = NULL; |
---|
196 | l = Rp_ChainGetNthLink(_curveList,n); |
---|
197 | |
---|
198 | if (l == NULL) { |
---|
199 | return NULL; |
---|
200 | } |
---|
201 | |
---|
202 | return (Curve *) Rp_ChainGetValue(l); |
---|
203 | } |
---|
204 | |
---|
205 | Rp_ChainLink * |
---|
206 | Plot::__searchCurveList(const char *name) const |
---|
207 | { |
---|
208 | if (name == NULL) { |
---|
209 | return NULL; |
---|
210 | } |
---|
211 | |
---|
212 | if (_curveList == NULL) { |
---|
213 | return NULL; |
---|
214 | } |
---|
215 | |
---|
216 | Rp_ChainLink *l = NULL; |
---|
217 | Rp_ChainLink *retval = NULL; |
---|
218 | |
---|
219 | // traverse the list looking for the match |
---|
220 | l = Rp_ChainFirstLink(_curveList); |
---|
221 | while (l != NULL) { |
---|
222 | Curve *c = (Curve *) Rp_ChainGetValue(l); |
---|
223 | const char *cname = c->propstr(Plot::id); |
---|
224 | if (cname != NULL) { |
---|
225 | if((*cname == *name) && (strcmp(cname,name) == 0)) { |
---|
226 | // we found matching entry, return it |
---|
227 | retval = l; |
---|
228 | break; |
---|
229 | } |
---|
230 | } |
---|
231 | l = Rp_ChainNextLink(l); |
---|
232 | } |
---|
233 | |
---|
234 | return retval; |
---|
235 | } |
---|
236 | |
---|
237 | int |
---|
238 | Plot::__curveCopyFxn(void **to, void *from) |
---|
239 | { |
---|
240 | if (from == NULL) { |
---|
241 | // return error, improper function call |
---|
242 | return -1; |
---|
243 | } |
---|
244 | |
---|
245 | Curve *c = new Curve(*((Curve *)from)); |
---|
246 | *to = (void *) c; |
---|
247 | |
---|
248 | return 0; |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | /**********************************************************************/ |
---|
253 | // METHOD: configureFromTree(ClientData p) |
---|
254 | /// configure the object from a Rappture1.1 based tree |
---|
255 | /** |
---|
256 | * Configure the object from a Rappture1.1 based tree |
---|
257 | */ |
---|
258 | |
---|
259 | // FIXME: this is an incomplete definition of how to |
---|
260 | // configure a plot object from a tree because there |
---|
261 | // currently is no xml description for plot objects |
---|
262 | void |
---|
263 | Plot::__configureFromTree(ClientData c) |
---|
264 | { |
---|
265 | Rp_ParserXml *p = (Rp_ParserXml *)c; |
---|
266 | if (p == NULL) { |
---|
267 | // FIXME: setup error |
---|
268 | return; |
---|
269 | } |
---|
270 | |
---|
271 | Rp_TreeNode node = Rp_ParserXmlElement(p,NULL); |
---|
272 | |
---|
273 | Rappture::Path pathObj(Rp_ParserXmlNodePath(p,node)); |
---|
274 | |
---|
275 | path(pathObj.parent()); |
---|
276 | name(Rp_ParserXmlNodeId(p,node)); |
---|
277 | |
---|
278 | return; |
---|
279 | } |
---|
280 | |
---|
281 | /**********************************************************************/ |
---|
282 | // METHOD: dumpToTree(ClientData p) |
---|
283 | /// dump the object to a Rappture1.1 based tree |
---|
284 | /** |
---|
285 | * Dump the object to a Rappture1.1 based tree |
---|
286 | */ |
---|
287 | |
---|
288 | void |
---|
289 | Plot::__dumpToTree(ClientData c) |
---|
290 | { |
---|
291 | if (c == NULL) { |
---|
292 | // FIXME: setup error |
---|
293 | return; |
---|
294 | } |
---|
295 | |
---|
296 | Rp_ParserXml *parser = (Rp_ParserXml *)c; |
---|
297 | |
---|
298 | Path p; |
---|
299 | |
---|
300 | p.parent(path()); |
---|
301 | p.last(); |
---|
302 | |
---|
303 | Rp_ChainLink *l = NULL; |
---|
304 | |
---|
305 | l = Rp_ChainFirstLink(_curveList); |
---|
306 | while (l != NULL) { |
---|
307 | Curve *c = (Curve *) Rp_ChainGetValue(l); |
---|
308 | |
---|
309 | //find who created the curve |
---|
310 | const char *ccreator = c->propstr(Plot::creator); |
---|
311 | if ((ccreator != NULL) && |
---|
312 | (*ccreator == 'p') && |
---|
313 | (strcmp(ccreator,"plot") == 0)) { |
---|
314 | // FIXME: check fields to see if the user specified value |
---|
315 | // plot defined curve, use plot's labels in curve's xml |
---|
316 | c->group(label()); |
---|
317 | c->label(label()); |
---|
318 | c->desc(desc()); |
---|
319 | |
---|
320 | |
---|
321 | const char *xlabel = propstr("xlabel"); |
---|
322 | const char *xdesc = propstr("xdesc"); |
---|
323 | const char *xunits = propstr("xunits"); |
---|
324 | const char *xscale = propstr("xscale"); |
---|
325 | const char *ylabel = propstr("ylabel"); |
---|
326 | const char *ydesc = propstr("ydesc"); |
---|
327 | const char *yunits = propstr("yunits"); |
---|
328 | const char *yscale = propstr("yscale"); |
---|
329 | |
---|
330 | if (xlabel || xdesc || xunits || xscale) { |
---|
331 | Array1D *cxaxis = c->getAxis(Plot::xaxis); |
---|
332 | cxaxis->label(xlabel); |
---|
333 | cxaxis->desc(xdesc); |
---|
334 | cxaxis->units(xunits); |
---|
335 | cxaxis->scale(xscale); |
---|
336 | } |
---|
337 | |
---|
338 | if (ylabel || ydesc || yunits || yscale) { |
---|
339 | Array1D *cyaxis = c->getAxis(Plot::yaxis); |
---|
340 | cyaxis->label(ylabel); |
---|
341 | cyaxis->desc(ydesc); |
---|
342 | cyaxis->units(yunits); |
---|
343 | cyaxis->scale(yscale); |
---|
344 | } |
---|
345 | |
---|
346 | } |
---|
347 | |
---|
348 | c->dump(RPCONFIG_TREE,parser); |
---|
349 | l = Rp_ChainNextLink(l); |
---|
350 | } |
---|
351 | } |
---|
352 | |
---|
353 | /**********************************************************************/ |
---|
354 | // METHOD: is() |
---|
355 | /// what kind of object is this |
---|
356 | /** |
---|
357 | * return hex value telling what kind of object this is. |
---|
358 | */ |
---|
359 | |
---|
360 | const int |
---|
361 | Plot::is() const |
---|
362 | { |
---|
363 | // return "plot" in hex |
---|
364 | return 0x706C6F74; |
---|
365 | } |
---|
366 | |
---|
367 | // -------------------------------------------------------------------- // |
---|