PhilJay / MPAndroidChart

A powerful 🚀 Android chart view / graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, panning and animations.
Other
37.53k stars 9.01k forks source link

Chart not show only in release build #4612

Open winnerawan opened 5 years ago

winnerawan commented 5 years ago

im in

minSdkVersion = 19 targetSdkVersion = 28 compileSdkVersion = 28 buildToolsVersion = '28.0.3' AndroidX classpath 'com.android.tools.build:gradle:3.4.2' https\://services.gradle.org/distributions/gradle-5.1.1-all.zip

proguard

-dontwarn com.github.mikephil.** -keep public class com.github.mikephil.** { public protected *; }

all fine in build type debug, but in release version the chart is gone

can somebody give me solutions?

my some code and screenshot below activity

@Override
showCharts(List<Chart> charts) {
  adapter.addCharts(charts);
}

adapter

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;

import androidx.databinding.DataBindingUtil;
import androidx.viewpager.widget.PagerAdapter;
import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.*;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.renderer.YAxisRenderer;
import com.github.mikephil.charting.utils.ColorTemplate;
import id.co.briit.custody.R;
import id.co.briit.custody.databinding.ItemChartViewBinding;
import id.co.briit.custody.ui.helper.ColorPieChart;
import id.co.briit.custody.utils.AppLogger;
import org.jetbrains.annotations.NotNull;

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

public class ChartViewPagerAdapter extends PagerAdapter {

    private LayoutInflater inflater;
    private Context context;
    private List<Chart> charts;

    public ChartViewPagerAdapter(Context context, List<Chart> mCharts) {
        this.context = context;
        this.charts = mCharts;
        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        ItemChartViewBinding binding = DataBindingUtil.inflate(inflater, R.layout.item_chart_view, container, false);
        assert binding != null;
        binding.getRoot().setTag(position);

        if (charts != null) {
            Chart chart = charts.get(position);
            AppLogger.e("charts not null");
            setUpPieChart(binding.pieChartView, chart);
            setUpBarChart(binding.barChartView, chart);
            if (position == 0) {
                binding.txtTitleChart.setText(context.getString(R.string.global_chart));
            } else if (position == 1) {
                binding.txtTitleChart.setText(context.getString(R.string.custody_chart));
            }
        } else {
            AppLogger.e("charts null");
        }
        container.addView(binding.getRoot());
        return binding.getRoot();
    }

    public void addCharts(List<Chart> chartList) {
        charts.clear();
        charts.addAll(chartList);
        notifyDataSetChanged();
    }

    private void setUpPieChart(PieChart pieChart, Chart chart) {
        PieDataSet pieDataSet;
        ArrayList<PieEntry> pieEntries = new ArrayList<>();
        PieData pieData;
        Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/ride_rewrite_neo_sans_medium.ttf");

        if (chart != null) {
            if (chart.getPieChart().getPercentage() < 100.00) {
                float in = chart.getPieChart().getPercentage().floatValue();
                AppLogger.e("IN LOG: " + in);
                float out = 100.00f - chart.getPieChart().getPercentage().floatValue();
                pieEntries.add(new PieEntry(in, 0));
                pieEntries.add(new PieEntry(out, 1));
//            pieChart.setRotationAngle(270);
            } else {
                pieEntries.add(new PieEntry(100.00f, 1));
                pieEntries.add(new PieEntry(00.00f, 0));
            }
        } else {
            AppLogger.e("chart null");
        }

        pieDataSet = new PieDataSet(pieEntries, null);
        pieData = new PieData(pieDataSet);

        pieChart.setRotationAngle(275);
        pieChart.setData(pieData);
        pieChart.setUsePercentValues(true);
        pieChart.setCenterTextRadiusPercent(2.f);
        pieChart.setDrawSliceText(true);
        pieChart.setDrawHoleEnabled(false);
        pieChart.setCenterTextTypeface(tf);
        pieChart.setRotationEnabled(false);
        pieChart.animateY(1400, Easing.EaseInOutQuad);
        pieChart.setDrawEntryLabels(true);
        pieChart.setDrawCenterText(true);
        pieChart.getDescription().setEnabled(false);
        pieChart.getLegend().setEnabled(false);
        pieData.setValueTypeface(tf);
        pieDataSet.setValueLinePart1OffsetPercentage(80.f);
        pieDataSet.setValueLinePart1Length(0.6f);
        pieDataSet.setValueLinePart2Length(0.5f);
//        pieDataSet.setUsingSliceColorAsValueLineColor(true);

        pieDataSet.setXValuePosition(PieDataSet.ValuePosition.INSIDE_SLICE);
        pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        pieChart.setExtraOffsets(15, 0, 15, 0);

        pieDataSet.setValueFormatter(new ChartValueFormatter());
        pieDataSet.setColors(ColorPieChart.PIE_COLORS);
        pieChart.setCenterTextColor(Color.BLACK);
        pieDataSet.setValueTextColor(Color.BLACK);
        pieDataSet.setValueTextSize(10f);
        pieDataSet.setSliceSpace(.1f);

        pieChart.invalidate();

    }

