我在android studio中使用MPAndroidChart条形图挣扎。它似乎在android 5或6下无法正常工作,但它在第4版工作正常。事实上它并不适合所有的布局!
Screen shot - Huawei honor 4x - android 4.4.2
Screen shot - Samsung S7 - android 6.0.1
我的配置:
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "android.parsic.parstel"
minSdkVersion 19
targetSdkVersion 19
versionCode 1
versionName '1.1'
}
我的绘画功能:
public void DrawChart(List<Cls_TestResult_HistoryGraph> MyTestHistory, String ChartName) {
mChart.setDrawGridBackground(false);
// no description text
mChart.getDescription().setEnabled(false);
// enable touch gestures
mChart.setTouchEnabled(true);
// enable scaling and dragging
mChart.setDragEnabled(true);
mChart.setScaleEnabled(false);
mChart.setPinchZoom(true);
mChart.getAxisRight().setEnabled(false);
mChart.getAxisLeft().setEnabled(false);
XAxis xAxisss = mChart.getXAxis();
xAxisss.setLabelCount(MyTestHistory.size(), true);
//xAxisss.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxisss.setTextSize(0f);
xAxisss.setTextColor(Color.TRANSPARENT);
setData(MyTestHistory, ChartName);
mChart.animateX(1000);
mChart.invalidate();
}
private void setData(List<Cls_TestResult_HistoryGraph> MyTestHistory, String ChartName) {
ArrayList<Entry> values = new ArrayList<Entry>();
for (Cls_TestResult_HistoryGraph TRH : MyTestHistory) {
values.add(new Entry(MyTestHistory.indexOf(TRH), (float) TRH.result));
}
LineDataSet set1;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (LineDataSet) mChart.getData().getDataSetByIndex(0);
set1.setValues(values);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
// create a dataset and give it a type
set1 = new LineDataSet(values, "");
set1.setDrawFilled(true);
set1.setFormLineDashEffect(new DashPathEffect(new float[]{10f, 5f}, 0f));
if (Utils.getSDKInt() >= 18) {
// fill drawable only supported on api level 18 and above
Drawable drawable = ContextCompat.getDrawable(this, R.drawable.fade_gray);
set1.setFillDrawable(drawable);
} else {
set1.setFillColor(Color.GRAY);
}
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(set1); // add the datasets
// create a data object with the datasets
LineData data = new LineData(dataSets);
// set data
mChart.setData(data);
}
}
和我的XML布局:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/LightBlue">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="150dp" android:id="@+id/Labconfirm_TestResultList_HistorySection"
android:background="@color/LabConfirm_TestList_Background">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<android.parsic.parstel.CustomControl.UC_TextView_EnglishNormal
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/LabConfirm_TestList_CommentTextColor"
android:singleLine="true"
android:layout_weight="0.33"
android:id="@+id/LabConfirm_TestList_ChartTestName"
android:text="1 of 2"
android:textSize="12dp"
android:textStyle="bold"
android:linksClickable="false"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="false"
android:layout_marginBottom="3dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="30dp" />
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chart1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
任何人都可以帮助我?
Ramin Hbb