invoker_yang 2019-11-17 10:45 采纳率: 0%
浏览 1793
已结题

如何解决Android MPAndroidChart中条形图 xAxis标签未居中的问题

x标签未在条形图中间正下方显示

代码:
/**
* 单数据集。设置柱状图样式,X轴为字符串,Y轴为数值
*
* @param barChart
* @param xAxisValue
* @param yAxisValue
*/
public void setBarChart(BarChart barChart, List xAxisValue, List yAxisValue) {
barChart.getDescription().setEnabled(false);//设置描述
barChart.setPinchZoom(true);//设置按比例放缩柱状图

    //设置自定义的markerView
    MyMarkerView markerView = new MyMarkerView(barChart.getContext(), R.layout.custom_marker_view);
    barChart.setMarker(markerView);

// MyMarkerView mv = new MyMarkerView(mContext, R.layout.custom_marker_view);
// mv.setChartView(chart); // For bounds control
// manager.setMarker(mv); // Set the marker to the

    //x坐标轴设置
    IAxisValueFormatter xAxisFormatter = new StringAxisValueFormatter(xAxisValue);//设置自定义的x轴值格式化器
    XAxis xAxis = barChart.getXAxis();//获取x轴
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置X轴标签显示位置
    xAxis.setDrawGridLines(false);//不绘制格网线
    xAxis.setGranularity(1f);//设置最小间隔,防止当放大时,出现重复标签。
    xAxis.setValueFormatter(xAxisFormatter);
    xAxis.setDrawLabels(true);

// xAxis.setXOffset(-10);
xAxis.setTextSize(8f);//设置标签字体大小
xAxis.setLabelCount(xAxisValue.size(),false);//设置标签显示的个数
// xAxis.setAxisMinimum(0f);
// xAxis.setAxisMaximum(1000f);
// xAxis.setAxisMinimum(1f);
// xAxis.setAxisMaximum(xAxisValue.size() - 1.1f);
xAxis.setValueFormatter(new IndexAxisValueFormatter(xAxisValue));

    xAxis.setCenterAxisLabels(true);//设置标签居中

    xAxis.setDrawAxisLine(true);

    xAxis.setLabelRotationAngle(60);




    //y轴设置
    YAxis leftAxis = barChart.getAxisLeft();//获取左侧y轴
    leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);//设置y轴标签显示在外侧
    leftAxis.setAxisMinimum(0f);//设置Y轴最小值
    leftAxis.setDrawGridLines(true);
    leftAxis.setDrawLabels(true);//禁止绘制y轴标签
    leftAxis.setDrawAxisLine(true);//禁止绘制y轴

    barChart.getAxisRight().setEnabled(false);//禁用右侧y轴

    //图例设置
    Legend legend = barChart.getLegend();
    legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//图例水平居中
    legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//图例在图表上方
    legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);//图例的方向为水平
    legend.setDrawInside(false);//绘制在chart的外侧
    legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);//图例中的文字方向

    legend.setForm(Legend.LegendForm.SQUARE);//图例窗体的形状
    legend.setFormSize(0f);//图例窗体的大小
    legend.setTextSize(16f);//图例文字的大小
    //legend.setYOffset(-2f);

    //设置柱状图数据
    setBarChartData(barChart, yAxisValue, "", null);

    barChart.setExtraBottomOffset(10);//距视图窗口底部的偏移,类似与paddingbottom
    barChart.setExtraTopOffset(30);//距视图窗口顶部的偏移,类似与paddingtop
    barChart.setFitBars(true);//使两侧的柱图完全显示
    barChart.animateX(100);//数据显示动画,从左往右依次显示
}


    问题截图:
    ![图片说明](https://img-ask.csdn.net/upload/201911/17/1573960883_278806.png)
  • 写回答

2条回答 默认 最新

  • dabocaiqq 2019-11-17 11:47
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作