在achartengine中将线性图的起始位置移动到右侧

时间:2014-08-31 09:14:24

标签: android graph achartengine

我最近开始使用achartengine。我想绘制一个简单的线性图 但偏离轴的原点偏移 - (0,0)。 我将x数组从原始值更改为从5开始。

这是我目前的代码

public class MainActivity extends Activity {

private String[] mMonth = new String[] {
            "Jan", "Feb" , "Mar", "Apr", "May", "Jun",
        };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    openChart();
}    
private void openChart(){
    int[] x = { 5,6,7,8,9};
    int[] income = { 240,467,259,570,500};
    int[] expense = {200, 521, 290, 219, 457};

    // Creating an  XYSeries for Income
    XYSeries incomeSeries = new XYSeries("Income");
    // Creating an  XYSeries for Income
    XYSeries expenseSeries = new XYSeries("Expenses");
    // Adding data to Income and Expense Series
    for(int i=0;i<x.length;i++){
        incomeSeries.add(x[i], income[i]);
        expenseSeries.add(x[i],expense[i]);
    }

    // Creating a dataset to hold each series
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
    // Adding Income Series to the dataset
    dataset.addSeries(incomeSeries);
    // Adding Expense Series to dataset
    dataset.addSeries(expenseSeries);       


    // Creating XYSeriesRenderer to customize incomeSeries
    XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
    incomeRenderer.setColor(Color.WHITE);
    incomeRenderer.setPointStyle(PointStyle.CIRCLE);
    incomeRenderer.setFillPoints(true);
    incomeRenderer.setLineWidth(2);
    incomeRenderer.setDisplayChartValues(true);

    // Creating XYSeriesRenderer to customize expenseSeries
    XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
    expenseRenderer.setColor(Color.BLUE);
    expenseRenderer.setPointStyle(PointStyle.CIRCLE);
    expenseRenderer.setFillPoints(true);
    expenseRenderer.setLineWidth(3);
    expenseRenderer.setDisplayChartValues(true);


    // Creating a XYMultipleSeriesRenderer to customize the whole chart
    XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
    multiRenderer.setXLabels(0);
    multiRenderer.setChartTitle("Income vs Expense Chart");
    multiRenderer.setXTitle("Year 2011");
    multiRenderer.setYTitle("Amount in Rupees");
    multiRenderer.setZoomButtonsVisible(true);              
    for(int i=0;i<x.length;i++){
        multiRenderer.addXTextLabel(i+1, mMonth[i]);            
    }       

    // Adding incomeRenderer and expenseRenderer to multipleRenderer
    // Note: The order of adding dataseries to dataset and renderers to multipleRenderer
    // should be same
    multiRenderer.addSeriesRenderer(incomeRenderer);
    multiRenderer.addSeriesRenderer(expenseRenderer);

    // Creating an intent to plot line chart using dataset and multipleRenderer
    Intent intent = ChartFactory.getLineChartIntent(getBaseContext(), dataset, multiRenderer);

    // Start Activity
    startActivity(intent);
}
}

1 个答案:

答案 0 :(得分:0)

听起来你正想控制图表的可见区域:

mRenderer.setXAxisMin(minValue);
mRenderer.setXAxisMax(maxValue);