开源GraphView的使用--数据统计

最近做室内定位需要绘出加速度传感器输出的三个方向的加速度曲线,找到了开源https://github.com/jjoe64/GraphView-Demos,省去了要重新学MatLab *=*。

在http://www.android-graphview.org/download--getting-started.html下载.jar包。

1、GraphView的使用和普通View的使用相同。在Layout中:

   <com.jjoe64.graphview.GraphViewandroid:layout_width="match_parent"android:layout_height="200dip"android:id="@+id/graph" />
2、支持三种图表:Line和Bar、Point。

<span style="white-space:pre">		</span>GraphView graph = (GraphView) findViewById(R.id.graph);LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {new DataPoint(0, 1),new DataPoint(1, 5),new DataPoint(2, 3),new DataPoint(3, 2),new DataPoint(4, 6)});
<span style="white-space:pre">		</span>graph.addSeries(series);

 GraphView graph = (GraphView) rootView.findViewById(R.id.graph);BarGraphSeries<DataPoint> series = new BarGraphSeries<DataPoint>(new DataPoint[] {new DataPoint(0, -2),new DataPoint(1, 5),new DataPoint(2, 3),new DataPoint(3, 2),new DataPoint(4, 6)});series.setSpacing(30);graph.addSeries(series);

<span style="white-space:pre">	</span>PointsGraphSeries<DataPoint> series3 = new PointsGraphSeries<DataPoint>(new DataPoint[] {new DataPoint(0, 0),new DataPoint(1, 3),new DataPoint(2, 1),new DataPoint(3, 0),new DataPoint(4, 4)});graph.addSeries(series3);series3.setShape(PointsGraphSeries.Shape.TRIANGLE);//设置点的形状series3.setColor(Color.YELLOW);

也可以在XML中使用,但通过.jar包的不支持此功能。

 <com.jjoe64.graphview.helper.GraphViewXMLandroid:layout_width="match_parent"android:layout_height="100dip"app:seriesData="0=5;2=5;3=0;4=2"app:seriesType="line"app:seriesColor="#ee0000" />

3、设置各种属性

设置每条曲线的标注:

<span style="white-space:pre">		</span>graph.getLegendRenderer().setVisible(true);graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);//右上角对每条线注释graph.getLegendRenderer().setTextColor(Color.WHITE);//标注字的颜色series.setTitle("foo");series1.setTitle("bar");

设置轴的数据显示格式:

<span style="white-space:pre">	</span>//设置轴分割数字格式NumberFormat nf = NumberFormat.getInstance();nf.setMinimumFractionDigits(1);//小数位数nf.setMinimumIntegerDigits(2);//整数部分位数graph.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter(nf, nf));

自定义画笔:

<span style="white-space:pre">	</span>//自定义画笔Paint paint = new Paint();paint.setStyle(Paint.Style.STROKE);paint.setStrokeWidth(10);paint.setPathEffect(new DashPathEffect(new float[]{8, 5}, 0));series1.setCustomPaint(paint);

点击事件:

<span style="white-space:pre">	</span>//线条点击事件series.setOnDataPointTapListener(new OnDataPointTapListener() {@Overridepublic void onTap(Series series, DataPointInterface dataPoint) {Toast.makeText(MainActivity.this, "Series1: On Data Point clicked: "+dataPoint, Toast.LENGTH_SHORT).show();}});

设置表格(分割线)颜色:

graph.getGridLabelRenderer().setGridColor(Color.WHITE);//表格颜色

实例展示:

自定义轴标签:

