GraphView 的横轴标题和标签没有出现
GraphView's horizontal axis title and labels aren't appearing
我正在尝试使用 Android GraphView 库 (http://www.android-graphview.org/),但无论我尝试什么,我似乎都无法显示横轴标题和标签。 Y-axis 和 Title 好像没有问题。
基于之前的 S.O。回答“GraphView, how to show x-axis label?”,应该和
一样简单
GridLabelRenderer gridLabel = graphView.getGridLabelRenderer();
gridLabel.setHorizontalAxisTitle("X Axis Title");
不过我已经试过了,它对我也不起作用。
我的代码如下:
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() 在 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);
结果是这样的:
我新建了一个Android Studio项目,把上面的代码复制过来,一切正常。沮丧的是,我采用了仅比较每个文件中的代码的蛮力方法。我发现的唯一区别是我在 AndroidManifest.xml
中关闭了硬件加速
<application
...
android:hardwareAccelerated="false">
...
</application>
从清单中删除该行后,X-axis 标题和标签终于出现了!
我正在尝试使用 Android GraphView 库 (http://www.android-graphview.org/),但无论我尝试什么,我似乎都无法显示横轴标题和标签。 Y-axis 和 Title 好像没有问题。
基于之前的 S.O。回答“GraphView, how to show x-axis label?”,应该和
一样简单 GridLabelRenderer gridLabel = graphView.getGridLabelRenderer();
gridLabel.setHorizontalAxisTitle("X Axis Title");
不过我已经试过了,它对我也不起作用。
我的代码如下:
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() 在 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);
结果是这样的:
我新建了一个Android Studio项目,把上面的代码复制过来,一切正常。沮丧的是,我采用了仅比较每个文件中的代码的蛮力方法。我发现的唯一区别是我在 AndroidManifest.xml
中关闭了硬件加速 <application
...
android:hardwareAccelerated="false">
...
</application>
从清单中删除该行后,X-axis 标题和标签终于出现了!