2015-03-19 22 views
7

Sto usando MPAndroidChart library. Voglio personalizzare le legende in MPAndroidChart. Nella libreria MPAndroidChart ho cercato di impostare la posizione delle legende. con il codice dato legend.setPosition (LegendPosition.BELOW_CHART_CENTER) ma non è in grado di farlo. Devo impostare leggende come mostrato nella seguente immaginePersonalizzazione della legenda MPAndroidChart

aiuto sarà apprezzeranno enter image description here

risposta

6

Nel tuo caso mi sento di raccomandare che si disattiva il Legend che viene disegnata dal grafico e invece venire con la propria implementazione.

chart.getLegend().setEnabled(false) 

Nel caso sopra indicato si avrà probabilmente bisogno di un ListView che prende i dati dal grafici Legend oggetto e lo visualizza.

Quando si guarda lo Legend class si noterà che ha variabili membro per colori ed etichette.

È possibile recuperare tali array (getColors(), getLegendLabels()) e utilizzarli per visualizzarli nello ListView.

+0

c'è un array che ha anche le percentuali? –

9

Si prega di cercare la risposta data MPAndroidChart - Legend labels are being cut off. Ho già fornito la risposta in base al tuo problema. Cerca il codice che ti sarà sicuramente d'aiuto. Si dovrà implementare leggende personalizzate con i loro colori leggende e le etichette seguendo le istruzioni riportate di seguito:

Fase 1

Legend legend = mChart.getLegend(); 

Fase 2

int colorcodes[] = legend.Colors(); 

Passi 3

for (int i = 0; i < legend.Colors().length-1; i++) { 
..... 
..... 
} 

Passi 4

allora si dovrà prendere un layout orizzontale o verticale e ottenere i codici di colore leggende e leggende etichetta e in base alla lunghezza leggende creare il layout e l'etichetta. Esempio di codice è il seguente:

 LinearLayout.LayoutParams parms_left_layout = new LinearLayout.LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     parms_left_layout.weight = 1F; 
     LinearLayout left_layout = new LinearLayout(context); 
     left_layout.setOrientation(LinearLayout.HORIZONTAL); 
     left_layout.setGravity(Gravity.CENTER); 
     left_layout.setLayoutParams(parms_left_layout); 

     LinearLayout.LayoutParams parms_legen_layout = new LinearLayout.LayoutParams(
       20, 20); 
     parms_legen_layout.setMargins(0, 0, 20, 0); 
     LinearLayout legend_layout = new LinearLayout(context); 
     legend_layout.setLayoutParams(parms_legen_layout); 
     legend_layout.setOrientation(LinearLayout.HORIZONTAL); 
     legend_layout.setBackgroundColor(colorcodes[i]); 
     left_layout.addView(legend_layout); 

     TextView txt_unit = new TextView(context); 
     txt_unit.setText(legend.getLabel(i)); 
     left_layout.addView(txt_unit); 

     LinearLayout.LayoutParams parms_middle_layout = new LinearLayout.LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     parms_middle_layout.weight = 1F; 
     LinearLayout middle_layout = new LinearLayout(this); 
     middle_layout.setOrientation(LinearLayout.HORIZONTAL); 
     middle_layout.setGravity(Gravity.CENTER); 
     middle_layout.setLayoutParams(parms_middle_layout); 

     TextView txt_leads = new TextView(this); 
     txt_leads.setText("450"); 
     middle_layout.addView(txt_leads); 

     LinearLayout.LayoutParams parms_right_layout = new LinearLayout.LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     parms_right_layout.weight = 1F; 
     LinearLayout right_layout = new LinearLayout(this); 
     right_layout.setOrientation(LinearLayout.HORIZONTAL); 
     right_layout.setGravity(Gravity.CENTER); 
     right_layout.setLayoutParams(parms_right_layout); 

     TextView txt_leads_percentage = new TextView(this); 
     txt_leads_percentage.setText(munit_percentage_list.get(i) + ""); 
     right_layout.addView(txt_leads_percentage); 

     childlayout.addView(left_layout); 
     childlayout.addView(middle_layout); 
     childlayout.addView(right_layout); 

E dopo questo aggiungere il vostro (layout di bambino che si è creato in fase di esecuzione) per il layout principale.

