MPAndroidChart Chart

1

I'm using MPAndroidChart to make graphics.

The problem is this, when I have too many items in the caption, it expands leaving the graphic small. Can you create a scroll in the legend instead of expanding it and consequently decreasing the graph?

-----Edit-----

@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_grafico_supervisor);setUpToolbar();if(getIntent().getExtras()!=null){idSupervisor=getIntent().getExtras().getInt("idSupervisor");
        nomeSupervisor = getIntent().getExtras().getString("nomeSupervisor");
        mes = getIntent().getExtras().getInt("mes");
        ano = getIntent().getExtras().getString("ano");
    } else{
        // Pegar de Singleton carregado em Login
    }

    configurarCampos();
    configurarGrafico();
}

 private void configurarCampos(){
    mainLayout = (LinearLayout) findViewById(R.id.linearGraficos);
    mChart = (PieChart) findViewById(R.id.chart1);
}

private void configurarGrafico(){
    List<VendedorMOD> vendedores = new VendedorDAO(getContext()).pesquisarPorData(idSupervisor, MascaraData.fromStringNormal(getDatas()[0]), MascaraData.fromStringNormal(getDatas()[1]));

    for(VendedorMOD v : vendedores){
        yData.add(v.getValor().doubleValue());
        xData.add(v.getNome());
    }

    // add pie chart to main layout
    //mainLayout.setBackgroundColor(Color.GRAY);

    // configure pie chart
    mChart.setUsePercentValues(true);
    mChart.setDescription("");

    // enable hole and configure
    mChart.setDrawHoleEnabled(true);
    mChart.setHoleColorTransparent(true);
    mChart.setHoleRadius(7);
    mChart.setTransparentCircleRadius(10);

    // enable rotation of the chart by touch
    mChart.setRotationAngle(0);
    mChart.setRotationEnabled(true);

    // add data
    addData();

    // customize legends
    Legend l = mChart.getLegend();
    l.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
    l.setXEntrySpace(7);
    l.setYEntrySpace(5);
    l.setTextSize(10f);
    l.setTextColor(Color.BLACK);
    l.setFormSize(10f);

    // legenda nao cortar na tela
    mChart.getLegend().setWordWrapEnabled(true);
    mChart.getLegend().setEnabled(false);
}
    
asked by anonymous 25.01.2016 / 14:15

2 answers

0

I have used this library a lot, but I have not put any subtitles in them.

In the application sample of the library there is a Pie Chart in which it is possible to add several data, and when it is adding the amount of legend also increases.

The caption is on the chart, I do not know if it suits your case.

On the official website of the library there is the source code a> in this example.

In the legend setting there are these items:

Legend l = mChart.getLegend();
l.setPosition(LegendPosition.RIGHT_OF_CHART);
l.setXEntrySpace(7f);
l.setYEntrySpace(0f);
l.setYOffset(0f);

It may not be the solution but a path to your problem.

I hope I have helped!

    
25.01.2016 / 17:52
-1

You really can not create the scroll. I put it in the English StackOverflow and the guy who created MPAndroidChart told me to lower the caption, but then I said that I had the big print just to simulate a lot of data ... So he did not answer any more so far. p>

But I solved my problem by creating my own caption, I set a LinearLayout below the graph, and in LinearLayout I put a recyclerview. In the Adapter I put a view to be painted its backgorund with the corresponding color of the graphic and a TextView in the front. It looks like this:

XML:

<?xmlversion="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearGraficos"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/gray_dark">

    <include layout="@layout/include_toolbar" />

    <com.github.mikephil.charting.charts.PieChart
        android:id="@+id/chart1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/white">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/gray"
            android:cacheColorHint="@android:color/transparent"
            android:clipToPadding="false"
            android:divider="@null"
            android:dividerHeight="0dp"
            android:listSelector="@color/black"
            android:scrollbarStyle="outsideOverlay"
            android:scrollbars="vertical" />
    </LinearLayout>
</LinearLayout>

Adapter:

public class LegendaAdapter extends RecyclerView.Adapter<LegendaAdapter.LegendaViewHolder> {
    private final Context context;
    private List<String> descricao;
    private List<Integer> colors;
    private List<Double> valores;

    public LegendaAdapter(Context context, List<String> descricao, List<Integer> colors, List<Double> valores) {
        this.descricao = descricao;
        this.context = context;
        this.colors = colors;
        this.valores = valores;
    }

    @Override
    public int getItemCount() {
        return this.descricao != null ? this.descricao.size() : 0;
    }

    @Override
    public LegendaViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_legenda_grafico, viewGroup, false);
        LegendaViewHolder holder = new LegendaViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(final LegendaViewHolder holder, final int position) {
        holder.tDescricaoLegenda.setText(descricao.get(position) + " - "
                + BigDecimal.valueOf(getPercent(position)).setScale(2, RoundingMode.HALF_EVEN) + "%");
        holder.viewFormLegenda.setBackgroundColor(colors.get(position));
    }

    public static class LegendaViewHolder extends RecyclerView.ViewHolder {
        public View viewFormLegenda;
        public TextView tDescricaoLegenda;

        public LegendaViewHolder(View view) {
            super(view);
            tDescricaoLegenda = (TextView) view.findViewById(R.id.tDescricaoLegenda);
            viewFormLegenda = (View) view.findViewById(R.id.viewFormLegenda);
        }
    }

    private double getPercent(int pos) {
        double valorTotal = 0;

        for (Double valor : valores) {
            valorTotal = valorTotal + valor;
        }

        double percent = (valores.get(pos) * 100) / valorTotal;

        return percent;
    }
}

And I disabled the default PieChart legend so:

mChart.getLegend().setEnabled(false);
    
25.01.2016 / 20:30