【发布时间】:2012-10-15 05:08:45
【问题描述】:
我正在设计一个 android 应用程序,我想在其中绘制通过蓝牙接收的实时数据。 我收到一个信号并对其进行处理以获得一些结果,我想实时显示它。我看到有各种用于绘制图表的 android 库。对于使用这样的库或使用 Javascript,我有点困惑。任何人都可以建议它是一个更好的选择吗?还有,要使用哪个 Android 库?
【问题讨论】:
我正在设计一个 android 应用程序,我想在其中绘制通过蓝牙接收的实时数据。 我收到一个信号并对其进行处理以获得一些结果,我想实时显示它。我看到有各种用于绘制图表的 android 库。对于使用这样的库或使用 Javascript,我有点困惑。任何人都可以建议它是一个更好的选择吗?还有,要使用哪个 Android 库?
【问题讨论】:
有许多用于 android 的图表库,但每次我有需求时,我都会使用 android native 2D graphics 框架和 Canvas。我从未寻找过其他选择。这很简单,你有很多控制权。好吧,只是通知..
最好使用flot-android-chart 来创建不同类型的图表。
或者你可以简单地使用achartengine
如果您想尝试在没有任何内置 jar 的情况下创建图表,请查看Bar Chart in Android With out any Built in jars(但它只是条形图)
【讨论】:
如果您想要 Android 的实时图表,那么 fastest Android Chart 库当前为 SciChart。
有一篇performance comparison 文章将 5 个开源和商业图表在实时条件下并驾齐驱,在所有测试中,SciChart 名列前茅,有时甚至有相当大的优势!
这使得 SciChart 适用于real-time trading apps、medical apps、scientific apps,甚至是运行 Android 作为操作系统的嵌入式系统。
披露:我是 SciChart 项目的技术负责人,你知道吗!
【讨论】:
这个 repo 看起来很有希望:Androidplot
它似乎支持从 gradle 导入,而不是在本地保存 jar 文件。我也考虑过 AnyChart,但它是一个付费使用的库,而 Androidplot 是在 Apache 2.0 许可下提供的。
自述文件截图:
只需从他们的quickstart guide 复制和粘贴,以防链接断开:
要在您的 gradle 项目中使用该库,请将以下内容添加到您的 build.gradle:
dependencies {
compile "com.androidplot:androidplot-core:1.5.7"
}
如果您使用 Proguard 混淆(默认情况下由 Android Studio 创建的项目),您还将 想将此添加到您的 proguard-rules.pro 文件中:
-keep class com.androidplot.** { *; }
创建 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 样式属性的完整列表是 在这里可用。虽然定期添加新属性, 并非所有可配置属性都可用。
如果缺少您需要的东西,请使用Fig Syntax 直接在 Plot 的 XML 中,为每个属性添加前缀“androidPlot”。示例:
androidPlot.title="My Plot"
现在让我们创建一个 Activity 来显示我们刚刚在 simple_xy_plot_example.xml 中定义的 XYPlot。
基本步骤是:
由于我们处理的是 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 目录:
<?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"/>
<?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"/>
【讨论】: