0

I'm trying to use the Android GraphView library (http://www.android-graphview.org/), but no matter what I try I can't seem to get the horizontal axis title and labels to appear. The Y-axis and Title don't seem to have any problems.

Based on this previous S.O. answer "GraphView, how to show x-axis label?", it should've been as easy as

    GridLabelRenderer gridLabel = graphView.getGridLabelRenderer();
    gridLabel.setHorizontalAxisTitle("X Axis Title");

I've tried that however and it doesn't work for me either.

My code is the following:

fragment_results.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ResultsFragment">

    <com.jjoe64.graphview.GraphView
        android:id="@+id/results_graph"
        android:layout_height="400dp"
        android:layout_width="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:visibility="visible"/>

</android.support.constraint.ConstraintLayout>

onCreate() in ResultsFragment.java

View v = inflater.inflate(R.layout.fragment_results, container, false);
mGraph = v.findViewById(R.id.results_graph);
mGraph.getViewport().setScalable(true); // enables horizontal zooming and scrolling
mGraph.getViewport().setScalableY(true); // enables vertical zooming and scrolling
mGraph.getViewport().setYAxisBoundsManual(true); // Prevents auto-rescaling the Y-axis
mGraph.getViewport().setXAxisBoundsManual(true); // Prevents auto-rescaling the X-axis
mGraph.setTitleTextSize(96);
mGraph.setTitle("Title");
mGraph.getGridLabelRenderer().setHumanRounding(true);
mGraph.getGridLabelRenderer().setVerticalAxisTitleTextSize(64);
mGraph.getGridLabelRenderer().setVerticalAxisTitle("Y Axis Title");
mGraph.getGridLabelRenderer().setHorizontalAxisTitleTextSize(64);
mGraph.getGridLabelRenderer().setHorizontalAxisTitle("X Axis Title");
mGraph.getGridLabelRenderer().setHorizontalLabelsVisible(true);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[]{
        new DataPoint(34.3, -21.0),
        new DataPoint(37.0, -21.0),
        new DataPoint(37.3, -18.0),
        new DataPoint(41.0, -18.0),
        new DataPoint(41.3, -15.0),
        new DataPoint(46.0, -15.0),
        new DataPoint(46.3, -12.0),
        new DataPoint(54.0, -12.0),
        new DataPoint(54.3, -9.0),
        new DataPoint(65.0, -9.0),
        new DataPoint(65.3, -6.0),
        new DataPoint(84.0, -6.0),
        new DataPoint(84.3, -3.0),
        new DataPoint(124.0, -3.0),
        new DataPoint(124.3, 0.0)
});
series.setTitle("TEST");
series.setColor(Color.rgb(115,230,115));
mGraph.addSeries(series);

The result is this:

graphview_noxaxis

Calseon
  • 325
  • 3
  • 18

1 Answers1

0

I made a new Android Studio project, copied the above code over, and everything worked just fine. Frustrated, I took the brute force approach of just comparing the code in every single file. The only difference I found was that I had hardware acceleration turned off in my AndroidManifest.xml

 <application
        ...
        android:hardwareAccelerated="false">
        ...
 </application>

After removing that line from the manifest, the X-axis title and labels finally showed up!

Calseon
  • 325
  • 3
  • 18