在mpandroidchart中添加坐标轴标题

8

I am trying to add axis titles to the bar chart created using mpandroid chart library. I am using more than one fragments in my activity and the bar chart is a fragment. I am adding data dynamically and everything is working fine. I want to add titles to x and y axis and when I searched I found that it is not supported by the library. They suggest we add the textview in chart or edit the library.

I know this issue is same as this post but they don't have the answer. How to add String Label to MPAndroidCharts

I tried adding a textview to view and it got added to the top of the graph. I tried giving layout_gravity but it is not working. How to add the textview to the bottom of the view. I also need to add y-axis title too.I tried adding to container and view too.

    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

I cannot add the textview to the activity beacuse everything is added dynamically and so I have to add the textview to each graph. How to solve this and bring it bottom of the graph.

5个回答

6
我已经找到解决方案,并将其发布供其他人参考。我从以下链接中找到了垂直文本视图。 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);

你有没有想过如何将这个应用到图表内的标签上? - Nilay Dani
@NilayDani:你是在说移动MPAndroidChart已有元素的位置吗? - Sriram Thiagarajan
不好意思,我在问能否旋转图表内的值文本?默认情况下是水平显示的,没有旋转选项... - Nilay Dani
@NilayDani:是的,我认为没有任何选项可以这样做。你必须等待开发人员添加该选项或自己浏览代码并进行更改。 - Sriram Thiagarajan

4
您可以像以下代码一样使用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));

1
我建议您在图表外使用additionalTextViews或修改库以适应您的需求。目前默认不支持此功能。

有误解。 - Andrea Cinesi

1

1
我们可以使用TextView来作为标签标题,并将Y轴标签旋转270度。
以下是xml代码的样子:
<TextView
    android:id="@+id/ylabel_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:rotation="270"
    android:text="Y-label" />


<TextView
    android:id="@+id/xlabel_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="X-label"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接