MPAndroidChart:图表不显示

MPAndroidChart: Chart Not Displaying

我在我的项目中添加了一个条形图 XML(下面的布局片段):

<RelativeLayout
    android:id="@+id/relative_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

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

<TextView
    android:id="@+id/stepsTitle_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:textSize="28sp"
    android:layout_below="@+id/chart"
    android:text="STEPS TODAY"
    android:layout_marginTop="8dp" />

然后我在我的 onCreate() 方法中向图表添加了数据:

    chart = (BarChart) findViewById(R.id.chart);
    chart.setLogEnabled(true);

    chart.setDrawBarShadow(false);
    chart.setDrawValueAboveBar(true);

    chart.setDescription("");

    // if more than 60 entries are displayed in the chart, no values will be
    // drawn
     chart.setMaxVisibleValueCount(60);

    List<String> usernamesArray = new ArrayList<String>();
    //ArrayList<Long> stepsArray = new ArrayList<Long>();
    List<BarEntry> entryArray = new ArrayList<BarEntry>();

    usernamesArray.add("a");
    usernamesArray.add("b");
    usernamesArray.add("c");

    BarEntry entry1 = new BarEntry(500, 0);
    BarEntry entry2 = new BarEntry(500, 1);
    BarEntry entry3 = new BarEntry(500, 2);
    entryArray.add(entry1);
    entryArray.add(entry2);
    entryArray.add(entry3);

    BarDataSet dataSet = new BarDataSet(entryArray, "Steps");
    dataSet.setColor(Color.rgb(104, 241, 175));
    BarData data = new BarData(usernamesArray, dataSet);
    //graphRow.addView(chart);
    //leaderboard_tableLayout.addView(graphRow);
    //chart = new BarChart(this);
    chart.setData(data);
    chart.fitScreen();
    chart.setBackgroundColor(Color.WHITE);
    chart.invalidate();

但是,图表对象没有显示;事实上,我在图表应该在的地方根本看不到任何东西。

对我可能遗漏的东西有什么想法吗?如有必要,我当然可以提供更多详细信息。

谢谢!

原来宽度和高度被设置为 0,所以我将它们从 "match_parent" 更改为以 dp 为单位的显式设置(分别为 400dp 和 200dp),然后解决了问题。

您可以使用以下代码根据设备设置宽度,无论是手机还是平板电脑。我只添加了几个参数,您还可以添加宽度和高度以使您的代码更智能。

代码-

 /* Start of setMyDevice() */
    public static void setMyDevice(int screen_density, XAxis xa) {

        Log.e("Logger:Utility", "setMyDevice() Called");
        // int screen_density = (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK);

        if (screen_density == Configuration.SCREENLAYOUT_SIZE_LARGE || screen_density == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
            //The device is a tablet or at least has the screen density of a tablet
            xa.setLabelRotationAngle(-35);
            xa.setDrawLabels(true);
            xa.setTextSize(14);
            Log.e("Logger:Utility", "setMyDevice-IT IS A TABLET");
        } else {
            xa.setLabelRotationAngle(-55);
            xa.setTextSize(10);
            Log.e("Logger:Utility", "setMyDevice-IT IS A MOBILE");
        }

    }
    /* End  of setMyDevice() */