我正在使用
MPAndroidChart library.我想在MPAndroidChart中自定义图例.在MPAndroidChart库中,我试图设置图例的位置.通过给定代码legend.setPosition(LegendPosition.BELOW_CHART_CENTER)但无法执行此操作.我必须设置图例如下图所示
帮助将是欣赏
解决方法
请查找给定答案
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,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);
然后将您(在运行时创建的子布局)添加到主布局.