我正在尝试将轴标题添加到使用mpandroid图表库创建的条形图中。我在我的活动中使用了多个片段,条形图是一个片段。我正在动态添加数据,一切正常。我想在x和y轴上添加标题,当我搜索时,我发现它不受库支持。他们建议我们在图表中添加textview或编辑库。
我知道这个问题与这篇文章相同,但他们没有答案。 How to add String Label to MPAndroidCharts
我尝试添加一个textview进行查看,并将其添加到图表的顶部。我尝试过给layout_gravity,但它没有用。如何将textview添加到视图的底部。我也需要添加y轴标题。我也尝试添加到容器和视图。
public class BarChartFragment extends Fragment {
ScreenUtility screenUtility;
BarChart bar;
String nameOfChart,xAxisDesciption,yAxisDescription;
TextView xAxisName;
public BarChartFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
//this.setRetainInstance(true);
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
}
public View getView(){
return bar.getRootView();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
screenUtility = new ScreenUtility(getActivity());
ArrayList<BarEntry> entries = new ArrayList<>();
BarDataSet dataSet = new BarDataSet(entries,nameOfChart);
dataSet.setColors(getColors());
dataSet.setStackLabels(new String[]{"CU1", "CU2"});
dataSet.setValueFormatter(new MyValueFormatter());
ArrayList<String> labels = new ArrayList<String>();
ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
dataSets.add(dataSet);
bar = new BarChart(getActivity());
BarData data = new BarData(labels,dataSets);
bar.setData(data);
bar.setDrawValuesForWholeStack(true);
bar.setDrawBarShadow(false);
bar.setDrawValueAboveBar(false);
bar.setHighlightEnabled(false);
bar.setDrawGridBackground(false);
bar.setDescription("");
XAxis x = bar.getXAxis();
x.setDrawGridLines(false);
x.setPosition(XAxis.XAxisPosition.BOTTOM);
YAxis yLabels = bar.getAxisLeft();
yLabels.setDrawGridLines(false);
YAxis yLabels1 = bar.getAxisRight();
yLabels1.setEnabled(false);
Legend legend = bar.getLegend();
legend.setPosition(Legend.LegendPosition.BELOW_CHART_RIGHT);
bar.animateY(2000);
int layoutHeight = screenUtility.getWidth() > screenUtility.getHeight() ? (int) screenUtility.getHeight() : (int) screenUtility.getWidth();
if(screenUtility.getHeight()>screenUtility.getWidth()) {
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) screenUtility.getWidth() - 20, layoutHeight);
bar.getRootView().setLayoutParams(new ViewGroup.LayoutParams(params));
}else{
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((int) (screenUtility.getWidth()/2) - 20, layoutHeight);
bar.getRootView().setLayoutParams(new ViewGroup.LayoutParams(params));
}
bar.setNoDataText("No Data is Available");
//Tried adding Textview Here
xAxisName = new TextView(getActivity());
xAxisName.setText("Date");
xAxisName.setGravity(Gravity.BOTTOM);
container.addView(xAxisName);
return bar.getRootView();
}
我无法将textview添加到活动中,因为所有内容都是动态添加的,因此我必须将textview添加到每个图表中。 如何解决这个问题并将其置于图表的底部。
答案 0 :(得分:5)
我找到了解决方案,我会将其发布以供其他人参考。我从以下链接找到了垂直文本视图。
Vertical (rotated) label in Android
xAxisName = new TextView(getActivity());
xAxisName.setText("Date");
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
params.setMargins(0, 0, 0, 20);
VerticalTextView yAxisName = new VerticalTextView(getActivity(),null);
yAxisName.setText("Yaxis Label");
FrameLayout.LayoutParams params2 = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
params2.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
container.addView(xAxisName, params);
container.addView(yAxisName,params2);
答案 1 :(得分:1)
我建议您(在图表外使用其他文本视图)或根据您的目的修改库。
默认情况下目前不支持。
答案 2 :(得分:1)
setSpaceTop
{{1}}有一种简单的方法,设置TextView之间的边距
答案 3 :(得分:0)
您可以使用 IndexAxisValueFormatter 这样的代码
final ArrayList<String> axiss = new ArrayList<>();
axiss.add("one");
axiss.add("two");
axiss.add("three");
axiss.add("four");
axiss.add("five");
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);
xAxis.setDrawLabels(true);
xAxis.setGranularity(1f);
xAxis.setLabelRotationAngle(+90);
xAxis.setValueFormatter(new IndexAxisValueFormatter(axiss));