Mpandroidchart

Asked

Viewed 600 times

1

I am using Mpandroidchart to make charts.

The problem is this, when I have many items in the caption, it expands leaving the chart small. You can create a scroll in the caption instead of expanding it and consequently decrease the chart?

Imagem do Problema

----- Edit-----

 @Override
protected void onCreate(Bundle savedInstanceState) {
    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);
}
  • Welcome to SOPT. Would like to [Edit] your post and add the code you are using, so we can analyze and suggest a change. Thank you

2 answers

0


I’ve been using this library a lot, but I haven’t put subtitles on them.

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

The caption is on the chart, I don’t know if it fits your case.

On the official library page is the source code of this example.

In the legend configuration 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’ve helped!

-1

There’s really no way to create the scroll. I posted there in English Stackoverflow and the guy who created the Mpandroidchart told me to decrease the caption, but then I said I was with big letter just to simulate a large amount of data... Then he didn’t answer until now.

But I solved my problem by creating my own caption, defining a Linearlayout below the chart, and in Linearlayout I put a recyclerview. In the Adapter I put a view to be painted your backgorund with the corresponding color of the chart and a Textview on the front. It looked like this:

Imagem

XML:

<?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: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 standard Piechart caption:

mChart.getLegend().setEnabled(false);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.