当前位置: 首页 > 知识库问答 >
问题:

MPAndroid 图表组合图表不显示数据

濮俭
2023-03-14

我正在尝试使用MPAndroid图表来显示2组数据。没有语法错误或崩溃,但没有显示任何数据。这是我的代码。

ublic class WorkoutSummary extends Activity {
private CombinedChart mChart;
List<Float> hr_list = new ArrayList<Float>();
List<Float> calorie_list = new ArrayList<>();
List<Float> steps = new ArrayList<Float>();
List<String> time_check_hr = new ArrayList<>();
List<String> time_check_steps = new ArrayList<>();

TextView calorie;

float total_calories = 0;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workoutsummary);


    mChart = (CombinedChart) findViewById(R.id.chart_workout);

    CombinedData data = new CombinedData();
    data.setData(lineData());
    data.setData(barData());
    mChart.setData(data);
    mChart.setDrawGridBackground(false);
    mChart.setBackgroundColor(Color.TRANSPARENT);
    mChart.setDrawBorders(false);


    mChart.getXAxis().setEnabled(false);

}

// this method is used to create data for line graph
public LineData lineData(){
    ArrayList<Entry> line = new ArrayList<>();
    for(Float x: hr_list){
     line.add(new Entry(x,hr_list.indexOf(x)));
        Log.d("Line is", String.valueOf(line));
        Log.d("time hr", String.valueOf(time_check_hr));

    }

    LineDataSet lineDataSet = new LineDataSet(line, "Heart Rate");
    lineDataSet.setColors(ColorTemplate.COLORFUL_COLORS);

    mChart.invalidate();
    LineData lineData = new LineData(time_check_hr,lineDataSet);

    return lineData;

}

// this method is used to create data for Bar graph
public BarData barData(){

    ArrayList<BarEntry> group1 = new ArrayList<>();
    for(Float x: steps){
        group1.add(new BarEntry(x,steps.indexOf(x)));

}
    BarDataSet barDataSet = new BarDataSet(group1, "Steps");
    barDataSet.setColor(Color.rgb(0, 155, 0));
    barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
    Log.d("group1 is", String.valueOf(group1.size()));
    Log.d("time steps", String.valueOf(time_check_steps.size()));

    BarData barData = new BarData(time_check_steps,barDataSet);

    return barData;

}

每次我运行时页面都会崩溃。日志显示,“一个或多个DataSet Entry数组比此ChartData对象的x值数组长。”我记录了我的x轴值并检查了,两个数组的大小都是一样的!

共有1个答案

文增
2023-03-14

您忘记正确初始化构造函数。您必须在构造函数中传递X轴字符串数组或数组列表。在您的情况下,它将time_check任何东西,无论是小时还是步骤。

 类似资料: