我正在设计一个Android应用程序,我想在线性图表中绘制通过蓝牙接收的实时数据。
我尝试使用float-android-chart,acharengine和android plot,但我不能打印图表,然后打印相同的图表,使用不同的x轴值(秒)和不同的值序列。
我尝试了我在互联网上找到的所有东西,但没有工作。
你能给我看一个非常简单的重新绘制图表的代码,或者我修改这两个代码中的一个以重新绘制char?
用android plot:
public class MainActivity extends Activity {
private XYPlot plot;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xy_plot_chart);
chart();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
plot.clear();
chart();
plot.redraw();
}
private void chart() {
// TODO Auto-generated method stub
plot = (XYPlot) findViewById(R.id.xyPlot);
List s1 = getSeries(20, 10);
XYSeries series1 = new SimpleXYSeries(s1,
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series 1");
List s2 = getSeries(20, 10);
XYSeries series2 = new SimpleXYSeries(s2,
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series 2");
LineAndPointFormatter s1Format = new LineAndPointFormatter();
s1Format.setPointLabelFormatter(new PointLabelFormatter());
s1Format.configure(getApplicationContext(),
R.xml.lpf1);
plot.addSeries(series1, s1Format);
LineAndPointFormatter s2Format = new LineAndPointFormatter();
s2Format.setPointLabelFormatter(new PointLabelFormatter());
s2Format.configure(getApplicationContext(),
R.xml.lpf2);
plot.addSeries(series2, s2Format);
plot.setTicksPerRangeLabel(1);
plot.getGraphWidget().setDomainLabelOrientation(-45);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private List getSeries(int count, int max) {
List series = new ArrayList();
Random rand = new Random();
for (int i = 1; i <= count; i++) {
int value = rand.nextInt(max);
series.add(rand.nextInt(max));
}
return series;
}
}
或使用achartengine:
public class MainActivity extends Activity {
//int n =0;
int count = 5;
Date[] dt = new Date[5];
int [] prova = { 0,1,2,3,4 };
long timerStart = System.currentTimeMillis();
int timeout = 0;
long[] timerNow = {0,0,0,0,0};
int[] visits = { 2000,2500,2700,2100,2800};
int[] views = {2200, 2700, 2900, 2800, 3200};
private GraphicalView mChart;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openChart();
}
private void openChart(){
//while(n<5){
/*
for(int i=0;i<count;i++){
GregorianCalendar gc = new GregorianCalendar(2012, 10, i+1);
dt[i] = gc.getTime();
}*/
for(int i=0 ;i<count;i++){
timerNow[i] = System.currentTimeMillis();
try {Thread.sleep(50);
} catch (InterruptedException e) {e.printStackTrace();}
}
// Creating TimeSeries for Visits
TimeSeries timeSeriesVisits = new TimeSeries("Visits");
// Creating TimeSeries for Views
TimeSeries timeSeriesViews= new TimeSeries("Views");
timeSeriesVisits = new TimeSeries("Visits");
timeSeriesViews = new TimeSeries("Views");
// Adding data to Visits and Views Series
for(int i=0;i<dt.length;i++){
timeSeriesVisits.add(timerNow[i], visits[i]);
timeSeriesViews.add(timerNow[i],views[i]);
}
// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Visits Series to the dataset
dataset.addSeries(timeSeriesVisits);
// Adding Visits Series to dataset
dataset.addSeries(timeSeriesViews);
// 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("Tempo");
multiRenderer.setYTitle("Count");
multiRenderer.setZoomButtonsVisible(true);
// Adding visitsRenderer and viewsRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to multipleRenderer
// should be same
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");
//mChart = (GraphicalView) ChartFactory.getLineChartView(getBaseContext(), dataset, multiRenderer);
//mChart = (GraphicalView) ChartFactory.getTimeChartIntent(getBaseContext(), dataset, multiRenderer, );
//mChart= ChartFactory.getTimeChartView(getBaseContext(), dataset, multiRenderer, "HH-MM-SS");
mChart = ChartFactory.getTimeChartView(getBaseContext(), dataset, multiRenderer, "Consumption");
multiRenderer.setClickEnabled(true);
multiRenderer.setSelectableBuffer(10);
//Adding the Line Chart to the LinearLayout
//chartContainer.addView(mChart);
//dataset.addSeries(timeSeriesVisits);
//dataset.addSeries(timeSeriesViews);
//mChart = ChartFactory.getTimeChartView(getBaseContext(), dataset, multiRenderer, "Consumption");
mChart.refreshDrawableState();
//mChart.repaint();
setContentView(mChart);
//n++;
//}
// 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();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:0)
OK DONE:
http://wptrafficanalyzer.in/blog/android-dynamic-chart-using-achartengine/
public class MainActivity extends Activity {
private GraphicalView mChart;
private XYSeries visitsSeries ;
private XYMultipleSeriesDataset dataset;
private XYSeriesRenderer visitsRenderer;
private XYMultipleSeriesRenderer multiRenderer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setting up chart
setupChart();
// Start plotting chart
new ChartTask().execute();
}
private void setupChart(){
// Creating an XYSeries for Visits
visitsSeries = new XYSeries("Unique Visitors");
// Creating a dataset to hold each series
dataset = new XYMultipleSeriesDataset();
// Adding Visits Series to the dataset
dataset.addSeries(visitsSeries);
// Creating XYSeriesRenderer to customize visitsSeries
visitsRenderer = new XYSeriesRenderer();
visitsRenderer.setColor(Color.BLUE);
visitsRenderer.setPointStyle(PointStyle.CIRCLE);
visitsRenderer.setFillPoints(true);
visitsRenderer.setLineWidth(2);
visitsRenderer.setDisplayChartValues(true);
// Creating a XYMultipleSeriesRenderer to customize the whole chart
multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setChartTitle("Visits Chart");
multiRenderer.setXTitle("Seconds");
multiRenderer.setYTitle("Count");
multiRenderer.setZoomButtonsVisible(true);
multiRenderer.setXAxisMin(0);
multiRenderer.setXAxisMax(100);
multiRenderer.setYAxisMin(0);
multiRenderer.setYAxisMax(10);
multiRenderer.setBarSpacing(1);
// Adding visitsRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(visitsRenderer);
// Getting a reference to LinearLayout of the MainActivity Layout
LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);
//mChart = (GraphicalView) ChartFactory.getBarChartView(getBaseContext(), dataset, multiRenderer, Type.DEFAULT);
mChart = (GraphicalView) ChartFactory.getLineChartView(getBaseContext(), dataset, multiRenderer);
// Adding the Line Chart to the LinearLayout
chartContainer.addView(mChart);
}
private class ChartTask extends AsyncTask<Void, String, Void>{
// Generates dummy data in a non-ui thread
@Override
protected Void doInBackground(Void... params) {
int i = 0;
try{
do{
String [] values = new String[2];
Random r = new Random();
int visits = r.nextInt(10);
values[0] = Integer.toString(i);
values[1] = Integer.toString(visits);
publishProgress(values);
Thread.sleep(1000);
i++;
}while(i<=100);
}catch(Exception e){ }
return null;
}
// Plotting generated data in the graph
@Override
protected void onProgressUpdate(String... values) {
visitsSeries.add(Integer.parseInt(values[0]), Integer.parseInt(values[1]));
mChart.repaint();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}