使用AChartEngine库的条形图

时间:2013-04-01 06:23:48

标签: android graph bar-chart achartengine unix-timestamp

我有一个使用AChartEngine库的条形图,如下所示::

public class MainActivity extends Activity {

  private String[] mMonth = new String[] {
            "Jan", "Feb" , "Mar", "Apr", "May", "Jun",
            "Jul", "Aug" , "Sep", "Oct", "Nov", "Dec"
        };

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Getting reference to the button btn_chart
    Button btnChart = (Button) findViewById(R.id.btn_chart);

    // Defining click event listener for the button btn_chart
    OnClickListener clickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Draw the Income vs Expense Chart
            openChart();                
        }
    };

    // Setting event click listener for the button btn_chart of the MainActivity layout
    btnChart.setOnClickListener(clickListener);

}

private void openChart(){
    int[] x = { 0,1,2,3,4,5,6,7 };
    int[] income = { 2000,2500,2700,3000,2800,3500,3700,3800};
    int[] expense = {2200, 2700, 2900, 2800, 2600, 3000, 3300, 3400 };

    // Creating an  XYSeries for Income
    //CategorySeries incomeSeries = new CategorySeries("Income");
    XYSeries incomeSeries = new XYSeries("Income");
    // Creating an  XYSeries for Income
    XYSeries expenseSeries = new XYSeries("Expense");
    // Adding data to Income and Expense Series
    for(int i=0;i<x.length;i++){            
        incomeSeries.add(i,income[i]);
        expenseSeries.add(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.rgb(130, 130, 230));
    incomeRenderer.setFillPoints(true);
    incomeRenderer.setLineWidth(2);
    incomeRenderer.setDisplayChartValues(true);

    // Creating XYSeriesRenderer to customize expenseSeries
    XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
    expenseRenderer.setColor(Color.rgb(220, 80, 80));
    expenseRenderer.setFillPoints(true);
    expenseRenderer.setLineWidth(2);
    expenseRenderer.setDisplayChartValues(true);        
    Calendar cal = Calendar.getInstance();
    cal.clear(Calendar.HOUR);
    // Creating a XYMultipleSeriesRenderer to customize the whole chart
    XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
    multiRenderer.setXLabels(0);
    multiRenderer.setChartTitle("Income vs Expense Chart");
    multiRenderer.setXTitle("Year 2012");
    multiRenderer.setYTitle("Amount in Dollars");
    multiRenderer.setZoomButtonsVisible(true);              
    for(int i=0; i< x.length;i++){
        multiRenderer.addXTextLabel(i, 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 bar chart using dataset and multipleRenderer      
    Intent intent = ChartFactory.getBarChartIntent(getBaseContext(), dataset, multiRenderer, Type.DEFAULT);

    // Start Activity
    startActivity(intent);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
 }
}

以上代码图如下所示:

pic http://i49.tinypic.com/2j5xhrs.png

但我需要绘制一个具有以下属性的图表:

  1. X轴必须显示当前时间。如下图所示 是某些地方&gt;凌晨2点
  2. X轴上的每个条形为5分钟柱
  3. 我每隔5分钟发送一个数据就应该根据它绘制一个条形图 值。
  4. 现在忽略Y轴参数只需要如何绘图所需 X-aixs
  5. 图表看起来像这样:

    pic http://i46.tinypic.com/13ypbw7.png

3 个答案:

答案 0 :(得分:5)

我做了类似这样的事情::用于静态数据

public class MainActivity extends Activity {
 final Calendar c = Calendar.getInstance();
        int mMinute = c.get(Calendar.MINUTE);
        int mHour = c.get(Calendar.HOUR_OF_DAY);
        int am =c.get(Calendar.AM_PM); 
       int[] x =new int[1920];
        int[] sleep = {4,3,2,1,4,3,2,1,4,3,2,1,4,1,1,1,4,4,2,2,2,3,3,2,2,2,2,2,1,1,1,1,1,1,4,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2};      
  public String getnext(int a)
  {
      String ap;
      if(am==1){ap="PM";}
      else{ap="AM";}
    String s= mHour+" "+ap;
    mMinute=mMinute+5;

    if (mHour >=12){
        mHour=mHour-12;
        switch(am){case 0:am=1; break; case 1:am=0;break;}
    }
        if(mMinute >= 60)
        {
            mHour= mHour+1;
            mMinute=mMinute-60;
        }
        //Log.d("Gr","mMinute: "+mMinute);
        if(mMinute==1 | mMinute==2 | mMinute==3 |mMinute==4 | mMinute==0)
        {s= mHour+" "+ap;}
        else{s="";}

        return (s);}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  //  setContentView(R.layout.activity_main);

    openChart();
   }


private void openChart(){
    XYSeries sleepSeries = new XYSeries("Sleep");

    for(int i=0;i<sleep.length;i++){   
        sleepSeries.add(i,sleep[i]);
    }


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


    // Creating XYSeriesRenderer to customize incomeSeries
    XYSeriesRenderer sleepRenderer = new XYSeriesRenderer();
    sleepRenderer.setColor(Color.GREEN);
    sleepRenderer.setFillPoints(true);

    sleepRenderer.setLineWidth((float) .2);

    // Creating a XYMultipleSeriesRenderer to customize the whole chart
    XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
    multiRenderer.setXLabels(0);
    multiRenderer.setYAxisMin(0);
    multiRenderer.setYLabels(0);
    multiRenderer.setYAxisMax(4);
    multiRenderer.setChartTitle("Sleep vs Time"); 
    multiRenderer.setBarSpacing(.5);
    //multiRenderer.setZoomButtonsVisible(true);
    multiRenderer.setLegendHeight((int) 5);
    multiRenderer.setPanEnabled(true, false);

    multiRenderer.addSeriesRenderer(sleepRenderer);

    // Creating an intent to plot bar chart using dataset and multipleRenderer      
    Intent intent = ChartFactory.getBarChartIntent(getBaseContext(), dataset, renderer, Type.DEFAULT);

    // Start Activity
    startActivity(intent);

}

我得到的图像

df http://i50.tinypic.com/jqtlpl.png

更新:::

用于实时数据,更新系列如

 static ArrayList<Integer> sleep = new ArrayList<Integer>();

当newval可用时

 sleep.add(newval);

然后在第一个清晰数据集(openChart();)中调用dataset.clear(); 然后拨打repaint();

答案 1 :(得分:4)

您可以在X轴上保留Unix时间戳并自行控制标签。您可以使用以下方法添加自定义X轴标签:

renderer.addXTextLabel(x, "label");

Unix时间戳是Date对象上的getTime()返回的值。因此,您可以将这些值添加到系列中:

series.add(date.getTime(), value);

然后,您可以为某些值添加自定义标签:

SimpleDateFormat format = new SimpleDateFormat("h a");
renderer.addXTextLabel(date.getTime(), format.format(date.getTime()));

答案 2 :(得分:0)

我认为根据您的要求,创建自定义条形图将变得简单。这是一个示例,您可以通过简单地使用5个要在图表上显示的数组来开发此示例。

Bar Chart in Android With out any Built in jars

只是,仔细检查并检查它。

主要优点是您无需使用任何预定义的库来绘制图表。它可以用简单的Android UI如TextView and ListView