Skip to content

Commit 30ade20

Browse files
committed
CSSTUDIO-3425 Add functionality to draw the Linear Meter with a logarithmic scale.
1 parent ba9b8d8 commit 30ade20

File tree

2 files changed

+116
-28
lines changed

2 files changed

+116
-28
lines changed

app/display/linearmeter/src/main/java/org/csstudio/display/extra/widgets/linearmeter/LinearMeterScale.java

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ public LinearMeterScale(PlotPartListener listener,
3939
int height,
4040
boolean horizontal,
4141
double min,
42-
double max)
42+
double max,
43+
boolean isLogarithmic)
4344
{
4445
super("", listener, horizontal, min, max);
4546
super.setBounds(0, 0, width, height);
47+
super.setLogarithmic(isLogarithmic);
4648
isHorizontal = horizontal;
4749
}
4850

@@ -102,26 +104,49 @@ public void paint(Graphics2D gc, Rectangle plot_bounds)
102104
FontMetrics scale_font_fontMetrics = gc.getFontMetrics(scale_font);
103105

104106
for (MajorTick<Double> tick : ticks.getMajorTicks()) {
105-
gc.drawLine((int) ((start_x + this.scale * (tick.getValue() - offset) )),
107+
if (isLogarithmic()) {
108+
gc.drawLine((int) ((start_x + this.scale * (Math.log10(tick.getValue()) - Math.log10(offset)) )),
109+
(int) ((start_y - 0.5 * TICK_LENGTH)),
110+
(int) ((start_x + this.scale * (Math.log10(tick.getValue()) - Math.log10(offset)) )),
111+
(int) ((start_y + 0.5 * TICK_LENGTH)));
112+
drawTickLabel(gc,
113+
(int) ((start_x + this.scale * (Math.log10(tick.getValue()) - Math.log10(offset)) - scale_font_fontMetrics.stringWidth(tick.getLabel())/2)),
114+
(int) (start_y + 0.5 * TICK_LENGTH + 2 + Math.round(Math.ceil((72.0 * (scale_font_fontMetrics.getAscent())) / 96.0))),
115+
tick.getLabel());
116+
}
117+
else {
118+
gc.drawLine((int) ((start_x + this.scale * (tick.getValue() - offset) )),
106119
(int) ((start_y - 0.5 * TICK_LENGTH)),
107120
(int) ((start_x + this.scale * (tick.getValue() - offset) )),
108121
(int) ((start_y + 0.5 * TICK_LENGTH)));
109-
drawTickLabel(gc,
110-
(int) ((start_x + this.scale * (tick.getValue() - offset) - scale_font_fontMetrics.stringWidth(tick.getLabel())/2)),
111-
(int) (start_y + 0.5 * TICK_LENGTH + 2 + Math.round(Math.ceil((72.0 * (scale_font_fontMetrics.getAscent())) / 96.0))),
112-
tick.getLabel());
122+
drawTickLabel(gc,
123+
(int) ((start_x + this.scale * (tick.getValue() - offset) - scale_font_fontMetrics.stringWidth(tick.getLabel())/2)),
124+
(int) (start_y + 0.5 * TICK_LENGTH + 2 + Math.round(Math.ceil((72.0 * (scale_font_fontMetrics.getAscent())) / 96.0))),
125+
tick.getLabel());
126+
}
113127
}
114128
} else {
115-
116129
for (MajorTick<Double> tick : ticks.getMajorTicks()) {
117-
gc.drawLine((int) (start_x - 0.5 * TICK_LENGTH),
130+
if (isLogarithmic()) {
131+
gc.drawLine((int) (start_x - 0.5 * TICK_LENGTH),
132+
(int) (start_y - this.scale * (Math.log10(tick.getValue()) - Math.log10(offset))),
133+
(int) (start_x + 0.5 * TICK_LENGTH),
134+
(int) (start_y - this.scale * (Math.log10(tick.getValue()) - Math.log10(offset))));
135+
drawTickLabel(gc,
136+
(int) (start_x + 4),
137+
(int) (start_y - this.scale * (Math.log10(tick.getValue()) - Math.log10(offset)) + Math.round((72.0 * scale_font.getSize()) / (96.0 * 2.0))),
138+
tick.getLabel());
139+
}
140+
else {
141+
gc.drawLine((int) (start_x - 0.5 * TICK_LENGTH),
118142
(int) (start_y - this.scale * (tick.getValue() - offset)),
119143
(int) (start_x + 0.5 * TICK_LENGTH),
120144
(int) (start_y - this.scale * (tick.getValue() - offset)));
121-
drawTickLabel(gc,
122-
(int) (start_x + 4),
123-
(int) (start_y - this.scale * (tick.getValue() - offset) + Math.round((72.0 * scale_font.getSize()) / (96.0 * 2.0))),
124-
tick.getLabel());
145+
drawTickLabel(gc,
146+
(int) (start_x + 4),
147+
(int) (start_y - this.scale * (tick.getValue() - offset) + Math.round((72.0 * scale_font.getSize()) / (96.0 * 2.0))),
148+
tick.getLabel());
149+
}
125150
}
126151
}
127152

app/display/linearmeter/src/main/java/org/csstudio/display/extra/widgets/linearmeter/RTLinearMeter.java

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public RTLinearMeter(double initialValue,
116116
height,
117117
isHorizontal,
118118
min,
119-
max);
119+
max,
120+
logScale);
120121

