X 轴标签未显示,使用 MPAndroidChart

Tir*_*ami 1 java android mpandroidchart

我想在我的折线图上有 x 轴标签(使用MPAndroidChart),但无论我做什么,我都无法显示它们。我有一个名为 setupChart 的方法,它处理该图表的所有内容,它是这样的:

private void setupChart(LineChart chart, LineData data, int color) {

    ((LineDataSet) data.getDataSetByIndex(0)).setCircleColorHole(Color.WHITE);
    ((LineDataSet) data.getDataSetByIndex(0)).setCircleColor(color);
    ((LineDataSet) data.getDataSetByIndex(0)).setColors(dataColors);
    data.getDataSetByIndex(0).setAxisDependency(YAxis.AxisDependency.LEFT);
    ((LineDataSet) data.getDataSetByIndex(0)).setDrawCircles(false);

    // no description text
    chart.getDescription().setEnabled(false);

    // mChart.setDrawHorizontalGrid(false);
    //
    // enable / disable grid background
    chart.setDrawGridBackground(false);

    // enable touch gestures
    chart.setTouchEnabled(true);

    // disable scaling and dragging
    chart.setDragEnabled(false);
    chart.setScaleEnabled(false);
    chart.setAutoScaleMinMaxEnabled(false);
    // if disabled, scaling can be done on x- and y-axis separately
    chart.setPinchZoom(false);
    chart.setBackgroundColor(Color.parseColor("#dddddd"));

    // set custom chart offsets (automatic offset calculation is hereby disabled)
    chart.setViewPortOffsets(10, 0, 10, 0);
    // add data
    chart.setData(data);

    // get the legend (only possible after setting data)
    Legend l = chart.getLegend();
    l.setEnabled(false);

    chart.getAxisLeft().setEnabled(false);

    chart.getAxisRight().setEnabled(false);
    chart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
    chart.getXAxis().setEnabled(false);
    chart.getXAxis().setDrawLabels(true);
    chart.getXAxis().setTextColor(context.getResources().getColor(R.color.colorPrimary));
    chart.getXAxis().setTypeface(Typeface.SANS_SERIF);
    YAxis yAxis = chart.getAxisLeft();
    yAxis.setAxisMinimum(0f);
    yAxis.setAxisMaximum(100f);

    final String[] weeks = new String[52];

    for(int i = 0; i < weeks.length; i++) weeks[i] = "Week " + (i+1);

    IAxisValueFormatter formatter = new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return weeks[(int) value];
        }
    };

    XAxis xAxis = chart.getXAxis();
    xAxis.setGranularity(1f);
    xAxis.setValueFormatter(formatter);
    // animate calls invalidate()...
    chart.animateXY(2000,2500);
}
Run Code Online (Sandbox Code Playgroud)

我不确定我做错了什么。

Tir*_*ami 5

我通过增加偏移量解决了这个问题:

chart.setViewPortOffsets(60, 0, 50, 60);
Run Code Online (Sandbox Code Playgroud)


siv*_*E35 5

linechart.setExtraBottomOffset(ff)
Run Code Online (Sandbox Code Playgroud)

修改偏移量也将帮助您解决这个问题。

调整 (ff) 浮点值以获得所需的空间。