如何使用Achartengine创建嵌套的x轴级xy图表

时间:2014-01-04 10:46:12

标签: android achartengine

enter image description here在我的应用程序中,我想使用achartengine创建一个时间距离xy折线图。因此,在x轴上,我想首先显示几个月的水平。然后在缩放后,水平必须转换一天。应该有缩放到缩放功能,这应该将图表从月更改为周,然后再进一步缩放到几天。那么任何想法?任何链接或任何建议将不胜感激。这是我的班级..  `package com.atos.graphreq;

import java.util.ArrayList; import java.util.Hashtable; import java.util.List;

import org.achartengine.ChartFactory; import org.achartengine.GraphicalView; import org.achartengine.model.XYMultipleSeriesDataset; import org.achartengine.model.XYSeries; import org.achartengine.renderer.XYMultipleSeriesRenderer; import org.achartengine.renderer.XYSeriesRenderer; import org.achartengine.renderer.XYSeriesRenderer.FillOutsideLine; import org.achartengine.tools.PanListener; import org.achartengine.tools.ZoomEvent; import org.achartengine.tools.ZoomListener;

import com.atos.graphreq.R; import com.atos.graphreq.database.Database; import com.atos.graphreq.model.AppZoyDebug;

import android.app.Activity; import android.database.Cursor; import android.graphics.Color; import android.graphics.Paint.Align; import android.os.Bundle; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams;

public class GraphActivity extends Activity {     私有GraphicalView mChartView;

XYMultipleSeriesRenderer renderer;
List<Double> xValues;
List<Double> yValues;
LinearLayout layout;
Database db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    layout = (LinearLayout) findViewById(R.id.graphlayout);
    db = new Database(this);
    db.open();
    xValues = new ArrayList<Double>();
    yValues = new ArrayList<Double>();
    int colors = Color.BLUE;

    // Initialize the XYMultipleSeriesRenderer class with line color blue
    renderer = buildRenderer(colors);

    int length = renderer.getSeriesRendererCount();

    // Assigning color area under the line graph
    for (int i = 0; i < length; i++) {
        FillOutsideLine fill = new FillOutsideLine(
                FillOutsideLine.Type.BOUNDS_ALL);
        fill.setColor(Color.parseColor("#81DAF5"));
        ((XYSeriesRenderer) renderer.getSeriesRendererAt(i))
                .addFillOutsideLine(fill);

    }

    // Set the chart with x level and y level titles and renderer
    setChartSettings(renderer, "X-Y Line Chart", "", "", 0, 12, 0, 10,
            Color.LTGRAY, Color.parseColor("#81DAF5"));
    renderer.setXLabels(0);
    //renderer.addXTextLabel(20, "month");
    renderer.setYLabels(0);
    renderer.setShowGrid(true);
    renderer.setXLabelsAlign(Align.CENTER);
    renderer.setYLabelsAlign(Align.RIGHT);
    renderer.setZoomButtonsVisible(true);
    renderer.setPanLimits(new double[] { 0, 12, 0, 11 });
    renderer.setZoomLimits(new double[] { 0, 12, 0, 10 });
    renderer.setZoomRate((float) 1.5);
    renderer.setMarginsColor(Color.parseColor("#FFEDED"));
    renderer.setXLabelsColor(Color.BLACK);
    renderer.setYLabelsColor(0, Color.BLACK);
    renderer.setApplyBackgroundColor(true);
    renderer.setBackgroundColor(Color.parseColor("#FBFBFC"));

    // Getting x and y axis text levels from database
    Cursor cursor1 = db.getAllXYFromDb();
    if (cursor1 != null) {
        AppZoyDebug.lOG("cursor1cursor1 ", " " + cursor1.getColumnCount());
        cursor1.moveToFirst();
        int axislength = getResources().getStringArray(R.array.xmonths).length;
        for (int k = 0; k < axislength; k++) {
            renderer.addXTextLabel(k,
                    getResources().getStringArray(R.array.xmonths)[k]);
            xValues.add(cursor1.getDouble(1));
            yValues.add(cursor1.getDouble(2));
            cursor1.moveToNext();
        }
        axislength = getResources().getStringArray(R.array.ypercent).length;
        for (int k = 0; k < axislength; k++) {
            renderer.addYTextLabel(k + 1,
                    getResources().getStringArray(R.array.ypercent)[k]);
            cursor1.moveToNext();
        }
        if (!cursor1.isClosed()) {
            cursor1.close();
        }

    }
    if (mChartView == null) {

        // initializing GraphicalView class by giving x and y coordinates
        // and the renderer variable
        mChartView = ChartFactory.getLineChartView(this,
                mDataset("", xValues, yValues), renderer);
        layout.addView(mChartView, new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    } else {
        mChartView.repaint();
    }

    mChartView.addZoomListener(new ZoomListener() {
          public void zoomApplied(ZoomEvent e) {

              if(e.isZoomIn())
              {
            double start = renderer.getXAxisMin();
            double stop = renderer.getXAxisMax();
            double step = (stop - start);
          //  renderer.clearXTextLabels();
            int j=0;
            for (double i = start; i <= stop; i += step) {
                renderer.removeXTextLabel(i);

             // renderer.addXTextLabel(i, ""+j++);


    /*renderer.setXAxisMin(start);
    renderer.setXAxisMax(stop);
    renderer.setPanLimits(new double[] { 0, 30 * 12, 0, 40 });
    renderer.setZoomLimits(new double[] { 0, 30* 12, 0, 40 });
           int constant1 = 0;
            for (int k = 0; k < 30 * 12; k++) {

                if (k % 30 == 0) {
                    constant1 = k / 30;
                    renderer.addXTextLabel(k, getResources()
                            .getStringArray(R.array.xmonths)[k / 30]);
                } else {
                    if (k >= 30) {
                        renderer.addXTextLabel(k, k
                                - (30 * constant1) + "");

                    } else {
                        renderer.addXTextLabel(k, k + "");
                    }
                }
            } */
                }
              }
          }

          public void zoomReset() {
          }
        }, true, true);


    /*mChartView.addPanListener(new PanListener() {

        @Override
        public void panApplied() {
            // TODO Auto-generated method stub
            double start = renderer.getXAxisMin();
            double stop = renderer.getXAxisMax();
            double step = (stop - start) /12;

            for (double i = start; i <= stop; i += step) {
                 renderer.removeXTextLabel(i);
              renderer.addXTextLabel(i, "1");
            }

        }
    });*/
}

private void setChartSettings(XYMultipleSeriesRenderer renderer3,
        String title, String xTitle, String yTitle, double xMin,
        double xMax, double yMin, double yMax, int axesColor,
        int labelsColor) {
    renderer3.setChartTitle(title);
    renderer3.setXTitle(xTitle);
    renderer3.setYTitle(yTitle);
    renderer3.setXAxisMin(xMin);
    renderer3.setXAxisMax(xMax);
    renderer3.setYAxisMin(yMin);
    renderer3.setYAxisMax(yMax);
    renderer3.setAxesColor(axesColor);
    renderer3.setLabelsColor(labelsColor);

}

private XYMultipleSeriesRenderer buildRenderer(int colors) {
    XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
    setRenderer(renderer, colors);
    return renderer;
}

// setting level text size and title text size
private void setRenderer(XYMultipleSeriesRenderer renderer2, int colors) {
    renderer2.setAxisTitleTextSize(16);
    renderer2.setChartTitleTextSize(20);
    renderer2.setLabelsTextSize(15);
    XYSeriesRenderer r = new XYSeriesRenderer();
    r.setColor(colors);
    renderer2.addSeriesRenderer(r);
}

private XYMultipleSeriesDataset mDataset(String titles,
        List<Double> xValues2, List<Double> yValues2) {
    XYMultipleSeriesDataset dataset1 = new XYMultipleSeriesDataset();
    addXYSeries(dataset1, titles, xValues2, yValues2, 0);
    return dataset1;
}

private void addXYSeries(XYMultipleSeriesDataset dataset, String titles,
        List<Double> xValues2, List<Double> yValues2, int scale) {

    Double[] xV = xValues2.toArray(new Double[xValues2.size()]);
    XYSeries series = new XYSeries(titles, scale);
    Double[] yV = yValues2.toArray(new Double[yValues2.size()]);
    int seriesLength = xV.length;
    for (int k = 0; k < seriesLength; k++) {
        series.add(xV[k], yV[k]);
    }
    dataset.addSeries(series);

}

@Override
protected void onDestroy() {
    super.onDestroy();
    db.close();
}

}`

1 个答案:

答案 0 :(得分:0)

您可以使用自定义文本X轴标签启动显示的图表数月:

renderer.addXTextLabel(x, "month");

您可以通过以下方式收听缩放事件:

// an example of handling the zoom events on the chart
mChartView.addZoomListener(new ZoomListener() {
  public void zoomApplied(ZoomEvent e) {
    String type = "out";
    if (e.isZoomIn()) {
      type = "in";
    }
    Log.i("Zoom", "Zoom " + type + " rate " + e.getZoomRate());
  }

  public void zoomReset() {
    Log.i("Zoom", "Reset");
  }
}, true, true);

然后,在zoomApplied方法中,您可以删除现有的X轴自定义标签,并在几天内添加新标签。

renderer.removeXTextLabel(x);
renderer.addXTextLabel(anotherX, "day");