近来要开发一款支持Android系统的与股票有关的软件,该软件需要显示股票走势图。开始想用最基础的画板和画笔去实现,然而实际设计时发现比较复杂,需要抽象出很多对象,代码量也很大,而且还存在很多难以预料的风险,所以就上网查了很多资料,希望能找到更好的实现方法。最后发现了Android图标开发库AChartEngine。先看下英文说明:
AChartEngine is a charting library for Android applications. It currently supports the following chart types:
line chart
area chart
scatter chart
time chart
bar chart
pie chart
bubble chart
doughnut chart
range (high-low) bar chart
dial chart / gauge
怎么样?是不是一个很强大呢?当然是针对Android来说的,如果对PC和Web来说这都是皮毛…^_^
先上图吧,这是我参考官方文档写的一个Demo,后面我会把代码和相关函数贴上来……
虽然很丑,但这是我的辛辛苦苦的劳动成果呢…呵呵
开始贴代码:
首先引入jar包,就不j细说了……
StockChartBuilder.java:
Java代码
-
-
-
-
-
-
-
-
-
-
-
- public class StockChartBuilder extends Activity {
- public static final String TYPE = "type";
-
- private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
-
- private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
-
- private XYSeries mCurrentSeries;
-
- private XYSeriesRenderer mCurrentRenderer;
-
- private String mDateFormat;
-
- private GraphicalView mChartView;
-
-
-
-
- @Override
- protected void onRestoreInstanceState(Bundle savedState) {
- super.onRestoreInstanceState(savedState);
- mDataset = (XYMultipleSeriesDataset) savedState
- .getSerializable("dataset");
- mRenderer = (XYMultipleSeriesRenderer) savedState
- .getSerializable("renderer");
- mCurrentSeries = (XYSeries) savedState
- .getSerializable("current_series");
- mCurrentRenderer = (XYSeriesRenderer) savedState
- .getSerializable("current_renderer");
- mDateFormat = savedState.getString("date_format");
- }
-
-
-
-
- @Override
- protected void onSaveInstanceState(Bundle outState) {
- super.onSaveInstanceState(outState);
- outState.putSerializable("dataset", mDataset);
- outState.putSerializable("renderer", mRenderer);
- outState.putSerializable("current_series", mCurrentSeries);
- outState.putSerializable("current_renderer", mCurrentRenderer);
- outState.putString("date_format", mDateFormat);
- }
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.xy_chart);
-
-
- String seriesTitle = "上证指数";
- XYSeries series = new XYSeries(seriesTitle);
- mDataset.addSeries(series);
- mCurrentSeries = series;
- XYSeriesRenderer seriesRenderer = new XYSeriesRenderer();
-
-
- seriesRenderer.setColor(Color.BLUE);
- seriesRenderer.setFillBelowLine(true);
- seriesRenderer.setFillBelowLineColor(Color.RED);
-
- seriesRenderer.setPointStyle(PointStyle.POINT);
- seriesRenderer.setLineWidth(3.0f);
-
- mRenderer.addSeriesRenderer(seriesRenderer);
- mRenderer.setYAxisMin(0d);
- mRenderer.setYAxisMax(120d);
- mRenderer.setXAxisMax(24d);
- mRenderer.setShowGrid(true);
- mRenderer.setXLabels(24);
- mRenderer.setChartTitle("上证指数-24小时走势图");
- mCurrentRenderer = seriesRenderer;
-
-
- double x = 0;
- double y = 0;
-
- int[][] xyValues = getValues();
-
- for (int i = 0; i < xyValues.length; i++) {
- x = xyValues[i][0];
- y = xyValues[i][1];
- mCurrentSeries.add(x, y);
- }
-
- }
-
-
-
-
- @Override
- protected void onResume() {
- super.onResume();
- if (mChartView == null) {
- LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
- mChartView = ChartFactory.getLineChartView(this, mDataset,
- mRenderer);
- layout.addView(mChartView, new LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
-
- } else {
- mChartView.setBackgroundResource(R.id.chart);
- mChartView.repaint();
- }
- }
-
-
-
-
-
-
- private int[][] getValues() {
- int[][] xyValues = { { 0, 88 }, { 1, 79 }, { 2, 75 }, { 3, 71 },
- { 4, 90 }, { 5, 88 }, { 6, 66 }, { 7, 65 }, { 8, 50 },
- { 9, 78 }, { 10, 67 }, { 11, 77 }, { 12, 79 }, { 13, 85 },
- { 14, 89 }, { 15, 93 }, { 16, 95 }, { 17, 99 }, { 18, 66 },
- { 19, 55 }, { 20, 44 } };
- return xyValues;
- }
-
-
-
-
-
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
-
- return super.onTouchEvent(event);
- }
- }
xy_chart.xml (Layout):
XML/HTML代码
- <?xml version="1.0" encoding="utf-8"?>
-
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
-
- <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:text="@string/add_values" />
-
- <TableLayout android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
-
- </TableLayout>
-
- <LinearLayout android:id="@+id/chart" android:orientation="horizontal"
- android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" />
-
- <LinearLayout android:orientation="horizontal"
- android:layout_width="fill_parent" android:layout_height="wrap_content" >
- </LinearLayout>
-
- </LinearLayout>
AndroidManifest.xml:
XML/HTML代码
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.cn.eagle"
- android:versionCode="1"
- android:versionName="1.0">
-
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=". StockChartBuilder "
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
-
- <activity android:name="org.achartengine.GraphicalActivity" />
-
- </application>
- </manifest>
strings.xml (values):
XML/HTML代码
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, ChartTest!</string>
- <string name="app_name">走势图(示例)</string>
- <string name="add_values">输入值生成走势图</string>
- <string name="x">X</string>
- <string name="y">Y</string>
- <string name="add">Add</string>
- <string name="new_series">New series</string>
- </resources>
打完收工^_^ ……