MPAndroidChart:如何以固定间隔设置 x 轴标签

san*_*jay 10 android mpandroidchart

我正在显示实时图表,该图表应按秒显示值。我的 X 轴是以秒为单位的时间。但是我无法在 x 轴上显示固定的时间间隔,即。0,1,2,...等等。X 轴值是自动计算的,两个 x 值之间的时间间隔变为 20 秒,这是我不想要的。我需要你的帮助来解决这个问题。

任何帮助将不胜感激。

Luk*_*ith 18

有一个替代解决方案可以在正常的 MPAndroidChart(iOS 或 Android)上运行。您可以使用axisMinimum 和axisMaximum 属性手动设置轴覆盖的范围。您还可以使用 setLabelCount 设置轴标签的数量。通过这些组合,您可以获得轴标签之间的均匀间隔。例如,axisMinimum 为 0,axisMaximum 为 100,setLabelCount 设置了 5 个标签,最终在范围的顶部和底部(分别为 0 和 100)有一个标签,中间有 3 个标签,这给你25 的固定差距。 Swift 3 代码:

chart.xAxis.setLabelCount(5, /*force: */true)
chart.xAxis.axisMinimum = 100
chart.xAxis.axisMaximum = 0
Run Code Online (Sandbox Code Playgroud)

为您提供偶数间隔:0、25、50、75、100。


fgu*_*eli 5

You can use a modified version of the library that allows users to tell the graph which labels should be drawn.

See the example below:

leftAxis.setShowSpecificLabelPositions(true);
leftAxis.setSpecificLabelPositions(new float[]{0, 10, 20, 50, 100, 300});
Run Code Online (Sandbox Code Playgroud)

Reference link: https://github.com/PhilJay/MPAndroidChart/pull/2692

Modified Library link: https://github.com/philippeauriach/MPAndroidChart/tree/custom-labels

  • @theLazyFinder 用户名签出 (3认同)
  • 未找到 setShowSpecificLabelPositions() 和 setSpecificLabelPositions() (2认同)
  • @theLazyFinder 你必须使用库的修改版本;https://github.com/philippeauriach/MPAndroidChart/tree/custom-labels (2认同)