在achartengine线图中维护两个x轴标签之间的距离

时间:2014-05-06 11:06:45

标签: android achartengine linegraph

我一直在使用achart引擎为android工作linegraph。一切都很好,我得到了我想要的东西但是。我需要增加每个x轴标签之间的距离。如果只有4个标签,那么图表看起来不错,空间足够大。但是,当x轴标签增加时,x轴标签之间的距离会减小。非常感谢帮助。

package com.example.graph;

import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import com.example.databasetest.R;
import com.example.databasetest.sqlite.Symptom;
import com.example.databasetest.sqlite.SymptomsDbHelper;

public class NewGraphActivity extends Activity {

    public int symID;

    public NewGraphActivity(int symID) {
        super();
        this.symID = symID;
    }

    public NewGraphActivity() {
        super();
    }

    public int sympIID;
    int total = 0;

    public List<Symptom> lss;

    private XYMultipleSeriesDataset dataset;

    private XYMultipleSeriesRenderer mrenderer;

    private XYSeries mCurrentSeries;

    private XYSeriesRenderer renderer;

    private GraphicalView mChartView;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        Bundle extras = getIntent().getExtras();
        sympIID = extras.getInt("symID");

        SymptomsDbHelper symdbc = new SymptomsDbHelper(getApplicationContext());
        lss = symdbc.getAllSymptomsForGraph(sympIID);

        dataset = getDemoDataset();
        mrenderer = getDemoRenderer();

        setContentView(R.layout.newgraphly);

        setRendererStyling();

        if (mChartView == null) {
            LinearLayout layout = (LinearLayout) findViewById(R.id.chartnew);
            mChartView = ChartFactory
                    .getLineChartView(this, dataset, mrenderer);
            mrenderer.setSelectableBuffer(100);
            layout.addView(mChartView, new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        } else
            mChartView.repaint();

    }

    private void setRendererStyling() {
        mrenderer.setApplyBackgroundColor(false);
        mrenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
        mrenderer.setAxisTitleTextSize(16);
        mrenderer.setChartTitleTextSize(20);
        mrenderer.setLabelsTextSize(15);
        mrenderer.setLegendTextSize(15);

        mrenderer.setYLabelsAlign(Align.RIGHT);
        mrenderer.setMargins(new int[] { 0, 80, 0, 0 });

        mrenderer.setZoomButtonsVisible(false);
        mrenderer.setPointSize(10);

        mrenderer.setYLabels(0);
        mrenderer.setXLabels(4);



        mrenderer.setYAxisMin(-0.5);
        mrenderer.setYAxisMax(3.5);

        mrenderer.setXAxisMin(-1);
        mrenderer.setXAxisMax(lss.size()+1);

        mrenderer.setShowGrid(true);

        mrenderer.setPanEnabled(true, false);

        mrenderer.setPanLimits(new double[] { -1, lss.size()+1, 0, 0 });


        mrenderer.setZoomEnabled(false, false);

        for (int j = 0; j < 4; j++) {

            mrenderer.addYTextLabel(j, this.changeIntValue(j));
        }

        for (int count = 0; count < lss.size(); count++) {

            mrenderer.addXTextLabel(count, lss.get(count).getDistinctDate());

        }
    }

    public String changeIntValue(int count) {
        if (count == 0) {

            return "None";
        } else if (count == 1) {
            return "Light";
        } else if (count == 2) {
            return "Medium";
        } else if (count == 3) {
            return "Severe";
        }

        return null;

    }

    private XYMultipleSeriesDataset getDemoDataset() {

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

        XYSeries firstSeries = new XYSeries("Sample series One");
        for (int i = 0; i < lss.size(); i++)
            firstSeries.add(i, lss.get(i).getRate());
        dataset.addSeries(firstSeries);

        return dataset;

    }

    private XYMultipleSeriesRenderer getDemoRenderer() {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setMargins(new int[] { 20, 30, 15, 0 });
        XYSeriesRenderer r = new XYSeriesRenderer();
        r.setColor(Color.BLUE);
        r.setPointStyle(PointStyle.CIRCLE);
        r.setFillBelowLine(false);
        r.setFillBelowLineColor(Color.WHITE);
        r.setFillPoints(true);

        renderer.addSeriesRenderer(r);

        return renderer;
    }

}

下面是图片。 enter image description here

2 个答案:

答案 0 :(得分:1)

这是因为没有为轴数设置最大值。因此,引擎将所有数据放入视图中。 你必须以这种方式设置......

XYMultipleSeriesRenderer.setXAxisMax();

XYMultipleSeriesRenderer.setXAxisMin();

此功能将解决您的问题。 我还想建议将X标签的角度设置为

XYMultipleSeriesRenderer.setXLabelsAngle(45);

感谢。

答案 1 :(得分:0)

您可以通过设置所需的大致标签数量来调整显示标签的数量:

renderer.setXLabels(5);