问题
(I fixed some issues with saving and loading regarding my previous posts)
I am saving some data (mydata) and the date as strings (dates_Strings).
In graph activity I load data and dates.And I convert dates_Strings to dates in order to use them in the plot.
Now , while entering some data for example "1","2","3" in 10/05/13 I am getting the following image.
All good until now.
If I try to enter some more data ("3,4,7") in another date (13/05/13) I am getting this image.
The dates are superimposed.
The MainActivity :
The code:
//copy the dates_asString to date (Dates) in order to use them in TimeSeries
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy",Locale.US);
Date convertedDate;
try{
for (int k=0;k<mydata.size();k++){
convertedDate = formatter.parse(dates_Strings.get(k));
date.clear();
date.add(convertedDate);
Log.d("line","convertedDate :"+convertedDate);
}
}catch ...
//TimeSeries series = new TimeSeries("Showing data");
//for (int i=0;i<mydata.size();i++){
// series.add(i,mydata.get(i));
//}
XYSeries series = new XYSeries("Showing data");
for (int i=0;i<mydata.size();i++){
series.add(i,mydata.get(i));
}
XYMultipleSeriesDataset dataset=new XYMultipleSeriesDataset();
dataset.addSeries(series);
XYSeriesRenderer renderer =new XYSeriesRenderer();
renderer.setColor(Color.YELLOW);
...
XYMultipleSeriesRenderer mRenderer =new XYMultipleSeriesRenderer();
mRenderer.addSeriesRenderer(renderer);
...
mRenderer.setXRoundedLabels(false);
mRenderer.setXLabels(0);
//mRenderer.setXAxisMax(mydata.size());
for (int i=0;i<mydata.size();i++){
mRenderer.addXTextLabel(i,dates_Strings.get(i));
}
I put in setXAxisMax the value 14/05/2013 and I entered data in 10/05 and 13/05 for example.
When I use setXAxisMin the plot is completely empty.
Another weird problem occurs if I don't use setXAxisMax . I enter "1" and "2" as data but the plot shows the "2" (second data) in two points. :
------------------UPDATE----------------------------------------
Ok, I think the problem lies during saving the data in the file.
The user enters some data (mydata) in an edittext field and then presses the save button which saves that data together with the current date.
For example , user enters "1" and presses "save".
user enters "2" and presses "save".
So, I have data "1" and "2" in the same date (08/05/13).
I want to save one instance of the date (because it is the same date) in order to have one point (date) in x axis and 2 points (1 and 2) in y axis.
I am saving as:
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (int i=0;i<mydata.size();i++){
bw.write(mydata.get(i)+","+dates_Strings.get(i)+"\n");
}
I thought sth like:
for (int i=1;i<mydata.size();i++){
bw.write(mydata.get(i)+",");
while (!(dates_Strings.get(i).equals(dates_Strings.get(i-1))))
bw.write(dates_Strings.get(i)+"\n");
}
but it saves only the last entered data..
回答1:
This looks like a wrong usage of the TimeSeries
. You are calling series.add(i, something)
where something
is mydata.get(i)
in your code. This means that the method add(double x, double y)
inherited by TimeSeries
from XYSeries
is called, so x
in your case ranges between 0
and mydata.size() - 1
.
Just make sure you are correctly calling the add(Date x, double value)
method in TimeSeries
.
来源:https://stackoverflow.com/questions/16418225/dates-in-x-axis-are-superimposed-plot-shows-the-last-entered-data-2-times-whe