【问题标题】:MPAndroidChart legend customizationMPAndroidChart 图例自定义
【发布时间】:2015-03-19 07:20:02
【问题描述】:

我正在使用MPAndroidChart library。我想在 MPAndroidChart 中自定义图例。在 MPAndroidChart 库中,我尝试设置图例的位置。通过给定的代码 legend.setPosition(LegendPosition.BELOW_CHART_CENTER) 但无法做到。我必须如下图所示设置图例

帮助将不胜感激

【问题讨论】:

    标签: android pie-chart mpandroidchart


    【解决方案1】:

    在您的情况下,我建议您禁用图表绘制的Legend,而是提出您自己的实现。

    chart.getLegend().setEnabled(false)
    

    在上面显示的情况下,您可能需要一个ListView,它从图表Legend 对象中获取数据并显示它。

    当您查看Legend class 时,您会注意到它具有颜色和标签的成员变量。

    您可以检索这些数组(getColors()getLegendLabels())并使用它们在ListView 中显示。

    【讨论】:

    • 有没有包含百分比的数组?
    【解决方案2】:

    请寻找给定的答案MPAndroidChart - Legend labels are being cut off。我已经根据你的问题提供了答案。 寻找给定的代码,这肯定会对你有所帮助。 您必须按照以下步骤使用其图例颜色和标签来实现自定义图例:

    第 1 步

    Legend legend = mChart.getLegend();
    

    第 2 步

    int colorcodes[] = legend.Colors();
    

    步骤 3

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

    步骤 4

    然后您将不得不采用一种水平或垂直布局并获取图例颜色代码和图例标签,并根据图例长度创建布局和标签。代码示例如下:

            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);
    

    然后将您的(您在运行时创建的子布局)添加到主布局。

    【讨论】:

    • 你也有默认禁用 Legend chart.getLegend().setEnabled(false)
    • 您可以使用 gridview 来执行此操作,并且适配器会收到标签名称和 linedataset 颜色
    • @Aman Rohila 如何隐藏 MPAndroidChart 条形图中的水平线。我试过这样 xAxis.setDrawAxisLine(false); xAxis.setDrawGridLines(false); xAxis.setGridColor(Color.parseColor("#00000000")); xAxis.setAxisLineColor(Color.parseColor("#00000000"));和 yAxis.setDrawAxisLine(false); yAxis.setDrawGridLines(false); yAxis.setGridColor(Color.parseColor("#00000000")); yAxis.setAxisLineColor(Color.parseColor("#00000000"));我仍然得到水平线。
    【解决方案3】:

    用于设置自定义图例:

    public void setLegends(){
    
    Legend l = holder.pieChart.getLegend();
    
    l.getEntries();
    
    l.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
    
    l.setYEntrySpace(10f);
    
    l.setWordWrapEnabled(true);
    
    LegendEntry l1=new LegendEntry("Male",Legend.LegendForm.CIRCLE,10f,2f,null,Color.YELLOW);
    LegendEntry l2=new LegendEntry("Female", Legend.LegendForm.CIRCLE,10f,2f,null,Color.RED);
    
    l.setCustom(new LegendEntry[]{l1,l2});
    
    l.setEnabled(true);
    
    }
    

    【讨论】:

    • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
    【解决方案4】:

    按照custom legend 的以下代码。 在布局资源中创建table_row_legend.xml

        <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>
    

    在您的饼图下方创建新的LinearLayout,并使用具有静态高度的滚动布局将父布局包装到饼图

        <?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>
    

    在您的活动类中进行如下更改

    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);
    }
    

    【讨论】:

      【解决方案5】:

      对于 Kotlin,命令是:

      mChart.legend.isEnabled = false
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多