📜  如何在 Android 中为标签添加后缀和前缀并将图例添加到图形?

📅  最后修改于: 2022-05-13 01:54:37.337000             🧑  作者: Mango

如何在 Android 中为标签添加后缀和前缀并将图例添加到图形?

当我们正在寻找表示一些统计数据的视图或在您的应用程序中寻找用于显示图形的 UI 时,因此在本文中,我们将看看创建 LineGraphView 并在我们的图形标签中添加和前缀安卓应用。在本文中,我们将在我们的 Android 应用程序中构建一个简单的折线图视图,我们将在我们的应用程序中显示一些示例数据并为标签添加后缀和前缀。

为标签添加后缀和前缀

分步实施

第 1 步:创建一个新项目

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

第 2 步:将其添加到 build.gradle 文件中

步骤 3:使用 activity_main.xml 文件

导航到app > res > layout > activity_main.xml并将以下代码添加到该文件中。下面是activity_main.xml文件的代码。

XML


  
    
  


Java
import android.graphics.Color;
import android.os.Bundle;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.jjoe64.graphview.DefaultLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.PointsGraphSeries;
  
public class MainActivity extends AppCompatActivity {
  
    GraphView graphView;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        graphView = findViewById(R.id.graphview);
          
        // For creating Point Graph Series We use PointGraphSeries
        PointsGraphSeries series = new PointsGraphSeries<>(getDataPoint());
  
        graphView.addSeries(series);
          
        // we use this method to define the 
        // shape that will be used for data points
        series.setShape(PointsGraphSeries.Shape.TRIANGLE);
          
        // we use this method to 
        // define the size of the shape
        series.setSize(50);
          
        // we use this method 
        // to set the color
        series.setColor(Color.RED);
          
        // adding the prefix and suffix here using gridlabelrenderer
        graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {
            @Override
            public String formatLabel(double value, boolean isValueX) {
                  
                // if valuex then add the prefix
  
                // return "$"+super.formatLabel(value, isValueX);
                // if valuex then add the suffix
  
                // return super.formatLabel(value, isValueX)+"$";
                if (isValueX) {
                    return "$" + super.formatLabel(value, isValueX);
                }
                return super.formatLabel(value, isValueX);
            }
        });
    }
  
    private DataPoint[] getDataPoint() {
        DataPoint[] dp = new DataPoint[]{
                new DataPoint(0, 1),
                new DataPoint(2, 7),
                new DataPoint(3, 5),
                new DataPoint(5, 2),
                new DataPoint(6, 7),
        };
        return dp;
    }
}


Java
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
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.DataPointInterface;
import com.jjoe64.graphview.series.PointsGraphSeries;
  
public class MainActivity extends AppCompatActivity {
      
    GraphView graphView;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        graphView = findViewById(R.id.graphview);
        PointsGraphSeries series = new PointsGraphSeries<>(getDataPoint());
  
        graphView.addSeries(series);
        series.setShape(PointsGraphSeries.Shape.TRIANGLE);
        series.setSize(50);
        series.setColor(Color.RED);
          
        // setting custom shape
        series.setCustomShape(new PointsGraphSeries.CustomShape() {
            @Override
            public void draw(Canvas canvas, Paint paint, float x, float y, DataPointInterface dataPoint) {
                paint.setStrokeWidth(5);
                canvas.drawLine(x - 20, y, x, y - 20, paint);
                canvas.drawLine(x, y - 20, x + 20, y, paint);
                canvas.drawLine(x + 20, y, x, y + 20, paint);
                canvas.drawLine(x - 20, y, x, y + 20, paint);
            }
        });
          
        // adding title
        series.setTitle("Title");
  
        // setting visibility to true
        graphView.getLegendRenderer().setVisible(true);
          
        // setting fix position for the title
        graphView.getLegendRenderer().setFixedPosition(4, 5);
          
