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

Last change on this file since 2092 was 2092, checked in by cmv, 23 years ago

intro PERC_GARDE pour localiser les pb cmv 14/7/2002

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