Give a linechart XAxis academic month order - javafx

I have created a simple line chart where I compare the current years values to past years (could be an average or not).
The issue is that as this is for UK Academic purposes I want the months to run from June through to June the following year.
Currently my code looks like:
xAxis.setLabel("Date/Month");
xAxis.setTickLabelRotation(90);
xAxis.setAutoRanging(false);
xAxis.setLowerBound(1);
xAxis.setUpperBound(12);
xAxis.setTickUnit(1.0);
xAxis.setTickLabelFormatter(new StringConverter<Number>() {
#Override
public String toString(Number month) {
logger.debug("Converting " + month);
return new DateFormatSymbols().getMonths()[month.intValue()-1];
}
#Override
public Number fromString(String string) {
try {
Date date = new SimpleDateFormat("MMMM").parse(string);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH);
} catch (ParseException ex) {
logger.error("Parse Exception");
}
return 0;
}
});
yAxis.setLabel("Units");
where the series are created using a simple function:
private XYChart.Series createSeries(String name, List<AccountUtilities> data) {
XYChart.Series series = new XYChart.Series();
series.setName(name);
for (AccountUtilities utility : data) {
String[] dateParts = utility.getStartdate().split("-");
Double dateCode = Double.valueOf(dateParts[1]);
series.getData().add(new XYChart.Data( dateCode , utility.getGasunits()));
}
return series;
}
My current thought is that I can reindex the months as June = 1, July = 2 etc. to get the values into the chart in the correct order and then decode them into their 'correct' values June = 6 when I get setTickLabelFormatter to do its thing but this seems a long way around.
Is there a nifty feature of javafx charting that I'm missing?
Current resources used:
javafx line chart with date axis
How to perform big numbers on axis (for example, 10^3 format) in chart?

So I followed my initial thought process and although it was more complicated than I initially thought with any offset that wasn't 6 months the following works for both 6 and 8.
The following method reorders Jan(1) to Dec(12) to the offsetted order e.g. Jun(1) to May(12) so the data appears in the desired order in the chart.
private Double getAcademicMonth(String baseDate) {
Integer dateCode = Integer.valueOf(baseDate);
//Any month greater than the accounting start month gets shifted by six.
if (dateCode > offset) { dateCode = dateCode - offset; }
else { dateCode = dateCode + (12-offset); }
return dateCode;
}
I then required a means of converting the months back on the fly when the chart was drawn so I got the correct month labels, which was achieved with setTickLabelFormatter
//We have to set tick label formatter now once the offset has been retrieved.
xAxis.setTickLabelFormatter(new StringConverter<Number>() {
#Override
public String toString(Number month) {
//Shift our months to account for the accounting start month
int dateCode = month.intValue();
if (dateCode <= (12 - offset) ) { dateCode = dateCode + offset; }
else { dateCode = dateCode - (12-offset); }
Month day = Month.of(dateCode);
return day.getDisplayName(TextStyle.SHORT, Locale.ENGLISH);
}
#Override
public Number fromString(String string) {
DateTimeFormatter parser = DateTimeFormatter.ofPattern("MMMM").withLocale(Locale.ENGLISH);
TemporalAccessor accessor = parser.parse(string);
return accessor.get(ChronoField.MONTH_OF_YEAR);
}
});

Related

Dart/Flutter How to compare two TimeOfDay times?

