comparison GEMBASSY-1.0.3/include/gplot.c @ 2:8947fca5f715 draft default tip

Uploaded
author ktnyt
date Fri, 26 Jun 2015 05:21:44 -0400
parents 84a17b3fad1f
children
comparison
equal deleted inserted replaced
1:84a17b3fad1f 2:8947fca5f715
1 /******************************************************************************
2 ** @source GEMBASSY plot routines
3 **
4 ** @version 1.0
5 ** @modified December 27 2012 Hidetoshi Itaya Created this file
6 ** @@
7 **
8 ** This library is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU Library General Public
10 ** License as published by the Free Software Foundation; either
11 ** version 2 of the License, or (at your option) any later version.
12 **
13 ** This library is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** Library General Public License for more details.
17 **
18 ** You should have received a copy of the GNU Library General Public
19 ** License along with this library; if not, write to the
20 ** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ** Boston, MA 02111-1307, USA.
22 ******************************************************************************/
23
24
25 #include "gplot.h"
26
27
28
29
30 /* @funclist gPlotFilebuff ****************************************************
31 **
32 ** Retrieves data from file buffer and plots the data using gPlotData
33 **
34 ******************************************************************************/
35
36 AjBool gPlotFilebuff(AjPFilebuff buff, AjPGraph graphs, gPlotParams *gpp)
37 {
38 AjPStr line = NULL;
39 AjPPStr temp = NULL;
40 AjPPStr name = NULL;
41 ajint i = 0;
42 ajint j = 0;
43 ajint col = 0;
44 ajint flag = 0;
45 float **data = NULL;
46
47 while(ajBuffreadLine(buff, &line))
48 {
49
50 /*
51 ** Allocate first time only
52 */
53
54 if(!col)
55 {
56 col = ajStrCalcCountC(line, ",") + 1;
57
58 if((temp = (AjPPStr)malloc(sizeof(AjPStr) * col)) == NULL)
59 {
60 return ajFalse;
61 }
62
63 if((name = (AjPPStr)malloc(sizeof(AjPStr) * col)) == NULL)
64 {
65 AJFREE(temp);
66 return ajFalse;
67 }
68
69 if((data = (float**)malloc(sizeof(float*) * col)) == NULL)
70 {
71 AJFREE(temp);
72 AJFREE(name);
73 return ajFalse;
74 }
75 for(i = 0; i < col; ++i)
76 {
77 if((data[i] = (float*)malloc(sizeof(float))) == NULL){
78 {
79 AJFREE(temp);
80 AJFREE(name);
81 for(j = 0; j < i; ++j)
82 {
83 AJFREE(data[j]);
84 }
85 AJFREE(data);
86 return ajFalse;
87 }
88 }
89 }
90 }
91
92 ajStrExchangeCC(&line, ",", "\n");
93 ajStrParseSplit(line, &temp);
94
95 for(i = 0; i < col; ++i)
96 {
97 if((data[i] = (float*)realloc(data[i], sizeof(float) * (j + 1))) == NULL)
98 {
99 AJFREE(temp);
100 AJFREE(name);
101 for(j = 0; j < i; ++j)
102 {
103 AJFREE(data[j]);
104 }
105 AJFREE(data);
106 return ajFalse;
107 }
108
109 ajStrRemoveLastNewline(&(temp[i]));
110 if(ajStrIsFloat(temp[i]))
111 {
112 ajStrToFloat(temp[i], &(data[i][j]));
113 ++flag;
114 }
115 else
116 {
117 name[i] = ajStrNewS(temp[i]);
118 }
119 }
120 j = flag ? j + 1 : j;
121 flag = 0;
122 }
123
124 (*gpp).data = data;
125 (*gpp).setNum = 0;
126 (*gpp).dataNum = j;
127 (*gpp).typeNum = col;
128 if(!(*gpp).names)
129 (*gpp).names = name;
130
131 if(j < 2)
132 gPlotFlip(gpp);
133
134 gPlotData(graphs, gpp);
135
136 for(i = 0; i < (*gpp).typeNum; ++i)
137 AJFREE((*gpp).data[i]);
138 AJFREE((*gpp).data);
139
140 data = NULL;
141
142 ajStrDel(&line);
143
144 return ajTrue;
145 }
146
147
148
149
150 /* @funclist gPlotData ********************************************************
151 **
152 ** Function to plot from given data
153 **
154 ******************************************************************************/
155
156 AjBool gPlotData(AjPGraph graphs, gPlotParams *gpp)
157 {
158 AjPGraphdata gd = NULL;
159
160 float min = 0.0;
161 float max = 0.0;
162 float dif = 0.0;
163 ajint i;
164 ajint j;
165
166 ajint setNum = (*gpp).setNum;
167 ajint dataNum = (*gpp).dataNum;
168 ajint typeNum = (*gpp).typeNum;
169 AjPStr title = (*gpp).title;
170 AjPStr xlab = (*gpp).xlab;
171 AjPStr ylab = (*gpp).ylab;
172 AjPPStr names = (*gpp).names;
173 float **data = (*gpp).data;
174
175 float x[dataNum];
176 float y[dataNum];
177 float begin = data[0][0];
178 float end = data[0][dataNum-1];
179 float range = end - begin;
180
181 int c[] = {1,3,9,13};
182
183 for(i = 1; i < typeNum; ++i)
184 {
185 for(j = 0; j < dataNum; ++j)
186 {
187 min = (min < data[i][j]) ? min : data[i][j];
188 max = (max > data[i][j]) ? max : data[i][j];
189 }
190 }
191
192 dif = (min == max) ? 20 : max - min;
193 max += dif / 20;
194 min -= dif / 20;
195
196 for(i = 1; i < typeNum; ++i)
197 {
198 gd = ajGraphdataNewI(dataNum);
199
200 ajGraphdataSetColour(gd, c[i-1]);
201 ajGraphdataSetMinmax(gd, begin, end, min, max);
202 ajGraphdataSetTruescale(gd, begin, end, min, max);
203 ajGraphdataSetTypeC(gd, "Multi 2D Plot");
204
205 for(j = 0; j < dataNum; ++j)
206 {
207 x[j] = data[0][j];
208 y[j] = data[i][j];
209 }
210
211 ajGraphdataAddXY(gd, x, y);
212 ajGraphDataAdd(graphs, gd);
213
214 if(typeNum > 2)
215 {
216 float len = 0.0;
217
218 for(j = 1; j < typeNum; ++j)
219 len = (len < (float)ajStrGetLen(names[j])) ?
220 (float)ajStrGetLen(names[j]) : len;
221
222 ajGraphAddLine(graphs,
223 range * 7.4/8 + begin, dif * (8.6-i*0.3)/8 + min,
224 range * 7.8/8 + begin, dif * (8.6-i*0.3)/8 + min,
225 c[i-1]);
226 ajGraphAddTextScaleS(graphs,
227 range * (7.3/8 - len*1/144) + begin,
228 dif * (8.6-i*0.3)/8 + min,
229 0, 0.4,
230 names[i]);
231 }
232
233 gd = NULL;
234 }
235
236 ajGraphxySetXstartF(graphs, begin);
237 ajGraphxySetXendF(graphs, end);
238 ajGraphxySetYstartF(graphs, min - ((max - min) / 10));
239 ajGraphxySetYendF(graphs, max + ((max - min) / 10));
240
241 //ajGraphSetTitleS(graphs, title);
242 ajGraphSetXlabelS(graphs, xlab);
243 ajGraphSetYlabelS(graphs, ylab);
244 ajGraphxySetflagOverlay(graphs, ajTrue);
245
246 ajGraphxyDisplay(graphs, AJFALSE);
247 ajGraphicsClose();
248
249 return ajTrue;
250 }
251
252
253
254
255 /* @funclist gPlotFlip ********************************************************
256 **
257 ** Function to flip x and y data
258 **
259 ******************************************************************************/
260
261 AjBool gPlotFlip(gPlotParams *gpp)
262 {
263 ajint setNum = (*gpp).setNum;
264 ajint dataNum = (*gpp).typeNum;
265 ajint typeNum = (*gpp).dataNum;
266 float **data = (*gpp).data;
267
268 float **newdata;
269 ajint i;
270 ajint j;
271
272 if((newdata = (float**)malloc(sizeof(float*) * typeNum)) == NULL)
273 return ajFalse;
274 else
275 for(i = 0; i < typeNum + 1; ++i)
276 if((newdata[i] = (float*)malloc(sizeof(float))) == NULL)
277 {
278 AJFREE(newdata);
279 return ajFalse;
280 }
281
282 for(i = 0; i < dataNum; ++i){
283 if((newdata[0] = (float*)realloc(newdata[0],
284 sizeof(float) * (i + 1))) == NULL)
285 {
286 for(j = 0; j < i; ++j)
287 AJFREE(newdata[j]);
288 AJFREE(newdata);
289 return ajFalse;
290 }
291 if((newdata[1] = (float*)realloc(newdata[1],
292 sizeof(float) * (i + 1))) == NULL)
293 {
294 for(j = 0; j < i; ++j)
295 AJFREE(newdata[j]);
296 AJFREE(newdata);
297 return ajFalse;
298 }
299 newdata[0][i] = i;
300 newdata[1][i] = data[i][0];
301 }
302
303 for(i = 0; i < dataNum; ++i)
304 {
305 AJFREE((*gpp).data[i]);
306 }
307 AJFREE((*gpp).data);
308
309 (*gpp).dataNum = dataNum;
310 (*gpp).typeNum = 2;
311 (*gpp).data = newdata;
312
313 return ajTrue;
314 }