121122
this.minMaxTolerance = minMaxTolerance;
122123
this.loLo = loLo;
@@ -190,7 +191,8 @@ public void redrawLinearMeterScale() {
190191
linearMeterScale.getBounds().height,
191192
linearMeterScale.isHorizontal(),
192193
linearMeterScale.getValueRange().getLow(),
193-
linearMeterScale.getValueRange().getHigh());
194+
linearMeterScale.getValueRange().getHigh(),
195+
linearMeterScale.isLogarithmic());
194196
linearMeterScale.setHorizontal(isHorizontal);
195197
});
196198
}
@@ -296,6 +298,13 @@ public void setDisplayMode(DisplayMode newDisplayMode) {
296298

297299
private DisplayMode displayMode = DisplayMode.NEEDLE;
298300

301+
public void setLogScale(boolean logScale) {
302+
withWriteLock(() -> {
303+
linearMeterScale.setLogarithmic(true);
304+
redraw();
305+
});
306+
}
307+
299308
private void runOnJavaFXThread(Runnable runnable) {
300309
if (Platform.isFxApplicationThread()) {
301310
runnable.run();
@@ -750,10 +759,20 @@ private Optional<Integer> computeIndicatorPosition(double value) {
750759
}
751760
return withReadLock(() -> {
752761
if (linearMeterScale.isHorizontal()) {
753-
return Optional.of((int) Math.round(marginLeft + pixelsPerScaleUnit * (value - linearMeterScale.getValueRange().getLow())));
762+
if (linearMeterScale.isLogarithmic()) {
763+
return Optional.of((int) Math.round(marginLeft + pixelsPerScaleUnit * (Math.log10(value) - Math.log10(linearMeterScale.getValueRange().getLow()))));
764+
}
765+
else {
766+
return Optional.of((int) Math.round(marginLeft + pixelsPerScaleUnit * (value - linearMeterScale.getValueRange().getLow())));
767+
}
754768
}
755769
else {
756-
return Optional.of((int) Math.round(linearMeterScale.getBounds().height - marginBelow - pixelsPerScaleUnit * (value - linearMeterScale.getValueRange().getLow())));
770+
if (linearMeterScale.isLogarithmic()) {
771+
return Optional.of((int) Math.round(linearMeterScale.getBounds().height - marginBelow - pixelsPerScaleUnit * (Math.log10(value) - Math.log10(linearMeterScale.getValueRange().getLow()))));
772+
}
773+
else {
774+
return Optional.of((int) Math.round(linearMeterScale.getBounds().height - marginBelow - pixelsPerScaleUnit * (value - linearMeterScale.getValueRange().getLow())));
775+
}
757776
}
758777
});
759778
}
@@ -1324,14 +1343,34 @@ private void layout() {
13241343
marginBelow += 1 + fontMetrics.getMaxAscent() + fontMetrics.getMaxDescent();
13251344
}
13261345

1327-
pixelsPerScaleUnit = (linearMeterScale.getBounds().width - marginLeft - marginRight - 1) / (linearMeterScale.getValueRange().getHigh() - linearMeterScale.getValueRange().getLow());
1346+
if (linearMeterScale.isLogarithmic()) {
1347+
pixelsPerScaleUnit = (linearMeterScale.getBounds().width - marginLeft - marginRight - 1) / (Math.log10(linearMeterScale.getValueRange().getHigh()) - Math.log10(linearMeterScale.getValueRange().getLow()));
1348+
}
1349+
else {
1350+
pixelsPerScaleUnit = (linearMeterScale.getBounds().width - marginLeft - marginRight - 1) / (linearMeterScale.getValueRange().getHigh() - linearMeterScale.getValueRange().getLow());
1351+
}
13281352
meterBreadth = Math.round(linearMeterScale.getBounds().height - marginAbove - marginBelow) - 1;
13291353

1330-
double x_loLoRectangle = marginLeft;
1331-
double x_lowRectangle = marginLeft + pixelsPerScaleUnit * (displayedLoLo - linearMeterScale.getValueRange().getLow());
1332-
double x_normalRectangle = marginLeft + pixelsPerScaleUnit * (displayedLow - linearMeterScale.getValueRange().getLow());
1333-
double x_highRectangle = marginLeft + pixelsPerScaleUnit * (displayedHigh - linearMeterScale.getValueRange().getLow());
1334-
double x_hiHiRectangle = marginLeft + pixelsPerScaleUnit * (displayedHiHi - linearMeterScale.getValueRange().getLow());
1354+
double x_loLoRectangle;
1355+
double x_lowRectangle;
1356+
double x_normalRectangle;
1357+
double x_highRectangle;
1358+
double x_hiHiRectangle;
1359+
if (linearMeterScale.isLogarithmic()) {
1360+
x_loLoRectangle = marginLeft;
1361+
x_lowRectangle = marginLeft + pixelsPerScaleUnit * (Math.log10(displayedLoLo) - Math.log10(linearMeterScale.getValueRange().getLow()));
1362+
x_normalRectangle = marginLeft + pixelsPerScaleUnit * (Math.log10(displayedLow) - Math.log10(linearMeterScale.getValueRange().getLow()));
1363+
x_highRectangle = marginLeft + pixelsPerScaleUnit * (Math.log10(displayedHigh) - Math.log10(linearMeterScale.getValueRange().getLow()));
1364+
x_hiHiRectangle = marginLeft + pixelsPerScaleUnit * (Math.log10(displayedHiHi) - Math.log10(linearMeterScale.getValueRange().getLow()));
1365+
}
1366+
else {
1367+
x_loLoRectangle = marginLeft;
1368+
x_lowRectangle = marginLeft + pixelsPerScaleUnit * (displayedLoLo - linearMeterScale.getValueRange().getLow());
1369+
x_normalRectangle = marginLeft + pixelsPerScaleUnit * (displayedLow - linearMeterScale.getValueRange().getLow());
1370+
x_highRectangle = marginLeft + pixelsPerScaleUnit * (displayedHigh - linearMeterScale.getValueRange().getLow());
1371+
x_hiHiRectangle = marginLeft + pixelsPerScaleUnit * (displayedHiHi - linearMeterScale.getValueRange().getLow());
1372+
1373+
}
13351374

13361375
loLoRectangle = new Rectangle((int) Math.round(x_loLoRectangle),
13371376
marginAbove,
@@ -1353,9 +1392,16 @@ private void layout() {
13531392
(int) (Math.round(x_hiHiRectangle) - Math.round(x_highRectangle)),
13541393
meterBreadth);
13551394

1395+
int hihiWidth;
1396+
if (linearMeterScale.isLogarithmic()) {
1397+
hihiWidth = (int) (Math.round(pixelsPerScaleUnit * (Math.log10(linearMeterScale.getValueRange().getHigh()) - Math.log10(displayedHiHi))));
1398+
}
1399+
else {
1400+
hihiWidth = (int) (Math.round(pixelsPerScaleUnit * (linearMeterScale.getValueRange().getHigh() - displayedHiHi)));
1401+
}
13561402
hiHiRectangle = new Rectangle((int) Math.round(x_hiHiRectangle),
13571403
marginAbove,
1358-
(int) (Math.round(pixelsPerScaleUnit * (linearMeterScale.getValueRange().getHigh() - displayedHiHi))),
1404+
hihiWidth,
13591405
meterBreadth);
13601406
}
13611407
else {
@@ -1382,13 +1428,30 @@ private void layout() {
13821428
marginBelow += 1 + fontMetrics.getMaxAscent() + fontMetrics.getMaxDescent();
13831429
}
13841430

1385-
pixelsPerScaleUnit = (linearMeterScale.getBounds().height - marginAbove - marginBelow - 1) / (linearMeterScale.getValueRange().getHigh() - linearMeterScale.getValueRange().getLow());
1431+
if (linearMeterScale.isLogarithmic()) {
1432+
pixelsPerScaleUnit = (linearMeterScale.getBounds().height - marginAbove - marginBelow - 1) / (Math.log10(linearMeterScale.getValueRange().getHigh()) - Math.log10(linearMeterScale.getValueRange().getLow()));
1433+
}
1434+
else {
1435+
pixelsPerScaleUnit = (linearMeterScale.getBounds().height - marginAbove - marginBelow - 1) / (linearMeterScale.getValueRange().getHigh() - linearMeterScale.getValueRange().getLow());
1436+
}
13861437
meterBreadth = Math.round(linearMeterScale.getBounds().width - marginLeft - marginRight);
13871438

1388-
double y_loLoRectangle = marginAbove + pixelsPerScaleUnit * (linearMeterScale.getValueRange().getHigh() - displayedLoLo);
1389-
double y_lowRectangle = marginAbove + pixelsPerScaleUnit * (linearMeterScale.getValueRange().getHigh() - displayedLow);
1390-
double y_normalRectangle = marginAbove + pixelsPerScaleUnit * (linearMeterScale.getValueRange().getHigh() - displayedHigh);
1391-
double y_highRectangle = marginAbove + pixelsPerScaleUnit * (linearMeterScale.getValueRange().getHigh() - displayedHiHi);
1439+
double y_loLoRectangle;
1440+
double y_lowRectangle;
1441+
double y_normalRectangle;
1442+
double y_highRectangle;
1443+
if (linearMeterScale.isLogarithmic()) {
1444+
y_loLoRectangle = marginAbove + pixelsPerScaleUnit * (Math.log10(linearMeterScale.getValueRange().getHigh()) - Math.log10(displayedLoLo));
1445+
y_lowRectangle = marginAbove + pixelsPerScaleUnit * (Math.log10(linearMeterScale.getValueRange().getHigh()) - Math.log10(displayedLow));
1446+
y_normalRectangle = marginAbove + pixelsPerScaleUnit * (Math.log10(linearMeterScale.getValueRange().getHigh()) - Math.log10(displayedHigh));
1447+
y_highRectangle = marginAbove + pixelsPerScaleUnit * (Math.log10(linearMeterScale.getValueRange().getHigh()) - Math.log10(displayedHiHi));
1448+
}
1449+
else {
1450+
y_loLoRectangle = marginAbove + pixelsPerScaleUnit * (linearMeterScale.getValueRange().getHigh() - displayedLoLo);
1451+
y_lowRectangle = marginAbove + pixelsPerScaleUnit * (linearMeterScale.getValueRange().getHigh() - displayedLow);
1452+
y_normalRectangle = marginAbove + pixelsPerScaleUnit * (linearMeterScale.getValueRange().getHigh() - displayedHigh);
1453+
y_highRectangle = marginAbove + pixelsPerScaleUnit * (linearMeterScale.getValueRange().getHigh() - displayedHiHi);
1454+
}
13921455

13931456
loLoRectangle = new Rectangle(marginLeft,
13941457
(int) Math.round(y_loLoRectangle),

0 commit comments

Comments
 (0)