diff --git a/MPChartExample/AndroidManifest.xml b/MPChartExample/AndroidManifest.xml index c819be2a8..b54f9a0b9 100644 --- a/MPChartExample/AndroidManifest.xml +++ b/MPChartExample/AndroidManifest.xml @@ -1,8 +1,8 @@ + android:versionCode="8" + android:versionName="1.0.8" > - + + diff --git a/MPChartExample/build.gradle b/MPChartExample/build.gradle index 055e5a9c8..3546d1c4f 100644 --- a/MPChartExample/build.gradle +++ b/MPChartExample/build.gradle @@ -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 { diff --git a/MPChartExample/res/layout/list_item_barchart.xml b/MPChartExample/res/layout/list_item_barchart.xml index cad04cf5c..81dbb27b8 100644 --- a/MPChartExample/res/layout/list_item_barchart.xml +++ b/MPChartExample/res/layout/list_item_barchart.xml @@ -7,6 +7,6 @@ + android:layout_height="200dp" /> diff --git a/MPChartExample/res/layout/list_item_linechart.xml b/MPChartExample/res/layout/list_item_linechart.xml new file mode 100644 index 000000000..984f62abf --- /dev/null +++ b/MPChartExample/res/layout/list_item_linechart.xml @@ -0,0 +1,12 @@ + + + + + + diff --git a/MPChartExample/res/layout/list_item_piechart.xml b/MPChartExample/res/layout/list_item_piechart.xml new file mode 100644 index 000000000..a473dd5d6 --- /dev/null +++ b/MPChartExample/res/layout/list_item_piechart.xml @@ -0,0 +1,12 @@ + + + + + + diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewBarChartActivity.java similarity index 97% rename from MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewChartActivity.java rename to MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewBarChartActivity.java index 3b906a2a2..1f202f9f0 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewChartActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewBarChartActivity.java @@ -29,7 +29,7 @@ * * @author Philipp Jahoda */ -public class ListViewChartActivity extends DemoBase { +public class ListViewBarChartActivity extends DemoBase { @Override protected void onCreate(Bundle savedInstanceState) { @@ -125,7 +125,7 @@ private ChartData generateData(int cnt) { ArrayList entries = new ArrayList(); 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); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewMultiChartActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewMultiChartActivity.java new file mode 100644 index 000000000..cd3a7ecf6 --- /dev/null +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/ListViewMultiChartActivity.java @@ -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 list = new ArrayList(); + + // 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 { + + public ChartDataAdapter(Context context, List 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 entries = new ArrayList(); + + 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 entries = new ArrayList(); + + 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 getQuarters() { + + ArrayList q = new ArrayList(); + q.add("1st Quarter"); + q.add("2nd Quarter"); + q.add("3rd Quarter"); + q.add("4th Quarter"); + + return q; + } + + private ArrayList getMonths() { + + ArrayList m = new ArrayList(); + 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; + } +} diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java index 6a539c457..707d5e890 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/fragments/SimpleFragment.java @@ -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"); diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/BarChartItem.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/BarChartItem.java new file mode 100644 index 000000000..fec447400 --- /dev/null +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/BarChartItem.java @@ -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; + } +} diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/ChartItem.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/ChartItem.java new file mode 100644 index 000000000..9abd27c5e --- /dev/null +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/ChartItem.java @@ -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); +} diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/LineChartItem.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/LineChartItem.java new file mode 100644 index 000000000..2c6baa7a3 --- /dev/null +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/LineChartItem.java @@ -0,0 +1,80 @@ +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.LineChart; +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 LineChartItem extends ChartItem { + + private ColorTemplate mCt; + private Typeface mTf; + + public LineChartItem(ChartData cd, Context c) { + super(cd); + + mCt = new ColorTemplate(); + mCt.addDataSetColor(R.color.vordiplom_4, c); + mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf"); + } + + @Override + public int getItemType() { + return TYPE_LINECHART; + } + + @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_linechart, null); + holder.chart = (LineChart) 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.setLineWidth(3f); + holder.chart.setCircleSize(5f); + 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 { + LineChart chart; + } +} diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java new file mode 100644 index 000000000..8864ceb51 --- /dev/null +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/listviewitems/PieChartItem.java @@ -0,0 +1,82 @@ +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.PieChart; +import com.github.mikephil.charting.data.ChartData; +import com.github.mikephil.charting.utils.ColorTemplate; +import com.github.mikephil.charting.utils.Legend; +import com.github.mikephil.charting.utils.Legend.LegendPosition; +import com.xxmassdeveloper.mpchartexample.R; + +public class PieChartItem extends ChartItem { + + private ColorTemplate mCt; + private Typeface mTf; + + public PieChartItem(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_PIECHART; + } + + @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_piechart, null); + holder.chart = (PieChart) convertView.findViewById(R.id.chart); + + convertView.setTag(holder); + + } else { + holder = (ViewHolder) convertView.getTag(); + } + + // apply styling + holder.chart.setColorTemplate(mCt); + holder.chart.setValueTypeface(mTf); + holder.chart.setDescription(""); + holder.chart.setHoleRadius(60f); + holder.chart.setTransparentCircleRadius(65f); + holder.chart.setCenterText("MPChart\nAndroid"); + holder.chart.setCenterTextTypeface(mTf); + holder.chart.setCenterTextSize(18f); + holder.chart.setDrawXValues(false); + holder.chart.setUsePercentValues(true); + + // space between slices + holder.chart.setSliceSpace(5f); + + // set data + holder.chart.setData(mChartData); + + Legend l = holder.chart.getLegend(); + l.setPosition(LegendPosition.RIGHT_OF_CHART); + + // do not forget to refresh the chart + holder.chart.invalidate(); + + return convertView; + } + + private static class ViewHolder { + PieChart chart; + } +} diff --git a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.java b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.java index 9ef9ee3ba..f6a7b0475 100644 --- a/MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.java +++ b/MPChartExample/src/com/xxmassdeveloper/mpchartexample/notimportant/MainActivity.java @@ -22,7 +22,8 @@ import com.xxmassdeveloper.mpchartexample.BarChartActivityMultiDataset; import com.xxmassdeveloper.mpchartexample.DrawChartActivity; import com.xxmassdeveloper.mpchartexample.LineChartActivity; -import com.xxmassdeveloper.mpchartexample.ListViewChartActivity; +import com.xxmassdeveloper.mpchartexample.ListViewBarChartActivity; +import com.xxmassdeveloper.mpchartexample.ListViewMultiChartActivity; import com.xxmassdeveloper.mpchartexample.MultiLineChartActivity; import com.xxmassdeveloper.mpchartexample.PieChartActivity; import com.xxmassdeveloper.mpchartexample.R; @@ -57,8 +58,11 @@ protected void onCreate(Bundle savedInstanceState) { "Charts in Fragments, awesome design.", "Demonstration of charts inside Fragments. In this example the focus was on the design and look and feel of the chart.")); objects.add(new ContentItem( - "Charts inside a ListView", - "Demonstrates the usage of different charts inside a ListView.")); + "BarChart inside ListView", + "Demonstrates the usage of a BarChart inside a ListView item.")); + objects.add(new ContentItem( + "Multiple charts inside ListView", + "Demonstrates the usage of different chart types inside a ListView.")); MyAdapter adapter = new MyAdapter(this, objects); @@ -108,7 +112,11 @@ public void onItemClick(AdapterView av, View v, int pos, long arg3) { startActivity(i); break; case 8: - i = new Intent(this, ListViewChartActivity.class); + i = new Intent(this, ListViewBarChartActivity.class); + startActivity(i); + break; + case 9: + i = new Intent(this, ListViewMultiChartActivity.class); startActivity(i); break; } diff --git a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java index 7557a029e..21dfc50e3 100644 --- a/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -501,7 +501,7 @@ protected void calcMinMax(boolean fixedValues) { // additional handling for space (default 10% space), spacing only // applies with non-rounded y-label - float space = mDeltaY / 100f * 10f; + float space = mDeltaY / 100f * 15f; if (mStartAtZero) { mYChartMin = 0; @@ -521,16 +521,13 @@ protected void prepareXLabels() { StringBuffer a = new StringBuffer(); - int length = (int) (((float) (mCurrentData.getXVals().get(0).length() + mCurrentData + float length = (int) (((float) (mCurrentData.getXVals().get(0).length() + mCurrentData .getXVals() .get(mCurrentData.getXValCount() - 1) .length()))); - if (mCurrentData.getXVals().get(0).length() <= 3) - length *= 2; - for (int i = 0; i < length; i++) { - a.append("h"); + a.append("H"); } mXLabels.mXLabelWidth = Utils.calcTextWidth(mXLabelPaint, a.toString());