bar-chart - 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
}
谢谢阅读!
解决方案
推荐阅读
- performance - Chrome:加载时在网站上冻结 2 秒
- bash - 如何使用 bash/sed 循环数字并延迟启动多个程序
- node.js - 在 AWS Lambda 上执行 nodejs-traceroute 模块时出现“spawn traceroute ENOENT”错误
- javascript - 如何通过 ethplorer 获取 USDT 余额
- elasticsearch - Elasticsearch - 如何以高亮返回所有片段
- google-sheets - 所有基于时间的触发器似乎同时运行
- node.js - 如何在 nodejs 上多次托管网站?
- html - 如何让 3 个盒子响应移动设备?
- java - java - 如何处理和记录Optional或者在java中抛出自定义异常?
- installation - 在 Windows 上的 R 4.0.5 上安装 dmetar