mpAndroidChart:条形图仅接受固定高度

时间:2018-06-08 23:18:09

标签: bar-chart mpandroidchart android-databinding

我遇到与this guy相同的问题 - 我的条形图只接受dp中的固定高度。如果我使用Fill-Parent,match_parent或wrap_content,图表将是平坦的,高度为零。使用固定尺寸不适用于不同的屏幕尺寸。

虽然这个问题已经浮现在互联网上(f.i。here),但似乎没有人有一个像样的解决方案。有任何想法吗? android:fillViewport =“true”也没有帮助。

我的XML:

    <?xml version="1.0" encoding="utf-8"?>

<layout>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        tools:context="com.example.myself.myroom.Frontend.StatisticFragment">

        <com.github.mikephil.charting.charts.BarChart
            android:id="@+id/testchart"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"(or fixed number of dp) />

    </FrameLayout> </layout>

相应的片段(仅图表部分):

  protected void onPostExecute(List<ItemS> result) {
// update the UI after background processes completes
            List<ItemS> dataObjects = result;

        List<BarEntry> entries = new ArrayList<BarEntry>();
        final List<String> labels = new ArrayList<String>();

        Integer testdummy = 0;
        for (ItemS data : dataObjects) {

            // turn your data into Entry objects
            entries.add(new BarEntry( testdummy, data.get_dAmount().floatValue() ));
            labels.add(data.getiCatID().toString());
            testdummy++;
        }

        BarData data = new BarData(new BarDataSet(entries, "Labelerik"));

        IAxisValueFormatter formatter = new IAxisValueFormatter() {

            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return labels.get((int) value);
            }

            // we don't draw numbers, so no decimal digits needed
            //@Override
            //public int getDecimalDigits() {  return 0; }
        };

        data.setBarWidth(0.9f); // set custom bar width
        oBinding.testchart.setData(data);
        oBinding.testchart.setFitBars(true); // make the x-axis fit exactly all bars
        oBinding.testchart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
        oBinding.testchart.getXAxis().setGranularity(1f); // minimum axis-step (interval) is 1
        oBinding.testchart.getXAxis().setValueFormatter(formatter);
        oBinding.testchart.invalidate(); // refresh
    }

感谢阅读!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我在XML-File中使用了错误的容器。它必须是RelativeLayout,而不是FrameLayout:

<layout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context="com.example.erik.myroom.Frontend.StatisticFragment">

    <com.github.mikephil.charting.charts.BarChart
        android:id="@+id/testchart"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>