4

の問題に気づき、修正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、最後のエントリと最後のエントリの前のエントリの間に非常に多くのスペースがあります。

また、すべてのデータを 1 つのデータセットに入れるとどのように見えるかをテストしました。も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

更新

単純に日ごとに色を分けたい場合は、すべてのエントリを 1 つの DataSet に入れ、データセットに 7 色 (日ごとに 1 つ) を追加します。その場合の x-values 配列は、さまざまな日付を含む、提供したエントリの配列と同じ長さである必要があります。

于 2015-02-21T17:23:59.720 に答える