I have an activity where I take the input from edit text and store it in an list.
I also store in list the current date.
Then , I press the save button which saves the above.
The next day the user enter some data more and save and so on.
I want to make a plot with x-axis date format and y axis the values the user entered.
In one activity I have:
...
String filename = "data.csv";
List<Double> mydata=new ArrayList<Double>();
List<Date> mydate=new ArrayList<Date>();
....value=(EditText) findViewById(R.id.enter_data);
...
switch (v.getId()){
case R.id.savebtn:
savefunc();
break;
case R.id.graphicsbtn:
Intent i = new Intent();
i.setClassName(this,LineGraph.class.getName());
this.startActivity(i);
break;
public void savefunc(){
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date();
try{
d=thedate.parse(filename);
mydate.add(d);
}
catch (ParseException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
double thedata=Double.parseDouble(value.getText().toString().trim());
mydata.add(thedata);
..
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (int i=0;i<mydate.size();i++){
bw.write(mydate.get(i)+","+mydata.get(i)+"\n");
...
In the LineGraph Activity:
public class LineGraph extends Activity {
private static List<Date> date = new ArrayList<Date>();
private static List<Double> data = new ArrayList<Double>();
public Intent getIntent(Context context){
readfunc();
TimeSeries series = new TimeSeries("Showing data");
for (int i=0;i<date.size();i++){
series.add(date.get(i),data.get(i));
}
The read function:
public void readfunc(){
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date();
try{
d=thedate.parse(filename);
}
catch..
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
do {
s = br.readLine();
if (s != null ){
String[] splitLine = s.split(",");
date.add(d);//Double.parseDouble(splitLine[0]));
data.add(Double.parseDouble(splitLine[1]));
I have these problems:
1) The file I receive is empty (some problem with the Date because the method for saving and reading from a file works).
2) At the graph screen appears a white background (of course no data because the file is empty) ,but why white background?I use the same code for other purposes and I don't receive a whitebackground.
3) I am not sure how to use Dates in x axis.Should I use List ? List ? .
------------------------UPDATE---------------------------------------------------------
Ok ,finally!(After user 'Dan' suggestion)
I used ChartFactory.getTimeChartView(this, dataset, mRenderer,"dd/MM/yyyy");
instead of ChartFactory.getLineChartIntent(context, dataset, mRenderer,"dd/MM/yyyy");
and you don't need to use String List , just Date List
The code that deal with your file must be something like this (not compiled):
public void savefunc(){
List<String> myDate = new ArrayList<String>(); //To store the formatted dates
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date(); //the current date
String sd = thedate.format(d); // sd contains "16/04/2013", the formatted date
myDate.add(sd);
double thedata=Double.parseDouble(value.getText().toString().trim());
mydata.add(thedata);
...
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
for (int i=0;i<mydate.size();i++){
bw.write(mydate.get(i)+","+mydata.get(i)+"\n");
}
}
public void readfunc(){
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d;
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
do {
s = br.readLine();
if (s != null ){
String[] splitLine = s.split(","); //first substring is the formatted date
date.add(thedate.parse(splitLine[0])); //do something with exception
data.add(Double.parseDouble(splitLine[1]));
...
Hope it helps.
for dynamic plots use GraphicalView rather then intent::
public GraphicalView mChartView;
creat a xml::
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:id="@+id/graph"
android:layout_width="fill_parent"
android:layout_height="145dip" >
</LinearLayout>
</RelativeLayout>
then in ur code :
LinearLayout layout = (LinearLayout) findViewById(R.id.graph);
mChartView = ChartFactory.mChartView = ChartFactory.getLineChartView(getBaseContext(), dataset, renderer)
layout.addView(mChartView);
After entering values you read from edit text fields right:
then you are passing the new values to add in respective arraylists
then you should call the code for linegraph again after adding new values and remember to clear series before reading arraylists (ie.
dataset.clear();
) ie. when line code function starts at beginning add dataset.clear(); because if u dont clear data will overlap and may through exception or line may look thick at old data ...
then call mChartView.repaint();
to refresh graph
来源:https://stackoverflow.com/questions/16014103/achartengine-cant-figure-how-to-use-dates-as-x-axis-the-file-i-save-is-empt