+1

Inoltre hai disabilitato la legenda di dedefault legend.getLegend(). SetEnabled (false) –

+1

Puoi farlo con un gridview e l'adattatore riceverà il nome dell'etichetta e il colore del set di riquadri – rguerra

+0

@Aman Rohila come nascondere la linea orizzontale in Bar Grafico di MPAndroidChart. Ho provato in questo modo xAxis.setDrawAxisLine (false); xAxis.setDrawGridLines (false); xAxis.setGridColor (Color.parseColor ("# 00000000")); xAxis.setAxisLineColor (Color.parseColor ("# 00000000")); E yAxis.setDrawAxisLine (false); yAxis.setDrawGridLines (false); yAxis.setGridColor (Colore.parseColor ("# 00000000")); yAxis.setAxisLineColor (Color.parseColor ("# 00000000")); Sto ancora ottenendo una linea orizzontale. –

0
Follow below code for custom legend. 
    create table_row_legend.xml in layout resource 

    <TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:weightSum="3"> 
     <LinearLayout 
      android:id="@+id/tv_color_container" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="center" 
      android:layout_weight="0.30" 
      android:orientation="horizontal" 
      android:gravity="right" 
      android:padding="5dp"> 

      <LinearLayout 
       android:id="@+id/tv_color" 
       android:layout_width="20dp" 
       android:layout_height="20dp" 
       android:layout_gravity="center" 

       android:orientation="horizontal" 
       /> 
     </LinearLayout> 

     <TextView 
      android:id="@+id/tv_label" 
      android:layout_width="0dp" 
      android:layout_gravity="top" 
      android:layout_weight="1.35" 

      android:gravity="left|top" 
      android:padding="3dp" 
      android:singleLine="true" 

      android:textColor="#2b2b2b" 
      android:textSize="16sp" /> 

     <TextView 
      android:id="@+id/tv_amt" 
      android:layout_width="0dp" 
      android:layout_weight="1.35" 
      android:gravity="left|top" 
      android:padding="3dp" 

      android:textColor="#2b2b2b" 
      android:textSize="16sp" /> 
    </TableRow> 



    Create new Linear layout below your Pie chart and wrap parent layout with scroll layout with static height to pie chart 

    <?xml version="1.0" encoding="utf-8"?> 
    <ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:background="#ffffff" 
     android:layout_height="match_parent"> 

     <RelativeLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginTop="10dp" 
      android:paddingBottom="10dp" 
      android:layout_marginBottom="10dp" 
      android:layout_centerInParent="true" 
      > 
     <com.github.mikephil.charting.charts.PieChart 
       android:id="@+id/chart1" 
       android:layout_width="match_parent" 
       android:layout_height="400dp" 
       android:layout_below="@+id/tv_info" 
       android:layout_centerHorizontal="true" 
       android:layout_marginTop="10dp" 

       android:background="#ffffff" 
       android:clickable="true" /> 

      <TableLayout 
       android:id="@+id/child_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/chart1" 
       android:orientation="vertical" /> 
     </RelativeLayout> 

    </ScrollView> 


**Make changes in your activity class as follows** 

public void setCustomLegend(){ 
     int colorcodes[] = l.getColors(); 
     Context context = DistributorGraphActivity.this; 
     for (int i = 0; i < l.getColors().length - 1; i++) { 
      LayoutInflater inflater = getLayoutInflater(); 
      TableRow tr = (TableRow)   inflater.inflate(R.layout.table_row_legend, 
        childlayout, false); 
      childlayout.addView(tr); 
      LinearLayout linearLayoutColorContainer=(LinearLayout) tr.getChildAt(0); 
      LinearLayout linearLayoutColor= (LinearLayout)   linearLayoutColorContainer.getChildAt(0); 
      TextView tvLabel = (TextView) tr.getChildAt(1); 
      TextView tvAmt = (TextView) tr.getChildAt(2); 
      linearLayoutColor.setBackgroundColor(colorcodes[i]); 
      tvLabel.setText(l.getLabel(i)); 
      tvAmt.setText(arrListDealerGraph.get(i).getAmt()); 
     } 
     mChart.getLegend().setWordWrapEnabled(true); 
     mChart.getLegend().setEnabled(false); 
} 
Problemi correlati