-
-
Notifications
You must be signed in to change notification settings - Fork 9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example with multiple chart types inside a ListView.
- Loading branch information
Showing
14 changed files
with
463 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewMultiChartActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/BarChartItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/ChartItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.