gpt4 book ai didi

java - 将轴标题添加到 mpandroidchart

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:20:09 24 4
gpt4 key购买 nike

我正在尝试将轴标题添加到使用 mpandroid 图表库创建的条形图中。我在我的 Activity 中使用了多个 fragment ,条形图是一个 fragment 。我正在动态添加数据,一切正常。我想在 x 轴和 y 轴上添加标题,当我搜索时发现图书馆不支持它。他们建议我们在图表中添加 TextView 或编辑库。

我知道这个问题与这篇文章相同,但他们没有答案。 How to add String Label to MPAndroidCharts

我尝试添加一个 TextView 来查看,它被添加到图表的顶部。我尝试提供 layout_gravity 但它不起作用。如何将 TextView 添加到 View 底部。我也需要添加 y 轴标题。我也尝试添加到容器和 View 。

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

enter image description here

我无法将 TextView 添加到 Activity 中,因为所有内容都是动态添加的,所以我必须将 TextView 添加到每个图形中。如何解决这个问题并将其置于图表底部。

最佳答案

我已经找到了解决方案,我会将其发布以供将来其他人引用。我从以下链接找到了垂直 TextView 。

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

关于java - 将轴标题添加到 mpandroidchart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30915304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com