java-org.hwo.ui/src/org/hwo/ui/diagram/TimePlotLabeler.java

66 lines
1.7 KiB
Java

package org.hwo.ui.diagram;
import org.hwo.ui.JDiagram;
public class TimePlotLabeler implements PlotLabeler{
@Override
public String getOrdinateLabel(JDiagram diagram, int ordinate, Double value) {
if (value == null)
return "";
if (value.isNaN())
return "";
Double window = diagram.getScaler(ordinate).getWindow();
int digits = ((Double)Math.log10(window)).intValue();
digits -= 4;
if (digits < 0){
return String.format(
String.format("%%.%df",-digits),
value
);
} else if (digits == 0){
return String.format("%1.2f",value);
} else {
return String.format(
String.format("%%%d.2f",digits),
value
);
}
}
@Override
public String getAbzisseLabel(JDiagram diagram, Double pos) {
return doubleSecToString(pos);
}
public static String doubleSecToString(Double pos) {
//pos == seconds
int hour, minute, second, millisecond;
hour = (int)(pos / 3600);
minute = ((int)(pos / 60) % 60);
second = (int)(pos * 1) % 60;
millisecond = (int)(pos * 1000) % 1000;
String sh, sm, ss, sms;
sh = String.format("%02d", hour);
sm = String.format("%02d", minute);
ss = String.format("%02d", second);
sms = String.format("%03d", millisecond);
return sh + ":" + sm + ":" + ss + "." + sms;
}
public static Double stringToDoubleSec(String stringTime) {
double ret = 0;
String val = stringTime.substring(0, 2);
ret = Double.parseDouble(val) * 3600.0; //hour
val = stringTime.substring(3,5);
ret += Double.parseDouble(val) * 60.0; //minutes
val = stringTime.substring(6,8);
ret += Double.parseDouble(val); //seconds
val = stringTime.substring(9,12);
ret += Double.parseDouble(val) / 1000.0; //milliseconds
return ret;
}
}