I have a temperature value from the Bluetooth device.
StringBuilder [] temp = new StringBuilder[1000];
it is not empty, it includes temperature value, for example, temp[1]=20, temp[2]=30 like this. But every one second this array change with news so my string is dynamic. I want to draw graph time-temp (x: time, y: temp) if I saw firstly temp[1] and draw ( a mean real-time graph). But the mean problem is, how can I draw the graph with the values (using Graphview library), I have StringBuilder and how to read them for the graph. ? Please help me I don't find any answer
Related
I'm developing fitness application where I would like to display chart of user running activities over months, so something like this:
Graph how I want it
I'm using MPAndroidChart for it, but I'm getting some weird values on the x axis (the y axis is alright).
I have a map (monthMileage in the code sample), where the key is the month and the year in string, so for example Jul 2020 as "072020" and the value is the total distance of km which user ran that month.
profileViewModel.monthOverview.observe(viewLifecycleOwner, Observer { monthMileage ->
val entries = ArrayList<BarEntry>()
for ((k, v) in monthMileage) {
entries.add(BarEntry(k, v.toFloat()))
}
val dataSet = BarDataSet(entries, "Monthly mileage")
val barData = BarData(dataSet)
activity_graph.xAxis.labelCount = entries.size
activity_graph.xAxis.valueFormatter = ActivityGraphFormatter()
activity_graph.axisRight.axisMinimum = 0.toFloat()
activity_graph.axisLeft.axisMinimum = 0.toFloat()
activity_graph.data = barData
activity_graph.invalidate()
})
This is how I'm trying to set data to the Bar Chart. The x value is the month-year string to Float and y is the total distance. I use custom value formatter, which looks weird, but should be working with the right values (and the values are still wrong even when I don't use the formatter so there's error probably somewhere else). In the whole process the values in the entries, data set and data are still the values I'm expecting, but when it's set to the graph it's just wrong.
For example for 72020 and 62020 I'm getting 65000 and 70000.
There's probably some correlation I don't see so I welcome all advicess or if anybody knows about some nicer chart library for android mobile app, I've been searching, but all the time everybody is just praising MPAndroidChart.
Having a list of objects that contain latitude and longitude values, I'd like to create a WKT String to have a POLYGON WKT in my Android application. This POLYGON will be send later to the server among other data as part of a json.
I know it's possible to do it using ArcGIS lib, but this solution one seems too robust for my usage. This would be the only usage of the lib in my app and adding this lib increased in 30Mb the size of the apk. I was expecting to find a more lightweight solution for this matter.
This question addresses the exact oposite of what I need. There are a couple libs mentioned on the answers but, checking their documentation, neither seems to create a WKT, only being able to create coordinates based on a WKT String instead.
How can I create a WKT String, having a list of latitude/longitude coordinates?
You didn't specify how the latitude/longitude coordinates are stored, so I assume they are stored in a double array containing interleaved latitude/longitude coordinates. The following function generates a polygon WKT from the array, assuming the polygon contains no holes.
public static String polygonWkt(double[] latlon){
if(latlon.length%2 != 0)
throw new IllegalArgumentException("latlon length is not even");
StringBuilder builder=new StringBuilder();
builder.append("POLYGON((");
for(int i=0;i<latlon.length;i+=2){
if(i>0){
builder.append(",");
}
builder.append(latlon[i]+" "+latlon[i+1]);
}
builder.append("))");
return builder.toString();
}
Also posted in GitHub Gist.
I'ms setting custom text labels on my line chart, using this on the renderer:
String[] xLabelsForTimeScale = getXLabelsForCurrentTimeScale(); //weekday names
for (int i = 0; i < xLabelsForTimeScale.length; i++)
{
mRenderer.addXTextLabel(i, xLabelsForTimeScale[i]);
}
mRenderer.setXLabels(0);
mRenderer.setShowCustomTextGridX(true);
when the chart renders, it's only displaying x labels for points where I have a non null series value. For example, assume that the xLabelsForTimeScale array is an array of weekday abbreviations. If I have only two values, say for Mon, Tue, it only shows labels for Mon and Tue. I'd like to display all the x axis labels, whether there's a series point or not. Sun - Sat, basically. How can I do that?
ADDITIONAL INFO ON REQUIREMENT: I don't want to add zero, dummy points within the series if I can at all help it. What I expect to happen is the line just renders between actual points, connecting each actual, valid point, in a linear fashion. If I add zero, dummy points, there will be a lot of pointy tops (lack of a better term) as the line draws between valid point data, and placeholder, dummy data. Hope that makes sense. Thanks.
You can avoid this for time charts by calling:
renderer.setXRoundedLabels(false);
I have a for loop that is looping through an array of rectangles as they appear. For each new rectangle that is added to the array a sprite is drawn on top of it. I want each sprite drawn on top of the rectangles to have its own variable that changes the sprite drawing. While on the screen mostly every rectangle should have a different sprite drawn on it ------------------------------------I cant figure out how to give each sprite its own variable with out giving the same one to all of the other sprites. This code ends up drawing the new same sprite on every rectangle. Here is what i have so far..
int arrayplace = 0;
for(Rectangle rain: rectangleArray) {
numberArray.add(arrayPlace, MathUtils.random(3);
//adds a variable to a certain spot on the array
// picks random# within range given
arrayplace++;
if (numberArray.get(arrayPlace)==1){
spritebatch.draw(spriteOne, rain.x, rain.y);
};
if (numberArray.get(arrayPlace)==2){
spritebatch.draw(spriteTwo, rain.x, rain.y);
}
if (numberArray.get(arrayPlace)==3){
spritebatch.draw(spriteThree, rain.x, rain.y);
}
}
The mistake is:
arrayplace++;
You increment arrayplace too early, so it points to an index behind the value you added.
The way random generators work, they generate a series of numbers based on a "seed" value. The first number in the series is going to be the same given the same seed. Are you using your random function correctly? Most likely the value of "10" you are passing to the random method is the seed. Do you need to call some other function to give you the next random number? Check you random function documentation. Here is the Java random doc : http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Random.html#Random(long) Refer to the usage of seed parameter to the constructor and to the method next() or nextInt()
Going further, I think you need to change the "seed" to the random generator every time you enter the loop, instead of using a constant 10.
I guess the reason you are getting the same random number is that you are initializing the class in each loop. so, you are generating the first number which will be the same each time.
Try to initialize MathUtils object outside the for-loop:
MathUtils m = new MathUtils();
And inside the loop, use this line to generate a number:
PointlessRect.x = m.random(10);
Hope this will work
I use actually the AndroidPlot library to use a simple chart in my android project but I don't know how i can change the values of domain zone.
More specific, this line:
mySimpleXYPlot.setDomainValueFormat(new DecimalFormat("#"));
In the web site said that i can use another formats and its true, but if I use for example:
SimpleDateFormat("dd-MM-yyyy")
In the chart appears "31-12-1969" in all domain values.
Somebody know how I can change that date? or use another format (like String)?
Late answer, but maybe will be useful to other people.
I also had a hard time with this issue, especially since I am Java beginner.
I implemented a custom formatter. Seems a bit ugly solution but it works. Idea is the following:
take only Y axis for values, and leave X axis as indexes into an array (as Androidplot tutorial suggests, use using ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value)
each time your data changes, you need to pass to the Plot a new formatter with your new data for X axis
Here are some code snippets.
First is the class which transforms array index to a custom label String:
public class MyIndexFormat extends Format {
public String[] Labels = null;
#Override
public StringBuffer format(Object obj,
StringBuffer toAppendTo,
FieldPosition pos) {
// try turning value to index because it comes from indexes
// but if is too far from index, ignore it - it is a tick between indexes
float fl = ((Number)obj).floatValue();
int index = Math.round(fl);
if(Labels == null || Labels.length <= index ||
Math.abs(fl - index) > 0.1)
return new StringBuffer("");
return new StringBuffer(Labels[index]);
}
And here is how I attach it to the Plot:
MyIndexFormat mif = new MyIndexFormat ();
mif.Labels = // TODO: fill the array with your custom labels
// attach index->string formatter to the plot instance
pricesPlot.getGraphWidget().setDomainValueFormat(mif);
This trick works also for dynamic updates.
NOTICE: Androidplot seems to have problems with drawing horizontal lines, so if your data has the same Y values, you might get strange results, I already asked for help on this issue.