我正在设计一个 android 应用程序,我想在其中绘制通过蓝牙接收的实时数据。我收到一个信号并对其进行处理以获得一些结果,我想实时显示它。我看到有各种用于绘制图表的 android 库。对于使用这样的库或使用 Javascript,我有点困惑。任何人都可以建议它是一个更好的选择吗?还有,要使用哪个 Android 库?
4 回答
有许多用于 android 的图表库,但每次我有需求时,我都会使用使用Canvas的 android 原生2D 图形框架。我从未寻找过其他选择。这很简单,你有很多控制权。好吧只是通知..
最好使用flot-android-chart来创建不同类型的图表。
或者你可以简单地使用achartengine
如果您想尝试在没有任何内置 jar 的情况下创建图表,只需看看这个Bar Chart in Android With out any Built in jars
(但它只是条形图)
这个 repo 看起来很有希望:Androidplot
它似乎支持从 gradle 导入,而不是在本地保存 jar 文件。我也考虑过 AnyChart,但它是一个付费使用的库,而 Androidplot 是在 Apache 2.0 许可下提供的。
自述文件截图:
只需从他们的快速入门指南中复制和粘贴,以防链接中断:
添加依赖
要在您的 gradle 项目中使用该库,请将以下内容添加到您的 build.gradle:
dependencies {
compile "com.androidplot:androidplot-core:1.5.7"
}
如果您使用 Proguard 混淆(默认情况下由 Android Studio 创建的项目),您还需要将其添加到您的 proguard-rules.pro 文件中:
-keep class com.androidplot.** { *; }
创建您的活动 XML 布局
创建 Android 项目框架后,创建res/layout/simple_xy_plot_example.xml 并添加一个 XYPlot 视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ap="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent">
<com.androidplot.xy.XYPlot
style="@style/APDefacto.Dark"
android:id="@+id/plot"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
ap:title="A Simple XY Plot"
ap:rangeTitle="range"
ap:domainTitle="domain"
ap:lineLabels="left|bottom"
ap:lineLabelRotationBottom="-45"/>
</LinearLayout>
此示例使用默认样式来装饰绘图。可在此处获得 XML 样式属性的完整列表。虽然会定期添加新属性,但并非所有可配置属性都可用。
如果缺少您需要的东西,请直接在 Plot 的 XML 中使用Fig Syntax ,并在每个属性前加上“androidPlot”。例子:
androidPlot.title="My Plot"
创建一个活动
现在让我们创建一个 Activity 来显示我们刚刚定义的 XYPlot simple_xy_plot_example.xml
。
基本步骤是:
- 创建一个 Series 实例并用要显示的数据填充它。
- 使用绘图实例注册一个或多个系列以及 Formatter 以描述系列在绘制时的外观。
- 绘制情节
由于我们正在处理 XY 数据,我们将使用 XYPlot、SimpleXYSeries(它是 XYSeries 接口的实现)和 LineAndPointFormatter:
import android.app.Activity;
import android.graphics.*;
import android.os.Bundle;
import com.androidplot.util.PixelUtils;
import com.androidplot.xy.SimpleXYSeries;
import com.androidplot.xy.XYSeries;
import com.androidplot.xy.*;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.util.*;
/**
* A simple XYPlot
*/
public class SimpleXYPlotActivity extends Activity {
private XYPlot plot;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_xy_plot_example);
// initialize our XYPlot reference:
plot = (XYPlot) findViewById(R.id.plot);
// create a couple arrays of y-values to plot:
final Number[] domainLabels = {1, 2, 3, 6, 7, 8, 9, 10, 13, 14};
Number[] series1Numbers = {1, 4, 2, 8, 4, 16, 8, 32, 16, 64};
Number[] series2Numbers = {5, 2, 10, 5, 20, 10, 40, 20, 80, 40};
// turn the above arrays into XYSeries':
// (Y_VALS_ONLY means use the element index as the x value)
XYSeries series1 = new SimpleXYSeries(
Arrays.asList(series1Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series1");
XYSeries series2 = new SimpleXYSeries(
Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");
// create formatters to use for drawing a series using LineAndPointRenderer
// and configure them from xml:
LineAndPointFormatter series1Format =
new LineAndPointFormatter(this, R.xml.line_point_formatter_with_labels);
LineAndPointFormatter series2Format =
new LineAndPointFormatter(this, R.xml.line_point_formatter_with_labels_2);
// add an "dash" effect to the series2 line:
series2Format.getLinePaint().setPathEffect(new DashPathEffect(new float[] {
// always use DP when specifying pixel sizes, to keep things consistent across devices:
PixelUtils.dpToPix(20),
PixelUtils.dpToPix(15)}, 0));
// just for fun, add some smoothing to the lines:
// see: http://androidplot.com/smooth-curves-and-androidplot/
series1Format.setInterpolationParams(
new CatmullRomInterpolator.Params(10, CatmullRomInterpolator.Type.Centripetal));
series2Format.setInterpolationParams(
new CatmullRomInterpolator.Params(10, CatmullRomInterpolator.Type.Centripetal));
// add a new series' to the xyplot:
plot.addSeries(series1, series1Format);
plot.addSeries(series2, series2Format);
plot.getGraph().getLineLabelStyle(XYGraphWidget.Edge.BOTTOM).setFormat(new Format() {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
int i = Math.round(((Number) obj).floatValue());
return toAppendTo.append(domainLabels[i]);
}
@Override
public Object parseObject(String source, ParsePosition pos) {
return null;
}
});
}
}
上面代码的一个可能令人困惑的部分是 LineAndPointFormatter 的初始化
您可能注意到它们对 xml 资源文件进行了神秘的引用。这实际上是使用Fig从 XML 配置实例属性。
如果您希望避免使用 XML 并将所有内容保留在 Java 中,只需替换代码:
LineAndPointFormatter series1Format =
new LineAndPointFormatter(this, R.xml.line_point_formatter_with_labels);
和:
LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);
一般来说,XML 配置应该尽可能地使用而不是编程配置,因为它在通过屏幕密度等定义属性方面产生了更大的灵活性。有关如何以编程方式配置格式化程序等的更多详细信息,请参阅最新的 Javadocs。
继续上面的原始示例,将这些文件添加到/res/xml目录中:
/res/xml/line_point_formatter_with_labels.xml
<?xml version="1.0" encoding="utf-8"?>
<config
linePaint.strokeWidth="5dp"
linePaint.color="#00AA00"
vertexPaint.color="#007700"
vertexPaint.strokeWidth="20dp"
fillPaint.color="#00000000"
pointLabelFormatter.textPaint.color="#CCCCCC"/>
/res/xml/line_point_formatter_with_labels_2.xml
<?xml version="1.0" encoding="utf-8"?>
<config
linePaint.strokeWidth="5dp"
linePaint.color="#0000AA"
vertexPaint.strokeWidth="20dp"
vertexPaint.color="#000099"
fillPaint.color="#00000000"
pointLabelFormatter.textPaint.color="#CCCCCC"/>