How can i show date & time in the X Axis (domain axis) of the graph - androidplot

I have a graph in which i want to show Date-Time in the X Axis and some values in the Y Axis.
I am wondering how I can show the date and time on the X Axis.
The Format in which i want to show is: dd/mm/yyyy hh:mm
My X Axis Data is :
Number[] xaxisSeries = {2407141120,2507141220,2607141640,2707141850,2807142027,2907142333};
plot.setDomainValueFormat(new SimpleDateFormat("dd/mm/yy hh:mm"));
But its not showing the correct values.
Please help me how should i show the Date-Time on my X Axis of the Graph

Here's what worked for me, based on this example. In my case, I wanted to show time only, and the x values were millis since the epoch.
mGraphView.getGraph().getLineLabelStyle(XYGraphWidget.Edge.BOTTOM)
.setFormat(new Format() {
private final DateFormat dateFormat = DateFormat.getTimeInstance();
#Override
public StringBuffer format(Object obj,
#NonNull StringBuffer toAppendTo,
#NonNull FieldPosition pos) {
Number timestamp = (Number) obj;
return dateFormat.format(timestamp, toAppendTo, pos);
}
#Override
public Object parseObject(String source, #NonNull ParsePosition pos) {
return null;
}
});

There's a tutorial on androidplot.com that describes the process. Additionally, here's a similar question which includes another example of how it's done.

Related

Generate histogram from JavaRDD

I am trying to write code for converting data in Java RDD to a histogram so that I can bin the data in a certain way. For example, for the data I want to create a histogram of sizes such that I can find out which bin contains how many entries of a certain size range. I am able to get the value in different RDD's but I am not sure what I am missing here.
Is there an easier way to do this?
0 - 1 GB - 2 entries
1 - 5GB - 4 entries
and so on
EntryWithSize {
long size;
String entryId;
String groupId;
}
JavaRDD<EntryWithSize> entries = getEntries();
JavaRDD<HistoSize> histoSizeJavaRDD = entryJavaRDD.keyBy(EntryWithSize::getGroupId)
.combineByKey(
HistoSize::new,
(HistoSize h, EntryWithSize y) -> h.mergeWith(new HistoSize(y)),
HistoSize::mergeWith
).values();
#Data
#AllArgsConstructor
static class HistoSize implements Serializable {
int oneGB;
int fiveGB;
public HistoSize(EntryWithSize entry) {
addSize(entry);
}
private void addSize(EntryWithSize entry) {
long size = entry.getSize();
if (size <= ONE_GB) {
oneGB++;
} else {
fiveGB++;
}
}
public HistoSize mergeWith(HistoSize other) {
oneGB += other.oneGB;
fiveGB += other.fiveGB;
return this;
}
}
I was able to get it working by using a reduce on final pair rdd. My test data was wrong which was causing red herring in the output.
Function2<HistoSize, HistoSize, HistoSize> reduceSumFunc = (a, b) -> (new HistoSize(
a.oneGB + b.oneGB,
a.fiveGB + b.fiveGB,
));
HistoSize finalSize = histoSizeJavaRDD.reduce(reduceSumFunc);

Array of dates returns object System datetime not the actual value of the array

Question is I have a function call "GetDatesBetween" what this does is looks at my two datepickers leave_s_dp and leave_e_dp and gets the dates between them. I then want to turn this array into a string for use in my full_range text box, But it always returns System.DateTime[]. Any help would be greatly appreciated.
public:
List<DateTime> ^GetDatesBetween(DateTime startDate, DateTime endDate)
{
List<DateTime> ^allDates = gcnew List<DateTime>();
for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
{
allDates->Add(date.Date);
}
return allDates;
}
private:
System::Void submit_button_Click(System::Object^ sender, System::EventArgs^ e) {
array<DateTime> ^dates = GetDatesBetween(leave_s_dp->Value.Date, leave_e_dp->Value.Date)->ToArray();
//array<DateTime> ^dates = GetDatesBetween(leave_s_dp->Value, leave_e_dp->Value)->ToArray();
String ^ days_between = dates->ToString();
full_range_text->Text = days_between;
}
You're calling ToString() on an array. That doesn't do what you expect it to. It's not clear exactly what you do expect it to do, but it's almost certainly not what you want.
You quite possibly want to call string.Join, e.g.
dates_between = String::Join(", ", dates);
That will just use the default date format though - which may not be what you want either.

Apache Spark Streaming: Median of windowed PairDStream by key

