hellocharts-android開源圖表庫(效果非常好)
之前我們介紹了一個非常優秀開源圖表庫 MPAndroidChart ,但是我們今天介紹的將是一個更為優秀的圖表庫,比MPAndroidChart效能更好,功能更完善,UI風格更美觀,座標軸更精細。
他就是github上出現的新專案HelloCharts。
HelloCharts支援以下chart型別:
-
Line chart(cubic lines, filled lines, scattered points)
-
Column chart(grouped, stacked, negative values)
-
Pie chart
-
Bubble chart
-
Combo chart(columns/lines)
-
Preview charts(for column chart and line chart)
此外還具有以下特點:
-
支援縮放、滑動以及平移。Zoom(pinch to zoom, double tap zoom), scroll and fling
-
支援自定義座標軸(比如座標軸位置:上下左右內部),支援自動生成座標軸。Custom and auto-generated axes(top, bottom, left, right, inside)
-
動畫(Animations)
-
支援預覽,即在chart下面會有一個座標密度更細的附屬chart,當選中附屬chart的某一區域,附屬chart上面的chart會顯示選中區域的更詳細情況。
下面是一些效果截圖:
我能用妙趣橫生來形容嗎、、
編譯以及使用方法
需要使用android 5.0的sdk編譯且需要匯入appcompat v21相容包。
每一種chart都可以在xml中定義:
1 2 3 4 |
<lecho.lib.hellocharts.view.LineChartView
android:id= "@+id/chart"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
/>
|
當然也可以在java程式碼中直接建立:
1 2 |
LineChartView
chart = new
LineChartView(context);
layout.addView(chart);
|
可以通過一些公共方法設定其行為屬性,下面是一些例子:
1 2 3 |
Chart.setInteractive(boolean
isInteractive);
Chart.setZoomType(ZoomType
zoomType);
Chart.setContainerScrollEnabled(boolean
isEnabled, ContainerScrollType type);
|
或者是用資料模型定義一些顯示的方式:
1 2 3 |
ChartData.setAxisXBottom(Axis
axisX);
ColumnChartData.setStacked(boolean
isStacked);
Line.setStrokeWidth(int
strokeWidthDp);
|
每一種chart都有自己的資料模型以及設定資料的方法,下面以LineChart為例:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
List<PointValue>
values = new
ArrayList<PointValue>();
values.add( new
PointValue(0, 2));
values.add( new
PointValue(1, 4));
values.add( new
PointValue(2, 3));
values.add( new
PointValue(3, 4));
//In
most cased you can call data model methods in builder-pattern-like manner.
Line
line = new
Line(values).setColor(Color.Blue).setCubic( true );
List<Line>
lines = new
ArrayList<Line>();
lines.add(line);
LineChartData
data = new
LineChartData();
data.setLines(lines);
LineChartView
chart = new
LineChartView(context);
chart.setLineChartData(data);
|
程式碼下載地址