MPAndroidChart:图表不显示 [英] MPAndroidChart: Chart Not Displaying
问题描述
我在我的项目XML中添加了一个BarChart(下面的布局片段):
I added a BarChart into my project XML (snippet of layout below):
<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()方法中向图表添加了数据:
And I added data to chart in my onCreate() method:
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();
但是,图表对象未显示;实际上,我在图表应有的位置根本看不到任何东西.
However, the chart object is not displaying; in fact, I do not see anything at all in the place where the chart should be.
对我可能会缺少的东西有什么想法吗?如有必要,我当然可以提供更多详细信息.
Any thoughts on what I may be missing? I can certainly provide more details if necessary.
谢谢!
推荐答案
因此,结果证明宽度和高度都设置为0,因此我将它们都从"match_parent"更改为以dp为单位的显式设置( 400dp和200dp)分别解决了该问题.
So it turns out that the width and height were being set to 0, so I changed them both from "match_parent" to an explicit setting in units of dp's (400dp and 200dp, respecitively), and that resolved the problem.
这篇关于MPAndroidChart:图表不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!