繁体   English   中英

如何在android中的GraphView中自定义x轴

[英]How to customize x-axis in GraphView in android

我想在android中使用GraphView绘制线图。 我能够在y轴上打印标签,但是不会显示x轴上的标签,也没有相对于x轴正确绘制点。 有人可以请帮助。

请在下面找到我的xml代码

<?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>

活动代码如下。

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);

    }
}

我得到的图表如下所示。

在此输入图像描述

我显示的y轴标签是-20,-15,-10等。但x轴给出了麻烦。 我有两个问题

  1. 我试过了所有东西但是x轴标签没有显示出来。

  2. 另外我注意到图表没有根据我提到的x轴标签正确绘制。(是的,我已经将x轴标记为不均匀(“0”,“100”,“1000”,“10000”,“24000”) “),GraphView中是否禁止使用?因此图形绘制错误?

如果是这种情况我可以使用均匀分布,但标签的显示至少对我来说很重要

有人可以请帮助。

提前致谢。

使用此代码:

graph1.getViewport().setYAxisBoundsManual(true);
graph1.getViewport().setXAxisBoundsManual(true);

点击这个我的输出:

使用此代码

graph1.getViewport().setYAxisBoundsManual(true);
graph1.getViewport().setXAxisBoundsManual(true);

确保将hardwareAccelerated设置为true只需打开应用程序的AndroidMainfest ,然后选择应用程序标记,或者如果要为特定活动启用hardwareAccelerated,请选择活动标记,然后输入此行

android:hardwareAccelerated="true"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM