java 将轴标题添加到 mpandroidchart

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30915304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:49:43  来源:igfitidea点击:

Adding Axis Titles to mpandroidchart

javaandroidmpandroidchart

提问by Sriram Thiagarajan

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.

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

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

我知道这个问题与这篇文章相同,但他们没有答案。 如何将字符串标签添加到 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.

我尝试添加一个文本视图来查看,它被添加到图表的顶部。我试过给 layout_gravity 但它不起作用。如何将文本视图添加到视图底部。我还需要添加 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();
    }

enter image description here

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.

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

回答by Sriram Thiagarajan

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.

我已经找到了解决方案,我会将其发布以供其他人将来参考。我从以下链接中找到了垂直文本视图。

Vertical (rotated) label in Android

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

回答by Andrea Cinesi

I suggest that you ( use additionalTextViews?outside the chart) or modify the library to your purpose.

我建议您(使用 additionalTextViews?在图表之外)或根据您的目的修改库。

It's currently not supported by default.

目前默认不支持。

回答by Alen Lee

There is a easy way that set the top axis spaceby setSpaceTop, set the margin between the TextView

有一种简单的方式,设置顶轴空间setSpaceTop,设置的TextView之间的余量

回答by omid

You can use IndexAxisValueFormatterlike code below

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

回答by Naresh

We can use TextView's for both the label titles and rotate the Y-axis label by 270 degrees.

我们可以将 TextView 用于标签标题并将 Y 轴标签旋转 270 度。

This is how xml code looks like:

这是 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"/>