如何将图形设置为无穷大?

时间:2018-04-27 17:45:56

标签: android android-graphview

我找到了示例“如何使用GraphView创建绘图”。并修改了我的任务。接下来是任务:“为y = ax + b构造图”。但是如何将图形设置为无穷大?或者我如何限制Viewport?我只是想削减图形的边缘。该功能看起来不受限制。

我想看到的内容: What I want to see

我拥有的:  What I have

MainActivity.java:

package practice.application.graphviewtutorial1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

public class MainActivity extends AppCompatActivity {

LineGraphSeries<DataPoint> series;

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

    Button button = (Button)findViewById(R.id.button2);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            double y,x;
            x = -5.0;
            GraphView graph = (GraphView) findViewById(R.id.graph1);
            series = new LineGraphSeries<DataPoint>();
            EditText editText = (EditText)findViewById(R.id.editText);
            EditText editText2 = (EditText)findViewById(R.id.editText2);
            double a = Double.parseDouble(editText.getText().toString());
            double b = Double.parseDouble(editText2.getText().toString());;
            for(int i =0; i<100; i++) {
                x = x + 0.1;
                y = a* x + b;
                series.appendData(new DataPoint(x, y), true, 100);
            }
            graph.addSeries(series);
            graph.getViewport().setMaxY(series.getHighestValueY());
            graph.getViewport().setMaxX(series.getHighestValueX());
        }
    });
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="practice.application.graphviewtutorial1.MainActivity"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <EditText
        android:id="@+id/editText"
        android:layout_width="195dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="number" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="number" />

    <Button
        android:id="@+id/button2"
        android:layout_width="221dp"
        android:layout_height="46dp"
        android:layout_weight="1"
        android:text="Button" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.jjoe64.graphview.GraphView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/graph1" />


</LinearLayout>

1 个答案:

答案 0 :(得分:1)

由于您正在处理线性函数,并且正如@Psytho注释中所指出的那样,您只需要开始和结束数据点:

检查此代码:

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

    Button button = (Button) findViewById(R.id.button2);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            double y, x;
            x = -5.0;
            GraphView graph = (GraphView) findViewById(R.id.graph1);
            series = new LineGraphSeries<DataPoint>();
            EditText editText = (EditText) findViewById(R.id.editText);
            EditText editText2 = (EditText) findViewById(R.id.editText2);
            double a = 0, b = 0;
            try {
                a = Double.parseDouble(editText.getText().toString());
                b = Double.parseDouble(editText2.getText().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }

            // Start point
            x = -10;
            y = getY(x, a, b);
            series.appendData(new DataPoint(x, y), true, 100);

            // End point
            x = 10;
            y = getY(x, a, b);
            series.appendData(new DataPoint(x, y), true, 100);

            // Manually set the boundaries
            graph.addSeries(series);
            graph.getViewport().setMaxY(10);
            graph.getViewport().setMaxX(10);

            graph.getViewport().setMinY(-10);
            graph.getViewport().setMinX(-10);
        }
    });

这会给你带来理想的效果。