我是java新手,我需要帮助动态绘制相同活动的图表。我正在使用BLuetooth从我的Arduino接收数据。我修改了BLuetoothChat示例代码以适应我的。我唯一需要知道的是如何从String数组中提取值并动态绘制在图表上。我从使用AChartEngine的教程中获取了一些想法,直到我留下了这些代码:
在LineGraph.java中
package com.example.fypapp;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.Point;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.content.Context;
import android.graphics.Color;
public class LineGraph {
private GraphicalView view;
private TimeSeries dataset = new TimeSeries("Force Sensor Reading");
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
private XYSeriesRenderer renderer = new XYSeriesRenderer(); //used for customizing
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
@SuppressWarnings("deprecation")
public LineGraph()
{
//add single dataset to multiple dataset
mDataset.addSeries(dataset);
//Customization for line 1
renderer.setColor(Color.WHITE);
renderer.setPointStyle(PointStyle.CIRCLE);
renderer.setFillPoints(false);
//enable customization
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.TRANSPARENT);
mRenderer.setChartTitle("Force Sensor Readings From Device");
float textSize = 30, textSmaller = 20, angle = 270;
mRenderer.setChartTitleTextSize(textSize);
mRenderer.setZoomEnabled(true, false);
//mRenderer.setZoomButtonsVisible(true);
mRenderer.setXTitle("Time (s)");
mRenderer.setYTitle("Grip force (kgf)");
mRenderer.setAxisTitleTextSize(textSize);
mRenderer.setYLabelsAngle(angle);
mRenderer.setLabelsTextSize(textSize);
mRenderer.setChartValuesTextSize(textSize);
mRenderer.setLegendTextSize(textSmaller);
mRenderer.setLabelsColor(Color.rgb(112, 128, 144)); //slate grey color
mRenderer.setAxesColor(Color.YELLOW);
mRenderer.setGridColor(Color.YELLOW);
mRenderer.setShowGrid(true);
mRenderer.setPanEnabled(true, false);
//adjusting margin width of y-axis (so y axis labels can be seen)
int[] i = mRenderer.getMargins();
i[0] += 50;//top
i[1] += 50;//left (increment here)
//i[2] = 0;//bottom
//i[3] = 0;//right
mRenderer.setMargins(i);
//add single renderer to multiple renderer
mRenderer.addSeriesRenderer(renderer);
}
public GraphicalView getView(Context context)
{
view = ChartFactory.getLineChartView(context, mDataset, mRenderer);
return view;
}
public void addNewPoints(com.example.fypapp.Point p)
{
dataset.add(p.getX(), p.getY());
}
}
在Point.java中(LineGraph.java中的addNewPoints函数的类)
package com.example.fypapp;
public class Point {
private int x;
private int y;
public Point( int x, int y)
{
this.x = x;
this.y = y;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
}
老实说,我不太清楚为什么需要点类或如何使用它。
在我的主类中,在建立蓝牙连接时调用的处理程序中有一个名为MESSAGE_READ的案例。 MESSAGE_READ情况下的代码将继续循环,因为它是从另一个类的run()连续调用的:
case MESSAGE_READ:
readMessage = (String) msg.obj;
if(readMessage != null)
{
Log.i(TAG, "Data sent is " + readMessage);
lineGraphPlot(readMessage); //a function I want to implement that plots the graph
}
count++;
break;
readMessage是一个公共静态String,当调用MESSAGE_READ时,Arduino会更新数值数据。
我可以知道如何编写一个名为lineGraphPlot的函数,它将动态显示LineGraph图形上的readMessage值吗?提前谢谢。
答案 0 :(得分:0)
你必须看到achartengine的示例代码,你将从中获得所有信息 http://www.achartengine.org/content/goodies.html