A.A*_*.A. 5 android mpandroidchart
我开始使用MPAndroidChart库来构建一个StackedBarChart显示三个y值.这是代码:
public class Plot
{
final Context context;
final BarData data;
private int count;
public StackedBarPlot(Context context)
{
this.context = context;
data = setData();
}
protected BarData setData()
{
final List<BarEntry> entries = new ArrayList<>();
for (DatabaseEntry entry : entryList)
{
final float total = (float) entry.getTotal();
final float[] y = {100 * entry.getN1() / total,
100 * entry.getN2() / total, 100 * entry.getN3() / total};
entries.add(new BarEntry(/*long*/entry.getDate(), y));
}
count = entries.size();
final BarDataSet dataset = new BarDataSet(entries, null);
dataset.setColors(new int[]{R.color.green, R.color.blue, R.color.red}, context);
dataset.setStackLabels(labels);
dataset.setDrawValues(true);
dataset.setVisible(true);
final BarData data = new BarData(dataset);
data.setBarWidth(0.9f);
return data;
}
public BarChart getChart(int id, View view)
{
final BarChart chart = (BarChart) view.findViewById(id);
chart.getAxisRight().setEnabled(false);
chart.getAxisLeft().setEnabled(false);
final Legend legend = chart.getLegend();
legend.setDrawInside(true);
legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
final XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(dateFormatter);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setLabelCount(count);
chart.getDescription().setEnabled(false);
chart.setData(data);
chart.setFitBars(true);
chart.invalidate();
return chart;
}
private final IAxisValueFormatter dateFormatter = new IAxisValueFormatter()
{
@Override
public String getFormattedValue(float value, AxisBase axis)
{
return new DateTime((long) value).toString(context.getString("E, MMM d"));
}
};
}
Run Code Online (Sandbox Code Playgroud)
然后在我Fragment,我打电话:
public class MyFragment extends Fragment
{
private Plot plot;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
plot = new Plot(getActivity());
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
final View view = inflater.inflate(R.layout.fragment, parent, false);
plot.getChart(R.id.chart, view);
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
并在MainActivity.java中
getFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
Run Code Online (Sandbox Code Playgroud)
main_activity.xml
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/navigation"/>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
fragment.xml之
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
问题是条形图无法正确渲染.我可以看到值,但条形图没有显示.有什么建议?
这不像一些以前的帖子所建议的那样在库中出错.可能你只是误解了每个条的宽度是如何确定的,就像我最初做的那样.
我在条形图上为我的x轴使用了一个长的毫秒时间戳.我意识到默认情况下MPAndroidChart将条形宽度设置为0.85f.想想这意味着什么.我的第一个时间戳是1473421800000f,下一个是1473508200000f:相差86400000f!现在,当每对观测值之间有86400000f时,我怎么能看到一个0.85f宽的条形?要解决此问题,您需要执行以下操作:
barData.setBarWidth(0.6f * widthBetweenObservations);
Run Code Online (Sandbox Code Playgroud)
因此,上面将条形的宽度设置为等于观察之间距离的60%.
我必须从这个StackedBarActivity例子开始,一次一点地往下看,直到找出导致问题的原因。它使用长时间戳来entry.getDate()表示 X 轴值,无论是否带有自定义IAxisValueFormatter. 这是此处报告的库中的一个错误。
这是我最终作为解决方法所做的事情。我得到了自时间戳以来的持续时间(以天为单位):
long diff = new Duration(entry.getDate(), DateTime.now().getMillis()).getStandardDays();
entries.add(new BarEntry(diff, y));
Run Code Online (Sandbox Code Playgroud)
然后按照我的习惯IAxisValueFormatter:
private final IAxisValueFormatter dateFormatter = new IAxisValueFormatter()
{
@Override
public String getFormattedValue(float value, AxisBase axis)
{
return LocalDate.now().minusDays((int) value).toString("EEE");
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1682 次 |
| 最近记录: |