MPAndroidChart-条形图未显示所有X轴标签

时间:2020-08-11 14:12:16

标签: java android mpandroidchart

2020年8月14日:在下面发布更新

我的活动中有一个BarChart,并且正在使用// code for random_number() function for(i in 1:5){ print(i); mean([1,2,random_number()]) sd([4,5,6]) } 显示自定义标签。我要在所有显示的条下面添加标签。但是,该图仅在某些条形下方显示标签,如下面的屏幕截图所示。即使缩放,问题仍然存在。

显示图形时,标签的位置使它们不可读(请参见下面的屏幕截图),但是在与图表进行任何交互后即可固定(但所有标签都不会显示)。

我尝试使用IndexAxisValueFormatter,并将布尔值设置为true,但是渲染没有变化。在XAxis.setLabelCount(entries.size(), true)所示的小节上设置限制也无济于事。

显示数据的方法是:

BarChart.setVisibleXRangeMaximum(3);

以下是首次创建活动时图表的显示方式:

Graph when first displayed

与图表互动(双击或缩放)时,图表显示如下:

Graph after interaction

我不确定如何解决此问题或原因。任何帮助将不胜感激!

更新:如果我将private void plotDataBarGraph(int[] xCoordinates, int[] yCoordinates, String label, String[] labels){ BarChart graph = findViewById(R.id.chart); graph.setScaleYEnabled(false); List<BarEntry> entries = new ArrayList<>(); for (int i = 0; i < xCoordinates.length; i++){ entries.add(new BarEntry(xCoordinates[i], yCoordinates[i])) ; } Collections.sort(entries, new EntryXComparator()); BarDataSet dataSet = new BarDataSet(entries,label); dataSet.setColor(Color.BLUE); dataSet.setValueTextColor(Color.BLACK); BarData lineData = new BarData(dataSet); graph.setData(lineData); XAxis xAxis = graph.getXAxis(); xAxis.setLabelCount(entries.size(), true); xAxis.setValueFormatter(new IndexAxisValueFormatter(labels)); xAxis.setLabelRotationAngle(-45); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); graph.invalidate(); // refresh } 替换为BarChart,则标签将按预期显示。但是,这不适合我需要的功能,因为我想创建一个分组的条形图(显示同一个月的两组数据)。有什么方法可以使用CombinedChart来实现?给它分配两个CombinedChart使其仅绘制分配的第二个。

1 个答案:

答案 0 :(得分:0)

尝试删除xAxis.setLabelCount(entries.size(), true) X轴默认显示其所有值。除非需要指定编号,否则无需强行设置标签计数。

摘自文档:

/**
     * sets the number of label entries for the y-axis max = 25, min = 2, default: 6, be aware
     * that this number is not
     * fixed (if force == false) and can only be approximated.
     *
     * @param count the number of y-axis labels that should be displayed
     * @param force if enabled, the set label count will be forced, meaning that the exact
     *              specified count of labels will
     *              be drawn and evenly distributed alongside the axis - this might cause labels
     *              to have uneven values
     */
    public void setLabelCount(int count, boolean force) {

        setLabelCount(count);
        mForceLabels = force;
    }