前面幾篇部落格中都是調用ChartFactory.get***Intent()方法,本節講的内容調用ChartFactory.get***View()方法,這個方法調用的結果可以嵌入到任何一個Activity中,作為Activity的一部分。
XYChartBuilder.java(源碼分析見注釋)
[java] view plain copy
- package org.achartengine.chartdemo.demo.chart;
- import java.io.File;
- import java.io.FileOutputStream;
- import org.achartengine.ChartFactory;
- import org.achartengine.GraphicalView;
- import org.achartengine.chart.PointStyle;
- import org.achartengine.chartdemo.demo.R;
- import org.achartengine.model.SeriesSelection;
- import org.achartengine.model.XYMultipleSeriesDataset;
- import org.achartengine.model.XYSeries;
- import org.achartengine.renderer.XYMultipleSeriesRenderer;
- import org.achartengine.renderer.XYSeriesRenderer;
- import org.achartengine.tools.PanListener;
- import org.achartengine.tools.ZoomEvent;
- import org.achartengine.tools.ZoomListener;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.CompressFormat;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.os.Environment;
- import android.view.View;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.LinearLayout;
- import android.widget.Toast;
- public class XYChartBuilder 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 Button mNewSeries;
- private Button mAdd;
- private EditText mX;
- private EditText mY;
- private GraphicalView mChartView;
- private int index = 0;
- @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);
- mX = (EditText) findViewById(R.id.xValue);
- mY = (EditText) findViewById(R.id.yValue);
- mRenderer.setApplyBackgroundColor(true);//設定是否顯示背景色
- mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));//設定背景色
- mRenderer.setAxisTitleTextSize(16); //設定軸标題文字的大小
- mRenderer.setChartTitleTextSize(20);//?設定整個圖表标題文字大小
- mRenderer.setLabelsTextSize(15);//設定刻度顯示文字的大小(XY軸都會被設定)
- mRenderer.setLegendTextSize(15);//圖例文字大小
- mRenderer.setMargins(new int[] { 30, 70, 0, 10 });//設定圖表的外邊框(上/左/下/右)
- mRenderer.setZoomButtonsVisible(true);//是否顯示放大縮小按鈕
- mRenderer.setPointSize(10);//設定點的大小(圖上顯示的點的大小和圖例中點的大小都會被設定)
- mAdd = (Button) findViewById(R.id.add);
- mNewSeries = (Button) findViewById(R.id.new_series);
- mNewSeries.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- String seriesTitle = "Series " + (mDataset.getSeriesCount() + 1);//圖例
- XYSeries series = new XYSeries(seriesTitle);//定義XYSeries
- mDataset.addSeries(series);//在XYMultipleSeriesDataset中添加XYSeries
- mCurrentSeries = series;//設定目前需要操作的XYSeries
- XYSeriesRenderer renderer = new XYSeriesRenderer();//定義XYSeriesRenderer
- mRenderer.addSeriesRenderer(renderer);//将單個XYSeriesRenderer增加到XYMultipleSeriesRenderer
- renderer.setPointStyle(PointStyle.CIRCLE);//點的類型是圓形
- renderer.setFillPoints(true);//設定點是否實心
- mCurrentRenderer = renderer;
- setSeriesEnabled(true);
- }
- });
- //增加一個點後重畫圖
- mAdd.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- double x = 0;
- double y = 0;
- try {
- x = Double.parseDouble(mX.getText().toString());
- } catch (NumberFormatException e) {
- // TODO
- mX.requestFocus();
- return;
- }
- try {
- y = Double.parseDouble(mY.getText().toString());
- } catch (NumberFormatException e) {
- // TODO
- mY.requestFocus();
- return;
- }
- mCurrentSeries.add(x, y);
- mX.setText("");
- mY.setText("");
- mX.requestFocus();
- if (mChartView != null) {
- mChartView.repaint();//重畫圖表
- }
- //生成圖檔儲存,注釋掉下面的代碼不影響圖表的生成.
- //-->start
- Bitmap bitmap = mChartView.toBitmap();
- try {
- File file = new File(Environment.getExternalStorageDirectory(), "test" + index++ + ".png");
- FileOutputStream output = new FileOutputStream(file);
- bitmap.compress(CompressFormat.PNG, 100, output);
- } catch (Exception e) {
- e.printStackTrace();
- }
- //-->end
- }
- });
- }
- @Override
- protected void onResume() {
- super.onResume();
- if (mChartView == null) {
- LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
- mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer);
- mRenderer.setClickEnabled(true);//設定圖表是否允許點選
- mRenderer.setSelectableBuffer(100);//設定點的緩沖半徑值(在某點附件點選時,多大範圍内都算點選這個點)
- mChartView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //這段代碼處理點選一個點後,獲得所點選的點在哪個序列中以及點的坐标.
- //-->start
- SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
- double[] xy = mChartView.toRealPoint(0);
- if (seriesSelection == null) {
- Toast.makeText(XYChartBuilder.this, "No chart element was clicked", Toast.LENGTH_SHORT)
- .show();
- } else {
- Toast.makeText(
- XYChartBuilder.this,
- "Chart element in series index " + seriesSelection.getSeriesIndex()
- + " data point index " + seriesSelection.getPointIndex() + " was clicked"
- + " closest point value X=" + seriesSelection.getXValue() + ", Y=" + seriesSelection.getValue()
- + " clicked point value X=" + (float) xy[0] + ", Y=" + (float) xy[1], Toast.LENGTH_SHORT).show();
- }
- //-->end
- }
- });
- mChartView.setOnLongClickListener(new View.OnLongClickListener() {
- @Override
- public boolean onLongClick(View v) {
- SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
- if (seriesSelection == null) {
- Toast.makeText(XYChartBuilder.this, "No chart element was long pressed",
- Toast.LENGTH_SHORT);
- return false; // no chart element was long pressed, so let something
- // else handle the event
- } else {
- Toast.makeText(XYChartBuilder.this, "Chart element in series index "
- + seriesSelection.getSeriesIndex() + " data point index "
- + seriesSelection.getPointIndex() + " was long pressed", Toast.LENGTH_SHORT);
- return true; // the element was long pressed - the event has been
- // handled
- }
- }
- });
- //這段代碼處理放大縮小
- //-->start
- mChartView.addZoomListener(new ZoomListener() {
- public void zoomApplied(ZoomEvent e) {
- String type = "out";
- if (e.isZoomIn()) {
- type = "in";
- }
- System.out.println("Zoom " + type + " rate " + e.getZoomRate());
- }
- public void zoomReset() {
- System.out.println("Reset");
- }
- }, true, true);
- //-->end
- //設定拖動圖表時背景列印出圖表坐标的最大最小值.
- mChartView.addPanListener(new PanListener() {
- public void panApplied() {
- System.out.println("New X range=[" + mRenderer.getXAxisMin() + ", " + mRenderer.getXAxisMax()
- + "], Y range=[" + mRenderer.getYAxisMax() + ", " + mRenderer.getYAxisMax() + "]");
- }
- });
- layout.addView(mChartView, new LayoutParams(LayoutParams.FILL_PARENT,
- LayoutParams.FILL_PARENT));
- boolean enabled = mDataset.getSeriesCount() > 0;
- setSeriesEnabled(enabled);
- } else {
- mChartView.repaint();
- }
- }
- private void setSeriesEnabled(boolean enabled) {
- mX.setEnabled(enabled);
- mY.setEnabled(enabled);
- mAdd.setEnabled(enabled);
- }
- }