TimeOfDay documentation has no comparison operator and primitive comparison does not work. My only solution that I can thinking of right now is to convert TimeOfDay to DateTime and use DateTime's difference method.
Does anyone have a better solution?
Convert it to a double then compare.
double toDouble(TimeOfDay myTime) => myTime.hour + myTime.minute/60.0
extension TimeOfDayExtension on TimeOfDay {
int compareTo(TimeOfDay other) {
if (hour < other.hour) return -1;
if (hour > other.hour) return 1;
if (minute < other.minute) return -1;
if (minute > other.minute) return 1;
return 0;
}
}
Thanks from #Lucas idea, you can calculate hour and minute by
TimeOfDay yourTime ;
TimOfDay nowTime = TimeOfDay.now()
double _doubleYourTime = yourTime.hour.toDouble() +
(yourTime.minute.toDouble() / 60);
double _doubleNowTime = nowTime.hour.toDouble() +
(nowTime.minute.toDouble() / 60);
double _timeDiff = _doubleYourTime - _doubleNowTime;
double _hr = _timeDiff.truncate();
double _minute = (_timeDiff - _timeDiff.truncate()) * 60;
print('Here your Happy $_hr Hour and also $_minute min');
I calculated the difference by turning both values into minute-counts, and comparing those :)
TimeOfDay now = TimeOfDay.now();
int nowInMinutes = now.hour * 60 + now.minute;
TimeOfDay testDate = TimeOfDay(hour: 2, minute: 20);
int testDateInMinutes = testDate.hour * 60 + testDate.minute;
You can use this method. Where you have to provide starttime and endTime in TimesofDay format.
getTime(startTime, endTime) {
bool result = false;
int startTimeInt = (startTime.hour * 60 + startTime.minute) * 60;
int EndTimeInt = (endTime.hour * 60 + endTime.minute) * 60;
int dif = EndTimeInt - startTimeInt;
if (EndTimeInt > startTimeInt) {
result = true;
} else {
result = false;
}
return result;
}
LikeThis
getTime(v1, v2);
TimeOfDay n = TimeOfDay.now();
int nowSec = (n.hour * 60 + n.minute) * 60;
int veiSec = (t.hour * 60 + t.minute) * 60;
int dif = veiSec - nowSec;
Card(
child: ListTile(
onTap: () {
showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
).then((TimeOfDay time) {
double _doubleyourTime =
time.hour.toDouble() + (time.minute.toDouble() /60);
double _doubleNowTime = TimeOfDay.now().hour.toDouble() +
(TimeOfDay.now().minute.toDouble() / 60);`enter code here`
if (_doubleyourTime > _doubleNowTime) {
print('correct format')
});
} else {
print('Sorry You can not set the time')
}
});
},
//dense: true,
leading: Icon(Icons.timer),
title: Text(
'Today On Time',`enter code here`
),
),
),
We can actually use the subtract operator.
Code to make magic happen :
Here I wanted to get the difference in time after the user selects the time (in TimeOfDay format) using showTimePicker()
// current time will be used to find the difference between the time selected by the user.
TimeOfDay _cur_time = TimeOfDay(hour: DateTime.now().hour, minute: DateTime.now().minute);
// scheduled time will be updated as soon as the user inputs a new time using the showTimePicker() function available in Flutter Material librabry.
TimeOfDay _scheduled_time = TimeOfDay(hour: DateTime.now().hour, minute: DateTime.now().minute);
// toDouble Function to convert time to double so that we can compare time and check that the time selected is greater than current time.
double toDouble(TimeOfDay myTime) => myTime.hour + myTime.minute / 60.0;
void _selectTime() async {
Flutter Material widget to select time.
final TimeOfDay? newTime = await showTimePicker(
context: context,
initialTime: _scheduled_time,
);
//Check if the selected time is greater than the cur time
if (toDouble(newTime!) > toDouble(_cur_time)) {
setState(() {
_scheduled_time = newTime;
});
}
}
Function to get the difference between cur time and selected time.
Duration _getDelayedDuration(){
var hourDelay = _scheduled_time.hour - _cur_time.hour;
print(hourDelay);
var minuteDelay = _scheduled_time.minute - _cur_time.minute;
print(minuteDelay);
return Duration(hours: hourDelay, minutes: minuteDelay);
}
Solution for negative duration calculations
All these answers are pretty good but they didn't help me when I had a user select a range of times in my app. In order to calculate the total duration of the specified time period, I tried all the solutions which work pretty well but fail in certain scenarios. The scenarios are:
When the start time comes after the end time (i.e- when the duration is supposed to be over 12 hours)
When the start time is before 12am at the night and end time is after that
And the code to overcome it is:
String durationFromTimeOfDay(TimeOfDay? start, TimeOfDay? end) {
if (start == null || end == null) return '';
// DateTime(year, month, day, hour, minute)
final startDT = DateTime(9, 9, 9, start.hour, start.minute);
final endDT = DateTime(9, 9, 10, end.hour, end.minute);
final range = DateTimeRange(start: startDT, end: endDT);
final hours = range.duration.inHours % 24;
final minutes = range.duration.inMinutes % 60;
final _onlyHours = minutes == 0;
final _onlyMinutes = hours == 0;
final hourText = _onlyMinutes
? ''
: '$hours${_onlyHours ? hours > 1 ? ' hours' : ' hour' : 'h'}';
final minutesText = _onlyHours
? ''
: '$minutes${_onlyMinutes ? minutes > 1 ? ' mins' : ' min' : 'm'}';
return hourText + minutesText;
}
It is important to note that you need to prefill the DateTime for end TimeOfDay with a day value which is greater than the same in start DateTime. The other parameters (for year and month) can be anything you want.
This outputs a really nicely formatted string that is short, concise, and extremely legible
This, however, doesn't satisfy the requirement that the solution is devoid of conversion to DateTime. But at least it uses a different approach over the difference method. And this makes the correct duration calculation more reliable in a few lines of code comparatively.
I don't think this is possible. You can use .subtract in DateTime as also .difference

