1111import org .jetbrains .annotations .ApiStatus ;
1212
1313import java .text .DecimalFormat ;
14+ import java .util .List ;
1415
1516@ ApiStatus .Experimental
1617public class GraphAxis {
@@ -27,21 +28,43 @@ public class GraphAxis {
2728 public float [] minorTicks = new float [16 ];
2829 public TextRenderer .Line [] tickLabels = new TextRenderer .Line [8 ];
2930 private float maxLabelWidth = 0 ;
30- public MajorTickFinder majorTickFinder = new AutoMajorTickFinder (10 );
31- public MinorTickFinder minorTickFinder = new AutoMinorTickFinder (1 );
31+ public MajorTickFinder majorTickFinder = new AutoMajorTickFinder (true );
32+ public MinorTickFinder minorTickFinder = new AutoMinorTickFinder (2 );
3233 public String label ;
3334 public float min , max ;
3435 public boolean autoLimits = true ;
35- public float [] data ;
3636
3737 public GraphAxis (GuiAxis axis ) {
3838 this .axis = axis ;
3939 }
4040
41- void compute () {
41+ void compute (List < Plot > plots ) {
4242 if (this .autoLimits ) {
43- this .min = FloatArrayMath .min (this .data );
44- this .max = FloatArrayMath .max (this .data );
43+ if (plots .isEmpty ()) {
44+ this .min = 0 ;
45+ this .max = 0 ;
46+ } else if (plots .size () == 1 ) {
47+ this .min = FloatArrayMath .min (plots .get (0 ).getData (this .axis ));
48+ this .max = FloatArrayMath .max (plots .get (0 ).getData (this .axis ));
49+ } else {
50+ float min = Float .MAX_VALUE , max = Float .MIN_VALUE ;
51+ for (Plot plot : plots ) {
52+ float m = FloatArrayMath .min (plot .getData (this .axis ));
53+ if (m < min ) min = m ;
54+ m = FloatArrayMath .max (plot .getData (this .axis ));
55+ if (m > max ) max = m ;
56+ }
57+ this .min = min ;
58+ this .max = max ;
59+ }
60+ if (this .axis .isVertical ()) {
61+ float padding = (this .max - this .min ) * 0.05f ;
62+ this .max += padding ;
63+ this .min -= padding ;
64+ }
65+ }
66+ if (this .majorTickFinder instanceof AutoMajorTickFinder tickFinder && tickFinder .isAutoAdjust ()) {
67+ tickFinder .calculateAutoTickMultiple (this .min , this .max );
4568 }
4669 this .majorTicks = this .majorTickFinder .find (this .min , this .max , this .majorTicks );
4770 this .minorTicks = this .minorTickFinder .find (this .min , this .max , this .majorTicks , this .minorTicks );
@@ -52,7 +75,7 @@ void compute() {
5275 textRenderer .setScale (TICK_LABEL_SCALE );
5376 this .maxLabelWidth = 0 ;
5477 float maxDiff = FloatArrayMath .max (FloatArrayMath .diff (this .majorTicks ));
55- int significantPlaces = (int ) Math .abs (Math .log10 (maxDiff )) + 1 ;
78+ int significantPlaces = (int ) Math .abs (Math .log10 (maxDiff )) + 2 ;
5679 DecimalFormat format = new DecimalFormat ();
5780 format .setMaximumFractionDigits (significantPlaces );
5881 for (int i = 0 ; i < this .tickLabels .length ; i ++) {
@@ -87,12 +110,12 @@ void drawGridLines(BufferBuilder buffer, GraphView view, GraphAxis other, boolea
87110 float [] pos = major ? this .majorTicks : this .minorTicks ;
88111 float dHalf = d / 2 ;
89112 if (axis .isHorizontal ()) {
90- float otherMin = view .transformYGraphToScreen (other .max );
91- float otherMax = view .transformYGraphToScreen (other .min );
113+ float otherMin = view .g2sY (other .max );
114+ float otherMax = view .g2sY (other .min );
92115 drawLinesOnHorizontal (buffer , view , pos , dHalf , otherMin , otherMax , r , g , b , a );
93116 } else {
94- float otherMin = view .transformXGraphToScreen (other .min );
95- float otherMax = view .transformXGraphToScreen (other .max );
117+ float otherMin = view .g2sX (other .min );
118+ float otherMax = view .g2sX (other .max );
96119 drawLinesOnVertical (buffer , view , pos , dHalf , otherMin , otherMax , r , g , b , a );
97120 }
98121 }
@@ -101,10 +124,10 @@ void drawTicks(BufferBuilder buffer, GraphView view, GraphAxis other, boolean ma
101124 float [] pos = major ? this .majorTicks : this .minorTicks ;
102125 float dHalf = thickness / 2 ;
103126 if (axis .isHorizontal ()) {
104- float otherMin = view .transformYGraphToScreen (other .min );
127+ float otherMin = view .g2sY (other .min );
105128 drawLinesOnHorizontal (buffer , view , pos , dHalf , otherMin - length , otherMin , r , g , b , a );
106129 } else {
107- float otherMin = view .transformXGraphToScreen (other .min );
130+ float otherMin = view .g2sX (other .min );
108131 drawLinesOnVertical (buffer , view , pos , dHalf , otherMin , otherMin + length , r , g , b , a );
109132 }
110133 }
@@ -115,7 +138,7 @@ private void drawLinesOnHorizontal(BufferBuilder buffer, GraphView view, float[]
115138 if (Float .isNaN (p )) break ;
116139 if (p < min || p > max ) continue ;
117140
118- p = view .transformXGraphToScreen (p );
141+ p = view .g2sX (p );
119142
120143 float x0 = p - dHalf ;
121144 float x1 = p + dHalf ;
@@ -129,7 +152,7 @@ private void drawLinesOnVertical(BufferBuilder buffer, GraphView view, float[] p
129152 if (Float .isNaN (p )) break ;
130153 if (p < min || p > max ) continue ;
131154
132- p = view .transformYGraphToScreen (p );
155+ p = view .g2sY (p );
133156
134157 float y0 = p - dHalf ;
135158 float y1 = p + dHalf ;
@@ -142,23 +165,23 @@ void drawLabels(GraphView view, GraphAxis other) {
142165 if (axis .isHorizontal ()) {
143166 textRenderer .setScale (TICK_LABEL_SCALE );
144167 textRenderer .setAlignment (Alignment .TopCenter , 100 );
145- float y = view .transformYGraphToScreen (other .min ) + TICK_LABEL_OFFSET ;
168+ float y = view .g2sY (other .min ) + TICK_LABEL_OFFSET ;
146169 for (int i = 0 ; i < this .majorTicks .length ; i ++) {
147170 float pos = this .majorTicks [i ];
148171 if (Float .isNaN (pos )) break ;
149172 if (pos < min || pos > max ) continue ;
150- textRenderer .setPos ((int ) (view .transformXGraphToScreen (pos ) - 50 ), (int ) y );
173+ textRenderer .setPos ((int ) (view .g2sX (pos ) - 50 ), (int ) y );
151174 textRenderer .draw (this .tickLabels [i ].getText ());
152175 }
153176 } else {
154177 textRenderer .setScale (TICK_LABEL_SCALE );
155178 textRenderer .setAlignment (Alignment .CenterRight , this .maxLabelWidth , 20 );
156- float x = view .transformXGraphToScreen (other .min ) - TICK_LABEL_OFFSET - this .maxLabelWidth ;
179+ float x = view .g2sX (other .min ) - TICK_LABEL_OFFSET - this .maxLabelWidth ;
157180 for (int i = 0 ; i < this .majorTicks .length ; i ++) {
158181 float pos = this .majorTicks [i ];
159182 if (Float .isNaN (pos )) break ;
160183 if (pos < min || pos > max ) continue ;
161- textRenderer .setPos ((int ) x , (int ) (view .transformYGraphToScreen (pos ) - 10 ));
184+ textRenderer .setPos ((int ) x , (int ) (view .g2sY (pos ) - 10 ));
162185 textRenderer .draw (this .tickLabels [i ].getText ());
163186 }
164187 }
0 commit comments