2

I am trying to draw a line graph using GraphView in android. I am able to print labels in the y axis, but the labels in the x-axis don't get displayed, also the points are not plotted correctly with respect to the x-axis. Can someone please help.

Please find my xml code below

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.GraphActivity">

    <TextView
        android:id="@+id/linetext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar Graph"
        android:textSize="16dp"
        android:layout_marginBottom="20dp"/>

    <com.jjoe64.graphview.GraphView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:title="Graph Title"
        android:id="@+id/graph"
        android:layout_below="@+id/linetext"/>

</RelativeLayout>

The activity code is as follows.

package com.example;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;
import com.jjoe64.graphview.helper.StaticLabelsFormatter;

public class GraphActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.graph_activity_layout);

        // Line Graph
        GraphView line_graph = (GraphView) findViewById(R.id.graph);
        LineGraphSeries<DataPoint> line_series =
                new LineGraphSeries<DataPoint>(new DataPoint[] {
                        new DataPoint(100, -20),
                        new DataPoint(1000, -15),
                        new DataPoint(10000, -10),
                        new DataPoint(12000, -5),
                        new DataPoint(14000, 0),
                        new DataPoint(16000, 5),
                        new DataPoint(18000, 10),
                        new DataPoint(19000, 15),
                        new DataPoint(21000, 20)
                });
        line_graph.addSeries(line_series);
        line_series.setDrawDataPoints(true);
        line_series.setDataPointsRadius(10);

        // set the bound

        // set manual X bounds
        StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(line_graph);
        staticLabelsFormatter.setVerticalLabels(new String[] {"-20", "-15", "-10","-5","0","5","10","15","20"});
        line_graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);

        // set manual Y bounds
        staticLabelsFormatter.setHorizontalLabels(new String[] {"0","100","1000","10000","24000"});
        line_graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);

        line_graph.getViewport().setScrollable(true);

    }
}

The graph I got is displayed below.

enter image description here

I get y-axis labels displayed that is -20,-15,-10 etc. But x axis gives trouble. I have 2 concerns

  1. I tried everything but then x-axis labels don't get displayed.

  2. Also I notice that the graph is not plotted correctly based on the x-axis labels that I have mentioned.(Yes i have labelled the x axis unevenly ("0","100","1000","10000","24000"), Is it disallowed in GraphView? hence the graph is plotted wrong?

If that is the case I can use a even distribution, but the display of labels at least is important to me

Can someone please help.

Thanks in advance.

qualitytest
  • 763
  • 1
  • 10
  • 18

3 Answers3

0

Use this code:

graph1.getViewport().setYAxisBoundsManual(true);
graph1.getViewport().setXAxisBoundsManual(true);
Jaap
  • 81,064
  • 34
  • 182
  • 193
sukumar v
  • 1
  • 1
0

click this my output:

use this code

graph1.getViewport().setYAxisBoundsManual(true);
graph1.getViewport().setXAxisBoundsManual(true);
Jaap
  • 81,064
  • 34
  • 182
  • 193
sukumar v
  • 1
  • 1
0

Make sure to set hardwareAccelerated to true Simply open the AndroidMainfest of the application then select application tag or select the activity tag if you want to enable hardwareAccelerated for a specific activity , and enter this line

android:hardwareAccelerated="true"
Yousef Elsayed
  • 115
  • 2
  • 8