如何在MPAndroidChart(线形图)中自定义DrawValues并显示X和Y轴?

时间:2019-11-30 05:09:48

标签: java android mpandroidchart linechart

我一直坚持在Android Studio中自定义和显示我的线图标签值。我没有语法错误,只是当我将.setdrawvalues设置为true时,它仅显示YaxisLeft而xAxis丢失了,我不知道如何在每个值后添加注释。

这是我的主要活动:

```@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    wfer = inflater.inflate(R.layout.fragment_weight, container, false);
    lineChart = wfer.findViewById(R.id.line_chart_weight);
    lineChart.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                @Override
                public void onGlobalLayout() {

                    lineChart.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                    initLineChart();
                }
            });

    return wfer;
}

private void initLineChart() {
    ProcessClass pc = new ProcessClass();
    DataClass dc = new DataClass();

    lineChart.getXAxis().setTextSize(10f);
    lineChart.getXAxis().setAxisMinimum(2);
    lineChart.getXAxis().setLabelCount(26);
    lineChart.getXAxis().setAxisMaximum(28f);
    lineChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);

    lineChart.getAxisLeft().setAxisMinimum(2);
    lineChart.getAxisLeft().setLabelCount(58);
    lineChart.getAxisLeft().setAxisMaximum(60f);

    lineChart.getAxisRight().setAxisMinimum(0);
    lineChart.getAxisRight().setLabelCount(4);
    lineChart.getAxisRight().setAxisMaximum(5f);

    LineDataSet line1 = new LineDataSet(pc.weightguideentry(), "Recommended");
    line1.setColor(Color.RED);
    line1.setCircleColor(Color.RED);
    line1.setLineWidth(2f);
    line1.setCircleRadius(2f);
    line1.setDrawCircles(true);
    line1.setDrawValues(true);

    LineDataSet line2 = new LineDataSet(pc.weightgrowthentry(dc.getRow(), dc.getProgressWeight(), dc.getProgressMonth()), "Your Baby");
    line2.setColor(Color.GREEN);
    line2.setCircleColor(Color.GREEN);
    line2.setLineWidth(2f);
    line2.setCircleRadius(2f);
    line2.setDrawCircles(true);
    line2.setDrawValues(true);

    LineData lineData = new LineData(line1,line2);
    lineChart.setData(lineData);
    lineChart.invalidate();
}```

这是输出: Output

例如,如果我的linedataset值是“ x:4 y:10”,那么我必须在图表中显示“ 4岁,体重10公斤”,但仅显示4.0。

我确定linedataset没有错误,因为它正确显示了图形。

这是我的xml布局:

<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"
    tools:context=".weight_fragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="M\nO\nN\nT\nH\nS"
            android:layout_gravity="center" />
        <com.github.mikephil.charting.charts.LineChart
            android:id="@+id/line_chart_weight"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:rotation="0"
            android:layout_weight="6">
        </com.github.mikephil.charting.charts.LineChart>
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="Y\nE\nA\nR\nS"
            android:layout_gravity="center" />
    </LinearLayout>

</FrameLayout>```

0 个答案:

没有答案