android-在x轴上带有日期的散点图—在X轴上添加自定义标签
作者:互联网
我想在x轴上绘制带有日期的散点图.
因为如果我使用ChartFactory.getTimeChartView我会收到一个折线图,问题是:
我将当前日期作为x轴,将数据(用户输入)作为y轴进行绘图,并将这些数据(日期和用户数据)存储在列表中以及文件中.
用户可能在一天内输入多次数据.某些数据可能相同,因此
如果我使用线图,则情节混乱.这就是为什么我需要散布的原因.
1)我该怎么做?
2)另外,有人可以指出一个在X轴上添加自定义标签以便使用线图的示例吗?
———————- UPDATE ————————— —–
我现在正在尝试执行此操作(不过我想知道是否可以按照上面的问题进行操作)
我将日期存储为列表字符串:
List<String> dates_asString=new ArrayList<String>();
我保存它们:
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");
在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);
解决方法:
如Dan in his answer所述,您可以使用renderer.addXTextLabel(x,“ text”);设置x轴标签.
您的情况::您在注释行上输入了错误,
您在哪里尝试两次添加x轴标签.
如下所示进行更改
TimeSeries series = new TimeSeries("Showing data");
for (int i=0;i<date_asDates.size();i++){
series.add(i,data.get(i)); // only this change ll work as u needed
}
更新:将您的代码更改为跟随它不会在添加新数据时崩溃
如果您只希望可见点,则不能与该点成线性关系,则更改此ChartFactory.getLineChartIntent(getBaseContext(),dataset,mRenderer,“ dd / MM / yyyy”);到ChartFactory.getScatterChartIntent(getBaseContext(),数据集,mRenderer,“ dd / MM / yyyy”);
MainActivity.java
package com.example.trying;
import java.io.BufferedWriter;
import java.io.File; // imports add all as before too
public class MainActivity extends Activity implements OnClickListener{
View savebtn;
View graphicsbtn;
EditText value,weight;
String filename = "mydata.csv";
public static List<String> dates_Strings = new ArrayList<String>();
public static List<Double> data = new ArrayList<Double>();
public static List<Date> date = new ArrayList<Date>();
List<Double> mydata=new ArrayList<Double>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
date.clear();
data.clear();
dates_Strings.clear();
//Set up click listeners
savebtn=(View) findViewById(R.id.savebtn);
savebtn.setOnClickListener(this);
graphicsbtn=(View) findViewById(R.id.graphicsbtn);
graphicsbtn.setOnClickListener(this);
value=(EditText) findViewById(R.id.enter_data);
}
//called when a button is clicked
public void onClick(View v) {
switch (v.getId()){
case R.id.savebtn:
savefunc();
break;
case R.id.graphicsbtn:
Intent i = new Intent(this,LineGraph.class);
startActivity(i);
break;
}
}
public void savefunc(){
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date();
String formattedDate=thedate.format(d);
Log.d("tag","format"+formattedDate);
dates_Strings.add(formattedDate);
try{
double thedata=Double.parseDouble(value.getText().toString().trim());
mydata.add(thedata);
Log.d("tag","thedata :"+thedata);
} catch (NumberFormatException e){
String message="Sorry you did't type anything";
Toast toast = Toast.makeText(getBaseContext(), message,Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100);
toast.show();
// Toast.makeText(getBaseContext(), "Sorry you did't type anything", Toast.LENGTH_SHORT).show();
}
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard, "MyFiles");
directory.mkdirs();
File file = new File(directory, filename);
FileOutputStream fos;
//saving them
try {
fos = new FileOutputStream(file);
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");
}
value.setText("");
bw.flush();
bw.close();
} catch (IOException e2) {
e2.printStackTrace();
}//catch
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
LineGraph.java
package com.example.trying;
import java.io.BufferedReader;
import java.io.File; // add all other imports
public class LineGraph extends MainActivity {
String filename = "mydata.csv";
private LinearLayout layout;
private GraphicalView mChartView;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.graph);
layout = (LinearLayout) findViewById(R.id.chart);
readfunc();
//trying to copy the dates_asString to date (Dates) 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_Strings.size();k++){
convertedDate = formatter.parse(dates_Strings.get(k));
date.add(convertedDate);
Log.d("line","convertedDate :"+convertedDate);
}
}catch (ParseException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
TimeSeries series = new TimeSeries("Showing data");
for (int i=0;i<dates_Strings.size();i++){
series.add(i,data.get(i)); //series.add(date.get(i),data.get(i));
}
XYMultipleSeriesDataset dataset=new XYMultipleSeriesDataset();
dataset.addSeries(series);
//renderer for decay
XYSeriesRenderer renderer =new XYSeriesRenderer();
renderer.setColor(Color.YELLOW);
renderer.setLineWidth(3);
renderer.setPointStyle(PointStyle.SQUARE);
XYMultipleSeriesRenderer mRenderer =new XYMultipleSeriesRenderer();
mRenderer.addSeriesRenderer(renderer);
mRenderer.setXTitle("Date");
mRenderer.setYTitle("Data");
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.BLACK);
mRenderer.setMarginsColor(Color.BLACK);
mRenderer.setXRoundedLabels(false);
mRenderer.setShowCustomTextGrid(true);
mRenderer.setXLabels(0);
for (int i=0;i<date.size();i++){
mRenderer.addXTextLabel(i,dates_Strings.get(i));
}
//mChartView = ChartFactory.getTimeChartView(this, dataset, mRenderer,"dd/MM/yyyy");
//mChartView = ChartFactory.getScatterChartView(this, dataset, mRenderer);
//layout.addView(mChartView);
Intent intent= ChartFactory.getLineChartIntent(getBaseContext(), dataset, mRenderer,"dd/MM/yyyy");
startActivity(intent);
}
public void readfunc(){
SimpleDateFormat thedate = new SimpleDateFormat("dd/MM/yyyy");
Date d=new Date();
String formattedDate=thedate.format(d);
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard, "MyFiles");
File file = new File(directory, filename);
String s;
FileInputStream fis;
try {
fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
do {
s = br.readLine();
if (s != null ){
String[] splitLine = s.split(",");
data.add(Double.parseDouble(splitLine[0]));
//dates_Strings.add(thedate.parse(splitLine[1]));
dates_Strings.add(formattedDate);
}
} while (s != null );
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
标签:achartengine,android 来源: https://codeday.me/bug/20191010/1883529.html