graph.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {@Overridepublic String formatLabel(double value, boolean isValueX) {if (isValueX) {// show normal x valuesreturn super.formatLabel(value, isValueX);} else {// show currency for y valuesreturn super.formatLabel(value, isValueX) + "*";}}});



X轴设置为时间:

        // generate DatesCalendar calendar = Calendar.getInstance();Date d1 = calendar.getTime();calendar.add(Calendar.DATE, 1);Date d2 = calendar.getTime();calendar.add(Calendar.DATE, 1);Date d3 = calendar.getTime();GraphView graph = (GraphView) rootView.findViewById(R.id.graph);// you can directly pass Date objects to DataPoint-Constructor// this will convert the Date to double via Date#getTime()LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {new DataPoint(d1, 1),new DataPoint(d2, 5),new DataPoint(d3, 3)});graph.addSeries(series);// set date label formattergraph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(getActivity()));graph.getGridLabelRenderer().setNumHorizontalLabels(3); // only 4 because of the space// set manual x bounds to have nice stepsgraph.getViewport().setMinX(d1.getTime());graph.getViewport().setMaxX(d3.getTime());graph.getViewport().setXAxisBoundsManual(true);// as we use dates as labels, the human rounding to nice readable numbers// is not nessecarygraph.getGridLabelRenderer().setHumanRounding(false);


设置X、Y轴 Bounds:

 // set manual X boundsgraph.getViewport().setXAxisBoundsManual(true);graph.getViewport().setMinX(0.5);graph.getViewport().setMaxX(3.5);// set manual Y boundsgraph.getViewport().setYAxisBoundsManual(true);graph.getViewport().setMinY(3.5);graph.getViewport().setMaxY(8);


设置图表可以缩放:

// enable scalinggraph.getViewport().setScalable(true);

设置Y轴可以Auto,图表可以横向滑动:

// enable scrollinggraph.getViewport().setScrollable(true);
这只左右两个Y轴:

// set second scalegraph.getSecondScale().addSeries(series2);// the y bounds are always manual for second scalegraph.getSecondScale().setMinY(0);graph.getSecondScale().setMaxY(100);series2.setColor(Color.RED);graph.getGridLabelRenderer().setVerticalLabelsSecondScaleColor(Color.RED);// legendseries.setTitle("foo");series2.setTitle("bar");graph.getLegendRenderer().setVisible(true);graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);graph.getViewport().setBackgroundColor(Color.GRAY);



使用 staticLables:

 // use static labels for horizontal and vertical labelsStaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph);staticLabelsFormatter.setHorizontalLabels(new String[] {"old", "middle", "new"});staticLabelsFormatter.setVerticalLabels(new String[] {"low", "middle", "high"});graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);


设置轴Lables:

 // titlesgraph.setTitle("Chart Title");graph.getGridLabelRenderer().setVerticalAxisTitle("Vertical Axis");graph.getGridLabelRenderer().setHorizontalAxisTitle("Horizontal Axis");

标签、背景色、字体、字大小、颜色....Styling:

 // styling grid/labelsgraph.getGridLabelRenderer().setGridColor(Color.RED);graph.getGridLabelRenderer().setHighlightZeroLines(false);graph.getGridLabelRenderer().setHorizontalLabelsColor(Color.GREEN);//水平轴字体颜色graph.getGridLabelRenderer().setVerticalLabelsColor(Color.RED);//垂直轴字颜色graph.getGridLabelRenderer().setVerticalLabelsAlign(Paint.Align.LEFT);graph.getGridLabelRenderer().setLabelVerticalWidth(150);graph.getGridLabelRenderer().setTextSize(40);//字大小graph.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.HORIZONTAL);//表格样式,水平线graph.getGridLabelRenderer().reloadStyles();graph.getGridLabelRenderer().setHorizontalLabelsAngle(120);//水平轴标签倾斜角// styling viewportgraph.getViewport().setBackgroundColor(Color.argb(255, 222, 222, 222));//图表背景色graph.getViewport().setDrawBorder(true);graph.getViewport().setBorderColor(Color.BLUE);// styling seriesseries.setTitle("Random Curve 1");series.setColor(Color.GREEN);series.setDrawDataPoints(true);series.setDataPointsRadius(10);series.setThickness(8);series2.setTitle("Random Curve 2");series2.setDrawBackground(true);series2.setBackgroundColor(Color.argb(100, 255, 255, 0));Paint paint = new Paint();paint.setStyle(Paint.Style.STROKE);paint.setStrokeWidth(10);paint.setPathEffect(new DashPathEffect(new float[]{8, 5}, 0));series2.setCustomPaint(paint);// styling legend 注释每条线代表什么graph.getLegendRenderer().setVisible(true);graph.getLegendRenderer().setTextSize(25);graph.getLegendRenderer().setBackgroundColor(Color.argb(150, 50, 0, 0));graph.getLegendRenderer().setTextColor(Color.WHITE);//graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);//graph.getLegendRenderer().setMargin(30);graph.getLegendRenderer().setFixedPosition(150, 0);

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/348701.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

json绑定到实体_绑定到JSON和XML –处理集合

json绑定到实体EclipseLink JAXB&#xff08;MOXy&#xff09;的优势之一是能够使用单个元数据集将对象模型映射到JSON和XML。 一个弱点是您需要在JSON键或XML元素上折中集合属性。 我很高兴地说这个问题已经在EclipseLink 2.5&#xff08;和EclipseLink 2.4.2&#xff09;中解…

Java JDK 10会有什么期望

由于我们刚刚习惯于9月发布的Java 9&#xff0c;因此距离下一代Java的发布只有几个月的时间了。 就在本月&#xff0c;计划中的Java Development Kit 10升级已进入开发的主要减速阶段。 在第一个初始阶段&#xff0c;可以修复一个到三个错误。 JDK 10是Java Standard Edition …

jpa配置映射包_JPA – Hibernate –包级别的类型映射

jpa配置映射包当我们最终成熟到可以在JPA中使用某些自定义类型映射时&#xff0c;我们通常会停留在某些提供程序特定的解决方案上&#xff0c;因为JPA本身并未定义任何用于执行此操作的机制。 让我为您展示一个JPA提供程序Hibernate的自定义类型映射定义的示例。 假设我们在项目…

弹簧和线程:异步

以前&#xff0c;我们开始使用spring和TaskExecutor &#xff0c;因此我们对如何在spring应用程序中使用线程更加熟悉。 但是&#xff0c;使用任务执行程序可能比较麻烦&#xff0c;尤其是当我们需要执行简单的操作时。 Spring的异步方法可以解决。 您不必为可运行对象和Tas…

C++一天一个程序(一)

例1: helloworld! #include int main() { std::cout << “Hello, world!n”; } 或者 #include using namespaces std; int main() { cout << “Hello, world!n”; } 换行还可以endl

我的对象命名

这是最常见的辩论之一。 大多数人对此主题有自己的见解&#xff0c;却没人能真正说出哪个是正确的。 我当然不能&#xff0c;但是尽管如此&#xff0c;我还是决定与大家分享我的想法&#xff0c;投入两美分&#xff0c;也许对某人会有帮助。 当我创建一个新类时&#xff0c;我…

C++一天一个程序(二)

#include #define NUMBER 4 int main() { std::cout << NUMBER << std::endl; } 或者 #include using namespaces std; int main() { cout << 4<< endl; } 注意: 第一段中NUMBER已经被定义&#xff0c;不可以在程序中再次赋值。建议不要用#define定义…

c++编写web服务_让我们编写一个文档样式的Web服务

c编写web服务您可能知道&#xff0c;我们可以使用四种主要的Web服务样式。 它们如下&#xff1a; 文件/文学 包装的文件/文学 RPC /编码 RPC /文字 当然&#xff0c;现在不建议使用RPC /编码样式。 如果您有兴趣&#xff0c;可以在此处找到这篇非常全面的文章&#xff0c;…

C++一天一个程序(三)

#include <stdio.h> class Trace { public:     Trace()  {noisy 0; }      void print(char* s)  { if (noisy)  printf("%s",s); }   void on()  { noisy 1; }      void off()   { noisy 0; }  private: int noisy;   };  C改…

Java 9:ServiceLoader

java.util.ServiceLoader类在运行时加载服务提供者/实现。 在编译时&#xff0c;ServiceLoader只需要知道Service接口。 借助Java9模块化&#xff0c;我们可以在运行时动态添加服务实现模块&#xff0c;而Application可以拥有新的实现&#xff0c;而不会影响任何事情&#xff0…

C++一天一个程序(四)

#include using namespace std; struct complex{  double real, imag;  complex(double 0.0, double 0.0); } complex&#xff1a;complex(double r, double i) {  real r; imag i; } inline ostream& operator<<(ostream &os, const complex &c) {…

C++一天一个程序(五)

(1)确定所求长方形的长和宽。 (2)确定计算长方形的周长和面积的公式并计算。 (3)输出计算结果。 (1)以面向过程程序设计思想编码。 #include using namespace std; void main(){ int perimeter,area; int length20,width10; perimeter2*(lengthwidth); arealength* width; cou…

netbeans 定制代码_将NetBeans代码模板弯曲到我的意愿

netbeans 定制代码任何阅读过我关于NetBeans的文章的人都知道&#xff0c;我真的很喜欢NetBeans的许多功能。 但是&#xff0c;最近&#xff0c;我发现自己对NetBeans特定功能的特定问题越来越恼火。 最终&#xff0c;它使我烦恼不已&#xff0c;促使我开始研究如何根据自己的喜…

一天一个C++程序(六)

数据类型转换应用示例。 #include using namespace std; int main() { int a,c,d,b322; float x,z,y4.56; char ch1‘d’,ch2; ay; xb; cch1; ch2b; zyb; dbch1; cout<<“a”<<a<<"\tx"<<x<<endl; cout<<“c”<<c<<…

早期更多失败– Java 8

快速失败或早期失败是一种软件工程概念&#xff0c;旨在通过在不应该发生的事情发生时立即停止执行来防止复杂问题的发生。 在之前的博客文章和演示中&#xff0c;我将详细介绍这种方法的优点&#xff0c;在此博客文章中&#xff0c;我将详细介绍Java 8中该思想的另一种用法。 …

C++一天一个程序(七)

#include using namespace std; int main() { cout<<“字符型 (char)所占字节数:”<<sizeof(char)<<endl; cout<<“无符号字符型(unsigned char)所占字节数:”<<sizeof( unsigned char)<<endl; cout<<“短整型( short int)所占字节数…

C++一天一个程序(八)

#include <iostream.h> void main() { int i,j,k; for(i1;i<6;i) { for(j1:j<6-i;j) cout<<" “; for(kl;k<: 2i- 1;k) cout<<"%"; cout<<endl; } for(i5;i>1;–) { for(j 1:j<6-i:j) cout<<" "; for(k…

url中传递对象参数_在URL参数中传递复杂对象

url中传递对象参数假设您想传递原始数据类型&#xff0c;例如复杂的Java对象 java.util.Data&#xff0c;java.lang.List&#xff0c;泛型类&#xff0c;数组以及通过URL参数所需的所有内容&#xff0c;以便在页面加载后在任何网页上预设默认值。 共同的任务&#xff1f; 是的…

C++两天一个程序(一)

#include  using namespace std;  main()   {    int  i 7;    int* ip &i;    int** ipp &ip;    cout << "Address " << ip << " contains " << *ip << endl;    cout << "A…

两个迭代器的故事

当您查看最受欢迎的Java面试问题时&#xff0c;可能会遇到有关快速故障和故障安全迭代器的问题&#xff1a; 故障快速迭代器和故障安全迭代器之间有什么区别&#xff1f; 简化的答案是&#xff1a; 如果在迭代过程中修改了集合&#xff0c;则快速失败迭代器将引发ConcurrentM…