Search code examples
javaandroidmpandroidchartandroid-dialogfragment

MP CHART in dialog fragment


I'm trying to customize an MP chart in a dialog fragment, but I'm not getting the desired results. I don't receive any little changes, but I do get what I really want in activity. I've also tried attaching a debugger but haven't been able to change the graph's line color or gradient.

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/graphbackground_bg"
    >

    <LinearLayout
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <com.github.mikephil.charting.charts.LineChart
            android:layout_width="350dp"
            android:layoutDirection="locale"
            android:layout_gravity="center"
            android:layout_height="600dp"
            android:id="@+id/symbolChart"/>
    </LinearLayout>
</LinearLayout>

Fragment

public class ScripChartFragment extends DialogFragment {
    LineChart lineChart;
    LineData lineData;
    LineDataSet set1;
    private Context context;
    View view;
    List<Entry> entryList = new ArrayList<>();
    public Drawable drawablePositive;

    public ScripChartFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view =  inflater.inflate(R.layout.fragment_scripchart, container, false);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        context = getContext();

        drawablePositive = ContextCompat.getDrawable(context, R.drawable.symbolchart_bg);
        lineChart = view.findViewById(R.id.symbolChart);

        setData(20,3);
        lineChart.getDescription().setEnabled(true);
        lineChart.setDrawGridBackground(true);
        lineChart.setDragEnabled(true);
        lineChart.getLegend().setEnabled(true);
        lineChart.setScaleEnabled(true);
        lineChart.setScaleYEnabled(true);
        lineChart.setScaleXEnabled(true);
        lineChart.setDrawGridBackground(false);
        lineChart.getXAxis().setEnabled(true);
        lineChart.getLineData().setDrawValues(true);
        lineChart.getXAxis().setDrawGridLines(true);
        lineChart.getXAxis().setDrawAxisLine(true);
        lineChart.getAxisLeft().setDrawGridLines(true);
        lineChart.getAxisRight().setDrawGridLines(true);
        lineChart.getAxisRight().setDrawZeroLine(true);
        lineChart.getAxisLeft().setDrawZeroLine(true);
        lineChart.getAxisRight().setDrawLabels(true);
        lineChart.getAxisLeft().setDrawLabels(true);
        lineChart.getAxisLeft().setEnabled(true);
        lineChart.getAxisRight().setEnabled(true);
        lineChart.setMaxHighlightDistance(150);
        lineChart.setViewPortOffsets(0, 0, 0, 0);
        lineChart.setTouchEnabled(false);
        lineChart.setPinchZoom(false);
        lineChart.notifyDataSetChanged();

        LineDataSet lineDataSet = new LineDataSet(entryList,"BuySell");
        lineData = new LineData(lineDataSet);
        lineChart.setData(lineData);

        return  view;
    }

    private void setData(int count, float range) {

        entryList = new ArrayList<>();

        LineData data;

        for (int i = 0; i < count; i++) {

            float val = (float) (Math.random() * range) - 30;
            entryList.add(new Entry(i, val));
        }

        set1 = new LineDataSet(entryList, "DataSet 1");
        set1.setFillDrawable(drawablePositive);
        set1.setFillAlpha(100);
        set1.setFillColor(Color.RED);
        set1.setColor( Color.RED);
        set1.setCircleColor(getResources().getColor(R.color.yellow));
        set1.setLineWidth(5f);
        set1.setDrawValues(false);

        set1.setFillFormatter(new IFillFormatter() {
            @Override
            public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
                return lineChart.getAxisLeft().getAxisMinimum();

            }
        });
        ArrayList<ILineDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);
        data = new LineData(dataSets);


        lineChart.setData(data);

    }
}

Solution

  • Inside your onCreateView method, at the bottom, you wrote this:

    
            LineDataSet lineDataSet = new LineDataSet(entryList,"BuySell");
            lineData = new LineData(lineDataSet);
            lineChart.setData(lineData);
    

    Comment out / remove these 3 lines. Then you'll get an output like this:enter image description here

    Explanation:

    You have 2 LineDataSet objects,

    1.     set1 = new LineDataSet(entryList, "DataSet 1");
      

    and

    1.      LineDataSet lineDataSet = new LineDataSet(entryList,"BuySell");
      

    And you are setting those color properties to the first object("DataSet 1"). But then you are showing the 2nd object ("BuySell") to the chart. That's why removing those 3 line solves the problem.