        // graphView.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);
        graphView.getLegendRenderer().setTextColor(Color.BLUE);
        graphView.getLegendRenderer().setTextSize(40);
    }
  
    private DataPoint[] getDataPoint() {
        DataPoint[] dp = new DataPoint[]{
                new DataPoint(0, 1),
                new DataPoint(2, 1),
                new DataPoint(3, 5),
                new DataPoint(6, 2),
                new DataPoint(7, 8),
        };
        return dp;
    }
}


第 4 步:使用MainActivity。 Java文件

转到主活动。 Java文件,参考如下代码。下面是MainActivity的代码。 Java文件。代码中添加了注释以更详细地理解代码。

Java

import android.graphics.Color;
import android.os.Bundle;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.jjoe64.graphview.DefaultLabelFormatter;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.PointsGraphSeries;
  
public class MainActivity extends AppCompatActivity {
  
    GraphView graphView;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        graphView = findViewById(R.id.graphview);
          
        // For creating Point Graph Series We use PointGraphSeries
        PointsGraphSeries series = new PointsGraphSeries<>(getDataPoint());
  
        graphView.addSeries(series);
          
        // we use this method to define the 
        // shape that will be used for data points
        series.setShape(PointsGraphSeries.Shape.TRIANGLE);
          
        // we use this method to 
        // define the size of the shape
        series.setSize(50);
          
        // we use this method 
        // to set the color
        series.setColor(Color.RED);
          
        // adding the prefix and suffix here using gridlabelrenderer
        graphView.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter() {
            @Override
            public String formatLabel(double value, boolean isValueX) {
                  
                // if valuex then add the prefix
  
                // return "$"+super.formatLabel(value, isValueX);
                // if valuex then add the suffix
  
                // return super.formatLabel(value, isValueX)+"$";
                if (isValueX) {
                    return "$" + super.formatLabel(value, isValueX);
                }
                return super.formatLabel(value, isValueX);
            }
        });
    }
  
    private DataPoint[] getDataPoint() {
        DataPoint[] dp = new DataPoint[]{
                new DataPoint(0, 1),
                new DataPoint(2, 7),
                new DataPoint(3, 5),
                new DataPoint(5, 2),
                new DataPoint(6, 7),
        };
        return dp;
    }
}

输出:

添加图例

要添加图例,请转到MainActivity。 Java文件,参考如下代码。下面是MainActivity的代码。 Java文件。代码中添加了注释以更详细地理解代码。

Java

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
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.DataPointInterface;
import com.jjoe64.graphview.series.PointsGraphSeries;
  
public class MainActivity extends AppCompatActivity {
      
    GraphView graphView;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        graphView = findViewById(R.id.graphview);
        PointsGraphSeries series = new PointsGraphSeries<>(getDataPoint());
  
        graphView.addSeries(series);
        series.setShape(PointsGraphSeries.Shape.TRIANGLE);
        series.setSize(50);
        series.setColor(Color.RED);
          
        // setting custom shape
        series.setCustomShape(new PointsGraphSeries.CustomShape() {
            @Override
            public void draw(Canvas canvas, Paint paint, float x, float y, DataPointInterface dataPoint) {
                paint.setStrokeWidth(5);
                canvas.drawLine(x - 20, y, x, y - 20, paint);
                canvas.drawLine(x, y - 20, x + 20, y, paint);
                canvas.drawLine(x + 20, y, x, y + 20, paint);
                canvas.drawLine(x - 20, y, x, y + 20, paint);
            }
        });
          
        // adding title
        series.setTitle("Title");
  
        // setting visibility to true
        graphView.getLegendRenderer().setVisible(true);
          
        // setting fix position for the title
        graphView.getLegendRenderer().setFixedPosition(4, 5);
          
        // graphView.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);
        graphView.getLegendRenderer().setTextColor(Color.BLUE);
        graphView.getLegendRenderer().setTextSize(40);
    }
  
    private DataPoint[] getDataPoint() {
        DataPoint[] dp = new DataPoint[]{
                new DataPoint(0, 1),
                new DataPoint(2, 1),
                new DataPoint(3, 5),
                new DataPoint(6, 2),
                new DataPoint(7, 8),
        };
        return dp;
    }
}

输出: