1 package org.opentrafficsim.draw.graphs;
2
3 import org.djunits.value.vdouble.scalar.Duration;
4 import org.djutils.exceptions.Throw;
5 import org.jfree.chart.JFreeChart;
6 import org.jfree.chart.axis.ValueAxis;
7 import org.jfree.chart.event.AxisChangeEvent;
8 import org.jfree.chart.event.AxisChangeListener;
9 import org.jfree.chart.plot.XYPlot;
10
11
12
13
14
15
16
17
18
19
20
21
22 public abstract class AbstractBoundedPlot extends AbstractPlot
23 {
24
25
26 private Double lowerDomainBound = null;
27
28
29 private Double upperDomainBound = null;
30
31
32 private Double lowerRangeBound = null;
33
34
35 private Double upperRangeBound = null;
36
37
38
39
40
41
42
43
44 public AbstractBoundedPlot(final PlotScheduler scheduler, final String caption, final Duration updateInterval,
45 final Duration delay)
46 {
47 super(scheduler, caption, updateInterval, delay);
48 }
49
50
51 @Override
52 protected void setChart(final JFreeChart chart)
53 {
54 Throw.when(!(chart.getPlot() instanceof XYPlot), IllegalArgumentException.class,
55 "AbstractBoundedPlot can only work with XYPlot.");
56
57 super.setChart(chart);
58
59 XYPlot xyPlot = chart.getXYPlot();
60 xyPlot.getDomainAxis().addChangeListener(new AxisChangeListener()
61 {
62
63 private boolean listen = true;
64
65
66 @SuppressWarnings("synthetic-access")
67 @Override
68 public void axisChanged(final AxisChangeEvent event)
69 {
70 if (!this.listen)
71 {
72 return;
73 }
74 this.listen = false;
75 constrainAxis(xyPlot.getDomainAxis(), AbstractBoundedPlot.this.lowerDomainBound,
76 AbstractBoundedPlot.this.upperDomainBound);
77 this.listen = true;
78 }
79 });
80 xyPlot.getRangeAxis().addChangeListener(new AxisChangeListener()
81 {
82
83 private boolean listen = true;
84
85
86 @SuppressWarnings("synthetic-access")
87 @Override
88 public void axisChanged(final AxisChangeEvent event)
89 {
90 if (!this.listen)
91 {
92 return;
93 }
94 this.listen = false;
95 constrainAxis(xyPlot.getRangeAxis(), AbstractBoundedPlot.this.lowerRangeBound,
96 AbstractBoundedPlot.this.upperRangeBound);
97 this.listen = true;
98 }
99 });
100 }
101
102
103
104
105
106 public void setLowerDomainBound(final Double bound)
107 {
108 this.lowerDomainBound = bound;
109 constrainAxis(getChart().getXYPlot().getDomainAxis(), this.lowerDomainBound, this.upperDomainBound);
110 }
111
112
113
114
115
116 public void setUpperDomainBound(final Double bound)
117 {
118 this.upperDomainBound = bound;
119 constrainAxis(getChart().getXYPlot().getDomainAxis(), this.lowerDomainBound, this.upperDomainBound);
120 }
121
122
123
124
125
126 public void setLowerRangeBound(final Double bound)
127 {
128 this.lowerRangeBound = bound;
129 constrainAxis(getChart().getXYPlot().getRangeAxis(), this.lowerRangeBound, this.upperRangeBound);
130 }
131
132
133
134
135
136 public void setUpperRangeBound(final Double bound)
137 {
138 this.upperRangeBound = bound;
139 constrainAxis(getChart().getXYPlot().getRangeAxis(), this.lowerRangeBound, this.upperRangeBound);
140 }
141
142
143
144
145
146
147
148 private void constrainAxis(final ValueAxis axis, final Double min, final Double max)
149 {
150 double xLow = axis.getLowerBound();
151 double xUpp = axis.getUpperBound();
152 if (min != null && max != null && xUpp - xLow > max - min)
153 {
154 axis.setLowerBound(min);
155 axis.setUpperBound(max);
156 }
157 else if (min != null && xLow < min)
158 {
159 axis.setLowerBound(min);
160 axis.setUpperBound(xUpp + (min - xLow));
161 }
162 else if (max != null && xUpp > max)
163 {
164 axis.setLowerBound(xLow - (xUpp - max));
165 axis.setUpperBound(max);
166 }
167 }
168
169
170 @Override
171 public void setAutoBoundDomain(final XYPlot plot)
172 {
173 if (this.lowerDomainBound != null)
174 {
175 plot.getDomainAxis().setLowerBound(this.lowerDomainBound);
176 }
177 if (this.upperDomainBound != null)
178 {
179 plot.getDomainAxis().setUpperBound(this.upperDomainBound);
180 }
181 }
182
183
184 @Override
185 public void setAutoBoundRange(final XYPlot plot)
186 {
187 if (this.lowerRangeBound != null)
188 {
189 plot.getRangeAxis().setLowerBound(this.lowerRangeBound);
190 }
191 if (this.upperRangeBound != null)
192 {
193 plot.getRangeAxis().setUpperBound(this.upperRangeBound);
194 }
195 }
196
197 }