Marc
Marc

Reputation: 1189

Android GraphView adjusting position of horizontal labels for round numbers

I am using Android Graph View to display line graphs. I need to change the horizontal labels to have nice rounded numbers. In the screen shot attached the values are

1
11:15
22:29
33:44

I need to adjust the position of the horizontal labels and the vertical lines that come off of it so the labels read

0
11:00
23:00
33:44 

The code for the above screen shot uses this base class

protected TextView text;
protected GraphView graph;

@Override
public View onCreateView(final LayoutInflater inflater,
                         ViewGroup container,
                         Bundle savedInstanceState)
{
    View view =  inflater.inflate(R.layout.ride_chart_fragment, null, false);

    graph = (GraphView) view.findViewById(R.id.graph);
    if (graph==null){
        Log.e("Rides","Graph should not ne null");
        return view;
    }
    Series series = linePoints();
    if (series==null) return view;


    graph.getGridLabelRenderer().setNumHorizontalLabels(4);

    graph.addSeries(series);
    setViewport(graph, series);

    text = (TextView)view.findViewById(R.id.textView);
    text.setVisibility(View.INVISIBLE);


    graph.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {
        @Override
        public String formatLabel(double value, boolean isValueX) {
            if (isValueX) {
                // show normal x values
                return UnitConverter.secondsToInterval((float) value);
            } else {

                return super.formatLabel(value, isValueX) + units(new Settings(inflater.getContext()));
            }
        }
    });

    graph.getGridLabelRenderer().setLabelVerticalWidth((int)getResources().getDimension(R.dimen.graph_label_vertical_width));
    graph.getGridLabelRenderer().setLabelHorizontalHeight((int)getResources().getDimension(R.dimen.graph_label_vertical_height));



    graph.getGridLabelRenderer().setGridColor(Color.parseColor("#20000000"));
    graph.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.BOTH);
    //graph.getGridLabelRenderer().
    return view;
}

and this child class to render the graph

protected Series linePoints() {

    //RideDetailActivity activity = (RideDetailActivity)getActivity();
    if (activity==null || activity.isFinishing() || activity.ride==null) return null;
    Settings settings = new Settings(activity);

    long rideId = activity.ride.sqlRide.getId();
    LezyneLinkApplication application = (LezyneLinkApplication)activity.getApplicationContext();
    DaoSession session = application.getDaoSession();
    RideElevationDao table = session.getRideElevationDao();
    List<RideElevation> elevations = table
            .queryBuilder()
            .where(RideElevationDao.Properties.RideId.eq(rideId))
            .orderAsc(RideElevationDao.Properties.Index)
            .list();
    DataPoint dataPoints[] = new DataPoint[elevations.size()];
    int index=0;
    boolean isMetric = settings.isMetric();
    for (RideElevation elevation : elevations){
        double y = elevation.getY();
        if (!settings.isMetric()){
            y = UnitConverter.convertMetersToFeet(y);
        }
        DataPoint point = new DataPoint(elevation.getX(),y);
        dataPoints[index] = point;
        index++;
    }

    LineGraphSeries series =  new LineGraphSeries<DataPoint>(dataPoints);
    series.setDrawBackground(true);
    series.setColor(Color.argb(0xFF, 0x8e, 0x00, 0xe8));
    series.setBackgroundColor(Color.argb(0x3F, 0x47, 0x2c, 0x17));
    series.setThickness(6);
    return series;
}

@Override
protected void setViewport(GraphView graph,Series series) {

    graph.getViewport().setXAxisBoundsManual(true);
    graph.getViewport().setMaxX(series.getHighestValueX());
    graph.getViewport().setYAxisBoundsManual(true);
    double lowest = series.getLowestValueY();
    double highest = series.getHighestValueY();
    graph.getViewport().setMinY(lowest);
    Settings settings = new Settings(context);
    if (settings.isMetric()){
        if (highest<lowest+121) highest = lowest+121;
    }
    else {
        if (highest<lowest+400) highest = lowest+400;
    }
    graph.getViewport().setMaxY(highest);
}

Im not seeing any way of doing this with the library as is so I am considering changing the source code. Did I miss something in the API? Anybody have any suggestions about where in the code to add this functionality.

Graph Screen Shot

Upvotes: 1

Views: 1846

Answers (1)

appsthatmatter
appsthatmatter

Reputation: 6417

you have two ways:

1) (difficult) with dynamic viewport take a look into the source code and find the point where the humanRound is done (GridLabelRenderer.java)

understand it, and modify ;)

2) use a fixed viewport and calculate on your own the min and max bounds and you can change the numberOfHorizontalLabels to get the best match.

Upvotes: 1

Related Questions