    private void setUpBarChart(BarChart barChart, Chart chart) {
        Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/ride_rewrite_neo_sans_medium.ttf");

        barChart.setDrawBarShadow(false);
        barChart.setDrawValueAboveBar(true);
        barChart.getDescription().setEnabled(false);
        barChart.setPinchZoom(false);

        barChart.setDrawGridBackground(false);
        barChart.getLegend().setEnabled(false);
        barChart.getAxisRight().setEnabled(false);
        ValueFormatter xAxisFormatter = new CaptureTimeFormatter(barChart);

        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTypeface(tf);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f); // only intervals of 1 day
        xAxis.setTextSize(7f);
        xAxis.setLabelCount(7);
        xAxis.setValueFormatter(xAxisFormatter);

        YAxis yAxis = barChart.getAxisLeft();
        yAxis.setTextSize(7f);
        yAxis.setTypeface(tf);

        YAxisRenderer yAxisRenderer = barChart.getRendererLeftYAxis();
        yAxisRenderer.getPaintAxisLabels().setTypeface(tf);

        ArrayList<BarEntry> values = new ArrayList<>();
        ArrayList<Integer> colors = new ArrayList<>();

        if (chart != null) {
            if (chart.getBarChart() != null) {
                for (int i = 0; i < chart.getBarChart().size(); i++) {
                    values.add(new BarEntry(i, chart.getBarChart().get(i).getPercentage().floatValue(), chart.getBarChart().get(i).getTime()));
                    AppLogger.e("val: " + values.get(0));
                    if (values.get(i).getY() >= 99.51f) {
                        colors.add(ColorTemplate.rgb("#1963EB"));
                    } else if (values.get(i).getY() >= 99.01 && values.get(i).getY() <= 99.50) {
                        colors.add(ColorTemplate.rgb("#FF00702F"));
                    } else if (values.get(i).getY() >= 98.01 && values.get(i).getY() <= 99.00) {
                        colors.add(ColorTemplate.rgb("#00c853"));
                    } else if (values.get(i).getY() >= 97.01 && values.get(i).getY() <= 98.00) {
                        colors.add(Color.YELLOW);
                    } else if (values.get(i).getY() >= 96.01 && values.get(i).getY() <= 97.00) {
                        colors.add(ColorTemplate.rgb("##F0623F"));
                    } else if (values.get(i).getY() >= 95.01 && values.get(i).getY() <= 96.00) {
                        colors.add(ColorTemplate.rgb("##E72565"));
                    } else if (values.get(i).getY() < 95) {
                        colors.add(Color.RED);
                    }
                }
            }
        } else {
            AppLogger.e("chart null");
        }

        BarDataSet dataSet;

        if (barChart.getData() != null &&
                barChart.getData().getDataSetCount() > 0) {
            dataSet = (BarDataSet) barChart.getData().getDataSetByIndex(0);
            dataSet.setValues(values);
            barChart.getData().notifyDataChanged();
            barChart.notifyDataSetChanged();

        } else {
            dataSet = new BarDataSet(values, null);
            dataSet.setValueFormatter(new ChartValueFormatter());
            dataSet.setDrawIcons(false);

            dataSet.setColors(colors);

            ArrayList<IBarDataSet> dataSets = new ArrayList<>();
            dataSets.add(dataSet);

            BarData data = new BarData(dataSets);
            data.setValueTextSize(6f);
            data.setValueTypeface(tf);
            data.setBarWidth(0.4f);

            barChart.setData(data);
            barChart.getXAxis().setAxisMaximum(0f + barChart.getBarData().getGroupWidth(0.01f, 0.4f) * 9f);

            barChart.invalidate();
        }
    }

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

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public void destroyItem(@NotNull ViewGroup container, int position, @NotNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public float getPageWidth(int position) {
        return (1f);
    }
}

debug debug

release release

rohangho commented 3 years ago

Has anybody got any solution? I am also facing this issue.

winnerawan commented 3 years ago

@rohangho add your model package name to proguard

rohangho commented 2 years ago

Hi sorry for the delayed response it got resolved. Thanks for the response though.