I want to have a scatter plot with dates in x-axis.
Because if I use ChartFactory.getTimeChartView
I receive a line graph and the problem is:
I make plot with current date as x axis and data (user input) as y axis.I store these data (date and user data) in Lists and also in a file.
The user may enter multiple times data during a day.And some of the data may be the same,so if I use a Linegraph the plot messes up.That's why I need scatter.
1)How can I do this?
2) Also, can someone point me to an example of adding custom labels on the X axis,in order to use a Linegraph?
----------------------UPDATE--------------------------------
I am trying to do this right now (i would like to know if it could be done as my questions above though)
I store dates as List String :
List<String> dates_asString=new ArrayList<String>();
I save them:
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date();
String formattedDate=thedate.format(d);
dates_asString.add(formattedDate);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (int i=0;i<mydata.size();i++){
bw.write(mydata.get(i)+","+dates_asString.get(i)+"\n");//+"\n");
In LineGraph:
private static List<String> dates_asString = new ArrayList<String>();
private static List<Double> data = new ArrayList<Double>();
private static List<Date> dates_asDates = new ArrayList<Date>();
loadfunc();
//trying to copy the dates_asString to dates_asDates in order to use them in TimeSeries
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date convertedDate=new Date();
try{
for (int k=0;k<dates_asString.size();k++){
convertedDate = formatter.parse(dates_asString.get(k));
date_asDates.add(convertedDate);
}
}catch (ParseException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
TimeSeries series = new TimeSeries("Showing data");
for (int i=0;i<date_asDates.size();i++){
series.add(date_asDates.get(i),data.get(i));
}
mRenderer.setXLabels(0);
for (int i=0;i<dates_asString.size();i++){
mRenderer.addXTextLabel(i,dates_asString.get(i));
}
public void loadfunc(){
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date();
String formattedDate=thedate.format(d);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
....
data.add(Double.parseDouble(splitLine[0]));
dates_asString.add(formattedDate);
About scatter type of chart:
Maybe you could just set proper parameters to XYSeriesRenderer:
and when you are calling factory:
I'm aware that it is not clean and proper way, but I don't have android here to test it or to figure out more into details...
I didn't test it, so give feed back to me please :).
Hope it helps, Toni
As told by Dan in his answer you can use
renderer.addXTextLabel(x, "text");
to set x-axis labels.In your case:: u made mistake at commented line , You where trying to add x-axis labels twice..
change it as shown below
UPDATE: Change Your code to following it wont crash on adding new data's
and if u want only points to be visible not linealong with point then change this
ChartFactory.getLineChartIntent(getBaseContext(), dataset, mRenderer,"dd/MM/yyyy");
toChartFactory.getScatterChartIntent(getBaseContext(), dataset, mRenderer,"dd/MM/yyyy");
MainActivity.java
LineGraph.java
You can add custom labels this way:
If you want to keep using the time chart approach then you can do this:
where
lineRenderer
is aXYSeriesRenderer
inside yourXYMultipleSeriesRenderer
.