简体   繁体   中英

How to set fixed horizontal labels on the X axis in graphview (Android Studio)

I'm using GraphView library and I would like to display the months of the year on the X axis . my problem is that the labels are overlapping on "portrait" mode. it looks better on "landscape" mode, although the are overlapping labels also.

that is how my graph looks on a"portrait" mode :

在此输入图像描述

that is how my graph looks on a"landscape" mode (much better) :

在此输入图像描述

When I try to make shorter labels (jan,feb,mar,apr,etc...) , or smaller font size it just getting worse(it looks like it creates more vertical lines):

在此输入图像描述

Thats my code :

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

Thank you for your help !

You can rotate the lables vertically by using:

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

Or make a few lables invisible if the no of x axis values is too large by using foll

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM