MPAndroidChart BarChart xValues Issue

时间:2015-02-21 12:38:00

标签: android mpandroidchart

我发现BarChart的{​​{1}}存在问题,需要修复。首先是我的代码:

MPAndroidChart

我的this.barChart = (BarChart) view.findViewById(R.id.bar_fragment_bar_chart); this.barChart.setDrawYValues(true); this.barChart.setDrawXLabels(false); this.barChart.setDrawValueAboveBar(true); this.barChart.setDrawBorder(false); this.barChart.setDrawGridBackground(false); this.barChart.setDrawHorizontalGrid(true); this.barChart.setDrawVerticalGrid(false); this.barChart.setDrawBarShadow(false); this.barChart.setDescription(""); this.barChart.setNoDataTextDescription(""); this.barChart.set3DEnabled(false); 是简单日期:

xValues

现在我有多个数据集:

ArrayList<String> xValues = new ArrayList<String>();

for(int i = 0; i < measureDataListEntry.size(); i++) {
    String StringValue = measureDataListEntry.get(i).getTime();
    Long value = Long.parseLong(StringValue) * 1000;
    DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN);
    String date = dateFormat.format(new Date(value));

    xValues.add(date);
}

我现在的问题是ArrayList<BarEntry> yValuesMonday = new ArrayList<BarEntry>(); ArrayList<BarEntry> yValuesTuesday = new ArrayList<BarEntry>(); ArrayList<BarEntry> yValuesWednesday = new ArrayList<BarEntry>(); ArrayList<BarEntry> yValuesThursday = new ArrayList<BarEntry>(); ArrayList<BarEntry> yValuesFriday = new ArrayList<BarEntry>(); ArrayList<BarEntry> yValuesSaturday = new ArrayList<BarEntry>(); ArrayList<BarEntry> yValuesSunday = new ArrayList<BarEntry>(); for(int i = 0; i < measureDataListEntry.size(); i++) { String stringValue = measureDataListEntry.get(i).getValue(); int dayOfWeek = Helper.getDayOfWeek(measureDataListEntry.get(i).getTime()); float value = Float.parseFloat(stringValue); switch(dayOfWeek) { case Calendar.MONDAY: yValuesMonday.add(new BarEntry(value, i)); break; case Calendar.TUESDAY: yValuesTuesday.add(new BarEntry(value, i)); break; case Calendar.WEDNESDAY: yValuesWednesday.add(new BarEntry(value, i)); break; case Calendar.THURSDAY: yValuesThursday.add(new BarEntry(value, i)); break; case Calendar.FRIDAY: yValuesFriday.add(new BarEntry(value, i)); break; case Calendar.SATURDAY: yValuesSaturday.add(new BarEntry(value, i)); break; case Calendar.SUNDAY: yValuesSunday.add(new BarEntry(value, i)); break; } } BarDataSet barDataSetMonday = new BarDataSet(yValuesMonday, this.getResources().getString(R.string.text_monday)); barDataSetMonday.setColor(this.getResources().getColor(R.color.diagramGreenColor)); BarDataSet barDataSetTuesday = new BarDataSet(yValuesTuesday, this.getResources().getString(R.string.text_tuesday)); barDataSetTuesday.setColor(this.getResources().getColor(R.color.diagramOrangeColor)); BarDataSet barDataSetWednesday = new BarDataSet(yValuesWednesday, this.getResources().getString(R.string.text_wednesday)); barDataSetWednesday.setColor(this.getResources().getColor(R.color.diagramPinkColor)); BarDataSet barDataSetThursday = new BarDataSet(yValuesThursday, this.getResources().getString(R.string.text_thursday)); barDataSetThursday.setColor(this.getResources().getColor(R.color.diagramBlackColor)); BarDataSet barDataSetFriday = new BarDataSet(yValuesFriday, this.getResources().getString(R.string.text_friday)); barDataSetFriday.setColor(this.getResources().getColor(R.color.diagramBlueColor)); BarDataSet barDataSetSaturday = new BarDataSet(yValuesSaturday, this.getResources().getString(R.string.text_saturday)); barDataSetSaturday.setColor(this.getResources().getColor(R.color.diagramRedColor)); BarDataSet barDataSetSunday = new BarDataSet(yValuesSunday, this.getResources().getString(R.string.text_sunday)); barDataSetSunday.setColor(this.getResources().getColor(R.color.diagramYellowColor)); ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>(); dataSets.add(barDataSetMonday); dataSets.add(barDataSetTuesday); dataSets.add(barDataSetWednesday); dataSets.add(barDataSetThursday); dataSets.add(barDataSetFriday); dataSets.add(barDataSetSaturday); dataSets.add(barDataSetSunday); BarData data = new BarData(xValues, dataSets); this.barChart.setData(data); this.barChart.animateY(1000); xLabels根本不匹配:

enter image description here

这些小条位于xValues的后面,最后一个条目与最后一个条目之前的条目之间有很大的空间。

我还测试了将所有数据放入一个数据集时的外观。 xLabels也与xLabels

不匹配

enter image description here

我该如何解决这个问题?

修改

enter image description here

您在xValues中看到的日期属于该栏。那么为什么xLabels的位置离条不远呢?

编辑2:

使用xLabel

LineChart

一切正常:

enter image description here

1 个答案:

答案 0 :(得分:1)

我不确定你想要什么。 但将x标签置于栏杆上方,请致电:

chart.getXLabels().setCenterXLabelText(true);

对于条形图内的组,也许您应该查看示例项目,看看这里做了什么:

https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java

<强>更新

如果您只是希望每天都有单独的颜色,请将所有条目放在一个DataSet中,并将7种颜色(每天一个)添加到数据集中。 在这种情况下,x值数组需要与您提供的条目数组一样长,包含不同的日期。