簡體   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