如何在graphview中设置X轴上的固定水平标签(Android Studio)

时间:2016-09-21 11:15:37

标签: java android-studio graph android-graphview

我正在使用GraphView库,我想在X轴上显示一年中的几个月。我的问题是标签在“纵向”模式下重叠。它在“风景”模式下看起来更好,尽管它们也是重叠的标签。

这就是我的图表在“肖像”模式下的显示方式:

enter image description here

这就是我的图表在“风景”模式下的表现(更好):

enter image description here

当我尝试制作更短的标签(jan,feb,mar,apr等等)或更小的字体大小时,它会变得更糟(看起来它会创建更多的垂直线条):

enter image description here

这是我的代码:

   GraphView graph_LastYear = (GraphView) findViewById(R.id.graph_yearly);
    GraphView graph_Last3Months = (GraphView) findViewById(R.id.graph_last3month);

    LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[]{

            new DataPoint(1, Sallries_WholeYear.get(0).getNetoPayment()),
            new DataPoint(2, Sallries_WholeYear.get(1).getNetoPayment()),
            new DataPoint(3, Sallries_WholeYear.get(2).getNetoPayment()),
            new DataPoint(4, Sallries_WholeYear.get(3).getNetoPayment()),
            new DataPoint(5, Sallries_WholeYear.get(4).getNetoPayment()),
            new DataPoint(6, Sallries_WholeYear.get(5).getNetoPayment()),
            new DataPoint(7, Sallries_WholeYear.get(6).getNetoPayment()),
            new DataPoint(8, Sallries_WholeYear.get(7).getNetoPayment()),
            new DataPoint(9, Sallries_WholeYear.get(8).getNetoPayment()),
            new DataPoint(10, Sallries_WholeYear.get(9).getNetoPayment()),
            new DataPoint(11, Sallries_WholeYear.get(10).getNetoPayment()),
            new DataPoint(12, Sallries_WholeYear.get(11).getNetoPayment())
    }
    );



    series.setDrawDataPoints(true);
    series.setColor(Color.BLACK);
    series.setDrawDataPoints(true);
    series.setDataPointsRadius(10);
    series.setThickness(8);

    graph_LastYear.addSeries(series);
    graph_LastYear.setTitle("Last Year");
    graph_LastYear.setTitleTextSize(60);

    graph_LastYear.getViewport().setScalable(true);
    graph_LastYear.getViewport().setScrollable(true);



    graph_LastYear.getGridLabelRenderer().setTextSize(40);
    graph_LastYear.getGridLabelRenderer().setNumHorizontalLabels(12);
    graph_LastYear.getGridLabelRenderer().setGridColor(Color.BLUE);


    StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph_LastYear);
    staticLabelsFormatter.setHorizontalLabels(new String[]{getString(R.string.Janury_Month), getString(R.string.February_Month), getString(R.string.March_Month), getString(R.string.April_Month), getString(R.string.My_Month), getString(R.string.June_Month), getString(R.string.July_Month), getString(R.string.August_Month),
            getString(R.string.September_Month), getString(R.string.October_Month), getString(R.string.November_Month), getString(R.string.December_Month)});
    graph_LastYear.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用以下方法垂直旋转标签:

GridLabelRenderer renderer = graphView.getGridLabelRenderer();
renderer.setHorizontalLabelsAngle(90);

或者,如果使用foll

,则x轴值的值过大,则会使一些标签不可见
GridLabelRenderer gridLabel = graphView.getGridLabelRenderer();
gridLabel.setLabelFormatter(new DefaultLabelFormatter(){
            @Override
            public String formatLabel(double value, boolean isValueX) {
                if (isValueX) {
                    // return as Integer
                    if (value % Commons.getDivisier(finalCategoryArray.length) == 0){
                        return ""+ finalCategoryArray[((int) value)];
                    }else {
                        return null;
                    }

                } else {
                    // show currency for y values
                    return super.formatLabel(value, isValueX);
                }
            }
        });

public static double getDivisier(int length) {
        if (length < 5){
                return 1;
        }else if (length < 10){
            return 2;
        }else if (length < 50){
            return 5;
        }else if (length < 100){
            return 10;
        }
        return 1;
    }