source: Sophya/trunk/SophyaPI/PI/piaxes.cc@ 2098

Last change on this file since 2098 was 2098, checked in by ansari, 23 years ago

Erreur compil/declaration i - Reza 16/7/2002

File size: 20.3 KB
Line 
1// Module PI : Peida Interactive PIAxes
2// Methodes de trace des axes
3// R. Ansari - 2002
4// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
5
6#include "machdefs.h"
7#include <stdio.h>
8#include <strings.h>
9#include <math.h>
10#include "piaxes.h"
11
12#define PERC_GARDE 0.02
13
14inline void dble_SWAP(double& a,double& b) {double tmp=a; a=b; b=tmp;}
15
16/* --Methode-- */
17PIAxes::PIAxes()
18{
19}
20
21/* --Methode-- */
22PIAxes::~PIAxes()
23{
24}
25
26/* --Methode-- */
27void PIAxes::DrawXYAxes(PIGraphicUC* g, PIGraphicAtt& gratt,
28 unsigned int flags, bool afsz)
29{
30 PIGrCoord xmin, xmax, ymin, ymax;
31 g->GetGrSpace(xmin, xmax, ymin, ymax);
32 DrawXYAxes(g, gratt, flags, afsz, (double)xmin, (double)xmax,
33 (double)ymin, (double)ymax);
34}
35
36/* --Methode-- */
37void PIAxes::DrawXYAxes(PIGraphicUC* g, PIGraphicAtt& gratt,
38 unsigned int flags, bool afsz,
39 double xmin, double xmax, double ymin, double ymax)
40{
41 Setup(g, xmin, xmax, ymin, ymax);
42 // g->NoClip();
43
44 if (gratt.GetLineAtt() == PI_NotDefLineAtt) g->SelLine(PI_ThinLine);
45 else g->SelLine(gratt.GetLineAtt());
46 if (gratt.GetColor() == PI_NotDefColor) g->SelForeground(PI_Black);
47 else g->SelForeground(gratt.GetColor());
48
49 if(afsz) {
50 double fsz = xMajTickLen*3.5;
51 g->SelFontSz(fsz);
52 }
53
54 if (flags & kStdAxes) {
55
56 // Les axes
57
58 g->DrawLine(xMin, (yMin+yMax)/2., xMax, (yMin+yMax)/2.);
59 g->DrawLine((xMin+xMax)/2., yMin, (xMin+xMax)/2., yMax);
60
61 // La grille en pointilles
62
63 if (flags & kGridOn) DrawGrid(g);
64
65 // Les ticks majeurs
66
67 if (flags & kMajTicks) {
68 DrawHTicks(g, (yMin+yMax)/2., xMajTickLen, xMajTickLen, xMajTicks);
69 DrawVTicks(g, (xMin+xMax)/2., yMajTickLen, yMajTickLen, yMajTicks);
70 }
71
72 // Les ticks mineurs
73
74 if (flags & kMinTicks) {
75 DrawHTicks(g, (yMin+yMax)/2., xMinTickLen, xMinTickLen, xMinTicks);
76 DrawVTicks(g, (xMin+xMax)/2., yMinTickLen, yMinTickLen, yMinTicks);
77 }
78
79 // Les labels
80
81 if (flags & kLabels) {
82 if (!aYdir)
83 DrawHLabels(g, (yMin+yMax)/2.-xMajTickLen*2, xMajTicks, PI_VerticalTop);
84 else
85 DrawHLabels(g, (yMin+yMax)/2.+xMajTickLen*2, xMajTicks, PI_VerticalTop);
86 if (!aXdir)
87 DrawVLabels(g, (xMin+xMax)/2.-yMajTickLen*2, yMajTicks, PI_HorizontalRight);
88 else
89 DrawVLabels(g, (xMin+xMax)/2.+yMajTickLen*2, yMajTicks, PI_HorizontalRight);
90 }
91
92 }
93
94 if (flags & kBoxAxes) {
95
96 // La boite
97
98 g->DrawLine(xMin, yMin, xMax, yMin);
99 g->DrawLine(xMax, yMin, xMax, yMax);
100 g->DrawLine(xMax, yMax, xMin, yMax);
101 g->DrawLine(xMin, yMax, xMin, yMin);
102
103 // Longueur des ticks
104
105 double extXMajTickLen = flags&kExtTicks ? xMajTickLen : 0;
106 double intXMajTickLen = flags&kIntTicks ? xMajTickLen : 0;
107 double extXMinTickLen = flags&kExtTicks ? xMinTickLen : 0;
108 double intXMinTickLen = flags&kIntTicks ? xMinTickLen : 0;
109 double extYMajTickLen = flags&kExtTicks ? yMajTickLen : 0;
110 double intYMajTickLen = flags&kIntTicks ? yMajTickLen : 0;
111 double extYMinTickLen = flags&kExtTicks ? yMinTickLen : 0;
112 double intYMinTickLen = flags&kIntTicks ? yMinTickLen : 0;
113
114 // La grille en pointilles
115
116 if (flags & kGridOn) DrawGrid(g);
117
118 // Les ticks majeurs
119
120 if (flags & kMajTicks) {
121 DrawHTicks(g, yMin, intXMajTickLen, extXMajTickLen, xMajTicks);
122 DrawHTicks(g, yMax, extXMajTickLen, intXMajTickLen, xMajTicks);
123 DrawVTicks(g, xMin, extYMajTickLen, intYMajTickLen, yMajTicks);
124 DrawVTicks(g, xMax, intYMajTickLen, extYMajTickLen, yMajTicks);
125 }
126
127 // Les ticks mineurs
128
129 if (flags & kMinTicks) {
130 DrawHTicks(g, yMin, intXMinTickLen, extXMinTickLen, xMinTicks);
131 DrawHTicks(g, yMax, extXMinTickLen, intXMinTickLen, xMinTicks);
132 DrawVTicks(g, xMin, extYMinTickLen, intYMinTickLen, yMinTicks);
133 DrawVTicks(g, xMax, intYMinTickLen, extYMinTickLen, yMinTicks);
134 }
135
136
137 // Les labels
138
139 if (flags & kLabels) {
140 if (!aYdir) {
141 DrawHLabels(g, g->DeltaUCY(yMin, -xMajTickLen*2), xMajTicks, PI_VerticalTop);
142 }
143 else {
144 DrawHLabels(g, g->DeltaUCY(yMax, xMajTickLen*2), xMajTicks, PI_VerticalTop);
145 }
146 if (!aXdir) {
147 DrawVLabels(g, g->DeltaUCX(xMin, -yMajTickLen*2), yMajTicks, PI_HorizontalRight);
148 }
149 else {
150 DrawVLabels(g, g->DeltaUCX(xMax, yMajTickLen*2), yMajTicks, PI_HorizontalRight);
151 }
152 }
153 }
154 g->Clip();
155
156}
157
158/* --Methode-- */
159void PIAxes::Setup(PIGraphicUC* g, double xmin, double xmax,
160 double ymin, double ymax)
161{
162
163 xMin = xmin; xMax = xmax;
164 yMin = ymin; yMax = ymax;
165
166 aXdir = g->isAxeXDirRtoL();
167 aYdir = g->isAxeYDirUpDown();
168 aXlog = g->isLogScaleX();
169 aYlog = g->isLogScaleY();
170
171 int ntick_x = (aXlog) ? 10 : 10;
172 if(aXlog) BestTicksLog(xMin,xMax,ntick_x,xMajTicks,xMinTicks);
173 else BestTicks(xMin,xMax,ntick_x,xMajTicks,xMinTicks);
174
175 int ntick_y = (aYlog) ? 12 : 12;
176 if(aYlog) BestTicksLog(yMin,yMax,ntick_y,yMajTicks,yMinTicks);
177 else BestTicks(yMin,yMax,ntick_y,yMajTicks,yMinTicks);
178
179 yMajTickLen = (xMax-xMin)/100;
180 yMinTickLen = (xMax-xMin)/250;
181 xMajTickLen = (yMax-yMin)/100;
182 xMinTickLen = (yMax-yMin)/250;
183}
184
185
186/* --Methode-- */
187void PIAxes::DrawHTicks(PIGraphicUC* g, double y, double tickUp,
188 double tickDown, vector<double>& xticks)
189{
190 if(xticks.size()==0) return;
191 for(unsigned int i=0;i<xticks.size();i++) {
192 if(xticks[i]<xMin) continue;
193 if(xticks[i]>xMax) break;
194 g->DrawLine(xticks[i],g->DeltaUCY(y,-tickDown),xticks[i],g->DeltaUCY(y,tickUp));
195 }
196}
197
198/* --Methode-- */
199void PIAxes::DrawVTicks(PIGraphicUC* g, double x, double tickLeft,
200 double tickRight, vector<double>& yticks)
201{
202 if(yticks.size()==0) return;
203 for(unsigned int i=0;i<yticks.size();i++) {
204 if(yticks[i]<yMin) continue;
205 if(yticks[i]>yMax) break;
206 g->DrawLine(g->DeltaUCX(x,-tickLeft),yticks[i],g->DeltaUCX(x,tickRight),yticks[i]);
207 }
208}
209
210/* --Methode-- */
211void PIAxes::DrawHLabels(PIGraphicUC* g, double y, vector<double>& xticks, unsigned long just)
212{
213 if(xticks.size()==0) return;
214
215 // Choix du bon format pour les labels des axes
216 string format; double xstep;
217 int npuiss = Le_Bon_Format(xticks,format,xstep);
218 double fac=(npuiss!=0)? fac=pow(10.,(double)npuiss): 1.;
219
220 char label[64];
221 double dum,xpixdeb,xpixfin,largpix;
222 g->UC2GrC(xMin-2.*(xMax-xMin),y,xpixfin,dum);
223 for(unsigned int i=0;i<xticks.size();i++) {
224 if(xticks[i]<xMin) continue;
225 if(xticks[i]>xMax) break;
226 //Attention erreur d'arrondi x->0 (on code 5.1698e-26 au lieu de 0)
227 double xx = (fabs(xticks[i]/xstep)<1.e-5) ? 0.: xticks[i];
228 sprintf(label,format.c_str(),xx/fac); Arrange_Label(label);
229 double largeur = g->CalcStringWidth(label);
230 g->DUC2GrC(largeur,0.,largpix,dum);
231 g->UC2GrC(xticks[i],y,xpixdeb,dum); xpixdeb -= largpix/2.;
232 //cout<<"xticks="<<xticks[i]<<" largpix="<<largpix
233 // <<" xpixdeb="<<xpixdeb<<" xpixfin="<<xpixfin<<endl;
234 if((aXdir && xpixdeb<xpixfin) || (!aXdir && xpixdeb>xpixfin)) {
235 g->DrawString(xticks[i],y,label,PI_HorizontalCenter|just);
236 xpixfin = xpixdeb + 1.1*largpix;
237 }
238 }
239
240 if(npuiss!=0) {
241 PIGrCoord asc,desc;
242 double h = g->GetFontHeight(asc,desc);
243 if((aYdir && (just&PI_VerticalBottom)) || (!aYdir && (just&PI_VerticalTop))) h=-h;
244 double xm = (aXdir)? xMin: xMax;
245 double ym = g->DeltaUCY(y,1.5*h);
246 sprintf(label,"%d",npuiss);
247 g->DrawCompString(xm,ym,"x 10",label,NULL,PI_HorizontalCenter|just);
248 }
249
250}
251
252/* --Methode-- */
253void PIAxes::DrawVLabels(PIGraphicUC* g, double x, vector<double>& yticks, unsigned long just)
254{
255 if(yticks.size()==0) return;
256
257 // Choix du bon format pour les labels des axes;
258 string format; double ystep;
259 int npuiss = Le_Bon_Format(yticks,format,ystep);
260 double fac=(npuiss!=0)? fac=pow(10.,(double)npuiss): 1.;
261
262 char label[64];
263 PIGrCoord asc,desc;
264 double dum,ypixdeb,ypixfin,hautpix,hauteur=g->GetFontHeight(asc,desc);
265 g->DUC2GrC(0.,hauteur,dum,hautpix);
266 g->UC2GrC(x,yMin-2.*(yMax-yMin),dum,ypixfin);
267 for(unsigned int i=0;i<yticks.size();i++) {
268 if(yticks[i]<yMin) continue;
269 if(yticks[i]>yMax) break;
270 double yy = (fabs(yticks[i]/ystep)<1.e-5) ? 0.: yticks[i];
271 sprintf(label,format.c_str(),yy/fac); Arrange_Label(label);
272 g->UC2GrC(x,yticks[i],dum,ypixdeb); ypixdeb -= hautpix/2.;
273 // -- Attention: ypix=0 est en haut de l'ecran
274 // (ypix croissants vers le bas de l'ecran)
275 // donc bien que yMin<yMax on a yMinPix>yMaxPix
276 //cout<<"yticks="<<yticks[i]<<" hautpix="<<hautpix
277 // <<" ypixdeb="<<ypixdeb<<" ypixfin="<<ypixfin<<endl;
278 if((aYdir && ypixdeb>ypixfin) || (!aYdir && ypixdeb<ypixfin)) {
279 g->DrawString(x,yticks[i],label,PI_VerticalCenter|just);
280 ypixfin = ypixdeb + 1.1*hautpix;
281 }
282 }
283
284 if(npuiss!=0) {
285 if(aYdir) hauteur = -hauteur;
286 double ym = (aYdir)? yMin: yMax; ym = g->DeltaUCY(ym,hauteur);
287 sprintf(label,"%d",npuiss);
288 g->DrawCompString(x,ym,"x 10",label,NULL,PI_VerticalBottom|just);
289 }
290
291}
292
293/* --Methode-- */
294void PIAxes::DrawGrid(PIGraphicUC* g)
295{
296 PILineAtt savlineatt = g->GetLineAtt();
297 g->SelLine(PI_ThinDottedLine);
298
299 if(xMajTicks.size()>0)
300 for(unsigned int i=0;i<xMajTicks.size();i++) {
301 if(xMajTicks[i]>xMax) break;
302 g->DrawLine(xMajTicks[i], yMin, xMajTicks[i], yMax);
303 }
304
305 if(yMajTicks.size()>0)
306 for(unsigned int i=0;i<yMajTicks.size();i++) {
307 if(yMajTicks[i]>yMax) break;
308 g->DrawLine(xMin, yMajTicks[i], xMax, yMajTicks[i]);
309 }
310
311 g->SelLine(savlineatt);
312}
313
314////////////////////////////////////////////////////////////////////////
315//////////////////// METHODES STATIQUES ////////////////////////////////
316////////////////////////////////////////////////////////////////////////
317/* --Methode-Static-- */
318void PIAxes::BestTicks(double xmin,double xmax,int nticks
319 ,vector<double>& majticks,vector<double>& minticks)
320// *** Calcul de l'intervalle entre les ticks et de la valeur du premier tick
321// pour un axe lineaire
322{
323 if(nticks<=0) nticks = 1;
324
325 double d=xmax-xmin; if(d<1.e-100) d=1.e-100;
326 double ld = log10(d);
327 double fld = floor( ((ld<0.)? -ld: ld) );
328 double del,del0;
329 fld = (ld>=0.)? fld-2.: -(fld+2.);
330 del0 = del = pow(10.,fld);
331 // *** Intervalle entre les ticks
332 // xmin xmax d ld fld -->fld del0
333 // 1 1500 1499 3.17 3 1 10^1
334 // 1 9500 9499 3.98 3 1 10^1
335 // 1 1.005 0.005 -2.3 3 -5 10^-5
336 // 1 1.995 0.995 -0.0022 1 -3 10^-3
337 // - Et recherche de la valeur del={del0,2*del0,...,20*del0,...}
338 // telle que "nticks*del" soit le plus petit nombre ">=xmax-xmin"
339 double fac[9] = {2.,5.,10.,20.,50.,100.,200.,500.,1000.};
340 for(int k=0;k<9;k++) {
341 //cout<<"BestTicks: "<<k<<" del="<<del<<" d/del="<<d/del<<"<"<<nticks<<endl;
342 if(d/del < (double)nticks) break;
343 del=fac[k]*del0;
344 }
345 double steptick=del;
346 //*** Valeur du premier tick
347 majticks.resize(0);
348 double xfirsttick = floor(fabs(xmin)/steptick)*steptick;
349 if(xmin<0.) xfirsttick *= -1.;
350 if(xfirsttick<xmin) xfirsttick += steptick;
351 while(xfirsttick<=xmax+steptick/10.)
352 {majticks.push_back(xfirsttick); xfirsttick+= steptick;}
353 //*** Gestion des ticks mineurs
354 minticks.resize(0);
355 if(majticks.size()>1) {
356 double steptickmin = steptick/5.;
357 double xfirsttickmin = majticks[0];
358 while(xfirsttickmin<=xmax+steptickmin/10.)
359 {minticks.push_back(xfirsttickmin); xfirsttickmin+= steptickmin;}
360 }
361}
362/* --Methode-Static-- */
363void PIAxes::BestTicksLog(double xmin,double xmax,int nticks
364 ,vector<double>& majticks,vector<double>& minticks)
365// *** Calcul des ticks pour un axe logarithmique
366{
367 if(nticks<=0) nticks = 1;
368 //cout<<"BestTicksLog: xmin="<<xmin<<" xmax="<<xmax<<" nticks="<<nticks<<endl;
369
370 // CMV_BUG: quand on fait func x 1 100 ..., Reza renvoit xmin=-0.9602 !!!
371 // Ceci est un bricolo qui permet d'obtenir un resultat moyen.
372 // A CORRIGER: pour Reza, ne pas surdimensionner la fenetre
373 // vers les xmin,ymin SI on demande une echelle log
374 // pour etre sur que xmin,ymin sera bien ce qui est demande.
375 if(xmin<=0.) {
376 cout<<"Error_BestTicksLog: xmin="<<xmin;
377 // On suppose que: xmin = xmin0 - PERC_GARDE*(xmax0-xmin0)
378 xmin = fabs((xmin+PERC_GARDE*xmax)/(1.+PERC_GARDE));
379 cout<<" corrected to xmin="<<xmin<<endl
380 <<" ===> First labels may be WRONG"<<endl;
381 }
382
383 // Si xmax<=0 ou si dynamique trop faible on garde BestTicks
384 if(xmax<=0. || xmax/xmin<5.) {
385 //cout<<"Choix de BestTicks xmax="<<xmax<<" xmax/xmin="<<xmax/xmin<<" <5"<<endl;;
386 BestTicks(xmin,xmax,nticks,majticks,minticks);
387 return;
388 }
389
390 int dmin=int(floor(log10(xmin)));
391 int dmax=int(floor(log10(xmax)));
392 if(dmax==dmin) dmax++; else if(dmax<dmin) dmax=dmin+1;
393 int inc = (dmax-dmin+1)/nticks; if(inc<1) inc=1;
394 //cout<<" dmin="<<dmin<<" dmax="<<dmax<<" inc="<<inc<<endl;
395
396 majticks.resize(0);
397 for(int i=dmin;i<=dmax;i+=inc) {
398 double x = pow(10.,(double)i);
399 if(x<xmin || x>xmax) continue;
400 majticks.push_back(x);
401 }
402 //cout<<"majticks.size()="<<majticks.size()<<endl;
403
404 // Pas de puissance de 10 dans l'intervalle
405 if(majticks.size()==0) {
406 BestTicks(xmin,xmax,nticks,majticks,minticks);
407 return;
408 }
409
410 unsigned int i;
411 // Pas suffisamment de ticks majeurs
412 if((int)majticks.size()<=nticks/2) {
413 int nins = nticks/(majticks.size()+1);
414 if(nins<=0) nins=1;
415 //cout<<"nins="<<nins<<endl;
416 // Sequence judicieuse pour remplir les ticks manquants
417 // nins = 1 on insere 3
418 // 2 2 5
419 // 3 2 4 6
420 // 4 1.5 2 4 6
421 // >=5 on reste au cas precedent
422 double seqmaj[4][4] = {{3.,0,0,0},{2.,5.,0,0},{2.,4.,6.,0},{1.5,2.,4.,6.}};
423 if(nins>4) nins=4;
424 vector<double> tmp;
425 for(i=0;i<=majticks.size();i++) {
426 double xt;
427 if(i<majticks.size()) xt = majticks[i]/10.;
428 else xt = majticks[i-1];
429 for(int n=0;n<nins;n++) {
430 double xins = seqmaj[nins-1][n]*xt;
431 if(xins<xmin || xins>xmax) continue;
432 tmp.push_back(xins);
433 }
434 if(i<majticks.size()) tmp.push_back(majticks[i]);
435 }
436 majticks = tmp;
437 }
438 //cout<<"...majticks.size()="<<majticks.size()<<endl;
439
440 // Les ticks mimneurs
441 minticks.resize(0);
442 for(i=0;i<majticks.size()-1;i++) {
443 double dx = (majticks[i+1]-majticks[i])/10.;
444 minticks.push_back(majticks[i]);
445 for(int j=2;j<=8;j+=2) {
446 double x = majticks[i] + j*dx;
447 if(x<xmin || x>xmax) continue;
448 minticks.push_back(x);
449 }
450 }
451 minticks.push_back(majticks[majticks.size()-1]);
452 //cout<<"...minticks.size()="<<minticks.size()<<endl;
453
454}
455
456/* --Methode-Static-- */
457int PIAxes::BonFormatAxes(double xmin,double xmax,double xstep
458 ,string& format,int typf,int add_digit)
459// *** Calcul format optimal pour ecrire les labels numeriques des axes:
460// ---- Input
461// . xmin,xmax : limites du plot sur l'axe considere.
462// . xstep : distance entre les ticks.
463// . add_digit : nombre de digits a ajouter au nombre de digits minimum.
464// . typf : type de format en sortie
465// 0 : format optimum %-nn.mme ou %-nn.mmf selon valeurs
466// 1 : format %-nn.mme
467// 2 : format %-nn.mmf pour imprimer x/10^npuiss
468// tel que x/10^npuiss soit entre 0 et 10
469// ---- Output
470// . format : le format d'impression
471// ---- Return:
472// Si typ=0 ou 1
473// "ndig" : nombre de digits necessaires pour distinguer
474// les valeurs xmin+k*dx (<=xmax)
475// Si typ=2
476// "npuiss" : tel que x/10^npuiss soit entre 0 et 10
477// Dans ce cas le format est celui qui imprime x/10^npuiss
478{
479 format = "%-5g"; // format par default
480 if(xmin>=xmax) {if(typf==2) return 0; else return -1;}
481
482 if(xstep<=0. || xstep>xmax-xmin) xstep = xmax-xmin;
483
484 double axmin=fabs(xmin), axmax=fabs(xmax);
485 if(axmin>axmax) dble_SWAP(axmin,axmax);
486
487 double l10amax = log10(axmax), l10xstep = log10(xstep);
488 int il10amax = int(floor(l10amax));
489
490 // choix du type de format
491 char ftype = 'e';
492 int npuiss = 0;
493 if(typf==2) {
494 npuiss = il10amax;
495 if(npuiss<-300 || npuiss>300) {
496 ftype='e'; npuiss=0;
497 } else {
498 // On recalcule les valeurs de decision pour axmax/10^npuiss, xstep/10^npuiss
499 l10amax -= (double)npuiss; l10xstep -= (double)npuiss;
500 il10amax = int(floor(l10amax));
501 ftype = 'f';
502 }
503 } else if(typf==1) {
504 ftype='e';
505 } else {
506 ftype='e';
507 // On evite d'ecrire +a.bbbe+ccc -> format %f
508 // Ex: 1.2345e+2 -> 123.45 / -1.2345e+2 -> -123.45
509 // 1.2345e-1 -> 0.12345 / -1.2345e-1 -> -0.12345
510 if((axmin>=1e-4 || axmin==0.) && axmax<1e4) ftype='f';
511 }
512
513 //printf("BonFormatAxes[npuiss=%d]: xmin=%-21.14e xmax=%-21.14e\n",npuiss,xmin,xmax);
514 //printf(" xstep=%-21.14e log10(xstep/10^%d)=%g\n",xstep,npuiss,l10xstep);
515 //printf(" axmax=%-21.14e log10(axmax/10^%d)=%g diff=%g\n"
516 // ,axmax,npuiss,l10amax,l10amax-l10xstep);
517
518 // Nombre de digits necessaires pour ecrire axmax et xstep
519 int ndig = il10amax - int(floor(l10xstep));
520 if(ndig<0) ndig *= -1; ndig += 1;
521 //printf("ndig=%d",ndig);
522
523 // Add more digits (or suppress digits)
524 ndig += add_digit; if(ndig<0) ndig=0;
525 //printf(" + %d ==> ndig=%d\n",add_digit,ndig);
526
527 // Calcul du bon format
528 char str[16];
529 if(ftype=='f') { // Calcul du format %-nn.mmf
530 int mm=-1, nn;
531 if(il10amax<0) { // +0.12345 +0.0012345 +0.0012345 ...
532 mm = ndig - il10amax - 1; nn = mm+3;
533 } else { // +1.2345 +12.345 +123.45 ...
534 mm = ndig - il10amax - 1; nn = ndig+2;
535 }
536 //printf("format %%f : mm=%d nn=%d\n",mm,nn);
537 if(mm<0.) mm=0; if(nn<mm+3) nn=mm+3;
538 sprintf(str,"%%-%d.%df",nn,mm);
539 } else if(ftype=='e') { // Calcul du format %-nn.mme
540 // +d.<--ddd-->e+123
541 // 1 2 34567 ==> nn=ndig+7 mm=ndig-1
542 sprintf(str,"%%-%d.%de",ndig+7,ndig-1);
543 }
544
545 format = str;
546 //printf("format=[%s]\n",format.c_str());
547
548 if(typf==2) return npuiss;
549 return ndig;
550}
551
552/* --Methode-- */
553int PIAxes::Le_Bon_Format(vector<double>& xticks,string& format,double& xstep)
554// Methode static de decision du bon format
555// Decide quel format est le mieux adapte pour ecrire les labels des axes
556// Decide si une puissance de 10 doit etre deportee en bout d'axe
557// - Input:
558// xticks : vecteur des ticks a ecrire (calcule par BestTicks)
559// - Output:
560// format : format a utiliser
561// xstep : step entre 2 graduations
562// - Return:
563// npuiss : si format choisit avec ecriture
564// avec label des puissances de 10 deporte
565// 0 : sinon
566{
567 format="%g"; xstep=1.;
568 if(xticks.size()<=1) return 0;
569
570 xstep=xticks[1]-xticks[0];
571 int npuiss = BonFormatAxes(xticks[0],xticks[xticks.size()-1],xstep,format,2,1);
572 if(npuiss>=-2 && npuiss<=3) {
573 npuiss = 0;
574 BonFormatAxes(xticks[0],xticks[xticks.size()-1],xstep,format,0,1);
575 }
576 return npuiss;
577}
578
579void PIAxes::Arrange_Label(char *label)
580// --- Mise en forme optimale du label numerique
581// Enleve les blancs et les zeros inutiles a la fin d'un label
582{
583 size_t lenl=strlen(label);
584 if(lenl==0) return;
585
586 // --- On enleve les blancs et plus au debut du label
587 if(label[0]==' ' || label[0]=='+') {
588 char *str=new char[lenl+2];
589 strcpy(str,label);
590 unsigned i=0;
591 for(i=0;i<lenl;i++) if(str[i]!=' ' && str[i]!='+') break;
592 strcpy(label,&str[i]);
593 delete [] str;
594 lenl=strlen(label);
595 if(lenl==0) return;
596 }
597
598 // --- On enleve les blancs a la fin du label
599 if(label[lenl-1]==' ') {
600 for(int i=lenl-1;i>=0;i--) {
601 if(label[i]!=' ') break;
602 label[i]='\0';
603 }
604 lenl=strlen(label);
605 }
606
607 // --- On enleve les e... E... non-significatifs
608 // ex: a.be-zzz a.be+zzz a.bezzz avec zzz=0,00,000,...
609 // Attention on n'enleve pas si: a.be+10
610 // Attention on ne fait rien si: e+10
611 char* e=index(label,'e');
612 if(e==NULL) e=index(label,'E');
613 if(e) {
614 for(int i=lenl-1;i>=0;i--) {
615 if(isdigit(label[i]) && label[i]!='0') break;
616 if(label[i]=='e' || label[i]=='E')
617 {label[i]='\0'; lenl=strlen(label); break;}
618 }
619 }
620
621 // --- On enleve les zeros non-significatifs a la fin du label
622 // On enleve des zeros ou le point si: ab. ab.czzz avec zzz=0,00,000,...
623 // Attention on n'enleve pas de zeros si: abzzz
624 // Attention a ne pas enlever des zeros si on a ab.ccce+a0
625 // Attention on traite 0eaaa -> 0
626 if(index(label,'.')==NULL) return; // Recherche d'un point
627 string stre;
628 if(e) {if(e==label) return; stre=e; *e='\0'; lenl=strlen(label);}
629 for(int i=lenl-1;i>=0;i--) {
630 if(label[i]=='0') label[i]='\0';
631 else if(label[i]=='.') {
632 if(i>0) label[i]='\0';
633 else {
634 // Attention: ".e+10" -> "1e+10" MAIS "." -> "0"
635 if(e) strcpy(label,"1"); else strcpy(label,"0");
636 }
637 break;
638 }
639 else break; // Ni un point ni un zero
640 }
641 if(e) {
642 if(strlen(label)==1) if(label[0]=='0') return;
643 strcat(label,stre.c_str());
644 }
645
646}
Note: See TracBrowser for help on using the repository browser.