Skip to content

Commit

Permalink
Added example with multiple chart types inside a ListView.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed Aug 4, 2014
1 parent 52a3240 commit 460e896
Show file tree
Hide file tree
Showing 14 changed files with 463 additions and 19 deletions.
7 changes: 4 additions & 3 deletions MPChartExample/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxmassdeveloper.mpchartexample"
android:versionCode="7"
android:versionName="1.0.7" >
android:versionCode="8"
android:versionName="1.0.8" >

<uses-sdk
android:minSdkVersion="9"
Expand Down Expand Up @@ -32,7 +32,8 @@
<activity android:name="DrawChartActivity"></activity>
<activity android:name="ScatterChartActivity"></activity>
<activity android:name="com.xxmassdeveloper.mpchartexample.fragments.SimpleChartDemo"></activity>
<activity android:name="ListViewChartActivity"></activity>
<activity android:name="ListViewBarChartActivity"></activity>
<activity android:name="ListViewMultiChartActivity"></activity>
</application>

</manifest>
4 changes: 2 additions & 2 deletions MPChartExample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId 'com.xxmassdeveloper.mpchartexample'
minSdkVersion 16
targetSdkVersion 19
versionCode 7
versionName '1.0.7'
versionCode 8
versionName '1.0.8'

sourceSets {
main {
Expand Down
2 changes: 1 addition & 1 deletion MPChartExample/res/layout/list_item_barchart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="180dp" />
android:layout_height="200dp" />

</LinearLayout>
12 changes: 12 additions & 0 deletions MPChartExample/res/layout/list_item_linechart.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="200dp" />

</LinearLayout>
12 changes: 12 additions & 0 deletions MPChartExample/res/layout/list_item_piechart.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="330dp" />

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @author Philipp Jahoda
*/
public class ListViewChartActivity extends DemoBase {
public class ListViewBarChartActivity extends DemoBase {

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -125,7 +125,7 @@ private ChartData generateData(int cnt) {
ArrayList<Entry> entries = new ArrayList<Entry>();

for (int i = 0; i < 12; i++) {
entries.add(new Entry((int) (Math.random() * 100), i));
entries.add(new Entry((int) (Math.random() * 70) + 30, i));
}

DataSet d = new DataSet(entries, "New DataSet " + cnt);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@

package com.xxmassdeveloper.mpchartexample;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.data.DataSet;
import com.github.mikephil.charting.data.Entry;
import com.xxmassdeveloper.mpchartexample.listviewitems.BarChartItem;
import com.xxmassdeveloper.mpchartexample.listviewitems.ChartItem;
import com.xxmassdeveloper.mpchartexample.listviewitems.LineChartItem;
import com.xxmassdeveloper.mpchartexample.listviewitems.PieChartItem;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

import java.util.ArrayList;
import java.util.List;

/**
* Demonstrates the use of charts inside a ListView. IMPORTANT: provide a
* specific height attribute for the chart inside your listview-item
*
* @author Philipp Jahoda
*/
public class ListViewMultiChartActivity extends DemoBase {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_listview_chart);

ListView lv = (ListView) findViewById(R.id.listView1);

ArrayList<ChartItem> list = new ArrayList<ChartItem>();

// 30 items
for (int i = 0; i < 30; i++) {

if(i % 3 == 0) {
list.add(new LineChartItem(generateData(i + 1), getApplicationContext()));
} else if(i % 3 == 1) {
list.add(new BarChartItem(generateData(i + 1), getApplicationContext()));
} else if(i % 3 == 2) {
list.add(new PieChartItem(generatePieChartData(i + 1), getApplicationContext()));
}
}

ChartDataAdapter cda = new ChartDataAdapter(getApplicationContext(), list);
lv.setAdapter(cda);
}

/** adapter that supports 3 different item types */
private class ChartDataAdapter extends ArrayAdapter<ChartItem> {

public ChartDataAdapter(Context context, List<ChartItem> objects) {
super(context, 0, objects);
}

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

@Override
public int getItemViewType(int position) {
// return the views type
return getItem(position).getItemType();
}

@Override
public int getViewTypeCount() {
return 3; // we have 3 different item-types
}
}

/**
* generates a random ChartData object with just one DataSet
*
* @return
*/
private ChartData generateData(int cnt) {

ArrayList<Entry> entries = new ArrayList<Entry>();

for (int i = 0; i < 12; i++) {
entries.add(new Entry((int) (Math.random() * 70) + 30, i));
}

DataSet d = new DataSet(entries, "New DataSet " + cnt);

ChartData cd = new ChartData(getMonths(), d);
return cd;
}

private ChartData generatePieChartData(int cnt) {

ArrayList<Entry> entries = new ArrayList<Entry>();

for (int i = 0; i < 4; i++) {
entries.add(new Entry((int) (Math.random() * 70) + 30, i));
}

DataSet d = new DataSet(entries, "New DataSet " + cnt);

ChartData cd = new ChartData(getQuarters(), d);
return cd;
}

private ArrayList<String> getQuarters() {

ArrayList<String> q = new ArrayList<String>();
q.add("1st Quarter");
q.add("2nd Quarter");
q.add("3rd Quarter");
q.add("4th Quarter");

return q;
}

private ArrayList<String> getMonths() {

ArrayList<String> m = new ArrayList<String>();
m.add("Jan");
m.add("Feb");
m.add("Mar");
m.add("Apr");
m.add("May");
m.add("Jun");
m.add("Jul");
m.add("Aug");
m.add("Sep");
m.add("Okt");
m.add("Nov");
m.add("Dec");

return m;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected ChartData generateLessData() {
for(int i = 0; i < count; i++) {
xVals.add("entry" + (i+1));

entries1.add(new Entry((float) (Math.random() * 100), i));
entries1.add(new Entry((float) (Math.random() * 60) + 40, i));
}

DataSet ds1 = new DataSet(entries1, "Quarterly Revenues 2014");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.xxmassdeveloper.mpchartexample.listviewitems;

import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.ChartData;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.XLabels;
import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
import com.xxmassdeveloper.mpchartexample.R;

public class BarChartItem extends ChartItem {

private ColorTemplate mCt;
private Typeface mTf;

public BarChartItem(ChartData cd, Context c) {
super(cd);

mCt = new ColorTemplate();
mCt.addDataSetColors(ColorTemplate.VORDIPLOM_COLORS, c);
mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf");
}

@Override
public int getItemType() {
return TYPE_BARCHART;
}

@Override
public View getView(int position, View convertView, Context c) {

ViewHolder holder = null;

if (convertView == null) {

holder = new ViewHolder();

convertView = LayoutInflater.from(c).inflate(
R.layout.list_item_barchart, null);
holder.chart = (BarChart) convertView.findViewById(R.id.chart);

convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}

// apply styling
holder.chart.setYLabelCount(5);
holder.chart.setColorTemplate(mCt);
holder.chart.setBarSpace(20f);
holder.chart.setYLabelTypeface(mTf);
holder.chart.setXLabelTypeface(mTf);
holder.chart.setValueTypeface(mTf);
holder.chart.setDescription("");
holder.chart.setDrawVerticalGrid(false);
holder.chart.setDrawGridBackground(false);

XLabels xl = holder.chart.getXLabels();
xl.setCenterXLabelText(true);
xl.setPosition(XLabelPosition.BOTTOM);

// set data
holder.chart.setData(mChartData);

// do not forget to refresh the chart
holder.chart.invalidate();

return convertView;
}

private static class ViewHolder {
BarChart chart;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.xxmassdeveloper.mpchartexample.listviewitems;

import android.content.Context;
import android.view.View;

import com.github.mikephil.charting.data.ChartData;

/**
* baseclass of the chart-listview items
* @author philipp
*
*/
public abstract class ChartItem {

protected static final int TYPE_BARCHART = 0;
protected static final int TYPE_LINECHART = 1;
protected static final int TYPE_PIECHART = 2;

protected ChartData mChartData;

public ChartItem(ChartData cd) {
this.mChartData = cd;
}

public abstract int getItemType();

public abstract View getView(int position, View convertView, Context c);
}
Loading

0 comments on commit 460e896

Please sign in to comment.