2021/3/22

1.今日收获内容
开始对表进行设计后期进行传值

package com.example.myapplication2;

import android.graphics.Color;
import android.os.Bundle;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatActivity;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;
import java.util.List;

public class picture extends AppCompatActivity {

    private BarChart barChart;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 去除状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_bar_chart1);
        initBarChart();
    }
    private void initBarChart() {
        barChart = findViewById(R.id.bar_chart1);
        barChart.getDescription().setEnabled(false); // 不显示描述
        barChart.setExtraOffsets(0, 0, 0, 20); // 设置饼图的偏移量,类似于内边距 ,设置视图窗口大小
        setAxis(); // 设置坐标轴
        setLegend(); // 设置图例
        setData();  // 设置数据
    }

    private void setLegend() {
        Legend legend = barChart.getLegend();
        legend.setFormSize(12f); // 图例的图形大小
        legend.setTextSize(15f); // 图例的文字大小
        legend.setDrawInside(true); // 设置图例在图中
        legend.setOrientation(Legend.LegendOrientation.VERTICAL); // 图例的方向为垂直
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); //显示位置,水平右对齐
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); // 显示位置,垂直上对齐
        // 设置水平与垂直方向的偏移量
        legend.setYOffset(55f);
        legend.setXOffset(30f);
    }


    private void setAxis() {
        // 设置x轴
        XAxis xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);  // 设置x轴显示在下方,默认在上方
        xAxis.setDrawGridLines(false); // 将此设置为true,绘制该轴的网格线。
        xAxis.setLabelCount(15);  // 设置x轴上的标签个数
        xAxis.setTextSize(5f); // x轴上标签的大小
        final String labelName[] = {"第1天", "第2天", "第3天", "第4天", "第5天","第6天", "第7天", "第8天", "第9天", "第10天","第11天","第12天", "第13天", "第14天", "第15天"};
        // 设置x轴显示的值的格式
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                if ((int) value < labelName.length) {
                    return labelName[(int) value];
                } else {
                    return "";
                }
            }
        });
        xAxis.setYOffset(15); // 设置标签对x轴的偏移量,垂直方向

        // 设置y轴,y轴有两条,分别为左和右
        YAxis yAxis_right = barChart.getAxisRight();
        yAxis_right.setAxisMaximum(37f);  // 设置y轴的最大值
        yAxis_right.setAxisMinimum(35f);  // 设置y轴的最小值
        yAxis_right.setEnabled(false);  // 不显示右边的y轴

        YAxis yAxis_left = barChart.getAxisLeft();
        yAxis_left.setAxisMaximum(37f);
        yAxis_left.setAxisMinimum(35f);
        yAxis_left.setTextSize(15f); // 设置y轴的标签大小
    }
    private void setData() {
        List<IBarDataSet> sets = new ArrayList<>();
        // 此处有两个DataSet,所以有两条柱子,BarEntry()中的x和y分别表示显示的位置和高度
        // x是横坐标,表示位置,y是纵坐标,表示高度
        List<BarEntry> barEntries1 = new ArrayList<>();
        barEntries1.add(new BarEntry(0, 36.2f));
        barEntries1.add(new BarEntry(1, 36.3f));
        barEntries1.add(new BarEntry(2, 36.3f));
        barEntries1.add(new BarEntry(3, 36.3f));
        barEntries1.add(new BarEntry(4, 36.5f));
        barEntries1.add(new BarEntry(5, 36.7f));
        barEntries1.add(new BarEntry(6, 36.9f));
        barEntries1.add(new BarEntry(7, 36.5f));
        barEntries1.add(new BarEntry(8, 36.8f));
        barEntries1.add(new BarEntry(9, 36.2f));
        barEntries1.add(new BarEntry(10, 36.4f));
        barEntries1.add(new BarEntry(11, 36.0f));
        barEntries1.add(new BarEntry(12, 36.7f));
        barEntries1.add(new BarEntry(13, 36.4f));
        barEntries1.add(new BarEntry(14, 36.5f));
        BarDataSet barDataSet1 = new BarDataSet(barEntries1, "");
        barDataSet1.setValueTextColor(Color.RED); // 值的颜色
        barDataSet1.setValueTextSize(15f); // 值的大小
        barDataSet1.setColor(Color.parseColor("#1AE61A")); // 柱子的颜色
        barDataSet1.setLabel("温度"); // 设置标签之后,图例的内容默认会以设置的标签显示
        // 设置柱子上数据显示的格式
        barDataSet1.setValueFormatter(new IValueFormatter() {
            @Override
            public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                // 此处的value默认保存一位小数
                return value + "°";
            }
        });

        sets.add(barDataSet1);

        BarData barData = new BarData(sets);
        barData.setBarWidth(0.2f); // 设置柱子的宽度
        barChart.setData(barData);
    }





}



2.遇到的问题
传值有点问题

3.明天目标

地图标记

原文地址:https://www.cnblogs.com/qiangini/p/14905825.html