我想使用MP Chart在Graph中显示每周数据。 我有四种类型的练习,并希望以图表形式显示
XML
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/chartWeeklyDistance"
android:layout_width="match_parent"
android:layout_height="@dimen/_170sdp"
android:layout_margin="@dimen/_5sdp" />
Java代码
private void updateWeeklyDistanceChart(){
chartWeeklyDistance.setPinchZoom(false);
chartWeeklyDistance.setDragEnabled(false);
chartWeeklyDistance.setDrawBarShadow(false);
chartWeeklyDistance.setDrawGridBackground(false);
chartWeeklyDistance.getDescription().setEnabled(false);
chartWeeklyDistance.setDrawValueAboveBar(false);
chartWeeklyDistance.setTouchEnabled(false);
chartWeeklyDistance.setHighlightFullBarEnabled(false);
XAxis xAxis = chartWeeklyDistance.getXAxis();
// xAxis.setTypeface(tfLight);
xAxis.setGranularity(1f);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setLabelCount(7);
xAxis.setCenterAxisLabels(true);
xAxis.setValueFormatter(new WeekDaysFormatter(chartWeeklyDistance));
chartWeeklyDistance.getAxisRight().setEnabled(false);
float groupSpace = 0.08f;
float barSpace = 0.03f; // x4 DataSet
float barWidth = 0.2f; // x4 DataSet
// (0.2 + 0.03) * 4 + 0.08 = 1.00 -> interval per "group"
int progress = 7;
int groupCount = progress;
int startYear = 1980;
int endYear = startYear + groupCount;
// tvX.setText(String.format(Locale.ENGLISH, "%d-%d", startYear, endYear));
// tvY.setText(String.valueOf(seekBarY.getProgress()));
ArrayList<BarEntry> values1 = new ArrayList<>();
ArrayList<BarEntry> values2 = new ArrayList<>();
ArrayList<BarEntry> values3 = new ArrayList<>();
ArrayList<BarEntry> values4 = new ArrayList<>();
float randomMultiplier = progress * 1000f;
for (int i = 0; i < 7; i++) {
values1.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
values2.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
values3.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
values4.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
}
BarDataSet set1, set2, set3, set4;
if (chartWeeklyDistance.getData() != null && chartWeeklyDistance.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) chartWeeklyDistance.getData().getDataSetByIndex(0);
set2 = (BarDataSet) chartWeeklyDistance.getData().getDataSetByIndex(1);
set3 = (BarDataSet) chartWeeklyDistance.getData().getDataSetByIndex(2);
set4 = (BarDataSet) chartWeeklyDistance.getData().getDataSetByIndex(3);
set1.setValues(values1);
set2.setValues(values2);
set3.setValues(values3);
set4.setValues(values4);
chartWeeklyDistance.getData().notifyDataChanged();
chartWeeklyDistance.notifyDataSetChanged();
} else {
// create 4 DataSets
set1 = new BarDataSet(values1, "Company A");
set1.setDrawValues(false);
set1.setColor(Color.rgb(104, 241, 175));
set2 = new BarDataSet(values2, "Company B");
set2.setColor(Color.rgb(164, 228, 251));
set2.setDrawValues(false);
set3 = new BarDataSet(values3, "Company C");
set3.setColor(Color.rgb(242, 247, 158));
set3.setDrawValues(false);
set4 = new BarDataSet(values4, "Company D");
set4.setColor(Color.rgb(255, 102, 0));
set4.setDrawValues(false);
BarData data = new BarData(set1, set2, set3, set4);
// data.setValueFormatter(new LargeValueFormatter());
// data.setValueTypeface(tfLight);
chartWeeklyDistance.setData(data);
}
// specify the width each bar should have
chartWeeklyDistance.getBarData().setBarWidth(barWidth);
// restrict the x-axis range
chartWeeklyDistance.getXAxis().setAxisMinimum(startYear);
// barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters
chartWeeklyDistance.getXAxis().setAxisMaximum(startYear + chartWeeklyDistance.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
chartWeeklyDistance.groupBars(startYear, groupSpace, barSpace);
chartWeeklyDistance.animateY(1000);
chartWeeklyDistance.invalidate();
}
WeekDaysFormatter ::
public class WeekDaysFormatter extends ValueFormatter {
private final String[] mDays = {"MO", "TU", "WE", "TH", "FR", "SA", "SU",};
private final BarLineChartBase<?> chart;
private int count;
public WeekDaysFormatter(BarLineChartBase<?> chart) {
this.chart = chart;
}
@Override
public String getFormattedValue(float value) {
Logger.log("WeekDaysFormatter", "value = "+value);
String day = "";
try {
day = mDays[count];
} catch (IndexOutOfBoundsException ex) {
count = 0;
day = mDays[count];
}
count++;
return day;
}
}
答案 0 :(得分:0)
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setLabelCount(6);
xAxis.setCenterAxisLabels(true);
String[] daysOfTheWeek = {"MO", "TU", "WE", "TH", "FR", "SA", "SU"};
xAxis.setValueFormatter(new ValueFormatter() {
@Override
public String getFormattedValue(float value) {
Logger.log("WeeklyDistance", "value = " + ((int) value));
if (((int) value) > -1 && ((int) value) < 7) {
return daysOfTheWeek[((int) value)];
}
return "";
}
});
chartWeeklyDistance.getXAxis().setAxisMinimum(0);