【问题标题】:MPAndroidChart Alignment of xAxisxAxis 的 MPAndroidChart 对齐方式
【发布时间】:2023-03-22 03:13:01
【问题描述】:

谁能帮助我解释为什么我的 xAxis 对齐不正确。我希望这些点与每个相应的文本标签(星期几)对齐

这是图表的样子:GRAPH PICTURE

以及产生它的代码

private void setupChart() {
    final ArrayList<Entry> points = new ArrayList<>();
    //Add the values into the graph
    points.add(new Entry(1,  17));
    points.add(new Entry(2,  14));
    points.add(new Entry(3,  10));
    points.add(new Entry(4,  2));
    points.add(new Entry(5,  20));
    //Format the dataset to look nice
    LineDataSet lineDataSet = new LineDataSet(points, "Experiments");
    lineDataSet.setColor(Color.MAGENTA);
    lineDataSet.setCircleColor(Color.GREEN);
    lineDataSet.setCircleRadius(5);
    lineDataSet.setValueTextSize(10);
    lineDataSet.setLineWidth(3);

    //Add the LineDataSet to the DataSets
    final ArrayList<ILineDataSet> dataSets = new ArrayList<>();
    dataSets.add(lineDataSet);

    final LineData lineData = new LineData(dataSets);
    chart.setData(lineData);
    //Format the chart nicely
    chart.setDrawGridBackground(false);
    chart.setDrawBorders(false);
    chart.getAxisRight().setEnabled(false);
    chart.getLegend().setEnabled(false);
    chart.getDescription().setEnabled(false);
    chart.setExtraBottomOffset(10);
    chart.setPinchZoom(false);

    //Get the XAxis of the grid
    XAxis xaxis = chart.getXAxis();
    //Format the xAxis
    final String[] dateStrings = new String[]{"Mon", "Tue", "Wed", "THU", "Fri"};
    xaxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xaxis.setLabelCount(5);

    xaxis.setLabelRotationAngle(-8);
    xaxis.setValueFormatter(new IAxisValueFormatter() {
        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return dateStrings[(int) value];
        }
    });
}

我在我的依赖项中使用 com.github.PhilJay:MPAndroidChart:v3.0.2

【问题讨论】:

    标签: android mpandroidchart


    【解决方案1】:

    使用下面的代码。

    xAxis.setLabelCount(5, true);
    

    而且,您可以在库文件中找到实现代码。

    public void setLabelCount(int count, boolean force) {
        setLabelCount(count);
        mForceLabels = force;
    }
    

    “force”参数决定是否重新计算标签位置。

    【讨论】:

      【解决方案2】:

      删除这一行:

      xaxis.setLabelCount(5);
      

      添加以下行:

      XAxis xAxis = chart.getXAxis();
      xAxis.setCenterAxisLabels(true);
      

      【讨论】:

        猜你喜欢
        • 2021-05-31
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多