summaryrefslogtreecommitdiff
path: root/texmf-dist/tex/latex/bargraph-js/bargraph-js.sty
blob: 4207cd2e2cc828b5b77b953380cbef0e1247b88c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
%%
%% This is file `bargraph-js.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% bargraph-js.dtx  (with options: `copyright,package')
%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% bargraph-js.sty package,                              %%
%% Copyright (C) 2019                                    %%
%%   dpstory@uakron.edu                                  %%
%%                                                       %%
%% This program can redistributed and/or modified under  %%
%% the terms of the LaTeX Project Public License         %%
%% Distributed from CTAN archives in directory           %%
%% macros/latex/base/lppl.txt; either version 1.2 of the %%
%% License, or (at your option) any later version.       %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\NeedsTeXFormat{LaTeX2e}[1997/12/01]
\ProvidesPackage{bargraph-js}
 [2019/04/07 v0.8 bargraph-js: Create bar graphs using form fields and JavaScript]
\RequirePackage{xkeyval}
\DeclareOptionX{dynamic}{\def\bgjs@importDynamic
  {\InputIfFileExists{dynam-js.def}{}{\PackageWarning{bargraph-js}
    {The file dynam-js.def cannot be found}}}}
\let\bgjs@importDynamic\relax
\ProcessOptionsX\relax
\AtEndOfPackage{\bgjs@importDynamic}
\RequirePackage{xcolor}
\RequirePackage{eforms}[2019/05/24]
\edef\bgjs@restoreCats{\catcode`\noexpand\"=\the\catcode`\"\relax}
\@makeother\"
\def\scaleFactorDef#1{\def\sc@leF@ctorDef{#1}}
\scaleFactorDef{2}
\define@key{bgrphenv}{width}[\linewidth]{\setlength{\@tempdima}{#1}%
  \ifxetex\else\addtolength{\@tempdima}{2bp}\fi
  \edef\bgrphenv@width{\the\@tempdima}\@tempdima=.99626\@tempdima
  \edef\bgrphenv@widthbp{\strip@pt\@tempdima}}
\define@key{bgrphenv}{height}[2in]{\setlength{\@tempdima}{#1}%
  \edef\bgrphenv@height{\the\@tempdima}\@tempdima=.99626\@tempdima
  \edef\bgrphenv@heightbp{\strip@pt\@tempdima}}
\newif\ifhorizontalbars \horizontalbarsfalse
\newcount\cntbars \cntbars=0
\newtoks\bgtoks
\define@choicekey{bgrphenv}{o}[\val\nr]{horiz,vert}{%
  \ifcase\nr\relax
    \horizontalbarstrue\or
    \horizontalbarsfalse\else
    \horizontalbarsfalse\fi}
\define@choicekey+{bgrphenv}{origin}{0,.5}[0]{\def\bgjs@origin{#1}}
  {\PackageWarning{bargraph-js}{Bad choice for origin,
  permissible values are 0 and .5.\MessageBreak
  Will use a value of 0. Try again}}
\def\bgjs@origin{0}
\newif\if@bgshowaxis \@bgshowaxisfalse % dps23 \catcode`\%=14\relax
\define@boolkey{bgrphenv}[@bg]{showaxis}[true]{}
\begin{defineJS}[\catcode`\!=0\relax]{\hided@t@Fmt}
if (typeof dataForEnv=="undefined")
  var dataForEnv=new Object;
dataForEnv["!p(1)"]=new Object;
dataForEnv["!p(1)"].width=!bgrphenv@widthbp;
dataForEnv["!p(1)"].height=!bgrphenv@heightbp;
dataForEnv["!p(1)"].horiz=!ifhorizontalbars(true)!else(false)!fi;
dataForEnv["!p(1)"].sf=!sc@leF@ctorDef;
dataForEnv["!p(1)"].bgs=[!bg@list];
dataForEnv["!p(1)"].origin=!bgjs@origin;
var _nO=!bgjs@origin;
var r=event.target.rect;
if (dataForEnv["!p(1)"].horiz){
  var _w1=_nO*dataForEnv["!p(1)"].width;
  dataForEnv["!p(1)"].maxDim=(_nO==0)?!%
!bgrphenv@widthbp:Math.min(_w1,!bgrphenv@widthbp-_w1);
  dataForEnv["!p(1)"].baseline=r[0]+_w1;
} else {
  var _h1=_nO*dataForEnv["!p(1)"].height;
  dataForEnv["!p(1)"].maxDim=(_nO==0)?!%
!bgrphenv@heightbp:Math.min(_h1,!bgrphenv@heightbp-_h1);
  dataForEnv["!p(1)"].baseline=r[3]+_h1;
}
dataForEnv["!p(1)"].values=new Object;
\end{defineJS}
\def\internalD@t@Hidden#1{%
  \llap{\textField[\cmd{\bParams{#1}\eParams\let\%\defjsLB}
  \autoCenter{n}\BC{}\BG{}\S{S}\textSize{0}\Ff{\FfReadOnly}
  \AA{\AAFormat{\hided@t@Fmt}}]{internalData.#1}{2bp}{2bp}}}
\def\bg@vs#1{\setlength\@tempdima{#1}\vskip\@tempdima}
\def\bg@hs#1{\setlength\@tempdima{#1}\hskip\@tempdima}
\let\oBgEnvs\@gobble
\newif\ifisbgenv\isbgenvfalse
\let\barNum\ef@Zero
\def\bg@warning{\PackageWarningNoLine{bargraph-js}
  {At least one more compile is required}\global\let\bg@warning\@empty}
\newenvironment{bargraphenv}[2][]{\offinterlineskip
  \let\vs\bg@vs\let\hs\bg@hs\global\isbgenvtrue
  \@ifundefined{envname@#2}{\global\@namedef{envname@#2}{#2}}
    {\PackageWarning{bargraph-js}
      {The name '#2' has been used in an earlier\MessageBreak
      bargraphenv environment, please choose another\MessageBreak
      name for the bargraphenv environment that\MessageBreak
      appears}}%
  \g@addto@macro\oBgEnvs{,"#2":\dl@lBrace\dl@rBrace}%
  \def\bgrphenv@name{#2}\setkeys{bgrphenv}{width,height,#1}%
  \ifhorizontalbars
    \@tempdima\bgrphenv@width\relax
    \@tempdima=\bgjs@origin\@tempdima
    \edef\bgjs@setorigin{\the\@tempdima}%
  \else % vertical
    \@tempdima\bgrphenv@height\relax
    \@tempdima=\bgjs@origin\@tempdima
    \edef\bgjs@setorigin{\the\@tempdima}%
  \fi
  \@tempdima=.99626\@tempdima
  \edef\bgrphenv@setoriginbp{\strip@pt\@tempdima}%
  \let\isdynamic\relax\let\bg@list\@gobble
  \global\let\barNum\ef@Zero
  \begin{minipage}[b][\bgrphenv@height][b]{\bgrphenv@width}\hfuzz4pt}
    {\hfill\end{minipage}\leavevmode
    \ifx\txtBgValues\relax\else
      \@ifundefined{OBgEnvs}{\bg@warning}{\txtBgValues
      \global\let\txtBgValues\relax}\fi
    \internalD@t@Hidden{\bgrphenv@name}}
\define@key{bargraph}{nbars}[0]{\gdef\nbars{#1}}
\def\nb@rsDef{0}
\def\nbars{0}
\define@key{bargraph}{gap}[0]{\gdef\bargap{#1bp}}
\def\b@rgapDef{0}
\def\bargap{0bp} % in bp units
\define@key{bargraph}{bardimen}{\gdef\bardimen{#1bp}}
\def\b@rdimenDef{20}
\def\bardimen{20bp} % in bp units
\def\bgjs@seto{\cntbars\@ne
  \ifx\barNum\ef@Zero\relax
    \global\let\barNum\ef@One
    \toks@={\ifhorizontalbars\else\ifxetex\else
      \hskip2bp\fi\fi}\else\toks@={}\fi\bgjs@gettype}
\def\bgjs@gettype{\@ifnextchar\relax
  {\bgjs@dynamic}{\bgjs@@getbartoks}}
\def\expbarfor#1\@nil{#1}
\def\bgjs@dynamic{\let\b@rforCommon\dyb@rforCommon
  \edef\bgrph@name{x\bgrph@name}\leavevmode
  \rlap{\barfor{bgnoname}}}
\def\bgjs@@getbartoks{\@ifnextchar\barfor
  {\bgjs@@getbarfor}{\@ifnextchar\cmd
    {\bgjs@@getcmd}{\relax}}}%
\def\hmrk{\hskip\bgjs@setorigin\relax}
\def\bgjs@@getbarfor\barfor#1{%
  \ifhorizontalbars % o=horiz
    \toks@=\expandafter{\the\toks@\leavevmode
      \rlap{\hmrk\bgjs@adj\barfor{#1}}}%
    \ifnum\cntbars=\nbars\relax
      \toks@=\expandafter{\the\toks@\vcgBdry[0bp]}\else
      \toks@=\expandafter{\the\toks@\vcgBdry[\bargap]}\fi
  \else % o=vert
    \toks@=\expandafter{\the\toks@
      \raisebox{\bgjs@setorigin+\bgjs@adj}{\barfor{#1}}}%
    \ifnum\cntbars=\nbars\relax
      \toks@=\expandafter{\the\toks@\cgBdry[0bp]}\else
      \toks@=\expandafter{\the\toks@\cgBdry[\bargap]}\fi
  \fi
  \advance\cntbars\@ne
  \ifnum\cntbars>\nbars
    \def\bgjs@next{\@ifnextchar\cmd{\bgjs@@getbartoks}
    {\let\\\expbarfor\the\toks@}}%
  \else
    \def\bgjs@next{\bgjs@@getbartoks}\fi
  \unskip\bgjs@next}
\def\bgjs@@getcmd\cmd#1{%
  \toks@=\expandafter{\the\toks@#1}%
  \ifhorizontalbars % o=horiz
    \ifnum\cntbars=\nbars\relax
      \toks@=\expandafter{\the\toks@}\else
      \toks@=\expandafter{\the\toks@}\fi
  \else % o=vert
    \ifnum\cntbars=\@ne
      \toks@=\expandafter{\the\toks@}\else
      \toks@=\expandafter{\the\toks@}\fi
  \fi
  \ifnum\cntbars>\nbars
    \def\bgjs@next{\@ifnextchar\cmd{\bgjs@@getbartoks}
    {\let\\\expbarfor\the\toks@}}\else
    \def\bgjs@next{\bgjs@@getbartoks}\fi
\bgjs@next}
\newenvironment{bargraph}[2][]{\def\bgrph@name{#2}%
  \g@addto@macro\bg@list{,"#2"}\setkeys{bargraph}{nbars=\nb@rsDef,
    bardimen=\b@rdimenDef,gap=\b@rgapDef,#1}%
  \edef\bg@sign{\ifxetex-\else+\fi}%
  \edef\bgjs@adj{\ifhorizontalbars
    \ifxetex\else\hskip2bp\fi\else+\ifxetex0pt\else2bp\fi\fi}%
  \if@bgshowaxis\ifhorizontalbars\else\leavevmode
    \smash{\makebox[0pt][l]{\raisebox{\bgjs@setorigin\bg@sign.5pt}%
      {\rule{\bgrphenv@width}{1pt}}}}\fi\fi
  \bgjs@seto}{\if@bgshowaxis\ifhorizontalbars
    \smash{\rlap{\setlength{\@tempdima}
      {\bgjs@setorigin\bg@sign.5pt}%
    \hskip\@tempdima\rule{1pt}{\bgrphenv@height}}}\fi\fi}
\def\txtBgValues{%\leavevmode
  \llap{\textField[\S{S}\H{N}\BG{}\BC{}\F{\FHidden}%
  \autoCenter{n}\DV{(\{\OBgEnvs\})}\V{(\{\OBgEnvs\})}
  ]{bgValues}{2bp}{2bp}}\llap{\textField[\S{S}\H{N}\BG{}\BC{}
  \F{\FHidden}\autoCenter{n}\DV{}\V{}]{barLabeling}{2bp}{2bp}}%
}
\gdef\bg@wrtBbValues{\immediate\write\@auxout{\string
  \gdef\string\OBgEnvs{\oBgEnvs}}}
\AtEndDocument{\ifisbgenv\bg@wrtBbValues\fi}
\def\barforCommon#1{\def\b@rforCommon{#1}}
\barforCommon{\S{S}\H{N}\BG{}\BC{}\F{\FHidden}\autoCenter{n}}
\def\dybarforCommon#1{\def\dyb@rforCommon{#1}}
\dybarforCommon{\S{S}\H{N}\BG{}\BC{}\F{\FHidden}}
\def\presetsbarfor#1#2#3{\protectedKeys{#1.#2@\bgrphenv@name}{#3}}
\def\presetsb@rfor#1{\@nameuse{\bgrph@name.#1@\bgrphenv@name}}
\def\barfor#1{\ifhorizontalbars\def\bgenv@angle{0}\else
  \def\bgenv@angle{90}\fi
  \@ifundefined{\bgrph@name.#1@\bgrphenv@name}
    {\let\@presets\@gobble}{\let\@presets\presetsb@rfor}%
  \pushButton[\presets{\b@rforCommon\R{\bgenv@angle}}
    \epresets{\@presets{#1}}
  ]{\bgrphenv@name @\bgrph@name.#1}{\bardimen}{\bardimen}}
\def\getBarName(#1,#2,#3){#1+"@"+#2+"."+#3}
\def\inputFor#1#2#3{% env, bar, name
  \textField[\cmd{\bParams{#1}{#2.#3}\eParams}
    \presets{\priorpresetinputfor{#1}{#2.#3}}
    \presets{\presetinputfor{#1}{#2.#3}}
    \AAonfocus{updateBG.activegraph="#1@#2.#3";}]%
    {#1.#2@#3}}
\def\priorpresetinputfor#1#2{\AddAAcalculate{%
  updateBG.env=getEnvName(event.targetName);\r
  updateBG(event.value,"#1@#2",\usebarlabel);\r}
  \AAcalculate{;}
}
\newcommand{\presetinputfor}[2]{%
  \AAkeystroke{AFNumber_Keystroke(0, 0, 0, 0, "", true);}
  \AAformat{AFNumber_Format(0, 0, 0, 0, "", true);}
  \AAvalidate{AFRange_Validate(true, 0, false, 0);}
}
\begin{defineJS}{\dsfForKeyStr}
if(event.willCommit) {
  var v=event.value;
  try { v=eval(v) } catch(e){};
  if(isNaN(v)) {
    app.beep(0);
    app.alert("Enter a nonnegative number")
    event.rc=false;
  }
}
\end{defineJS}
\newcommand{\displaysfFor}[2][]{%
  \textField[\DV{2}\V{2}\TU{Current scale factor}
  \AAkeystroke{\dsfForKeyStr}
  \AAvalidate{AFRange_Validate(true, 0, false);}
  #1]{txtRescale.#2}%
}
\begin{defineJS}[\catcode`\!=0\relax]{\msfForMU}
var f=this.getField("txtRescale.!p(1)");
var v=eval(f.value);
if (v>0){
  dataForEnv["!p(1)"].sf=v;
  rescaleBargraph("!p(1)");
}
\end{defineJS}
\newcommand{\manualsfFor}[2][]{%
  \pushButton[\cmd{\bParams{#2}\eParams}
    \AAmouseup{\msfForMU}#1]{btnRescale.#2}%
}
\def\barLabelsTU#1{\def\usebarlabel{#1}\ifx\usebarlabel\@empty
  \def\usebarlabel{""}\fi}
\barLabelsTU{""}
\def\barLabelsNoTU#1{\def\@rgi{#1}\ifx\@empty
  \edef\barLabelsNoTUJS{\barLabelsNoTUJSDef}\else
  \def\barLabelsNoTUJS{#1}\fi}
\def\barLabelsNoTUJSDef{o.barname+": "+o.bar+", Value: "+o.value}
\expandafter\barLabelsNoTU\expandafter{\barLabelsNoTUJSDef}
\def\simpleBarLabels(#1,#2){"Bar for "+#1+", Value: "+#2;}
\newcommand\labelFld[4][]{\textField[\Ff{\FfReadOnly}\F{\FHidden}
  \BC{}\BG{}#1\autoCenter{n}\DV{#2}\V{#2}]{#3@\bgrphenv@name}{#4}}
\begin{defineJS}[\catcode`\!=0\relax]{\bgOpenAction}
if(oBarLabeling.needsRestore) {
  var bgTO=app.setTimeOut("bgRestoreData();!%
app.clearTimeOut(bgTO);!%
oBarLabeling.needsRestore=false",1000);
}
\end{defineJS}
\begingroup\@makeother\%\let\%\defjsLB
\thisPageAction{\JS{\bgOpenAction}}{}
\endgroup
\begin{willSave}
bgSaveData();
\end{willSave}
\def\barDefColor{color.blue}
\newcommand{\populateCommaData}{%
  populateCommaData.usebarlabel=\usebarlabel;\r
  populateCommaData
}
\begin{insDLJS*}{bgjs}
\begin{newsegment}{AeB: bargraph-js}
/*
        Document Level JavaScript
        bargraph-js Package
        D. P. Story copyright \the\year
*/
\end{newsegment}
\begin{newsegment}{Bar Graph of preset bars}
var _scaleFactorDef=\sc@leF@ctorDef;
var oBarLabeling = new Object;
    oBarLabeling.needsRestore=true;
function updateBG (v,fld,o) {
  if (typeof v=="undefined") return;
  if (updateBG.activegraph!=fld) return;
  var useDefLabeling=(typeof o=="string");
  var env=updateBG.env;
  var w=dataForEnv[env].width;
  var h=dataForEnv[env].height;
  var baseline=dataForEnv[env].baseline
  var isHoriz=dataForEnv[env].horiz;
  var bRescaleNeeded=false;
  var f=this.getField(fld);
  var cachedValue=getBarValue(env,fld);
  saveBarValue(env,fld,v); // dps13
  bRescaleNeeded=getNewScaleFactor(env,v);
  var sf=dataForEnv[env].sf;
  sf=(sf<0)?-sf:sf;
  var r=f.rect;
  if(isHoriz) { // dps01
    if (v<0) {
     if (cachedValue<0) // no change in sign
        r[0]=r[2]+(v*sf);
      else { // change from pos to neg
        r[2]=r[0];
        r[0]=r[2]+(v*sf);
      }
    } else {
      if (cachedValue<0) { // from neg to pos
        r[0]=r[2];
        r[2]=r[0]+(v*sf);
      } else // from pos to pos
        r[2]=r[0]+(v*sf);
      }
  } else { // vertical
   if (v<0) { // dps01
   if (cachedValue<0) { // no change in sign
        r[3]=r[1]+(v*sf);
   } else { // switching from pos to neg
        r[1]=r[3];
     r[3]=r[1]+(v*sf);
      }
   } else {
      if (cachedValue<0)  // change in sign: neg to pos
     r[3]=r[1]+(v*sf);
      else // no change
        r[1]=r[3]+(v*sf);
    }
  }
  f.rect=r;
  if (typeof oBarLabeling[fld]=="undefined")
    oBarLabeling[fld]=(f.userName!="")?f.userName:-1;
  if (oBarLabeling[fld]==-1)f.userName=barLabelsDef(fld,v);
  else {
    if (typeof o!="undefined")
      f.userName=(useDefLabeling)?((o=="")?%
(simpleBarLabels(fld,v,oBarLabeling[fld])):%
(simpleBarLabels(fld,v,o))):o(fld,v);
  } 
  f.fillColor=(color.equal(f.fillColor,color.transparent))?%
\barDefColor:f.fillColor;
  f.display=display.visible;
  if(bRescaleNeeded) {
    var g=this.getField(env);
    if (g!=null)g.readonly=true;
    app.setTimeOut("rescaleBargraph(\""+env+"\")",5);
  }
}
function parseFld(fld,v) {
  var pos=fld.indexOf("@");
  var env=fld.substring(0,pos);
  fld=fld.substring(pos+1);
  pos=fld.indexOf(".");
  var barname=fld.substring(0,pos);
  var bar=fld.substring(pos+1);
  parseFld.env=env;
  parseFld.barname=barname;
  parseFld.bar=bar;
  parseFld.value=v;
}
function barLabelsDef(fld,v){
  parseFld(fld,v);
  var o=parseFld;
  var strOrFnc=\barLabelsNoTUJS;
  return ((typeof strOrFnc=="string")?strOrFnc:strOrFnc(fld,v));
}
function simpleBarLabels(fld,v){
  if (arguments.length==2)
     return customBarLabels(fld,v);
  else {
    parseFld(fld,v);
    var s=arguments[2];
    return replaceLblVars(s);
  }
}
function getNewScaleFactor(env,v) {
  var w=dataForEnv[env].width;
  var h=dataForEnv[env].height;
  var maxDimen=dataForEnv[env].maxDim;
  var nO=dataForEnv[env].origin;
  var sf=dataForEnv[env].sf;
  var isHoriz=dataForEnv[env].horiz;
  v=(v<0)?-v:v;
  if(isHoriz) {
    maxDimen=(nO==0)?w:maxDimen;
    if( sf*v > maxDimen ) {
      sf=maxDimen/v;
      dataForEnv[env].sf=sf;
      return true;
    }
  } else {
    maxDimen=(nO==0)?h:maxDimen;
    if ( sf*v > maxDimen ) {
      sf=maxDimen/v;
      dataForEnv[env].sf=sf;
      return true;
    }
  }
  return false;
}
// <env>.<barname>@<name> --> <env>@<barbane>.<name>
function getBarName(name) {
  var pos=name.indexOf(".");
  var env=name.substring(0,pos);
  name=name.substring(pos+1);
  return env+"@"+name.replace(/@/,".");
}
function getEnvName(name) {
  var pos=name.indexOf(".");
  var env=name.substring(0,pos);
  return env;
}
function rescaleBargraph(env) {
  var oInputFor=this.getField(env);
  this.delay=true;
  // date input by the \\inputFor
  if (oInputFor!=null) {
    var a=oInputFor.getArray()
    for (var i=0; i<a.length; i++) {
      updateBG.activegraph=getBarName(a[i].name);
      if (a[i].display!=display.hidden) {
        this.calculate=false;
        a[i].value=1*a[i].value + 1;
        this.calculate=true;
        a[i].value=1*a[i].value - 1;
      }
    }
  } else {
    var aBars=dataForEnv[env].bgs;
    for (var i=0; i<aBars.length; i++) {
      var bars=this.getField(env+"@"+aBars[i]); // dps07
      var widgets=bars.getArray();
      for (var j=0; j<widgets.length; j++) {
        var name=widgets[j].name;
        var Value=getBarValue(env,name); // dps13
        updateBG.env=env;
        updateBG.activegraph=name;
        updateBG(Value,updateBG.activegraph);
      }
    }
  }
  var f=this.getField("txtRescale."+env);
  if (f!=null) f.value=Math.round(dataForEnv[env].sf*1000)/1000;
  this.delay=false;
  var g=this.getField(env);
  if (g!=null)g.readonly=false;
}
function optimizeScaling(env) {
  var w=dataForEnv[env].width;
  var h=dataForEnv[env].height;
  var sf=dataForEnv[env].sf;
  var isHoriz=dataForEnv[env].horiz;
  var aBGs=dataForEnv[env].bgs;
  for (var i=0; i<aBGs.length; i++) {
    var o=this.getField(env+"@"+aBGs[i]); // dps07
    var a=o.getArray();
    maxFldName=a[0].name;
    if (isHoriz) { // if horizontal bars
      var maxDim=0; maxFldName="";
      for (var i=0; i<a.length; i++) {
        if (a[i].display==display.hidden) continue;
        var thisDim=a[i].rect[2]-a[i].rect[0];
        thisDim=(thisDim<0)?-thisDim:thisDim; // dps01
        if ( thisDim > maxDim ) {
          maxDim=thisDim;
          maxFldName=a[i].name;
        }
      }
      if (maxFldName=="") return;
  // get name of input field env.bar@name
      var v=getBarValue(env,maxFldName);
  // now rescale all bars
     v=(v<0)?-v:v; // dps01
      if (v !=0) {
        sf=w/v; // if v !=0
        dataForEnv[env].sf=sf;
        var g=this.getField(env);
        if (g!=null)g.readonly=true;
        app.setTimeOut("rescaleBargraph(\""+env+"\")",5);
      }
    } else { // if vertical bars
      var maxDim=0; maxFldName="";
      for (var i=0; i< a.length; i++) {
        if (a[i].display==display.hidden) continue;
        var thisDim=a[i].rect[1]-a[i].rect[3];
        thisDim=(thisDim<0)?-thisDim:thisDim; // dps01
        if ( thisDim > maxDim ) {
          maxDim=thisDim;
          maxFldName=a[i].name;
        }
      }
      if (maxFldName=="") return;
      var v=getBarValue(env,maxFldName); // dps13
      if ( typeof v == "undefined" ) return;
  // now rescale all bars
     v=(v<0)?-v:v; // dps01
      if (v !=0) {
        sf=h/v;
        dataForEnv[env].sf=sf;
        var g=this.getField(env);
        if (g!=null)g.readonly=true;
        app.setTimeOut("rescaleBargraph(\""+env+"\")",5);
      }
    }
  }
}
function resetBargraphs() {
  this.calculate=false;
  this.resetForm(arguments);
  for (var n=0; n<arguments.length; n++){
  var f=this.getField("internalData."+arguments[n]);
  if (f!=null) {
      dataForEnv[arguments[n]].sf=_scaleFactorDef;
      var env=arguments[n]; // dps01
      var isHoriz=dataForEnv[env].horiz;
     aBGs=dataForEnv[arguments[n]].bgs;
      // an environment name
    for (var i=0; i<aBGs.length; i++) {
        var o=this.getField(env+"@"+aBGs[i]); // dps07
    var a=o.getArray();
    o.display=display.hidden;
        if (isHoriz) {
          for(j=0;j<a.length;j++) {
            var name=a[j].name; // dps01
            var value=getBarValue(env,name); // dps01
            if (value<0) { // dps01
              var r=a[j].rect;
              r[0]=r[2]+20;
              a[j].rect=r;
            }
            saveBarValue(arguments[n],a[j].name,"0");
          }
        } else {
          for(j=0;j<a.length;j++) {
            var name=a[j].name; // dps01
            var value=getBarValue(env,name); // dps01
            if (value<0) { // dps01
              var r=a[j].rect;
              r[3]=r[1]+20;
              a[j].rect=r;
            }
            saveBarValue(arguments[n],a[j].name,"0");
          }
        }
      }
    }
  }
  this.calculate=true;
}
// fld="<bgenv-name><bg-name>.<bar-name>", v=value of field
function customBarLabels(fld,v){
  var f=this.getField(fld);
  if ( oBarLabeling[fld] != -1 ) {
    var s=oBarLabeling[fld];
    if (s.indexOf("@v@")==-1)
      return barLabelsDef(fld,v);
    else {
      parseFld(fld,v);
      return replaceLblVars(s);
    }
  } else return barLabelsDef(fld,v);
}
function replaceLblVars(s) {
  var o=parseFld;
  s=s.replace(/@env@/g,o.env);
  s=s.replace(/@barname@/g,o.barname);
  s=s.replace(/@bar@/g,o.bar);
  s=s.replace(/@v@/g,o.value);
  return s;
}
function getBarValue(env,name){
  var f=this.getField("bgValues");
  var o=eval(f.value);
  var v=o[env][name];
  var retn=(typeof v=="undefined")?0:v;
  return retn;
}
function saveBarValue(env,name,v) {
  var f=this.getField("bgValues");
  var o=eval(f.value);
  o[env][name]=v;
  f.value=o.toSource();
}
\end{newsegment}
\begin{newsegment}{Toggle field visibility}
function toggleFldVisibility(name,bOk) {
  var f=this.getField(name);
  f.display=(bOk)?display.visible:display.hidden;
}
\end{newsegment}
\begin{newsegment}{Comma-delimited Data}
function populateCommaData(env,bar,str) {
  var value;
  var f=this.getField(env+"@"+bar);
  var g=f.getArray();
  var aValue=str.split(",");
  var bOk=(arguments.length>3)?arguments[3](aValue):true;
  if (bOk) {
    for (var i=0; i<g.length; i++){
      updateBG.env=env;
      updateBG.activegraph=g[i].name;
      value=(typeof(aValue[i])=="undefined")?0:aValue[i];
      updateBG(value,updateBG.activegraph,%
populateCommaData.usebarlabel);
    }
  } else app.alert(arguments[3].msg);
}
\end{newsegment}
\begin{newsegment}{Validate input data for comma-delimited data}
validateArrayNonNegNums.msg="Enter only non-negative numbers";
function validateArrayNonNegNums(aValue){
  var bOk=true;
  for (var i=0; i<aValue.length; i++) {
    var x=1*aValue[i];
    if(isNaN(x)) {bOk=false; break;}
    else if (x<0) {bOk=false; break;}
  }
  return bOk
}
validateArrayOfNums.msg="Enter only numerical values";
function validateArrayOfNums(aValue){
  var bOk=true;
  for (var i=0; i<aValue.length; i++) {
    var x=1*aValue[i];
    if(isNaN(x)) {bOk=false; break;}
  }
  return bOk
}
\end{newsegment}
\begin{newsegment}{Restore Data}
function bgSaveData() {
  var f=this.getField("barLabeling");
  if(f!=null)f.value=oBarLabeling.toSource();
}
function bgRestoreData(){
  var f=this.getField("barLabeling");
  if(f!=null&&f.value!="")oBarLabeling=eval(f.value);
}
\end{newsegment}
\end{insDLJS*}
\bgjs@restoreCats
\endinput
%%
%% End of file `bargraph-js.sty'.