How to show desired number of setTickLabelsVisible false in line chart javafx

I am trying to build a time series line chart for stock market data. I am getting the input dynamically, and its been plotted on the line chart.
All I want is to plot every tick data on the graph but want to show only desired number of tick labels.
Ex: Say I started to build the graph on 10 AM, and i am plotting on 3 sec, so after 4 hours, i.e 1 PM, i want every tick to be plotted but the the label on Xaxis should be of my choice, say only 4 tick labels (10AM,11AM,12AM,1PM).
Kindly see the desired graph:
Thanks a lot in advance. :)
Code:
#FXML
private void buttonGraph(ActionEvent event) throws ParseException{
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime initTime = LocalTime.now();
Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
Session session = cluster.connect("pass_master");
lineChart.getData().clear();
ResultSet results2 = session.execute("select count(*) as ct from pass8 where tkn ="+basic.get(tkn.getText())+" and timestmp <= "+etDate+" and timestmp >= "+stDate);
String total = null;
for( Row row : results2){
total = row.toString();
}
System.out.println(total);
System.out.println(total.substring(4, total.length()-1));
int totalrows = Integer.valueOf(total.substring(4, total.length()-1));
System.out.println("This is int value : "+totalrows);
XYChart.Series<String,Double> series = new XYChart.Series<>();
y.setAutoRanging(false);
y.setSide(Side.RIGHT);
if (graphUpdater == null) {
graphUpdater =new Thread(new Runnable() {
double min = 10000000;
double max = 0;
String chartData ="";
String invisible = "";
int i = 0;
XYChart.Data item;
public void run() {
while(running) {
try {
Quote quote = MulticastReciever.getInstance().getQuote(tkn.getText());
if (quote!=null) {
LocalDateTime now = LocalDateTime.now();
double value = quote.getLast();
if (value < min) {
min = value;
}
if (value > max) {
max = value;
}
i++;
a1.add(value);
if (i % 5 == 0 ){
chartData = dtf.format(now);
x.setTickLabelFill(Color.RED);
item = new XYChart.Data<>(chartData, value);
}
else {
invisible += (char)32;
chartData = dtf.format(now);
x.setTickMarkVisible(false);
item = new XYChart.Data<>(chartData, value);
}
series.getData().add(item);
lineChart.setCreateSymbols(false);
lineChart.setLegendVisible(false);
y.setLowerBound(min-(.001*min));
y.setUpperBound(max+(.001*max));
y.setTickLabelFill(Color.RED);
x.setTickMarkVisible(false);
y.setTickMarkVisible(false);
}
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
graphUpdater.start();
}
System.out.println("previousToken:"+previousToken+", GC:"+this);
int currentToken = Integer.valueOf(basic.get(tkn.getText()));
if (previousToken!=null) {
System.out.println("Going to unsubscribe token:"+previousToken);
MulticastReciever.getInstance().unsubscribeMarketData(previousToken);
}
MulticastReciever.getInstance().subscribeMarketData(tkn.getText(), currentToken);
previousToken = currentToken;
int factor=10;
if(totalrows>300){
factor =totalrows/300;
}
else
{
factor=1;
}
System.out.println(factor);
int count = 0;
x.setTickLabelRotation(45.0);
System.out.println("This is the property: "+x.isTickLabelsVisible());
x.setTickLabelGap(5000.0);
System.out.println("getTickLabelGap:"+x.getTickLabelGap());
x.setTickLabelGap(10.0);
System.out.println("getTickLabelGap:"+x.getTickLabelGap());
lineChart.getData().addAll(series);
a1.clear();
for(final XYChart.Data<String,Double> data :series.getData()){
data.getNode().addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
lbl.setText("X :"+data.getXValue()+"\nY :"+ String.valueOf(data.getYValue()));
Tooltip.install(data.getNode(), new Tooltip("X :"+data.getXValue()+"\nY :"+ String.valueOf(data.getYValue())));
}
});
}
cluster.close();
}

How to take data simultaneously from multiple Android device sensor and write to data stream?

I'm using an android device to retrieve data from the accelerometer and light sensors. I'm writing the data to an OutputStream which sends the data to an Arduino over Bluetooth. I want to create a string like the one below-
"X Value(Accelerometer), Y Value(Accelerometer),Light-Sensor value"
But the problem is I can't get two value arrays from the same SensorEvent. It only seems to contain accelerometer values. This is what I have till now...
#Override
public void onSensorChanged(SensorEvent event) {
actualTime = System.currentTimeMillis();
if(event.sensor.getType() == Sensor.TYPE_GRAVITY) {
int x = (int) event.values[0];
int y = (int) event.values[1];
String grav = x + "," + y;
if (mConnectedThread != null) {
if(actualTime - lastUpdate > 500) {
mConnectedThread.write(grav.getBytes());
lastUpdate = actualTime;
}
}
}
else if(event.sensor.getType() == Sensor.TYPE_LIGHT) {
actualTime = System.currentTimeMillis();
float value = event.values[0];
if(mConnectedThread != null) {
if(actualTime - lastUpdate > 500) {
String val = "" + value;
mConnectedThread.write("click".getBytes());
lastUpdate = actualTime;
}
}
}
In the If you are just sending the x and y values from your Sensor.TYPE_GRAVITY event.
In the Else you are sending the word "click" encoded as bytes when the Sensor.TYPE_LIGHT event occurs. It does not seem likely that this is what you want
As you are triggering on 2 different events it's difficult to see how you could send all the data at once unless you store events, combine them later and then send.

Kinect skeleton Scaling strange behaviour

I am trying to scale a skeleton to match to the sizes of another skeleton.
My algoritm do the following:
Find the distance between two joints of the origin skeleton and the destiny skeleton using phytagorean teorem
divide this two distances to find a multiply factor.
Multiply each joint by this factor.
Here is my actual code:
public static Skeleton ScaleToMatch(this Skeleton skToBeScaled, Skeleton skDestiny)
{
Joint newJoint = new Joint();
double distanciaOrigem = 0;
double distanciaDestino = 0;
double fator = 1;
SkeletonPoint pos = new SkeletonPoint();
foreach (BoneOrientation bo in skToBeScaled.BoneOrientations)
{
distanciaOrigem = FisioKinectCalcs.Distance3DBetweenJoint(skToBeScaled.Joints[bo.StartJoint], skToBeScaled.Joints[bo.EndJoint]);
distanciaDestino = FisioKinectCalcs.Distance3DBetweenJoint(skDestiny.Joints[bo.StartJoint], skDestiny.Joints[bo.EndJoint]);
if (distanciaOrigem > 0 && distanciaDestino > 0)
{
fator = (distanciaDestino / distanciaOrigem);
newJoint = skToBeScaled.Joints[bo.EndJoint]; // escaling only the end joint as the BoneOrientatios starts from HipCenter, i am scaling from center to edges.
// applying the new values to the joint
pos = new SkeletonPoint()
{
X = (float)(newJoint.Position.X * fator),
Y = (float)(newJoint.Position.Y * fator),
Z = (float)(newJoint.Position.Z * fator)
};
newJoint.Position = pos;
skToBeScaled.Joints[bo.EndJoint] = newJoint;
}
}
return skToBeScaled;
}
Every seems to work fine except for the hands and foots
Look at this images
I have my own skeleton over me, and my skeleton scaled to the sizes of another person, but the hands and foots still crazy. (but code looks right)
Any suggestion?
It's hard to say without running the code, but it somewhat "looks good".
What I would validate though, is your
if (distanciaOrigem > 0 && distanciaDestino > 0)
If distanciaOrigem is very close to 0, but even just epsilon away from 0, it won't be picked up by the if, and then
fator = (distanciaDestino / distanciaOrigem);
Will result in a very large number!
I would suggest to smooth the factor so it generally fits the proper scale. Try this code:
private static Dictionary<JointType, double> jointFactors = null;
static CalibrationUtils()
{
InitJointFactors();
}
public static class EnumUtil
{
public static IEnumerable<T> GetValues<T>()
{
return Enum.GetValues(typeof(T)).Cast<T>();
}
}
private static void InitJointFactors()
{
var jointTypes = EnumUtil.GetValues<JointType>();
jointFactors = new Dictionary<JointType, double>();
foreach(JointType type in jointTypes)
{
jointFactors.Add(type, 0);
}
}
private static double SmoothenFactor(JointType jointType, double factor, int weight)
{
double currentValue = jointFactors[jointType];
double newValue = 0;
if(currentValue != 0)
newValue = (weight * currentValue + factor) / (weight + 1);
else
newValue = factor;
jointFactors[jointType] = newValue;
return newValue;
}
When it comes to factor usage just use the SmoothenFactor method first:
public static Skeleton ScaleToMatch(this Skeleton skToBeScaled, Skeleton skDestiny, double additionalFactor = 1)
{
Joint newJoint = new Joint();
double distanceToScale = 0;
double distanceDestiny = 0;
double factor = 1;
int weight = 500;
SkeletonPoint pos = new SkeletonPoint();
Skeleton newSkeleton = null;
KinectHelper.CopySkeleton(skToBeScaled, ref newSkeleton);
SkeletonPoint hipCenterPosition = newSkeleton.Joints[JointType.HipCenter].Position;
foreach(BoneOrientation bo in skToBeScaled.BoneOrientations)
{
distanceToScale = Distance3DBetweenJoints(skToBeScaled.Joints[bo.StartJoint], skToBeScaled.Joints[bo.EndJoint]);
distanceDestiny = Distance3DBetweenJoints(skDestiny.Joints[bo.StartJoint], skDestiny.Joints[bo.EndJoint]);
if(distanceToScale > 0 && distanceDestiny > 0)
{
factor = (distanceDestiny / distanceToScale) * additionalFactor;
newJoint = skToBeScaled.Joints[bo.EndJoint]; // escaling only the end joint as the BoneOrientatios starts from HipCenter, i am scaling from center to edges.
factor = SmoothenFactor(newJoint.JointType, factor, weight);
pos = new SkeletonPoint()
{
X = (float)((newJoint.Position.X - hipCenterPosition.X) * factor + hipCenterPosition.X),
Y = (float)((newJoint.Position.Y - hipCenterPosition.Y) * factor + hipCenterPosition.Y),
Z = (float)((newJoint.Position.Z - hipCenterPosition.Z) * factor + hipCenterPosition.Z)
};
newJoint.Position = pos;
newSkeleton.Joints[bo.EndJoint] = newJoint;
}
}
return newSkeleton;
}
I also modified your ScaleToMatch method as you see. There was a need to move joints in relation to HipCenter position. Also new positions are saved to a new Skeleton instance so they are not used in further vector calculations.
Experiment with the weight but since our bones length is constant you can use big numbers like 100 and more to be sure that wrong Kinect readings do not disturb the correct scale.
Here's an example of how it helped with scaling HandRight joint position:
The weight was set to 500. The resulting factor is supposed to be around 2 (because the base skeleton was purposely downscaled by a factor of 2).
I hope it helps!

Path planning to get close to an unreachable target

I'm working on a game which has tank battles on a tiled map. If a tank is on a cell, that cell is considered unpassable in the A* algorithm, therefore, whenever an unit needs to attack another, I need to plan a path which brings the attacker into range (if range=1, then next to the target).
Currently, I use an iterative approach with increasing radius to find a path to a nearby cell and choose a cell which minimizes the A-Cell-B distance. Unfortunately, this is slow for one unit, not to mention for 50 units.
Is there a way to extract a partial path from a regular A* search data structures?
Just for reference, here is the implementation I have.
Set<T> closedSet = U.newHashSet();
Map<T, T> cameFrom = U.newHashMap();
final Map<T, Integer> gScore = U.newHashMap();
final Map<T, Integer> hScore = U.newHashMap();
final Map<T, Integer> fScore = U.newHashMap();
final Comparator<T> smallestF = new Comparator<T>() {
#Override
public int compare(T o1, T o2) {
int g1 = fScore.get(o1);
int g2 = fScore.get(o2);
return g1 < g2 ? -1 : (g1 > g2 ? 1 : 0);
}
};
Set<T> openSet2 = U.newHashSet();
List<T> openSet = U.newArrayList();
gScore.put(initial, 0);
hScore.put(initial, estimation.invoke(initial, destination));
fScore.put(initial, gScore.get(initial) + hScore.get(initial));
openSet.add(initial);
openSet2.add(initial);
while (!openSet.isEmpty()) {
T current = openSet.get(0);
if (current.equals(destination)) {
return reconstructPath(cameFrom, destination);
}
openSet.remove(0);
openSet2.remove(current);
closedSet.add(current);
for (T loc : neighbors.invoke(current)) {
if (!closedSet.contains(loc)) {
int tentativeScore = gScore.get(current)
+ distance.invoke(current, loc);
if (!openSet2.contains(loc)) {
cameFrom.put(loc, current);
gScore.put(loc, tentativeScore);
hScore.put(loc, estimation.invoke(loc, destination));
fScore.put(loc, gScore.get(loc) + hScore.get(loc));
openSet.add(loc);
Collections.sort(openSet, smallestF);
openSet2.add(loc);
} else
if (tentativeScore < gScore.get(loc)) {
cameFrom.put(loc, current);
gScore.put(loc, tentativeScore);
hScore.put(loc, estimation.invoke(loc, destination));
fScore.put(loc, gScore.get(loc) + hScore.get(loc));
Collections.sort(openSet, smallestF);
}
}
}
}
return Collections.emptyList();
A solution that seems to work (replacing the last return Collections.emptyList();):
// if we get here, there was no direct path available
// find a target location which minimizes initial-L-destination
if (closedSet.isEmpty()) {
return Pair.of(false, Collections.<T>emptyList());
}
T nearest = Collections.min(closedSet, new Comparator<T>() {
#Override
public int compare(T o1, T o2) {
int d1 = trueDistance.invoke(destination, o1);
int d2 = trueDistance.invoke(destination, o2);
int c = U.compare(d1, d2);
if (c == 0) {
d1 = trueDistance.invoke(initial, o1);
d2 = trueDistance.invoke(initial, o2);
c = U.compare(d1, d2);
}
return c;
}
});
return Pair.of(true, reconstructPath(cameFrom, nearest));
Where the trueDistance gives the eucleidian distance of two points. (The base algorithm uses a simpler function yielding 1000 for X-X or YY neightbor, 1414 for XY neighbor).

Resources