Android折线图hellocharts使用以及固定Y轴坐标

Android折线图hellocharts使用以及固定Y轴坐标,第1张

hellochart,非常适合初入门的开发者使用,简单快捷,引入也很方便,而且界面美观。

我的项目需要固定Y轴的游标显示,hellochart中Y轴默认的范围是根据数据的最大值和最小值决定的,不一定从原点开始,为解决这个问题我在网上一顿巴拉,最终得以解决,现在分享给正在巴拉的你。

 Viewport v = new Viewport(chart.getMaximumViewport());
        //折线图的纵轴(从上到下这么看)的最大值和最小值
        v.bottom = 0f;
        v.top = 200f;
        chart.setMaximumViewport(v);
        //这2个属性的设置一定要在lineChart.setMaximumViewport(v)这个方法之后,
          不然显示的坐标数据不能左右滑动查看更多数据的
        //折线图的横轴(从左到右这么看)的最大值和最小值
        v.left =0;
        v.right = 10;
        chart.setCurrentViewport(v);

话不多少先上图,再上代码。

 

 

首先添加依赖

dependencies {
    //添加
    implementation 'com.github.lecho:hellocharts-library:1.5.8@aar'
}

我的布局layout.xml



    

    

java代码


import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.os.Bundle;
import android.util.EventLogTags;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Entity;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

public class LineChartActivity extends AppCompatActivity {
private Button button;
private TextView textViewLine;
private LineChartView chart;
private String[] date = {"0","1","2","3","4","5","6","7","8"};//X轴的数据
private double[] doubles;//x轴对应的y值,即 图表的每个数据点
//声明集合泛型为PointValue(x轴对应的y值)与AxisValue(x轴的值)
private float maxY;//Y轴最大值
public  List mPointValues = new ArrayList();
public  List mAxisXValues = new ArrayList();
private List mAxisYValues = new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_line_chart);
        chart=findViewById(R.id.lineChartView);
        button=findViewById(R.id.buttonLine);
        //这是我接收数据的数组,项目需要,你可自定义一个装载数据的数组
        Intent intent1 = getIntent();
        doubles = new double[8];
        doubles[0]=intent1.getDoubleExtra("doubleVarArray0",666);
        doubles[1]=intent1.getDoubleExtra("doubleVarArray1",666);
        doubles[2]=intent1.getDoubleExtra("doubleVarArray2",666);
        doubles[3]=intent1.getDoubleExtra("doubleVarArray3",666);
        doubles[4]=intent1.getDoubleExtra("doubleVarArray4",666);
        doubles[5]=intent1.getDoubleExtra("doubleVarArray5",666);
        doubles[6]=intent1.getDoubleExtra("doubleVarArray6",666);
        doubles[7]=intent1.getDoubleExtra("doubleVarArray7",666);
        //返回键
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent toSpectrumReproduction = new Intent(LineChartActivity.this,SpectrumReproductionActivity.class);
                startActivity(toSpectrumReproduction);
                LineChartActivity.this.finish();
            }
        });

        textViewLine=findViewById(R.id.textViewLine);

        //找出doubles中最大值max,这个max值就是我Y轴的最大坐标
        double max = doubles[0];
        for(int i = 1; i< doubles.length;i++) {
            if (doubles[i]>max){
                max= doubles[i];
            }
        }
        maxY = (float) max;//设置Y轴最大值
        getAxisPoints();//获取坐标点
        getAxisXLables();//获取x轴的标注
        initLineChart();//初始化
        //固定Y轴的范围,如果没有这个,Y轴的范围会根据数据的最大值和最小值决定
        Viewport v = new Viewport(chart.getMaximumViewport());
        //折线图的纵轴(从上到下这么看)的最大值和最小值
        v.bottom = 0f;
        v.top = (float) max;
        chart.setMaximumViewport(v);
        //这2个属性的设置一定要在lineChart.setMaximumViewport(v)这个方法之后,不然显示的坐标数 
          据是不能左右滑动查看更多数据的
        //折线图的横轴(从左到右这么看)的最大值和最小值
        v.left =0;
        v.right = 10;
        chart.setCurrentViewport(v);
    }

    private void initLineChart() {
        Line line=new Line(mPointValues).setColor(Color.parseColor("#FFCD41"));
        List lines = new ArrayList();
        line.setShape(ValueShape.CIRCLE);//折线图上每个数据点的形状  这里是圆形 
        line.setCubic(false);//曲线是否平滑,即是曲线还是折线
        line.setFilled(false);//是否填充曲线的面积
        line.setHasLabels(true);//曲线的数据坐标是否加上备注
        //line.setHasLabelsOnlyForSelected(true);//点击数据坐标提示数据(设置了这个 
                                                   line.setHasLabels(true);就无效)
        line.setHasLines(true); //是否用线显示。如果为false 则没有曲线只有点显示
        line.setHasPoints(true);//是否显示圆点 如果为false 则没有原点只有点显示(每个数据点都 
                                  是个大的圆点)
        lines.add(line);
        LineChartData data = new LineChartData();
        data.setLines(lines);
        //坐标轴
        Axis axisX = new Axis(); //X轴
        axisX.setHasTiltedLabels(false);  //X坐标轴字体是斜的显示还是直的,true是斜的显示
        axisX.setTextColor(Color.GRAY);  //设置字体颜色
        //axisX.setName("date");  //表格名称
        axisX.setTextSize(10);//设置字体大小
        //axisX.setMaxLabelChars(4); //x轴上最多显示几个X轴坐标
        axisX.setValues(mAxisXValues);  //填充X轴的坐标名称
        data.setAxisXBottom(axisX); //x 轴在底部
        axisX.setHasLines(true); //x 轴分割线

        /**Y轴是根据数据的大小自动设置Y轴上限*/
        Axis axisY = new Axis();//Y轴
        axisY.setName(" ");//y轴标注
        axisY.setTextSize(10);//设置字体大小
        axisY.setHasLines(false);
        axisY.setValues(mAxisYValues);

        data.setAxisYLeft(axisY);
        axisY.setTextColor(Color.GRAY);
        data.setAxisYLeft(axisY);  //Y轴设置在左边
        chart.setInteractive(false);
        chart.setZoomEnabled(false);
        chart.setLineChartData(data);
        chart.setVisibility(View.VISIBLE);
    }
    /**
     * 图表的每个点的显示
     */
    private void getAxisPoints() {
        for(int i = 0; i

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/992556.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)