開源GraphView的使用--數(shù)據(jù)統(tǒng)計
最近做室內(nèi)定位需要繪出加速度傳感器輸出的三個方向的加速度曲線,找到了開源https://github.com/jjoe64/GraphView-Demos,省去了要重新學(xué)MatLab *=*。
在http://www.android-graphview.org/download--getting-started.html下載.jar包。
1、GraphView的使用和普通View的使用相同。
2、支持三種圖表:Line和Bar、Point。
GraphView?graph?=?(GraphView)?findViewById(R.id.graph);
LineGraphSeriesseries?=?new?LineGraphSeries(new?DataPoint[]?{
?new?DataPoint(0,?1),
???? new?DataPoint(1,?5),
???? new?DataPoint(2,?3),
???? new?DataPoint(3,?2),
???? new?DataPoint(4,?6)
});graph.addSeries(series);?GraphView?graph?=?(GraphView)?rootView.findViewById(R.id.graph);
????????BarGraphSeriesseries?=?new?BarGraphSeries(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);PointsGraphSeriesseries3?=?new?PointsGraphSeries(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);//設(shè)置點的形狀
series3.setColor(Color.YELLOW);也可以在XML中使用,但通過.jar包的不支持此功能。
3、設(shè)置各種屬性
設(shè)置每條曲線的標注:
graph.getLegendRenderer().setVisible(true);
graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);//右上角對每條線注釋
graph.getLegendRenderer().setTextColor(Color.WHITE);//標注字的顏色
series.setTitle("foo");
series1.setTitle("bar");
設(shè)置軸的數(shù)據(jù)顯示格式:
//設(shè)置軸分割數(shù)字格式 NumberFormat?nf?=?NumberFormat.getInstance(); nf.setMinimumFractionDigits(1);//小數(shù)位數(shù) nf.setMinimumIntegerDigits(2);//整數(shù)部分位數(shù) graph.getGridLabelRenderer().setLabelFormatter(new?DefaultLabelFormatter(nf,?nf));
自定義畫筆:
//自定義畫筆
Paint?paint?=?new?Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10);
paint.setPathEffect(new?DashPathEffect(new?float[]{8,?5},?0));
series1.setCustomPaint(paint);
點擊事件:
//線條點擊事件
series.setOnDataPointTapListener(new?OnDataPointTapListener()?{
????@Override
????public?void?onTap(Series?series,?DataPointInterface?dataPoint)?{
????????Toast.makeText(MainActivity.this,?"Series1:?On?Data?Point?clicked:?"+dataPoint,?Toast.LENGTH_SHORT).show();
????}
});
設(shè)置表格(分割線)顏色:
graph.getGridLabelRenderer().setGridColor(Color.WHITE);//表格顏色
實例展示:
自定義軸標簽:
graph.getGridLabelRenderer().setLabelFormatter(new?DefaultLabelFormatter()?{
????????????@Override
????????????public?String?formatLabel(double?value,?boolean?isValueX)?{
????????????????if?(isValueX)?{
????????????????????//?show?normal?x?values
????????????????????return?super.formatLabel(value,?isValueX);
????????????????}?else?{
????????????????????//?show?currency?for?y?values
????????????????????return?super.formatLabel(value,?isValueX)?+?"*";
????????????????}
????????????}
????????});
X軸設(shè)置為時間:
????????//?generate?Dates
????????Calendar?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()
????????LineGraphSeriesseries?=?new?LineGraphSeries(new?DataPoint[]?{
????????????????new?DataPoint(d1,?1),
????????????????new?DataPoint(d2,?5),
????????????????new?DataPoint(d3,?3)
????????});
????????graph.addSeries(series);
????????//?set?date?label?formatter
????????graph.getGridLabelRenderer().setLabelFormatter(new?DateAsXAxisLabelFormatter(getActivity()));
????????graph.getGridLabelRenderer().setNumHorizontalLabels(3);?//?only?4?because?of?the?space
????????//?set?manual?x?bounds?to?have?nice?steps
????????graph.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?nessecary
????????graph.getGridLabelRenderer().setHumanRounding(false);設(shè)置X、Y軸 Bounds:
?//?set?manual?X?bounds ????????graph.getViewport().setXAxisBoundsManual(true); ????????graph.getViewport().setMinX(0.5); ????????graph.getViewport().setMaxX(3.5); ????????//?set?manual?Y?bounds ????????graph.getViewport().setYAxisBoundsManual(true); ????????graph.getViewport().setMinY(3.5); ????????graph.getViewport().setMaxY(8);
設(shè)置圖表可以縮放:
//?enable?scaling ????????graph.getViewport().setScalable(true);
設(shè)置Y軸可以Auto,圖表可以橫向滑動:
//?enable?scrolling ????????graph.getViewport().setScrollable(true);
這只左右兩個Y軸:
//?set?second?scale
????????graph.getSecondScale().addSeries(series2);
????????//?the?y?bounds?are?always?manual?for?second?scale
????????graph.getSecondScale().setMinY(0);
????????graph.getSecondScale().setMaxY(100);
????????series2.setColor(Color.RED);
????????graph.getGridLabelRenderer().setVerticalLabelsSecondScaleColor(Color.RED);
????????//?legend
????????series.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?labels
????????StaticLabelsFormatter?staticLabelsFormatter?=?new?StaticLabelsFormatter(graph);
????????staticLabelsFormatter.setHorizontalLabels(new?String[]?{"old",?"middle",?"new"});
????????staticLabelsFormatter.setVerticalLabels(new?String[]?{"low",?"middle",?"high"});
????????graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);設(shè)置軸Lables:
?//?titles
????????graph.setTitle("Chart?Title");
????????graph.getGridLabelRenderer().setVerticalAxisTitle("Vertical?Axis");
????????graph.getGridLabelRenderer().setHorizontalAxisTitle("Horizontal?Axis");標簽、背景色、字體、字大小、顏色....Styling:
?//?styling?grid/labels
????????graph.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?viewport
????????graph.getViewport().setBackgroundColor(Color.argb(255,?222,?222,?222));//圖表背景色
????????graph.getViewport().setDrawBorder(true);
????????graph.getViewport().setBorderColor(Color.BLUE);
????????//?styling?series
????????series.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);