I want to calculate the median value of a PairDStream for the values of each key.
I already tried the following, which is very unefficient:
JavaPairDStream<String, Iterable<Float>> groupedByKey = pairDstream.groupByKey();
JavaPairDStream<String, Float> medianPerPlug1h = groupedByKey.transformToPair(new Function<JavaPairRDD<String,Iterable<Float>>, JavaPairRDD<String,Float>>() {
public JavaPairRDD<String, Float> call(JavaPairRDD<String, Iterable<Float>> v1) throws Exception {
return v1.mapValues(new Function<Iterable<Float>, Float>() {
public Float call(Iterable<Float> v1) throws Exception {
List<Float> buffer = new ArrayList<Float>();
long count = 0L;
Iterator<Float> iterator = v1.iterator();
while(iterator.hasNext()) {
buffer.add(iterator.next());
count++;
}
float[] values = new float[(int)count];
for(int i = 0; i < buffer.size(); i++) {
values[i] = buffer.get(i);
}
Arrays.sort(values);
float median;
int startIndex;
if(count % 2 == 0) {
startIndex = (int)(count / 2 - 1);
float a = values[startIndex];
float b = values[startIndex + 1];
median = (a + b) / 2.0f;
} else {
startIndex = (int)(count/2);
median = values[startIndex];
}
return median;
}
});
}
});
medianPerPlug1h.print();
Can somebody help me with a more efficient transaction? I have about 1950 different keys, each can get to 3600 (1 data point per second, window of 1 hour) values, where to find the median of.
Thank you!
First thing is that I don't know why are you using Spark for that kind of task. It doesn't seem to be related to big data considering you got just few thousand of values. It may make things more complicated. But let's assume you're planning to scale up to bigger datasets.
I would try to use some more optimized algorithm for finding median than just sorting values. Sorting an array of values runs in O(n * log n) time.
You could think about using some linear-time median algorithm like Median of medians
1) avoid using groupbykey; reducebykey is more efficient than groupbykey.
2) reduceByKeyAndWindow(Function2,windowduration,slideDuration) can serve you better.
example:
JavaPairDStream merged=yourRDD.reduceByKeyAndWindow(new Function2() {
public String call(String arg0, String arg1) throws Exception {
return arg0+","+arg1;
}
}, Durations.seconds(windowDur), Durations.seconds(slideDur));
Assume output from this RDD will be like this :
(key,1,2,3,4,5,6,7)
(key,1,2,3,4,5,6,7).
now for each key , you can parse this , you will have the count of values,
so : 1+2+3+4+5+6+7/count
Note: i used string just to concatenate.
I hope it helps :)

How do I assign a String at Array1[x] to an int at Array2[x]?

I'm trying to organize data I am given from a text file, there are for 4 pieces of info on each line (City, country, population, and date). I wanted to have an array for each so I first put it all into one big String array and started to separate them into 4 arrays but I needed to change the Population info to an int array but it says *
"Type mismatch: cannot convert from element type int to String"
//Separate the information by commas
while(sc.hasNextLine()){
String line = sc.nextLine();
input = line.split(",");
//Organize the data into 4 seperate arrays
for(int x=0; x<input.length;x++){
if(x%4==0){
cities[x] = input[x];
}
if(x%4==1){
countries[x] = input[x];
}
if(x%4==2){
population[x] = Integer.parseInt(input[x]);
}
if(x%4==3){
dates[x] = input[x];
}
}
}
And when I print out the arrays they have a bunch of nulls in between each data. I'm planning to create objects that have the 4 pieces of data so that I can then sort them by population, dates etc... I'm pretty new to working with objects so if anyone has a better way of getting the 4 pieces of data into an object cause I haven't figured a way yet :/ My end goal was to have an array of these objects that I can u different sorting methods on them
I would recommend doing something like this:
public class MyData {
private String city;
private String country;
private Integer population;
private String date;
public MyData(String city, String, country, Integer population, String date) {
this.city = city;
this.country = country;
this.population = population;
this.date = date;
}
// Add getters and setters here
}
And then in the file you're posting about:
...
ArrayList<MyData> allData = new ArrayList<MyData>();
while(sc.hasNextLine()) {
String[] values = sc.nextLine().split(",");
allData.add(new MyData(values[0], values[1], Integer.parseInt(values[2]), values[3]));
}
...
You need an object to store the data in so that you keep the relationship between the values in each column.
Also, I'm just assuming you're using Java here. Which language we're talking about is something you should include in your question or as a tag.
The problem is with your x index. If you look carefully at your "for" you will see that it will insert a value at every 3 positions.
try
int index = 0;
while(sc.hasNextLine()){
String line = sc.nextLine();
input = line.split(",");
//Organize the data into 4 seperate arrays
for(int x=0; x<input.length;x++){
if(x%4==0){
cities[index] = input[x];
}
if(x%4==1){
countries[index] = input[x];
}
if(x%4==2){
population[index] = Integer.parseInt(input[x]);
}
if(x%4==3){
dates[index] = input[x];
}
}
++index;
}

I am beginner in j2me.In J2me Ticker function, How to apply differnt Color in Single Ticker?

*I am developing one j2me-Lwuit Project for Nokia s40 devices.I have some problem abuot ticker. I have apply Only one color for tiker.But i want differnt color to apply for single ticker.This is my code for Ticker:
Ticker tick;
String tickerText=" ";
Label lblIndice=new Label();
Label ticker=new Label("");
for (int i = 0; i < tickerIndiceData.size(); i++)
{
tickerText +=" "+tickerIndiceData.elementAt(i).toString();
tickerText +=" "+tickerValueData.elementAt(i).toString();
tickerText +=" "+"("+tickerChangeData.elementAt(i).toString()+")";
lblIndice.setText(" "+tickerIndiceData.elementAt(i).toString());
lblValue.setText(" "+tickerValueData.elementAt(i).toString());
double val=Double.parseDouble(tickerChangeData.elementAt(i).toString());
if(val>0)
{
ticker.getStyle().setFgColor(0X2E9F37);
}
else
{
ticker.getStyle().setFgColor(0XFF0000);
}
lblChange.setText(" "+"("+val+")");
}
System.out.println("TICKER==="+tickerText);
ticker.setText(tickerText);
ticker.getStyle().setFont(Font.createSystemFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_SMALL));
ticker.startTicker(50, true);*
LWUIT doesn't support different colors for a label (hence ticker) since that would require quite a bit of processing.
Implementing a ticker from scratch in LWUIT is pretty easy though. Just derive label and override paint as such:
public void paint(Graphics g) {
UIManager.getInstance().setFG(g, this);
Style style = l.getStyle();
Font f = style.getFont();
boolean isTickerRunning = l.isTickerRunning();
int txtW = f.stringWidth(text);
// update this to draw two strings one with the color that's already set and the
// other with the color you want
g.drawString(getText(), getShiftText() + getX(), getY(),style.getTextDecoration());
}

Resources