【问题标题】:Swift ios : Implement scroll on Bar chartSwift ios:在条形图上实现滚动
【发布时间】:2021-08-31 07:33:05
【问题描述】:

尝试使用缩放 x 到 2 来实现条形图上的滚动以进行放大。但问题是 x 轴值未与条形图居中对齐。 标签计数基于日期和月份值。

barChartView.xAxis.axisMinimum = 0.0
barChartView.xAxis.axisMaximum = Double(labels.count - 1)

barChartView.setVisibleXRangeMaximum(Double(labels.count)/2)
or 
barChartView.zoom(scaleX: 2, scaleY: 0, x: 0, y: 0)

对于像这样实现的滚动,

barChartView.xAxis.setLabelCount(Int(Double(labels.count)/2), force: true)

请向我们建议正确的方法,以避免 x 轴值与条形图不对齐。

【问题讨论】:

    标签: ios swift charts bar-chart ios-charts


    【解决方案1】:

    根据您的代码,我假设您的预期行为是图表

    • 最多显示所有可用柱的一半
    • 允许用户无限放大(最小值 = 0)
    • 允许用户滚动(左/右)

    如果是这种情况,您将应用以下代码:

    barChartView.xAxis.axisMinimum = 0.0
    barChartView.xAxis.axisMaximum = Double(labels.count - 1)
    
    barChartView.setVisibleXRangeMaximum(Double(labels.count)/2)
    barChartView.xAxis.setLabelCount(Int(Double(labels.count)/2), force: false)
    

    应该避免.zoom,因为它设置了多个不需要的值(scaleYxy),因此在滚动时会添加不需要的功能。

    force 标志设置为false 很重要。这是导致您未对齐的参数,如文档中所述:

    https://weeklycoding.com/mpandroidchart-documentation/axis-general/

    setLabelCount(int count, boolean force):设置y轴的标签数量。请注意,这个数字不是固定的(如果 force == false),只能近似。如果启用 force (true),则会绘制精确指定的标签计数 - 这可能会导致轴上出现奇数。

    【讨论】:

    • 感谢您的回答。将 force 设置为 false 时效果很好。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 2013-08-03
    • 1970-01-01
    相关资源
    最近更新 更多