4

我注意到 of 存在问题BarChartMPAndroidChart需要修复。首先我的代码:

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和根本不xValues匹配:

在此处输入图像描述

条形放置在远离的后面,xLabels并且在最后一个条目和最后一个条目之前的条目之间有非常大的空间。

我还测试了将所有数据放入一个数据集时的外观。xLabels也不xValues匹配:

在此处输入图像描述

我该如何解决这个问题?

编辑

在此处输入图像描述

您在此处看到的日期xLabels属于该栏。那么为什么这个位置xLabel离吧台那么远呢?

编辑2:

LineChart

protected void initData() {
    List<MeasureDataListEntry> measureDataListEntry = null;

    if(this.favourite) {
        measureDataListEntry = this.sessionMeasureDataListFavourite.getMeasureDataList().getMeasureDataListEntries();
    } else {
        measureDataListEntry = this.sessionMeasureDataList.getMeasureDataList().getMeasureDataListEntries();
    }

    ArrayList<String> xValues = new ArrayList<String>();
    ArrayList<Entry> yValuesMonday = new ArrayList<Entry>();
    ArrayList<Entry> yValuesTuesday = new ArrayList<Entry>();
    ArrayList<Entry> yValuesWednesday = new ArrayList<Entry>();
    ArrayList<Entry> yValuesThursday = new ArrayList<Entry>();
    ArrayList<Entry> yValuesFriday = new ArrayList<Entry>();
    ArrayList<Entry> yValuesSaturday = new ArrayList<Entry>();
    ArrayList<Entry> yValuesSunday = new ArrayList<Entry>();

    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);
    }

    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 Entry(value, i));
            break;
        case Calendar.TUESDAY:
            yValuesTuesday.add(new Entry(value, i));
            break;
        case Calendar.WEDNESDAY:
            yValuesWednesday.add(new Entry(value, i));
            break;
        case Calendar.THURSDAY:
            yValuesThursday.add(new Entry(value, i));
            break;
        case Calendar.FRIDAY:
            yValuesFriday.add(new Entry(value, i));
            break;
        case Calendar.SATURDAY:
            yValuesSaturday.add(new Entry(value, i));
            break;
        case Calendar.SUNDAY:
            yValuesSunday.add(new Entry(value, i));
            break;
        }
    }

    LineDataSet lineDataSetMonday = new LineDataSet(yValuesMonday, this.getResources().getString(R.string.text_monday));

    lineDataSetMonday.setColor(this.getResources().getColor(R.color.diagramGreenColor));
    lineDataSetMonday.setCircleColor(this.getResources().getColor(R.color.diagramGreenColor));
    lineDataSetMonday.setLineWidth(1f);
    lineDataSetMonday.setCircleSize(4f);
    lineDataSetMonday.setFillAlpha(65);
    lineDataSetMonday.setFillColor(this.getResources().getColor(R.color.diagramGreenColor));

    LineDataSet lineDataSetTuesday = new LineDataSet(yValuesTuesday, this.getResources().getString(R.string.text_tuesday));

    lineDataSetTuesday.setColor(this.getResources().getColor(R.color.diagramOrangeColor));
    lineDataSetTuesday.setCircleColor(this.getResources().getColor(R.color.diagramOrangeColor));
    lineDataSetTuesday.setLineWidth(1f);
    lineDataSetTuesday.setCircleSize(4f);
    lineDataSetTuesday.setFillAlpha(65);
    lineDataSetTuesday.setFillColor(this.getResources().getColor(R.color.diagramOrangeColor));

    LineDataSet lineDataSetWednesday = new LineDataSet(yValuesWednesday, this.getResources().getString(R.string.text_wednesday));

    lineDataSetWednesday.setColor(this.getResources().getColor(R.color.diagramPinkColor));
    lineDataSetWednesday.setCircleColor(this.getResources().getColor(R.color.diagramPinkColor));
    lineDataSetWednesday.setLineWidth(1f);
    lineDataSetWednesday.setCircleSize(4f);
    lineDataSetWednesday.setFillAlpha(65);
    lineDataSetWednesday.setFillColor(this.getResources().getColor(R.color.diagramPinkColor));

    LineDataSet lineDataSetThursday = new LineDataSet(yValuesThursday, this.getResources().getString(R.string.text_thursday));

    lineDataSetThursday.setColor(this.getResources().getColor(R.color.diagramBlackColor));
    lineDataSetThursday.setCircleColor(this.getResources().getColor(R.color.diagramBlackColor));
    lineDataSetThursday.setLineWidth(1f);
    lineDataSetThursday.setCircleSize(4f);
    lineDataSetThursday.setFillAlpha(65);
    lineDataSetThursday.setFillColor(this.getResources().getColor(R.color.diagramBlackColor));

    LineDataSet lineDataSetFriday = new LineDataSet(yValuesFriday, this.getResources().getString(R.string.text_friday));

    lineDataSetFriday.setColor(this.getResources().getColor(R.color.diagramBlueColor));
    lineDataSetFriday.setCircleColor(this.getResources().getColor(R.color.diagramBlueColor));
    lineDataSetFriday.setLineWidth(1f);
    lineDataSetFriday.setCircleSize(4f);
    lineDataSetFriday.setFillAlpha(65);
    lineDataSetFriday.setFillColor(this.getResources().getColor(R.color.diagramBlueColor));

    LineDataSet lineDataSetSaturday = new LineDataSet(yValuesSaturday, this.getResources().getString(R.string.text_saturday));

    lineDataSetSaturday.setColor(this.getResources().getColor(R.color.diagramRedColor));
    lineDataSetSaturday.setCircleColor(this.getResources().getColor(R.color.diagramRedColor));
    lineDataSetSaturday.setLineWidth(1f);
    lineDataSetSaturday.setCircleSize(4f);
    lineDataSetSaturday.setFillAlpha(65);
    lineDataSetSaturday.setFillColor(this.getResources().getColor(R.color.diagramRedColor));

    LineDataSet lineDataSetSunday = new LineDataSet(yValuesSunday, this.getResources().getString(R.string.text_sunday));

    lineDataSetSunday.setColor(this.getResources().getColor(R.color.diagramYellowColor));
    lineDataSetSunday.setCircleColor(this.getResources().getColor(R.color.diagramYellowColor));
    lineDataSetSunday.setLineWidth(1f);
    lineDataSetSunday.setCircleSize(4f);
    lineDataSetSunday.setFillAlpha(65);
    lineDataSetSunday.setFillColor(this.getResources().getColor(R.color.diagramYellowColor));

    ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();

    dataSets.add(lineDataSetMonday);
    dataSets.add(lineDataSetTuesday);
    dataSets.add(lineDataSetWednesday);
    dataSets.add(lineDataSetThursday);
    dataSets.add(lineDataSetFriday);
    dataSets.add(lineDataSetSaturday);
    dataSets.add(lineDataSetSunday);

    LineData data = new LineData(xValues, dataSets);

    this.lineChart.setData(data);
    this.lineChart.animateX(1000);
}

一切正常:

在此处输入图像描述

4

1 回答 1

1

我不确定你想要什么。但是要将 x 标签居中在条形上方,请调用:

chart.getXLabels().setCenterXLabelText(true);

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

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

更新

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

于 2015-02-21T17:23:59.720 回答