source: trunk/src/objects/RpPlot.cc @ 1528

Last change on this file since 1528 was 1528, checked in by dkearney, 15 years ago

updating the objects code and adding some more examples describing how i want developers to use the objects. some of the objects don't work yet, like the tree, anything with an example should work

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