7

I'm new in android and I want to use achartengine to implement time chart that represent the daily outgoing call durations from call log.

Does any one know how can I do this and what should I do?

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Basant
  • 305
  • 1
  • 8
  • 23

1 Answers1

24

So you need a layout (main.xml):

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/chart"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" />

    <LinearLayout
        android:id="@+id/layXzoom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:orientation="horizontal"
        android:padding="5dp" >
    </LinearLayout>

</RelativeLayout>

and activity (TestgrafActivity.java):

    package si.pd.testgraf;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

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

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;

public class TestgrafActivity extends Activity {
    /** Called when the activity is first created. */
    private XYMultipleSeriesDataset mDataset;
    private XYMultipleSeriesRenderer mRenderer;
    List<double[]> values = new ArrayList<double[]>();
    private GraphicalView mChartView;
    private TimeSeries time_series;

    // chart container
    private LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        layout = (LinearLayout) findViewById(R.id.chart);

        // create dataset and renderer
        mDataset = new XYMultipleSeriesDataset();
        mRenderer = new XYMultipleSeriesRenderer();
        mRenderer.setAxisTitleTextSize(16);
        mRenderer.setChartTitleTextSize(20);
        mRenderer.setLabelsTextSize(15);
        mRenderer.setLegendTextSize(15);
        mRenderer.setPointSize(3f);

        XYSeriesRenderer r = new XYSeriesRenderer();
        r.setColor(Color.GREEN);
        r.setPointStyle(PointStyle.CIRCLE);
        r.setFillPoints(true);
        mRenderer.addSeriesRenderer(r);
        mRenderer.setClickEnabled(true);
        mRenderer.setSelectableBuffer(20);
        mRenderer.setPanEnabled(true);

        time_series = new TimeSeries("test");

        mDataset.addSeries(time_series);

        fillData();

        mChartView = ChartFactory.getTimeChartView(this, mDataset, mRenderer,
                "H:mm:ss");

        layout.addView(mChartView);
    }

    private void fillData() {
        long value = new Date().getTime() - 3 * TimeChart.DAY;
        for (int i = 0; i < 100; i++) {
            time_series.add(new Date(value + i * TimeChart.DAY / 4), i);
        }
    }
}

I didn't tested the code so maybe there are some errors. Just wanted that you get the picture.

I did test at home. If you give me an email, I can send you eclipse project.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
toni
  • 361
  • 7
  • 19
  • thanks Toni ,i'll test this code and i think it will help me so much and this is my email basant-mohamed@hotmail.com – Basant Jan 31 '12 at 12:31
  • please rate it useful then @AshishDwivedi :) – toni Mar 20 '12 at 17:13
  • Very useful code! Could you explain what is the purpose of layXzoom? It seems you didn't include its code! But anyway, great place to start aChart!! – Radu Apr 19 '12 at 10:24
  • @Radu : layXzoom is just a LinearLayout view, which I forgot to remove from example. I used it in one of my projects for X-axis zoom buttons container. :) Please rate it if it is useful to you! Thanks, Toni – toni Apr 19 '12 at 16:35
  • Already rated it! Good stuff, got me started - I can take it from here, thanks again! – Radu Apr 20 '12 at 13:27
  • @toni:Hello ,can you help me with this please? http://stackoverflow.com/questions/16014103/achartengine-cant-figure-how-to-use-dates-as-x-axis-the-file-i-save-is-empt/16048188?noredirect=1#16048188 – George Apr 18 '13 at 13:18
  • @toni:Ok I did it!Following your example!Thanks a lot! (upvoted) – George Apr 18 '13 at 13:41
  • @George: sorry I didn't saw the comment... but great that you did it :) – toni Apr 19 '13 at 06:32
  • @toni, your answer duplicates the X-axis and all of my graph lines are in a straight line vertically instead. Here's how the X-axis displayed: 00:00:00 - 00:00:00 - 01:00:00 - 01:00:00 – David Dimalanta Nov 07 '14 at 02:19