diff --git a/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt b/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt index b2dc828522..68dfaa2cae 100644 --- a/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/BarChartActivity.kt @@ -31,8 +31,8 @@ import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.Fill import com.github.mikephil.charting.utils.MPPointF import info.appdev.chartexample.DataTools.Companion.getValues -import info.appdev.chartexample.custom.DayAxisValueFormatter -import info.appdev.chartexample.custom.MyAxisValueFormatter +import info.appdev.chartexample.formatter.DayAxisValueFormatter +import info.appdev.chartexample.formatter.MyAxisValueFormatter import info.appdev.chartexample.custom.XYMarkerView import info.appdev.chartexample.notimportant.DemoBase diff --git a/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt b/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt index 300b6106a1..a358dfebeb 100644 --- a/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/ListViewBarChartActivity.kt @@ -27,8 +27,6 @@ import androidx.core.net.toUri /** * Demonstrates the use of charts inside a ListView. IMPORTANT: provide a * specific height attribute for the chart inside your ListView item - * - * @author Philipp Jahoda */ class ListViewBarChartActivity : DemoBase() { override fun onCreate(savedInstanceState: Bundle?) { diff --git a/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt b/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt index e7460e2f5b..270a5877ce 100644 --- a/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/ListViewMultiChartActivity.kt @@ -65,15 +65,15 @@ class ListViewMultiChartActivity : DemoBase() { } /** adapter that supports 3 different item types */ - private class ChartDataAdapter(context: Context, objects: MutableList) : ArrayAdapter(context, 0, objects) { + private class ChartDataAdapter(context: Context, objects: MutableList) : ArrayAdapter(context, 0, objects) { override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { - return getItem(position)!!.getView(position, convertView, context) + return getItem(position)!!.getView(position, convertView, context)!! } override fun getItemViewType(position: Int): Int { // return the views type val ci = getItem(position) - return if (ci != null) ci.getItemType() else 0 + return if (ci != null) ci.itemType else 0 } override fun getViewTypeCount(): Int { diff --git a/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt b/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt index e19a6d1da8..352dfdaaf2 100644 --- a/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt +++ b/app/src/main/java/info/appdev/chartexample/StackedBarActivity.kt @@ -26,8 +26,8 @@ import com.github.mikephil.charting.interfaces.datasets.IBarDataSet import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.ColorTemplate import info.appdev.chartexample.DataTools.Companion.getValues -import info.appdev.chartexample.custom.MyAxisValueFormatter -import info.appdev.chartexample.custom.MyValueFormatter +import info.appdev.chartexample.formatter.MyAxisValueFormatter +import info.appdev.chartexample.formatter.MyValueFormatter import info.appdev.chartexample.notimportant.DemoBase class StackedBarActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { diff --git a/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.java b/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.java deleted file mode 100644 index 873f438632..0000000000 --- a/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.java +++ /dev/null @@ -1,31 +0,0 @@ -package info.appdev.chartexample.custom; - -import android.graphics.Canvas; -import android.graphics.Paint; - -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.renderer.scatter.IShapeRenderer; -import com.github.mikephil.charting.utils.Utils; -import com.github.mikephil.charting.utils.ViewPortHandler; - -/** - * Custom shape renderer that draws a single line. - * Created by philipp on 26/06/16. - */ -public class CustomScatterShapeRenderer implements IShapeRenderer -{ - - @Override - public void renderShape(Canvas c, IScatterDataSet dataSet, ViewPortHandler viewPortHandler, - float posX, float posY, Paint renderPaint) { - - final float shapeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeSize()) / 2f; - - c.drawLine( - posX - shapeHalf, - posY - shapeHalf, - posX + shapeHalf, - posY + shapeHalf, - renderPaint); - } -} diff --git a/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.kt b/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.kt new file mode 100644 index 0000000000..8d3ddfab20 --- /dev/null +++ b/app/src/main/java/info/appdev/chartexample/custom/CustomScatterShapeRenderer.kt @@ -0,0 +1,28 @@ +package info.appdev.chartexample.custom + +import android.graphics.Canvas +import android.graphics.Paint +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import com.github.mikephil.charting.renderer.scatter.IShapeRenderer +import com.github.mikephil.charting.utils.Utils +import com.github.mikephil.charting.utils.ViewPortHandler + +/** + * Custom shape renderer that draws a single line. + */ +class CustomScatterShapeRenderer : IShapeRenderer { + override fun renderShape( + canvas: Canvas, dataSet: IScatterDataSet, viewPortHandler: ViewPortHandler?, + posX: Float, posY: Float, renderPaint: Paint + ) { + val shapeHalf = Utils.convertDpToPixel(dataSet.getScatterShapeSize()) / 2f + + canvas.drawLine( + posX - shapeHalf, + posY - shapeHalf, + posX + shapeHalf, + posY + shapeHalf, + renderPaint + ) + } +} diff --git a/app/src/main/java/info/appdev/chartexample/custom/DayAxisValueFormatter.kt b/app/src/main/java/info/appdev/chartexample/formatter/DayAxisValueFormatter.kt similarity index 98% rename from app/src/main/java/info/appdev/chartexample/custom/DayAxisValueFormatter.kt rename to app/src/main/java/info/appdev/chartexample/formatter/DayAxisValueFormatter.kt index 3dd38d72ee..a3c0012ab8 100644 --- a/app/src/main/java/info/appdev/chartexample/custom/DayAxisValueFormatter.kt +++ b/app/src/main/java/info/appdev/chartexample/formatter/DayAxisValueFormatter.kt @@ -1,4 +1,4 @@ -package info.appdev.chartexample.custom +package info.appdev.chartexample.formatter import com.github.mikephil.charting.charts.BarLineChartBase import com.github.mikephil.charting.components.AxisBase diff --git a/app/src/main/java/info/appdev/chartexample/custom/MyAxisValueFormatter.kt b/app/src/main/java/info/appdev/chartexample/formatter/MyAxisValueFormatter.kt similarity index 90% rename from app/src/main/java/info/appdev/chartexample/custom/MyAxisValueFormatter.kt rename to app/src/main/java/info/appdev/chartexample/formatter/MyAxisValueFormatter.kt index 78ae2a1008..f0a4f70e67 100644 --- a/app/src/main/java/info/appdev/chartexample/custom/MyAxisValueFormatter.kt +++ b/app/src/main/java/info/appdev/chartexample/formatter/MyAxisValueFormatter.kt @@ -1,4 +1,4 @@ -package info.appdev.chartexample.custom +package info.appdev.chartexample.formatter import com.github.mikephil.charting.components.AxisBase import com.github.mikephil.charting.formatter.IAxisValueFormatter diff --git a/app/src/main/java/info/appdev/chartexample/custom/MyFillFormatter.kt b/app/src/main/java/info/appdev/chartexample/formatter/MyFillFormatter.kt similarity index 91% rename from app/src/main/java/info/appdev/chartexample/custom/MyFillFormatter.kt rename to app/src/main/java/info/appdev/chartexample/formatter/MyFillFormatter.kt index a63f824cb5..7abb0f7699 100644 --- a/app/src/main/java/info/appdev/chartexample/custom/MyFillFormatter.kt +++ b/app/src/main/java/info/appdev/chartexample/formatter/MyFillFormatter.kt @@ -1,4 +1,4 @@ -package info.appdev.chartexample.custom +package info.appdev.chartexample.formatter import com.github.mikephil.charting.formatter.IFillFormatter import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider @@ -10,4 +10,4 @@ class MyFillFormatter(private val fillPos: Float) : IFillFormatter { // your logic could be here return fillPos } -} +} \ No newline at end of file diff --git a/app/src/main/java/info/appdev/chartexample/custom/MyValueFormatter.kt b/app/src/main/java/info/appdev/chartexample/formatter/MyValueFormatter.kt similarity index 92% rename from app/src/main/java/info/appdev/chartexample/custom/MyValueFormatter.kt rename to app/src/main/java/info/appdev/chartexample/formatter/MyValueFormatter.kt index 701229650a..1fc517afbe 100644 --- a/app/src/main/java/info/appdev/chartexample/custom/MyValueFormatter.kt +++ b/app/src/main/java/info/appdev/chartexample/formatter/MyValueFormatter.kt @@ -1,4 +1,4 @@ -package info.appdev.chartexample.custom +package info.appdev.chartexample.formatter import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.formatter.IValueFormatter diff --git a/app/src/main/java/info/appdev/chartexample/custom/YearXAxisFormatter.kt b/app/src/main/java/info/appdev/chartexample/formatter/YearXAxisFormatter.kt similarity index 92% rename from app/src/main/java/info/appdev/chartexample/custom/YearXAxisFormatter.kt rename to app/src/main/java/info/appdev/chartexample/formatter/YearXAxisFormatter.kt index 036b172e5a..822d0007a3 100644 --- a/app/src/main/java/info/appdev/chartexample/custom/YearXAxisFormatter.kt +++ b/app/src/main/java/info/appdev/chartexample/formatter/YearXAxisFormatter.kt @@ -1,4 +1,4 @@ -package info.appdev.chartexample.custom +package info.appdev.chartexample.formatter import com.github.mikephil.charting.components.AxisBase import com.github.mikephil.charting.formatter.IAxisValueFormatter diff --git a/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.java b/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.java deleted file mode 100644 index 053211a060..0000000000 --- a/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.java +++ /dev/null @@ -1,113 +0,0 @@ -package info.appdev.chartexample.fragments; -import android.graphics.Typeface; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import android.util.Log; -import android.view.LayoutInflater; -import android.view.MotionEvent; -import android.view.View; -import android.view.ViewGroup; -import android.widget.FrameLayout; - -import com.github.mikephil.charting.charts.BarChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.listener.ChartTouchListener; -import com.github.mikephil.charting.listener.OnChartGestureListener; - -import info.appdev.chartexample.R; -import info.appdev.chartexample.custom.MyMarkerView; - - -public class BarChartFrag extends SimpleFragment implements OnChartGestureListener { - - @NonNull - public static Fragment newInstance() { - return new BarChartFrag(); - } - - private BarChart chart; - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.frag_simple_bar, container, false); - - // create a new chart object - chart = new BarChart(getActivity()); - chart.getDescription().setEnabled(false); - chart.setOnChartGestureListener(this); - - MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.custom_marker_view); - mv.setChartView(chart); // For bounds control - chart.setMarker(mv); - - chart.setDrawGridBackground(false); - chart.setDrawBarShadow(false); - - Typeface tf = Typeface.createFromAsset(requireContext().getAssets(), "OpenSans-Light.ttf"); - - chart.setData(generateBarData(1, 20000)); - - Legend l = chart.getLegend(); - l.setTypeface(tf); - - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setTypeface(tf); - leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) - - chart.getAxisRight().setEnabled(false); - - XAxis xAxis = chart.getXAxis(); - xAxis.setEnabled(false); - - // programmatically add the chart - FrameLayout parent = v.findViewById(R.id.parentLayout); - parent.addView(chart); - - return v; - } - - @Override - public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { - Log.i("Gesture", "START"); - } - - @Override - public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) { - Log.i("Gesture", "END"); - chart.highlightValues(null); - } - - @Override - public void onChartLongPressed(MotionEvent me) { - Log.i("LongPress", "Chart long pressed."); - } - - @Override - public void onChartDoubleTapped(MotionEvent me) { - Log.i("DoubleTap", "Chart double-tapped."); - } - - @Override - public void onChartSingleTapped(MotionEvent me) { - Log.i("SingleTap", "Chart single-tapped."); - } - - @Override - public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) { - Log.i("Fling", "Chart fling. VelocityX: " + velocityX + ", VelocityY: " + velocityY); - } - - @Override - public void onChartScale(MotionEvent me, float scaleX, float scaleY) { - Log.i("Scale / Zoom", "ScaleX: " + scaleX + ", ScaleY: " + scaleY); - } - - @Override - public void onChartTranslate(MotionEvent me, float dX, float dY) { - Log.i("Translate / Move", "dX: " + dX + ", dY: " + dY); - } - -} diff --git a/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.kt b/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.kt new file mode 100644 index 0000000000..3a3097baf9 --- /dev/null +++ b/app/src/main/java/info/appdev/chartexample/fragments/BarChartFrag.kt @@ -0,0 +1,97 @@ +package info.appdev.chartexample.fragments + +import android.graphics.Typeface +import android.os.Bundle +import android.util.Log +import android.view.LayoutInflater +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout +import androidx.fragment.app.Fragment +import com.github.mikephil.charting.charts.BarChart +import com.github.mikephil.charting.listener.ChartTouchListener.ChartGesture +import com.github.mikephil.charting.listener.OnChartGestureListener +import info.appdev.chartexample.R +import info.appdev.chartexample.custom.MyMarkerView + +class BarChartFrag : SimpleFragment(), OnChartGestureListener { + private var chart: BarChart? = null + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + val v = inflater.inflate(R.layout.frag_simple_bar, container, false) + + // create a new chart object + chart = BarChart(requireActivity()) + chart!!.description.isEnabled = false + chart!!.onChartGestureListener = this + + val mv = MyMarkerView(activity, R.layout.custom_marker_view) + mv.chartView = chart // For bounds control + chart!!.setMarker(mv) + + chart!!.setDrawGridBackground(false) + chart!!.setDrawBarShadow(false) + + val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") + + chart!!.setData(generateBarData(1, 20000f)) + + val l = chart!!.legend + l.typeface = tf + + val leftAxis = chart!!.axisLeft + leftAxis.typeface = tf + leftAxis.setAxisMinimum(0f) // this replaces setStartAtZero(true) + + chart!!.axisRight.isEnabled = false + + val xAxis = chart!!.xAxis + xAxis.isEnabled = false + + // programmatically add the chart + val parent = v.findViewById(R.id.parentLayout) + parent.addView(chart) + + return v + } + + override fun onChartGestureStart(me: MotionEvent?, lastPerformedGesture: ChartGesture?) { + Log.i("Gesture", "START") + } + + override fun onChartGestureEnd(me: MotionEvent?, lastPerformedGesture: ChartGesture?) { + Log.i("Gesture", "END") + chart!!.highlightValues(null) + } + + override fun onChartLongPressed(me: MotionEvent?) { + Log.i("LongPress", "Chart long pressed.") + } + + override fun onChartDoubleTapped(me: MotionEvent?) { + Log.i("DoubleTap", "Chart double-tapped.") + } + + override fun onChartSingleTapped(me: MotionEvent?) { + Log.i("SingleTap", "Chart single-tapped.") + } + + override fun onChartFling(me1: MotionEvent?, me2: MotionEvent?, velocityX: Float, velocityY: Float) { + Log.i("Fling", "Chart fling. VelocityX: $velocityX, VelocityY: $velocityY") + } + + override fun onChartScale(me: MotionEvent?, scaleX: Float, scaleY: Float) { + Log.i("Scale / Zoom", "ScaleX: $scaleX, ScaleY: $scaleY") + } + + override fun onChartTranslate(me: MotionEvent?, dX: Float, dY: Float) { + Log.i("Translate / Move", "dX: $dX, dY: $dY") + } + + companion object { + fun newInstance(): Fragment { + return BarChartFrag() + } + } +} diff --git a/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.java b/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.java deleted file mode 100644 index fd6aeb4cc0..0000000000 --- a/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.java +++ /dev/null @@ -1,56 +0,0 @@ -package info.appdev.chartexample.fragments; -import android.graphics.Typeface; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import info.appdev.chartexample.R; - -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; - - -public class ComplexityFragment extends SimpleFragment { - - @NonNull - public static Fragment newInstance() { - return new ComplexityFragment(); - } - - @SuppressWarnings("FieldCanBeLocal") - private LineChart chart; - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.frag_simple_line, container, false); - - chart = v.findViewById(R.id.lineChart1); - - chart.getDescription().setEnabled(false); - - chart.setDrawGridBackground(false); - - chart.setData(getComplexity()); - chart.animateX(3000); - - Typeface tf = Typeface.createFromAsset(requireContext().getAssets(), "OpenSans-Light.ttf"); - - Legend l = chart.getLegend(); - l.setTypeface(tf); - - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setTypeface(tf); - - chart.getAxisRight().setEnabled(false); - - XAxis xAxis = chart.getXAxis(); - xAxis.setEnabled(false); - - return v; - } -} diff --git a/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.kt b/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.kt new file mode 100644 index 0000000000..cc48c31277 --- /dev/null +++ b/app/src/main/java/info/appdev/chartexample/fragments/ComplexityFragment.kt @@ -0,0 +1,48 @@ +package info.appdev.chartexample.fragments + +import android.graphics.Typeface +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.github.mikephil.charting.charts.LineChart +import info.appdev.chartexample.R + +class ComplexityFragment : SimpleFragment() { + private var chart: LineChart? = null + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + val v = inflater.inflate(R.layout.frag_simple_line, container, false) + + chart = v.findViewById(R.id.lineChart1) + + chart!!.description.isEnabled = false + + chart!!.setDrawGridBackground(false) + + chart!!.setData(complexity) + chart!!.animateX(3000) + + val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") + + val l = chart!!.legend + l.typeface = tf + + val leftAxis = chart!!.axisLeft + leftAxis.typeface = tf + + chart!!.axisRight.isEnabled = false + + val xAxis = chart!!.xAxis + xAxis.isEnabled = false + + return v + } + + companion object { + fun newInstance(): Fragment { + return ComplexityFragment() + } + } +} diff --git a/app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.java b/app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.java deleted file mode 100644 index bd698348c0..0000000000 --- a/app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.java +++ /dev/null @@ -1,65 +0,0 @@ -package info.appdev.chartexample.fragments; -import android.graphics.Color; -import android.graphics.Typeface; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import info.appdev.chartexample.R; - -import android.text.SpannableString; -import android.text.style.ForegroundColorSpan; -import android.text.style.RelativeSizeSpan; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.github.mikephil.charting.charts.PieChart; -import com.github.mikephil.charting.components.Legend; - - -public class PieChartFrag extends SimpleFragment { - - @NonNull - public static Fragment newInstance() { - return new PieChartFrag(); - } - - @SuppressWarnings("FieldCanBeLocal") - private PieChart chart; - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.frag_simple_pie, container, false); - - chart = v.findViewById(R.id.pieChart1); - chart.getDescription().setEnabled(false); - - Typeface tf = Typeface.createFromAsset(requireContext().getAssets(), "OpenSans-Light.ttf"); - - chart.setCenterTextTypeface(tf); - chart.setCenterText(generateCenterText()); - chart.setCenterTextSize(10f); - chart.setCenterTextTypeface(tf); - - // radius of the center hole in percent of maximum radius - chart.setHoleRadius(45f); - chart.setTransparentCircleRadius(50f); - - Legend l = chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); - l.setOrientation(Legend.LegendOrientation.VERTICAL); - l.setDrawInside(false); - - chart.setData(generatePieData()); - - return v; - } - - private SpannableString generateCenterText() { - SpannableString s = new SpannableString("Revenues\nQuarters 2015"); - s.setSpan(new RelativeSizeSpan(2f), 0, 8, 0); - s.setSpan(new ForegroundColorSpan(Color.GRAY), 8, s.length(), 0); - return s; - } -} diff --git a/app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.kt b/app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.kt new file mode 100644 index 0000000000..8b86a1b59b --- /dev/null +++ b/app/src/main/java/info/appdev/chartexample/fragments/PieChartFrag.kt @@ -0,0 +1,60 @@ +package info.appdev.chartexample.fragments + +import android.graphics.Color +import android.graphics.Typeface +import android.os.Bundle +import android.text.SpannableString +import android.text.style.ForegroundColorSpan +import android.text.style.RelativeSizeSpan +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.github.mikephil.charting.charts.PieChart +import com.github.mikephil.charting.components.Legend +import info.appdev.chartexample.R + +class PieChartFrag : SimpleFragment() { + private var chart: PieChart? = null + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + val v = inflater.inflate(R.layout.frag_simple_pie, container, false) + + chart = v.findViewById(R.id.pieChart1) + chart!!.description.isEnabled = false + + val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") + + chart!!.setCenterTextTypeface(tf) + chart!!.centerText = generateCenterText() + chart!!.setCenterTextSize(10f) + chart!!.setCenterTextTypeface(tf) + + // radius of the center hole in percent of maximum radius + chart!!.holeRadius = 45f + chart!!.transparentCircleRadius = 50f + + val l = chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.TOP + l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + l.orientation = Legend.LegendOrientation.VERTICAL + l.setDrawInside(false) + + chart!!.setData(generatePieData()) + + return v + } + + private fun generateCenterText(): SpannableString { + val s = SpannableString("Revenues\nQuarters 2015") + s.setSpan(RelativeSizeSpan(2f), 0, 8, 0) + s.setSpan(ForegroundColorSpan(Color.GRAY), 8, s.length, 0) + return s + } + + companion object { + fun newInstance(): Fragment { + return PieChartFrag() + } + } +} diff --git a/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.java b/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.java deleted file mode 100644 index f7c7c3d1f3..0000000000 --- a/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.java +++ /dev/null @@ -1,70 +0,0 @@ -package info.appdev.chartexample.fragments; - -import android.graphics.Typeface; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.github.mikephil.charting.charts.ScatterChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; - -import info.appdev.chartexample.R; -import info.appdev.chartexample.custom.MyMarkerView; - - -public class ScatterChartFrag extends SimpleFragment { - - @NonNull - public static Fragment newInstance() { - return new ScatterChartFrag(); - } - - @SuppressWarnings("FieldCanBeLocal") - private ScatterChart chart; - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.frag_simple_scatter, container, false); - - chart = v.findViewById(R.id.scatterChart1); - chart.getDescription().setEnabled(false); - - Typeface tf = Typeface.createFromAsset(requireContext().getAssets(), "OpenSans-Light.ttf"); - - MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.custom_marker_view); - mv.setChartView(chart); // For bounds control - chart.setMarker(mv); - - chart.setDrawGridBackground(false); - chart.setData(generateScatterData(6, 10000)); - - XAxis xAxis = chart.getXAxis(); - xAxis.setEnabled(true); - xAxis.setPosition(XAxisPosition.BOTTOM); - - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setTypeface(tf); - - YAxis rightAxis = chart.getAxisRight(); - rightAxis.setTypeface(tf); - rightAxis.setDrawGridLines(false); - - Legend l = chart.getLegend(); - l.setWordWrapEnabled(true); - l.setTypeface(tf); - l.setFormSize(14f); - l.setTextSize(9f); - - // increase the space between legend & bottom and legend & content - l.setYOffset(13f); - chart.setExtraBottomOffset(16f); - - return v; - } -} diff --git a/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.kt b/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.kt new file mode 100644 index 0000000000..04c384b089 --- /dev/null +++ b/app/src/main/java/info/appdev/chartexample/fragments/ScatterChartFrag.kt @@ -0,0 +1,61 @@ +package info.appdev.chartexample.fragments + +import android.graphics.Typeface +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.github.mikephil.charting.charts.ScatterChart +import com.github.mikephil.charting.components.XAxis.XAxisPosition +import info.appdev.chartexample.R +import info.appdev.chartexample.custom.MyMarkerView + +class ScatterChartFrag : SimpleFragment() { + private var chart: ScatterChart? = null + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + val v = inflater.inflate(R.layout.frag_simple_scatter, container, false) + + chart = v.findViewById(R.id.scatterChart1) + chart!!.description.isEnabled = false + + val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") + + val mv = MyMarkerView(activity, R.layout.custom_marker_view) + mv.chartView = chart // For bounds control + chart!!.setMarker(mv) + + chart!!.setDrawGridBackground(false) + chart!!.setData(generateScatterData(6, 10000f)) + + val xAxis = chart!!.xAxis + xAxis.isEnabled = true + xAxis.position = XAxisPosition.BOTTOM + + val leftAxis = chart!!.axisLeft + leftAxis.typeface = tf + + val rightAxis = chart!!.axisRight + rightAxis.typeface = tf + rightAxis.setDrawGridLines(false) + + val l = chart!!.legend + l.isWordWrapEnabled = true + l.typeface = tf + l.formSize = 14f + l.textSize = 9f + + // increase the space between legend & bottom and legend & content + l.yOffset = 13f + chart!!.extraBottomOffset = 16f + + return v + } + + companion object { + fun newInstance(): Fragment { + return ScatterChartFrag() + } + } +} diff --git a/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.java b/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.java deleted file mode 100644 index 249c4f5ba2..0000000000 --- a/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.java +++ /dev/null @@ -1,194 +0,0 @@ -package info.appdev.chartexample.fragments; - -import android.graphics.Color; -import android.graphics.Typeface; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.github.mikephil.charting.charts.ScatterChart; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.BarDataSet; -import com.github.mikephil.charting.data.BarEntry; -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.data.LineData; -import com.github.mikephil.charting.data.LineDataSet; -import com.github.mikephil.charting.data.PieData; -import com.github.mikephil.charting.data.PieDataSet; -import com.github.mikephil.charting.data.PieEntry; -import com.github.mikephil.charting.data.ScatterData; -import com.github.mikephil.charting.data.ScatterDataSet; -import com.github.mikephil.charting.interfaces.datasets.IBarDataSet; -import com.github.mikephil.charting.interfaces.datasets.ILineDataSet; -import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet; -import com.github.mikephil.charting.utils.ColorTemplate; -import com.github.mikephil.charting.utils.FileUtils; -import info.appdev.chartexample.DataTools; - -import java.util.ArrayList; - -@SuppressWarnings({"SameParameterValue", "WeakerAccess"}) -public abstract class SimpleFragment extends Fragment { - - private Typeface tf; - - public SimpleFragment() { - - } - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - tf = Typeface.createFromAsset(requireContext().getAssets(), "OpenSans-Regular.ttf"); - return super.onCreateView(inflater, container, savedInstanceState); - } - - protected BarData generateBarData(int dataSets, float range) { - int count = 12; - Double[] values = DataTools.Companion.getValues(count); - ArrayList sets = new ArrayList<>(); - - for(int i = 0; i < dataSets; i++) { - - ArrayList entries = new ArrayList<>(); - - for(int j = 0; j < count; j++) { - entries.add(new BarEntry(j, (float) (values[j].floatValue() * range) + range / 4)); - } - - BarDataSet ds = new BarDataSet(entries, getLabel(i)); - ds.setColors(ColorTemplate.VORDIPLOM_COLORS); - sets.add(ds); - } - - BarData d = new BarData(sets); - d.setValueTypeface(tf); - return d; - } - - protected ScatterData generateScatterData(int dataSets, float range) { - int count = 100; - Double[] values = DataTools.Companion.getValues(count); - ArrayList sets = new ArrayList<>(); - - ScatterChart.ScatterShape[] shapes = ScatterChart.ScatterShape.getAllDefaultShapes(); - - for(int i = 0; i < dataSets; i++) { - - ArrayList entries = new ArrayList<>(); - - for(int j = 0; j < count; j++) { - entries.add(new Entry(j, (float) (values[j].floatValue() * range) + range / 4)); - } - - ScatterDataSet ds = new ScatterDataSet(entries, getLabel(i)); - ds.setScatterShapeSize(12f); - ds.setScatterShape(shapes[i % shapes.length]); - ds.setColors(ColorTemplate.COLORFUL_COLORS); - ds.setScatterShapeSize(9f); - sets.add(ds); - } - - ScatterData d = new ScatterData(sets); - d.setValueTypeface(tf); - return d; - } - - /** - * generates less data (1 DataSet, 4 values) - * @return PieData - */ - protected PieData generatePieData() { - - int count = 4; - Double[] values = DataTools.Companion.getValues(count); - ArrayList entries1 = new ArrayList<>(); - - for(int i = 0; i < count; i++) { - entries1.add(new PieEntry((float) ((values[i].floatValue() * 60) + 40), "Quarter " + (i+1))); - } - - PieDataSet ds1 = new PieDataSet(entries1, "Quarterly Revenues 2015"); - ds1.setColors(ColorTemplate.VORDIPLOM_COLORS); - ds1.setSliceSpace(2f); - ds1.setValueTextColor(Color.WHITE); - ds1.setValueTextSize(12f); - - PieData d = new PieData(ds1); - d.setValueTypeface(tf); - - return d; - } - - protected LineData generateLineData() { - - ArrayList sets = new ArrayList<>(); - LineDataSet ds1 = new LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().getAssets(), "sine.txt"), "Sine function"); - LineDataSet ds2 = new LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().getAssets(), "cosine.txt"), "Cosine function"); - - ds1.setLineWidth(2f); - ds2.setLineWidth(2f); - - ds1.setDrawCircles(false); - ds2.setDrawCircles(false); - - ds1.setColor(ColorTemplate.VORDIPLOM_COLORS[0]); - ds2.setColor(ColorTemplate.VORDIPLOM_COLORS[1]); - - // load DataSets from files in assets folder - sets.add(ds1); - sets.add(ds2); - - LineData d = new LineData(sets); - d.setValueTypeface(tf); - return d; - } - - protected LineData getComplexity() { - - ArrayList sets = new ArrayList<>(); - - LineDataSet ds1 = new LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().getAssets(), "n.txt"), "O(n)"); - LineDataSet ds2 = new LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().getAssets(), "nlogn.txt"), "O(nlogn)"); - LineDataSet ds3 = new LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().getAssets(), "square.txt"), "O(n\u00B2)"); - LineDataSet ds4 = new LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().getAssets(), "three.txt"), "O(n\u00B3)"); - - ds1.setColor(ColorTemplate.VORDIPLOM_COLORS[0]); - ds2.setColor(ColorTemplate.VORDIPLOM_COLORS[1]); - ds3.setColor(ColorTemplate.VORDIPLOM_COLORS[2]); - ds4.setColor(ColorTemplate.VORDIPLOM_COLORS[3]); - - ds1.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[0]); - ds2.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[1]); - ds3.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[2]); - ds4.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[3]); - - ds1.setLineWidth(2.5f); - ds1.setCircleRadius(3f); - ds2.setLineWidth(2.5f); - ds2.setCircleRadius(3f); - ds3.setLineWidth(2.5f); - ds3.setCircleRadius(3f); - ds4.setLineWidth(2.5f); - ds4.setCircleRadius(3f); - - - // load DataSets from files in assets folder - sets.add(ds1); - sets.add(ds2); - sets.add(ds3); - sets.add(ds4); - - LineData d = new LineData(sets); - d.setValueTypeface(tf); - return d; - } - - private final String[] mLabels = new String[] { "Company A", "Company B", "Company C", "Company D", "Company E", "Company F" }; - - private String getLabel(int i) { - return mLabels[i]; - } -} diff --git a/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.kt b/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.kt new file mode 100644 index 0000000000..87d5728b6b --- /dev/null +++ b/app/src/main/java/info/appdev/chartexample/fragments/SimpleFragment.kt @@ -0,0 +1,183 @@ +package info.appdev.chartexample.fragments + +import android.graphics.Color +import android.graphics.Typeface +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.github.mikephil.charting.charts.ScatterChart.ScatterShape +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.BarDataSet +import com.github.mikephil.charting.data.BarEntry +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.data.LineData +import com.github.mikephil.charting.data.LineDataSet +import com.github.mikephil.charting.data.PieData +import com.github.mikephil.charting.data.PieDataSet +import com.github.mikephil.charting.data.PieEntry +import com.github.mikephil.charting.data.ScatterData +import com.github.mikephil.charting.data.ScatterDataSet +import com.github.mikephil.charting.interfaces.datasets.IBarDataSet +import com.github.mikephil.charting.interfaces.datasets.ILineDataSet +import com.github.mikephil.charting.interfaces.datasets.IScatterDataSet +import com.github.mikephil.charting.utils.ColorTemplate +import com.github.mikephil.charting.utils.FileUtils +import info.appdev.chartexample.DataTools.Companion.getValues + +abstract class SimpleFragment : Fragment() { + private var tf: Typeface? = null + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Regular.ttf") + return super.onCreateView(inflater, container, savedInstanceState) + } + + protected fun generateBarData(dataSets: Int, range: Float): BarData { + val count = 12 + val values = getValues(count) + val sets = ArrayList() + + for (i in 0..() + + for (j in 0..() + + val shapes = ScatterShape.getAllDefaultShapes() + + for (i in 0..() + + for (j in 0..() + + for (i in 0..() + val ds1 = LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().assets, "sine.txt"), "Sine function") + val ds2 = LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().assets, "cosine.txt"), "Cosine function") + + ds1.setLineWidth(2f) + ds2.setLineWidth(2f) + + ds1.setDrawCircles(false) + ds2.setDrawCircles(false) + + ds1.setColor(ColorTemplate.VORDIPLOM_COLORS[0]) + ds2.setColor(ColorTemplate.VORDIPLOM_COLORS[1]) + + // load DataSets from files in assets folder + sets.add(ds1) + sets.add(ds2) + + val d = LineData(sets) + d.setValueTypeface(tf) + return d + } + + protected val complexity: LineData + get() { + val sets = ArrayList() + + val ds1 = + LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().assets, "n.txt"), "O(n)") + val ds2 = + LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().assets, "nlogn.txt"), "O(nlogn)") + val ds3 = + LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().assets, "square.txt"), "O(n\u00B2)") + val ds4 = + LineDataSet(FileUtils.loadEntriesFromAssets(requireContext().assets, "three.txt"), "O(n\u00B3)") + + ds1.setColor(ColorTemplate.VORDIPLOM_COLORS[0]) + ds2.setColor(ColorTemplate.VORDIPLOM_COLORS[1]) + ds3.setColor(ColorTemplate.VORDIPLOM_COLORS[2]) + ds4.setColor(ColorTemplate.VORDIPLOM_COLORS[3]) + + ds1.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[0]) + ds2.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[1]) + ds3.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[2]) + ds4.setCircleColor(ColorTemplate.VORDIPLOM_COLORS[3]) + + ds1.setLineWidth(2.5f) + ds1.circleRadius = 3f + ds2.setLineWidth(2.5f) + ds2.circleRadius = 3f + ds3.setLineWidth(2.5f) + ds3.circleRadius = 3f + ds4.setLineWidth(2.5f) + ds4.circleRadius = 3f + + + // load DataSets from files in assets folder + sets.add(ds1) + sets.add(ds2) + sets.add(ds3) + sets.add(ds4) + + val d = LineData(sets) + d.setValueTypeface(tf) + return d + } + + private val labels: Array = arrayOf("Company A", "Company B", "Company C", "Company D", "Company E", "Company F") + + private fun getLabel(i: Int): String { + return labels[i] + } +} diff --git a/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.java b/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.java deleted file mode 100644 index 68858b64cb..0000000000 --- a/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.java +++ /dev/null @@ -1,58 +0,0 @@ -package info.appdev.chartexample.fragments; -import android.graphics.Typeface; -import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.fragment.app.Fragment; -import info.appdev.chartexample.R; - -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.github.mikephil.charting.charts.LineChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.components.XAxis; -import com.github.mikephil.charting.components.YAxis; - - -public class SineCosineFragment extends SimpleFragment { - - @NonNull - public static Fragment newInstance() { - return new SineCosineFragment(); - } - - @SuppressWarnings("FieldCanBeLocal") - private LineChart chart; - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.frag_simple_line, container, false); - - chart = v.findViewById(R.id.lineChart1); - - chart.getDescription().setEnabled(false); - - chart.setDrawGridBackground(false); - - chart.setData(generateLineData()); - chart.animateX(3000); - - Typeface tf = Typeface.createFromAsset(requireContext().getAssets(), "OpenSans-Light.ttf"); - - Legend l = chart.getLegend(); - l.setTypeface(tf); - - YAxis leftAxis = chart.getAxisLeft(); - leftAxis.setTypeface(tf); - leftAxis.setAxisMaximum(1.2f); - leftAxis.setAxisMinimum(-1.2f); - - chart.getAxisRight().setEnabled(false); - - XAxis xAxis = chart.getXAxis(); - xAxis.setEnabled(false); - - return v; - } -} diff --git a/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.kt b/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.kt new file mode 100644 index 0000000000..98064fa25f --- /dev/null +++ b/app/src/main/java/info/appdev/chartexample/fragments/SineCosineFragment.kt @@ -0,0 +1,50 @@ +package info.appdev.chartexample.fragments + +import android.graphics.Typeface +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import com.github.mikephil.charting.charts.LineChart +import info.appdev.chartexample.R + +class SineCosineFragment : SimpleFragment() { + private var chart: LineChart? = null + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + val v = inflater.inflate(R.layout.frag_simple_line, container, false) + + chart = v.findViewById(R.id.lineChart1) + + chart!!.description.isEnabled = false + + chart!!.setDrawGridBackground(false) + + chart!!.setData(generateLineData()) + chart!!.animateX(3000) + + val tf = Typeface.createFromAsset(requireContext().assets, "OpenSans-Light.ttf") + + val l = chart!!.legend + l.typeface = tf + + val leftAxis = chart!!.axisLeft + leftAxis.typeface = tf + leftAxis.setAxisMaximum(1.2f) + leftAxis.setAxisMinimum(-1.2f) + + chart!!.axisRight.isEnabled = false + + val xAxis = chart!!.xAxis + xAxis.isEnabled = false + + return v + } + + companion object { + fun newInstance(): Fragment { + return SineCosineFragment() + } + } +} diff --git a/app/src/main/java/info/appdev/chartexample/fragments/ViewPagerSimpleChartDemo.kt b/app/src/main/java/info/appdev/chartexample/fragments/ViewPagerSimpleChartDemo.kt index 00eeba977c..396736ca77 100644 --- a/app/src/main/java/info/appdev/chartexample/fragments/ViewPagerSimpleChartDemo.kt +++ b/app/src/main/java/info/appdev/chartexample/fragments/ViewPagerSimpleChartDemo.kt @@ -25,7 +25,10 @@ import info.appdev.chartexample.notimportant.DemoBase class ViewPagerSimpleChartDemo : DemoBase() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN) + window.setFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN, + WindowManager.LayoutParams.FLAG_FULLSCREEN + ) setContentView(R.layout.activity_awesomedesign) val pager = findViewById(R.id.pager) pager.offscreenPageLimit = 3 diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.java b/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.java deleted file mode 100644 index 20e34da0f4..0000000000 --- a/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.java +++ /dev/null @@ -1,92 +0,0 @@ -package info.appdev.chartexample.listviewitems; - -import android.annotation.SuppressLint; -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.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.BarData; -import com.github.mikephil.charting.data.ChartData; - -import info.appdev.chartexample.R; - -public class BarChartItem extends ChartItem { - - private final Typeface mTf; - - public BarChartItem(ChartData cd, Context c) { - super(cd); - - mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf"); - } - - @Override - public int getItemType() { - return TYPE_BARCHART; - } - - @SuppressLint("InflateParams") - @Override - public View getView(int position, View convertView, Context c) { - - ViewHolder holder; - - if (convertView == null) { - - holder = new ViewHolder(); - - convertView = LayoutInflater.from(c).inflate( - R.layout.list_item_barchart, null); - holder.chart = convertView.findViewById(R.id.chart); - - convertView.setTag(holder); - - } else { - holder = (ViewHolder) convertView.getTag(); - } - - // apply styling - holder.chart.getDescription().setEnabled(false); - holder.chart.setDrawGridBackground(false); - holder.chart.setDrawBarShadow(false); - - XAxis xAxis = holder.chart.getXAxis(); - xAxis.setPosition(XAxisPosition.BOTTOM); - xAxis.setTypeface(mTf); - xAxis.setDrawGridLines(false); - xAxis.setDrawAxisLine(true); - - YAxis leftAxis = holder.chart.getAxisLeft(); - leftAxis.setTypeface(mTf); - leftAxis.setLabelCount(5, false); - leftAxis.setSpaceTop(20f); - leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) - - YAxis rightAxis = holder.chart.getAxisRight(); - rightAxis.setTypeface(mTf); - rightAxis.setLabelCount(5, false); - rightAxis.setSpaceTop(20f); - rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) - - mChartData.setValueTypeface(mTf); - - // set data - holder.chart.setData((BarData) mChartData); - holder.chart.setFitBars(true); - - // do not forget to refresh the chart -// holder.chart.invalidate(); - holder.chart.animateY(700); - - return convertView; - } - - private static class ViewHolder { - BarChart chart; - } -} diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.kt b/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.kt new file mode 100644 index 0000000000..675b033365 --- /dev/null +++ b/app/src/main/java/info/appdev/chartexample/listviewitems/BarChartItem.kt @@ -0,0 +1,77 @@ +package info.appdev.chartexample.listviewitems + +import android.annotation.SuppressLint +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.components.XAxis.XAxisPosition +import com.github.mikephil.charting.data.BarData +import com.github.mikephil.charting.data.ChartData +import info.appdev.chartexample.R + +class BarChartItem(cd: ChartData<*>, c: Context) : ChartItem(cd) { + private val typeface: Typeface? = Typeface.createFromAsset(c.assets, "OpenSans-Regular.ttf") + + override val itemType: Int + get() = TYPE_BARCHART + + @SuppressLint("InflateParams") + override fun getView(position: Int, convertView: View?, c: Context?): View? { + var convertView = convertView + val holder: ViewHolder + + if (convertView == null) { + holder = ViewHolder() + + convertView = LayoutInflater.from(c).inflate( + R.layout.list_item_barchart, null + ) + holder.chart = convertView.findViewById(R.id.chart) + + convertView.tag = holder + } else { + holder = convertView.tag as ViewHolder + } + + // apply styling + holder.chart!!.description.isEnabled = false + holder.chart!!.setDrawGridBackground(false) + holder.chart!!.setDrawBarShadow(false) + + val xAxis = holder.chart!!.xAxis + xAxis.position = XAxisPosition.BOTTOM + xAxis.typeface = typeface + xAxis.setDrawGridLines(false) + xAxis.setDrawAxisLine(true) + + val leftAxis = holder.chart!!.axisLeft + leftAxis.typeface = typeface + leftAxis.setLabelCount(5, false) + leftAxis.spaceTop = 20f + leftAxis.setAxisMinimum(0f) // this replaces setStartAtZero(true) + + val rightAxis = holder.chart!!.axisRight + rightAxis.typeface = typeface + rightAxis.setLabelCount(5, false) + rightAxis.spaceTop = 20f + rightAxis.setAxisMinimum(0f) // this replaces setStartAtZero(true) + + chartData.setValueTypeface(typeface) + + // set data + holder.chart!!.setData(chartData as BarData?) + holder.chart!!.setFitBars(true) + + // do not forget to refresh the chart +// holder.chart.invalidate(); + holder.chart!!.animateY(700) + + return convertView + } + + private class ViewHolder { + var chart: BarChart? = null + } +} diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.java b/app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.java deleted file mode 100644 index eb5300c2ae..0000000000 --- a/app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.java +++ /dev/null @@ -1,29 +0,0 @@ -package info.appdev.chartexample.listviewitems; - -import android.content.Context; -import android.view.View; - -import com.github.mikephil.charting.data.ChartData; - -/** - * Base class of the Chart ListView items - * @author philipp - * - */ -@SuppressWarnings("unused") -public abstract class ChartItem { - - static final int TYPE_BARCHART = 0; - static final int TYPE_LINECHART = 1; - static final int TYPE_PIECHART = 2; - - ChartData mChartData; - - ChartItem(ChartData cd) { - this.mChartData = cd; - } - - public abstract int getItemType(); - - public abstract View getView(int position, View convertView, Context c); -} diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.kt b/app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.kt new file mode 100644 index 0000000000..431814adc8 --- /dev/null +++ b/app/src/main/java/info/appdev/chartexample/listviewitems/ChartItem.kt @@ -0,0 +1,21 @@ +package info.appdev.chartexample.listviewitems + +import android.content.Context +import android.view.View +import com.github.mikephil.charting.data.ChartData + +/** + * Base class of the Chart ListView items + */ +@Suppress("unused") +abstract class ChartItem internal constructor(@JvmField var chartData: ChartData<*>) { + abstract val itemType: Int + + abstract fun getView(position: Int, convertView: View?, c: Context?): View? + + companion object { + const val TYPE_BARCHART: Int = 0 + const val TYPE_LINECHART: Int = 1 + const val TYPE_PIECHART: Int = 2 + } +} diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.java b/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.java deleted file mode 100644 index 7d95061314..0000000000 --- a/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.java +++ /dev/null @@ -1,89 +0,0 @@ - -package info.appdev.chartexample.listviewitems; - -import android.annotation.SuppressLint; -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.components.XAxis; -import com.github.mikephil.charting.components.XAxis.XAxisPosition; -import com.github.mikephil.charting.components.YAxis; -import com.github.mikephil.charting.data.ChartData; -import com.github.mikephil.charting.data.LineData; - -import info.appdev.chartexample.R; - -public class LineChartItem extends ChartItem { - - private final Typeface mTf; - - public LineChartItem(ChartData cd, Context c) { - super(cd); - - mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf"); - } - - @Override - public int getItemType() { - return TYPE_LINECHART; - } - - @SuppressLint("InflateParams") - @Override - public View getView(int position, View convertView, Context c) { - - ViewHolder holder; - - if (convertView == null) { - - holder = new ViewHolder(); - - convertView = LayoutInflater.from(c).inflate( - R.layout.list_item_linechart, null); - holder.chart = convertView.findViewById(R.id.chart); - - convertView.setTag(holder); - - } else { - holder = (ViewHolder) convertView.getTag(); - } - - // apply styling - // holder.chart.setValueTypeface(mTf); - holder.chart.getDescription().setEnabled(false); - holder.chart.setDrawGridBackground(false); - - XAxis xAxis = holder.chart.getXAxis(); - xAxis.setPosition(XAxisPosition.BOTTOM); - xAxis.setTypeface(mTf); - xAxis.setDrawGridLines(false); - xAxis.setDrawAxisLine(true); - - YAxis leftAxis = holder.chart.getAxisLeft(); - leftAxis.setTypeface(mTf); - leftAxis.setLabelCount(5, false); - leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) - - YAxis rightAxis = holder.chart.getAxisRight(); - rightAxis.setTypeface(mTf); - rightAxis.setLabelCount(5, false); - rightAxis.setDrawGridLines(false); - rightAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true) - - // set data - holder.chart.setData((LineData) mChartData); - - // do not forget to refresh the chart - // holder.chart.invalidate(); - holder.chart.animateX(750); - - return convertView; - } - - private static class ViewHolder { - LineChart chart; - } -} diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.kt b/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.kt new file mode 100644 index 0000000000..2ed37997e2 --- /dev/null +++ b/app/src/main/java/info/appdev/chartexample/listviewitems/LineChartItem.kt @@ -0,0 +1,73 @@ +package info.appdev.chartexample.listviewitems + +import android.annotation.SuppressLint +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.components.XAxis.XAxisPosition +import com.github.mikephil.charting.data.ChartData +import com.github.mikephil.charting.data.LineData +import info.appdev.chartexample.R + +class LineChartItem(cd: ChartData<*>, c: Context) : ChartItem(cd) { + private val typeface: Typeface? = Typeface.createFromAsset(c.assets, "OpenSans-Regular.ttf") + + override val itemType: Int + get() = TYPE_LINECHART + + @SuppressLint("InflateParams") + override fun getView(position: Int, convertView: View?, c: Context?): View { + var convertView = convertView + val holder: ViewHolder + + if (convertView == null) { + holder = ViewHolder() + + convertView = LayoutInflater.from(c).inflate( + R.layout.list_item_linechart, null + ) + holder.chart = convertView.findViewById(R.id.chart) + + convertView.tag = holder + } else { + holder = convertView.tag as ViewHolder + } + + // apply styling + // holder.chart.setValueTypeface(mTf); + holder.chart!!.description.isEnabled = false + holder.chart!!.setDrawGridBackground(false) + + val xAxis = holder.chart!!.xAxis + xAxis.position = XAxisPosition.BOTTOM + xAxis.typeface = typeface + xAxis.setDrawGridLines(false) + xAxis.setDrawAxisLine(true) + + val leftAxis = holder.chart!!.axisLeft + leftAxis.typeface = typeface + leftAxis.setLabelCount(5, false) + leftAxis.setAxisMinimum(0f) // this replaces setStartAtZero(true) + + val rightAxis = holder.chart!!.axisRight + rightAxis.typeface = typeface + rightAxis.setLabelCount(5, false) + rightAxis.setDrawGridLines(false) + rightAxis.setAxisMinimum(0f) // this replaces setStartAtZero(true) + + // set data + holder.chart!!.setData(chartData as LineData?) + + // do not forget to refresh the chart + // holder.chart.invalidate(); + holder.chart!!.animateX(750) + + return convertView + } + + private class ViewHolder { + var chart: LineChart? = null + } +} diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.java b/app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.java deleted file mode 100644 index 8a8180c172..0000000000 --- a/app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.java +++ /dev/null @@ -1,106 +0,0 @@ - -package info.appdev.chartexample.listviewitems; - -import android.annotation.SuppressLint; -import android.content.Context; -import android.graphics.Color; -import android.graphics.Typeface; -import android.text.SpannableString; -import android.text.style.ForegroundColorSpan; -import android.text.style.RelativeSizeSpan; -import android.view.LayoutInflater; -import android.view.View; - -import com.github.mikephil.charting.charts.PieChart; -import com.github.mikephil.charting.components.Legend; -import com.github.mikephil.charting.data.ChartData; -import com.github.mikephil.charting.data.PieData; -import com.github.mikephil.charting.formatter.PercentFormatter; -import com.github.mikephil.charting.utils.ColorTemplate; - -import info.appdev.chartexample.R; - -public class PieChartItem extends ChartItem { - - private final Typeface mTf; - private final SpannableString mCenterText; - - public PieChartItem(ChartData cd, Context c) { - super(cd); - - mTf = Typeface.createFromAsset(c.getAssets(), "OpenSans-Regular.ttf"); - mCenterText = generateCenterText(); - } - - @Override - public int getItemType() { - return TYPE_PIECHART; - } - - @SuppressLint("InflateParams") - @Override - public View getView(int position, View convertView, Context c) { - - ViewHolder holder; - - if (convertView == null) { - - holder = new ViewHolder(); - - convertView = LayoutInflater.from(c).inflate( - R.layout.list_item_piechart, null); - holder.chart = convertView.findViewById(R.id.chart); - - convertView.setTag(holder); - - } else { - holder = (ViewHolder) convertView.getTag(); - } - - // apply styling - holder.chart.getDescription().setEnabled(false); - holder.chart.setHoleRadius(52f); - holder.chart.setTransparentCircleRadius(57f); - holder.chart.setCenterText(mCenterText); - holder.chart.setCenterTextTypeface(mTf); - holder.chart.setCenterTextSize(9f); - holder.chart.setUsePercentValues(true); - holder.chart.setExtraOffsets(5, 10, 50, 10); - - mChartData.setValueFormatter(new PercentFormatter()); - mChartData.setValueTypeface(mTf); - mChartData.setValueTextSize(11f); - mChartData.setValueTextColor(Color.WHITE); - // set data - holder.chart.setData((PieData) mChartData); - - Legend l = holder.chart.getLegend(); - l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); - l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); - l.setOrientation(Legend.LegendOrientation.VERTICAL); - l.setDrawInside(false); - l.setYEntrySpace(0f); - l.setYOffset(0f); - - // do not forget to refresh the chart - // holder.chart.invalidate(); - holder.chart.animateY(900); - - return convertView; - } - - private SpannableString generateCenterText() { - SpannableString s = new SpannableString("MPAndroidChart\ncreated by\nPhilipp Jahoda"); - s.setSpan(new RelativeSizeSpan(1.6f), 0, 14, 0); - s.setSpan(new ForegroundColorSpan(ColorTemplate.VORDIPLOM_COLORS[0]), 0, 14, 0); - s.setSpan(new RelativeSizeSpan(.9f), 14, 25, 0); - s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, 25, 0); - s.setSpan(new RelativeSizeSpan(1.4f), 25, s.length(), 0); - s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), 25, s.length(), 0); - return s; - } - - private static class ViewHolder { - PieChart chart; - } -} diff --git a/app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.kt b/app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.kt new file mode 100644 index 0000000000..767338c0a5 --- /dev/null +++ b/app/src/main/java/info/appdev/chartexample/listviewitems/PieChartItem.kt @@ -0,0 +1,96 @@ +package info.appdev.chartexample.listviewitems + +import android.annotation.SuppressLint +import android.content.Context +import android.graphics.Color +import android.graphics.Typeface +import android.text.SpannableString +import android.text.style.ForegroundColorSpan +import android.text.style.RelativeSizeSpan +import android.text.style.StyleSpan +import android.view.LayoutInflater +import android.view.View +import com.github.mikephil.charting.charts.PieChart +import com.github.mikephil.charting.components.Legend +import com.github.mikephil.charting.data.ChartData +import com.github.mikephil.charting.data.PieData +import com.github.mikephil.charting.formatter.PercentFormatter +import com.github.mikephil.charting.utils.ColorTemplate +import info.appdev.chartexample.R + +class PieChartItem(cd: ChartData<*>, c: Context) : ChartItem(cd) { + private val typeface: Typeface? = Typeface.createFromAsset(c.assets, "OpenSans-Regular.ttf") + private val centerText: SpannableString + + init { + centerText = generateCenterSpannableText() + } + + override val itemType: Int + get() = TYPE_PIECHART + + @SuppressLint("InflateParams") + override fun getView(position: Int, convertView: View?, c: Context?): View { + var convertView = convertView + val holder: ViewHolder + + if (convertView == null) { + holder = ViewHolder() + + convertView = LayoutInflater.from(c).inflate( + R.layout.list_item_piechart, null + ) + holder.chart = convertView.findViewById(R.id.chart) + + convertView.tag = holder + } else { + holder = convertView.tag as ViewHolder + } + + // apply styling + holder.chart!!.description.isEnabled = false + holder.chart!!.holeRadius = 52f + holder.chart!!.transparentCircleRadius = 57f + holder.chart!!.centerText = centerText + holder.chart!!.setCenterTextTypeface(typeface) + holder.chart!!.setCenterTextSize(9f) + holder.chart!!.setUsePercentValues(true) + holder.chart!!.setExtraOffsets(5f, 10f, 50f, 10f) + + chartData.setValueFormatter(PercentFormatter()) + chartData.setValueTypeface(typeface) + chartData.setValueTextSize(11f) + chartData.setValueTextColor(Color.WHITE) + // set data + holder.chart!!.setData(chartData as PieData?) + + val l = holder.chart!!.legend + l.verticalAlignment = Legend.LegendVerticalAlignment.TOP + l.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT + l.orientation = Legend.LegendOrientation.VERTICAL + l.setDrawInside(false) + l.yEntrySpace = 0f + l.yOffset = 0f + + // do not forget to refresh the chart + // holder.chart.invalidate(); + holder.chart!!.animateY(900) + + return convertView + } + + private fun generateCenterSpannableText(): SpannableString { + val s = SpannableString("AndroidChart\ndeveloped by AppDevNext") + s.setSpan(RelativeSizeSpan(1.7f), 0, 14, 0) + s.setSpan(StyleSpan(Typeface.NORMAL), 14, s.length - 15, 0) + s.setSpan(ForegroundColorSpan(Color.GRAY), 14, s.length - 15, 0) + s.setSpan(RelativeSizeSpan(.8f), 14, s.length - 15, 0) + s.setSpan(StyleSpan(Typeface.ITALIC), s.length - 14, s.length, 0) + s.setSpan(ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length - 14, s.length, 0) + return s + } + + private class ViewHolder { + var chart: PieChart? = null + } +} diff --git a/app/src/main/java/info/appdev/chartexample/notimportant/MenuAdapter.kt b/app/src/main/java/info/appdev/chartexample/notimportant/MenuAdapter.kt index 256da36206..929bd5496c 100644 --- a/app/src/main/java/info/appdev/chartexample/notimportant/MenuAdapter.kt +++ b/app/src/main/java/info/appdev/chartexample/notimportant/MenuAdapter.kt @@ -43,7 +43,7 @@ internal class MenuAdapter(context: Context, objects: List?>?) : return inflatedView } - private inner class ViewHolder { + private class ViewHolder { var tvName: TextView? = null var tvDesc: TextView? = null } diff --git a/screenshotsToCompare9/StartTest_smokeTestStart-31-ListViewMultiChartActivity-Multiple-1SampleClick.png b/screenshotsToCompare9/StartTest_smokeTestStart-31-ListViewMultiChartActivity-Multiple-1SampleClick.png index 9f382b4c80..c0dfe4641c 100644 Binary files a/screenshotsToCompare9/StartTest_smokeTestStart-31-ListViewMultiChartActivity-Multiple-1SampleClick.png and b/screenshotsToCompare9/StartTest_smokeTestStart-31-ListViewMultiChartActivity-Multiple-1SampleClick.png differ