📌  相关文章
📜  Android中的点图系列

📅  最后修改于: 2021-05-10 14:38:07             🧑  作者: Mango

我们已经看到了在Android中使用简单的折线图和BarChart实现来以图形格式表示数据。表示数据的另一种图形格式是点图系列。在本文中,我们将介绍Android中Point Graph Series的实现。

我们将在本文中构建什么?

我们将构建一个简单的应用程序,在该应用程序中,我们将在Android的点图视图中显示示例数据。下面提供了一个示例视频,以使您对我们在本文中将要做的事情有个大概的了解。注意,我们将使用Java语言实现该项目。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

第2步:将依赖项添加到build.gradle(Module:app)

导航到Gradle脚本> build.gradle(Module:app)并将以下依赖项添加到“依赖项”部分。

添加以上依赖项后,现在同步您的项目,现在我们将转向实现GraphView。

步骤3:使用activity_main.xml文件

导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。

XML


  
    
    
      


Java
import android.os.Bundle;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.PointsGraphSeries;
  
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initializing our variable for graph view.
        GraphView graphView = findViewById(R.id.idGraphView);
          
        // on below line we are creating a new data
        // point series for our point graph series.
        // we are calling get data point method to add 
        // data point to our point graph series.
        PointsGraphSeries series = new PointsGraphSeries<>(getDataPoint());
          
        // below line is to add series
        // to our graph view.
        graphView.addSeries(series);
          
        // below line is to activate
        // horizontal scrolling.
        graphView.getViewport().setScrollable(true);
          
        // below line is to activate horizontal 
        // zooming and scrolling.
        graphView.getViewport().setScalable(true);
          
        // below line is to activate vertical and 
        // horizontal zoom with scrolling.
        graphView.getViewport().setScalableY(true);
          
        // below line is to activate vertical scrolling.
        graphView.getViewport().setScrollableY(true);
          
        // below line is to set shape 
        // for the point of graph view.
        series.setShape(PointsGraphSeries.Shape.TRIANGLE);
          
        // below line is to set 
        // the size of our shape.
        series.setSize(12);
          
        // below line is to add color 
        // to our shape of graph view.
        series.setColor(R.color.purple_200);
    }
  
    private DataPoint[] getDataPoint() {
        // creating a variable for data point.
        DataPoint[] dataPoints = new DataPoint[]
                {
                        // on below line we are adding a new
                        // data point to our Data Point class.
                        new DataPoint(0, 1),
                        new DataPoint(1, 2),
                        new DataPoint(2, 3),
                        new DataPoint(3, 5),
                        new DataPoint(4, 1),
                        new DataPoint(4, 3),
                        new DataPoint(5, 3),
                        new DataPoint(6, 2)
                };
        // at last we are returning
        // the data point class.
        return dataPoints;
    }
}


步骤4:使用MainActivity。 Java文件

转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.os.Bundle;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.PointsGraphSeries;
  
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initializing our variable for graph view.
        GraphView graphView = findViewById(R.id.idGraphView);
          
        // on below line we are creating a new data
        // point series for our point graph series.
        // we are calling get data point method to add 
        // data point to our point graph series.
        PointsGraphSeries series = new PointsGraphSeries<>(getDataPoint());
          
        // below line is to add series
        // to our graph view.
        graphView.addSeries(series);
          
        // below line is to activate
        // horizontal scrolling.
        graphView.getViewport().setScrollable(true);
          
        // below line is to activate horizontal 
        // zooming and scrolling.
        graphView.getViewport().setScalable(true);
          
        // below line is to activate vertical and 
        // horizontal zoom with scrolling.
        graphView.getViewport().setScalableY(true);
          
        // below line is to activate vertical scrolling.
        graphView.getViewport().setScrollableY(true);
          
        // below line is to set shape 
        // for the point of graph view.
        series.setShape(PointsGraphSeries.Shape.TRIANGLE);
          
        // below line is to set 
        // the size of our shape.
        series.setSize(12);
          
        // below line is to add color 
        // to our shape of graph view.
        series.setColor(R.color.purple_200);
    }
  
    private DataPoint[] getDataPoint() {
        // creating a variable for data point.
        DataPoint[] dataPoints = new DataPoint[]
                {
                        // on below line we are adding a new
                        // data point to our Data Point class.
                        new DataPoint(0, 1),
                        new DataPoint(1, 2),
                        new DataPoint(2, 3),
                        new DataPoint(3, 5),
                        new DataPoint(4, 1),
                        new DataPoint(4, 3),
                        new DataPoint(5, 3),
                        new DataPoint(6, 2)
                };
        // at last we are returning
        // the data point class.
        return dataPoints;
    }
}

现在运行您的应用程序,并查看该应用程序的输出。

输出:

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!