首页 > 解决方案 > mpAndroidChart:条形图只接受固定高度

问题描述

我和这个人有同样的问题——我的条形图只接受 dp 中的固定高度。如果我使用 Fill-Parent、match_parent 或 wrap_content,图表将是平坦的,高度为零。并且使用固定尺寸不适用于不同的屏幕尺寸。

虽然这个问题已经在互联网上流传(fi 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
    }

谢谢阅读!

标签: bar-chartmpandroidchartandroid-databinding

解决方案


推荐阅读