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

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

adding dump/configure interface to curve object, plot object is broken

File size: 7.6 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    c->name(name);
97
98    // can't use "xaxis" kinda strings here have to allocate it forreal
99    c->axis(Plot::xaxis,"","","","",x,nPts);
100    c->axis(Plot::yaxis,"","","","",y,nPts);
101    c->propstr(Plot::format,fmt);
102    c->propstr(Plot::creator,"plot");
103
104    if (_curveList == NULL) {
105        _curveList = Rp_ChainCreate();
106        if (_curveList == NULL) {
107            // raise error and exit
108        }
109    }
110
111    Rp_ChainAppend(_curveList,c);
112
113    return *this;
114}
115
116
117/**********************************************************************/
118// METHOD: add()
119/// Add an xy curve to the object
120/**
121 * Add an xy curve to the object.
122 * returns curve id
123 */
124
125Plot&
126Plot::add(
127    Curve *c,
128    const char *name)
129{
130    if (c == NULL) {
131       // raise memory error and exit
132       return *this;
133    }
134
135    c->propstr(Plot::id,name);
136
137    if (_curveList == NULL) {
138        _curveList = Rp_ChainCreate();
139        if (_curveList == NULL) {
140            // raise error and exit
141        }
142    }
143
144    Rp_ChainAppend(_curveList,c);
145
146    return *this;
147}
148
149
150/**********************************************************************/
151// METHOD: count()
152/// return number of curves in this object
153/**
154 * count of the number of curves contained in this object
155 * returns curve count
156 */
157
158size_t
159Plot::count() const
160{
161    // return (size_t) Rp_ChainGetLength(_curveList);
162    return Rp_ChainGetLength(_curveList);
163}
164
165/**********************************************************************/
166// METHOD: curve()
167/// Retrieve an xy curve from the object
168/**
169 * Get a named xy curve.
170 * returns pointer to Rappture::Curve
171 */
172
173Curve *
174Plot::curve(
175    const char *name) const
176{
177    Curve *c = NULL;
178    Rp_ChainLink *l = NULL;
179
180    l = __searchCurveList(name);
181    if (l != NULL) {
182        c = (Curve *) Rp_ChainGetValue(l);
183    }
184    return c;
185}
186
187/**********************************************************************/
188// METHOD: getNthCurve()
189/// Return the Nth Curve object
190/**
191 * Return the Nth Curve
192 */
193
194Curve *
195Plot::getNthCurve(size_t n) const
196{
197    Rp_ChainLink *l = NULL;
198    l = Rp_ChainGetNthLink(_curveList,n);
199
200    if (l == NULL) {
201        return NULL;
202    }
203
204    return (Curve *) Rp_ChainGetValue(l);
205}
206
207Rp_ChainLink *
208Plot::__searchCurveList(const char *name) const
209{
210    if (name == NULL) {
211        return NULL;
212    }
213
214    if (_curveList == NULL) {
215        return NULL;
216    }
217
218    Rp_ChainLink *l = NULL;
219    Rp_ChainLink *retval = NULL;
220
221    // traverse the list looking for the match
222    l = Rp_ChainFirstLink(_curveList);
223    while (l != NULL) {
224        Curve *c = (Curve *) Rp_ChainGetValue(l);
225        const char *cname = c->propstr(Plot::id);
226        if (cname != NULL) {
227            if((*cname == *name) && (strcmp(cname,name) == 0)) {
228                // we found matching entry, return it
229                retval = l;
230                break;
231            }
232        }
233        l = Rp_ChainNextLink(l);
234    }
235
236    return retval;
237}
238
239int
240Plot::__curveCopyFxn(void **to, void *from)
241{
242    if (from == NULL) {
243        // return error, improper function call
244        return -1;
245    }
246
247    Curve *c = new Curve(*((Curve *)from));
248    *to = (void *) c;
249
250    return 0;
251}
252
253/**********************************************************************/
254// METHOD: xml()
255/// Return the xml of this object
256/**
257 * returns the xml of this object
258 */
259
260const char *
261Plot::xml(size_t indent, size_t tabstop)
262{
263
264    Rp_ChainLink *l = NULL;
265
266    _tmpBuf.clear();
267
268    Rp_ParserXml *parser = Rp_ParserXmlCreate();
269
270    l = Rp_ChainFirstLink(_curveList);
271    while (l != NULL) {
272        Curve *c = (Curve *) Rp_ChainGetValue(l);
273
274        //find who created the curve
275        const char *ccreator = c->propstr(Plot::creator);
276        if ((ccreator != NULL) &&
277            (*ccreator == 'p') &&
278            (strcmp(ccreator,"plot") == 0)) {
279            // FIXME: check fields to see if the user specified value
280            // plot defined curve, use plot's labels in curve's xml
281            const char *xlabel = propstr("xlabel");
282            const char *xdesc  = propstr("xdesc");
283            const char *xunits = propstr("xunits");
284            const char *xscale = propstr("xscale");
285            const char *ylabel = propstr("ylabel");
286            const char *ydesc  = propstr("ydesc");
287            const char *yunits = propstr("yunits");
288            const char *yscale = propstr("yscale");
289
290            if (xlabel || xdesc || xunits || xscale) {
291                Array1D *cxaxis = c->getAxis(Plot::xaxis);
292                cxaxis->label(xlabel);
293                cxaxis->desc(xdesc);
294                cxaxis->units(xunits);
295                cxaxis->scale(xscale);
296            }
297
298            if (ylabel || ydesc || yunits || yscale) {
299                Array1D *cyaxis = c->getAxis(Plot::yaxis);
300                cyaxis->label(ylabel);
301                cyaxis->desc(ydesc);
302                cyaxis->units(yunits);
303                cyaxis->scale(yscale);
304            }
305        }
306
307        c->dump(RPCONFIG_TREE,parser);
308        l = Rp_ChainNextLink(l);
309    }
310    _tmpBuf.appendf("%s",Rp_ParserXmlXml(parser));
311    Rp_ParserXmlDestroy(&parser);
312
313    // remove trailing newline
314    //_tmpBuf.remove(1);
315    // append terminating null character
316    //_tmpBuf.append("\0",1);
317
318    return _tmpBuf.bytes();
319}
320
321/**********************************************************************/
322// METHOD: is()
323/// what kind of object is this
324/**
325 * return hex value telling what kind of object this is.
326 */
327
328const int
329Plot::is() const
330{
331    // return "plot" in hex
332    return 0x706C6F74;
333}
334
335// -------------------------------------------------------------------- //
Note: See TracBrowser for help on using the repository browser.