Wiki

Clone wiki

Graphlib / Home

#Graphlib

This library has been discontinued. We advise people to use the MPAndroidChart library instead. You can check it out at https://github.com/PhilJay/MPAndroidChart

Graphs for Android made easy !

Graphlib is an Android library that makes it easy to integrate graphs in your app, by leveraging Android's all known Adapter component. It supports 3 types of graph representations: line, bar or pie. Use whichever you think works best for your app. See screenshots of the three at the bottom of this Wiki.

Graphlib supports Android v2.2 (Froyo, API 8) and above.

Graphlib is currently in alpha and is actively being worked on. We do not recommend using it in production apps yet, unless you acknowledge its lack of stability. We do however accept ideas and fixes.

#Installing

###Maven Add the following Maven dependency exchanging x.x.x for the latest release.

<dependency>
    <groupId>com.mobigosoft</groupId>
    <artifactId>graphlib</artifactId>
    <version>x.x.x</version>
</dependency>

###Gradle Add the following Gradle dependency. By using '+' as the version, you automatically get the new library version after each release.

dependencies {
    compile 'com.mobigosoft:graphlib:+'
}

#Getting Started

1) Add the graph you want to your activity or fragment XML file. BarGraph for bar, LineGraph for line and PieGraph for pie.

 <com.mobigosoft.graphlib.view.BarGraph
            android:id="@+id/activity_main_bargraph"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

2) Get the reference to the added graph inside your activity or fragment class. You'd usually do this in onCreate() for Activities or onCreateView() for Fragments. Then set your adapter.

  BarGraph barGraph = (BarGraph) findViewById(R.id.activity_main_bargraph);
  MyAdapter myAdapter = new MyAdapter();
  barGraph.setAdapter(myAdapter);

3) Create your adapter (called MyAdapter in the above example). It would look something like this if your data model were a Person. Your adapter must implement the graph corresponding interface in order to provide the functionality needed for the graph to draw its data. The available interfaces are named conveniently after their graph counterparts: BarGraphAdapter, LineGraphAdapter and PieGraphAdapter. Please note however that PieGraphAdapter does not support labels, as the representation for pie labels can vary much more than for the other types of graphs. It does however support displaying the pie slice values.

public class PeopleBarGraphAdapter extends BaseAdapter implements BarGraphAdapter
{
    private ArrayList<Person> mPeopleList = new ArrayList<Person>();


    public PeopleBarGraphAdapter() {}

    public void setList(ArrayList<Person> list)
    {
        mPeopleList = list;
    }

    @Override
    public int getCount()
    {
        return mPeopleList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mPeopleList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) { return null; }


    @Override
    public int getBarsCount()
    {
        return mPeopleList.size();
    }

    @Override
    public int getBarColor(int position)
    {
        return mPeopleList.get(position).getColor();
    }

    @Override
    public int getLabelColor(int position)
    {
        return mPeopleList.get(position).getColor();
    }

    @Override
    public int getValue(int position)
    {
        return mPeopleList.get(position).getAge();
    }

    @Override
    public String getLabel(int position)
    {
        return mPeopleList.get(position).getName();
    }
}

4) If you need to support multiple random colors for your bar or pie graphs, you can use the ColorGenerator class to generate some colors in your app.

5) For more detailed examples, you can have a look at the samples included with the library.

#Screenshots

Screenshot_2014-06-15-23-16-42.png Screenshot_2014-06-15-23-16-54.png Screenshot_2014-06-15-23-17-01.png

Updated