将轴标题添加到mpandroidchart [英] Adding Axis Titles to mpandroidchart

查看:82
本文介绍了将轴标题添加到mpandroidchart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将轴标题添加到使用mpandroid图表库创建的条形图中.我在活动中使用了多个片段,而条形图是一个片段.我正在动态添加数据,并且一切正常.我想在x和y轴上添加标题,当我搜索时发现该库不支持该标题.他们建议我们在图表中添加textview或编辑库.

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.

我知道这个问题与这篇文章相同,但是他们没有答案. 如何将字符串标签添加到MPAndroidCharts

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

我尝试添加一个textview进行查看,并将其添加到了图形的顶部.我试着给layout_gravity,但它不起作用.如何将textview添加到视图的底部.我还需要添加y轴标题.我也尝试添加到容器并查看.

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

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

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.

推荐答案

我已经找到了解决方案,并将其发布给他人参考.我从下面的链接中找到了垂直的textview.

I have found the solution and I will post it for future reference for other people. I found the vertical textview from the following link.

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

这篇关于将轴标题添加到mpandroidchart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