Reputation: 87
I am trying to use MPAndroid chart to display 2 sets of data. There is no syntax error or crash but no data is being displayed. Here's my code.
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;
}
The page crashes every time I run. The log says, "One or more of the DataSet Entry arrays are longer than the x-values array of this ChartData object." I logged my x-axis values and checked, the size of both the arrays are same!
Upvotes: 0
Views: 728
Reputation: 257
You forgot to initialize the constructor properly. You have to pass X-axis string array or arraylist in the constructor. In your case it would be time_check whatever, either hr or steps.
Upvotes: 1