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.
I get y-axis labels displayed that is -20,-15,-10 etc. But x axis gives trouble. I have 2 concerns
I tried everything but then x-axis labels don't get displayed.
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.
Use this code:
graph1.getViewport().setYAxisBoundsManual(true);
graph1.getViewport().setXAxisBoundsManual(true);
click this my output:
use this code
graph1.getViewport().setYAxisBoundsManual(true);
graph1.getViewport().setXAxisBoundsManual(true);
确保将hardwareAccelerated设置为true只需打开应用程序的AndroidMainfest ,然后选择应用程序标记,或者如果要为特定活动启用hardwareAccelerated,请选择活动标记,然后输入此行
android:hardwareAccelerated="true"
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.