我希望在x轴上有一个日期的散点图。
因为如果我使用ChartFactory.getTimeChartView
,我会收到一个折线图,问题是:
我将当前日期为x轴,数据(用户输入)为y轴。我将这些数据(日期和用户数据)存储在列表中,也存储在文件中。
用户可以在一天内输入多次数据。并且一些数据可能是相同的,所以 如果我使用Linegraph,情节就会混乱。这就是我需要分散的原因。
1)我该怎么做?
2)另外,有人能指出我在X轴上添加自定义标签的例子,以便使用Linegraph吗?
---------------------- 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);
答案 0 :(得分:1)
正如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(), dataset, 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();
}
}
}
答案 1 :(得分:0)
您可以通过以下方式添加自定义标签:
renderer.addXTextLabel(x, "label");
如果您想继续使用时间表方法,那么您可以这样做:
lineRenderer.setPointStyle(style);
lineRenderer.setLineWidth(0);
其中lineRenderer
是XYSeriesRenderer
内的XYMultipleSeriesRenderer
。
答案 2 :(得分:0)
关于图表的分散类型:
也许你可以为XYSeriesRenderer设置适当的参数:
XYSeriesRenderer r = new XYSeriesRenderer();
r.setColor(Color.GREEN);//green point
r.setPointStyle(PointStyle.CIRCLE);//point in shape of circle
r.setFillPoints(true);//enable points
r.setLineWidth(0);//set line to 0 width - invisible
mRenderer.addSeriesRenderer(r);
当你打电话给工厂时:
mChartView = ChartFactory.getTimeChartView(this, mDataset, mRenderer,"H:mm:ss");
我知道它不是干净和正确的方式,但我没有安卓在这里测试它或更详细地了解...
我没有测试过,所以请给我反馈:)。
希望它有所帮助, 托尼