iOS-Charts xAxis Labels切断

时间:2017-03-27 20:10:30

标签: swift3 ios-charts

我正在使用iOS-Charts来显示水平条形图。 左侧的x轴标签是截止的。 只有当我双击图表时,才会出现正确的尺寸,标签不会再被切断。

这是我正在使用的代码

    func setChart(_ dataPoints: [(String,Int)], chart: HorizontalBarChartView) {

    chart.noDataText = "No data available."

    var dataEntries: [BarChartDataEntry] = []

    let maxNumberEntries = dataPoints.count

    var xAxisLabel: [String] = []

    var counter:Int = maxNumberEntries-1
    for _ in 0..<maxNumberEntries {
        let dataEntry = BarChartDataEntry(x: Double(counter), yValues: [Double(dataPoints[counter].1)], label: dataPoints[counter].0)
        dataEntries.append(dataEntry)
        xAxisLabel.append(dataPoints[counter].0)
        counter -= 1
    }

    xAxisLabel = xAxisLabel.reversed()

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "")
    let chartData = BarChartData(dataSet: chartDataSet)
    chart.data = chartData
    chart.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)

    // disable zoom of chart
    chart.pinchZoomEnabled = false
    chart.scaleXEnabled = false
    chart.scaleYEnabled = false

    chart.chartDescription?.text = ""
    chart.legend.enabled = false

    // disable selection of bars
    chartDataSet.highlightEnabled = false
    chartDataSet.valueFont = NSUIFont.systemFont(ofSize: 10)

    let numberFormatter = ValueFormatter()
    chartData.setValueFormatter(numberFormatter)

    // specify the width each bar should have
    let barWidth = 0.8
    chartData.barWidth = barWidth

    let formato:BarChartFormatter = BarChartFormatter()
    formato.strings = xAxisLabel
    let xaxis:XAxis = XAxis()

    _ = formato.stringForValue(Double(1), axis: xaxis)
    xaxis.valueFormatter = formato
    chart.xAxis.valueFormatter = xaxis.valueFormatter

    let xAxis = chart.xAxis
    xAxis.labelPosition = XAxis.LabelPosition.bottom // label at bottom
    xAxis.drawGridLinesEnabled = false
    xAxis.granularity = 1.0
    xAxis.labelCount = maxNumberEntries
    xAxis.labelRotationAngle = 0

    // Don't show other axis
    let leftAxis = chart.leftAxis
    leftAxis.enabled = false
    let rightAxis = chart.rightAxis
    rightAxis.enabled = false

}

知道如何解决这个问题吗?

截图:

cut-off xAxis labels

after double tap the labels are not cut-off anymore

4 个答案:

答案 0 :(得分:7)

如果即使在notifyDataSetChanged之后您的值也被切断,请尝试像这样抵消它:

tickers <- c('EFA', 'MSFT', 'GOOG', 'AMZN', 'VXX', 'M', 'SPY')
# Create a new environment to store the getSymbols() results
portf <- new.env()
# Import ticker data, storing it in the 'portf' environment
getSymbols(tickers, src = 'yahoo', from = '2006-01-01', env = portf)

# Loop over each object in 'portf',
# and calculate the weekly adjusted close return for the last year
wkly <- lapply(portf, function(x) { periodReturn(tail(Ad(x), 252), 'weekly') })

# Merge the weekly returns into one xts object
wkly <- setNames(Reduce(merge, wkly), names(wkly))

答案 1 :(得分:4)

我通过调用

解决了这个问题
chart.fitScreen()
所有数据通过后,每个条形图

答案 2 :(得分:1)

如果有人一直在努力并且答案没有帮助,一旦你设置了一个包含新数据的图表,不要忘记在图表上调用notifyDataSetChanged,否则标签将被剪切。那是我的理由。

答案 3 :(得分:0)

HorizontalBarChart标签切割问题解决了,您只需要将此代码放入图表设置即可:

chart.extraRightOffset = 30