![在这里输入图像描述] [1]我正在使用achartengine绘制android中的图形,我想在snapshoot中添加起始结束日期。使用日期启用此代码并保存数据.Achar引擎基本上用于图表所以帮助mei如何实现这个
public class MainActivity extends Activity {
private GraphicalView mChart;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openChart();
}
private void openChart(){
int count = 5;
Date[] dt = new Date[5];
for(int i=0;i<count;i++){
GregorianCalendar gc = new GregorianCalendar(2012, 10, i+1);
dt[i] = gc.getTime();
}
int[] visits = { 2000,2500,2700,2100,2800};
int[] views = {2200, 2700, 2900, 2800, 3200};
// Creating TimeSeries for Visits
TimeSeries visitsSeries = new TimeSeries("Visits");
// Creating TimeSeries for Views
TimeSeries viewsSeries = new TimeSeries("Views");
// Adding data to Visits and Views Series
for(int i=0;i<dt.length;i++){
visitsSeries.add(dt[i], visits[i]);
viewsSeries.add(dt[i],views[i]);
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Visits Series to the dataset
dataset.addSeries(visitsSeries);
// Adding Visits Series to dataset
dataset.addSeries(viewsSeries);
// Creating XYSeriesRenderer to customize visitsSeries
XYSeriesRenderer visitsRenderer = new XYSeriesRenderer();
visitsRenderer.setColor(Color.WHITE);
visitsRenderer.setPointStyle(PointStyle.CIRCLE);
visitsRenderer.setFillPoints(true);
visitsRenderer.setLineWidth(2);
visitsRenderer.setDisplayChartValues(true);
// Creating XYSeriesRenderer to customize viewsSeries
XYSeriesRenderer viewsRenderer = new XYSeriesRenderer();
viewsRenderer.setColor(Color.YELLOW);
viewsRenderer.setPointStyle(PointStyle.CIRCLE);
viewsRenderer.setFillPoints(true);
viewsRenderer.setLineWidth(2);
viewsRenderer.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setChartTitle("Visits vs Views Chart");
multiRenderer.setXTitle("Days");
multiRenderer.setYTitle("Count");
multiRenderer.setZoomButtonsVisible(true);
// Adding visitsRenderer and viewsRenderer to multipleRenderer
multiRenderer.addSeriesRenderer(visitsRenderer);
multiRenderer.addSeriesRenderer(viewsRenderer);
// Getting a reference to LinearLayout of the MainActivity Layout
LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);
// Creating a Time Chart
mChart = (GraphicalView) ChartFactory.getTimeChartView(getBaseContext(), dataset, multiRenderer,"dd-MMM-yyyy");
multiRenderer.setClickEnabled(true);
multiRenderer.setSelectableBuffer(10);
// Setting a click event listener for the graph
mChart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Format formatter = new SimpleDateFormat("dd-MMM-yyyy");
SeriesSelection seriesSelection = mChart.getCurrentSeriesAndPoint();
if (seriesSelection != null) {
int seriesIndex = seriesSelection.getSeriesIndex();
String selectedSeries="Visits";
if(seriesIndex==0)
selectedSeries = "Visits";
else
selectedSeries = "Views";
// Getting the clicked Date ( x value )
long clickedDateSeconds = (long) seriesSelection.getXValue();
Date clickedDate = new Date(clickedDateSeconds);
String strDate = formatter.format(clickedDate);
// Getting the y value
int amount = (int) seriesSelection.getValue();
// Displaying Toast Message
Toast.makeText(
getBaseContext(),
selectedSeries + " on " + strDate + " : " + amount ,
Toast.LENGTH_SHORT).show();
}
}
});
// Adding the Line Chart to the LinearLayout
chartContainer.addView(mChart);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
![Here is a image ][2]
答案 0 :(得分:0)
在x轴上添加minumim和最大日期(如果它是你想要的):
multiRenderer.setXLabels(0); //in order to define custom labels
multiRenderer.setXAxisMin(initial_date.size());
multiRenderer.setXAxisMax(final_date.size());
其中final_date必须在您上次约会后的某个日期。
更新:
您必须使用:multiRenderer.addXTextLabel(1,"10/04");
你可以循环(假设日期是一个包含日期的列表):
for (int i=0;i<dates.size();i++){
multiRenderer.addXTextLabel(i,dates.get(i));
}
但你必须使用multiRenderer.setXLabels(0);在上面之前为了定义你的标签。