ios图表3 - 每个数据点绘制X轴标签

时间:2017-06-02 16:02:35

标签: ios swift ios-charts

我在图表上有30个数据点,但图表的X轴分割数字,并且没有为每个数据点提供标签。见下文。

![1]:https://i.stack.imgur.com/I1vAP.png

class TrendChartViewController: UIViewController {
@IBOutlet var combinedChart: CombinedChartView!
var viewModel: TrendChartViewModelProtocol! {
    didSet {
        updateChart()
    }
}

private func updateChart() {
    if let dataset = viewModel.dataset {
        let data = CombinedChartData()
        data.lineData = LineChartData(dataSets: [dataset.current, dataset.py])
        data.barData = getBarChartData(dataset: dataset.budget)
        combinedChart.data = data
        combinedChart.xAxis.valueFormatter = DateValueFormatter(timeframe: viewModel.timeframe)
        removeGridLines()
    }
}

private func getBarChartData(dataset: BarChartDataSet) -> BarChartData {
    var barChartData = BarChartData(dataSet: dataset)
    barChartData.barWidth = 0.45
    return barChartData
}

private func removeGridLines() {
    combinedChart.xAxis.drawGridLinesEnabled = false
    combinedChart.leftAxis.drawGridLinesEnabled = false
    combinedChart.rightAxis.drawGridLinesEnabled = false
    combinedChart.drawGridBackgroundEnabled = false
}

以下是我们的一个数据集...

的示例
private func getCurrentDataSet() -> LineChartDataSet {
    let values = timeframe == .days ? report?.charts[0].data.days : report?.charts[0].data.months
    let currentEntries: [ChartDataEntry] = values?
        .enumerated()
        .flatMap {
            return ChartDataEntry(x: Double($0), y: $1.current[unit.rawValue] ?? 0)
        } ?? []

    let currentDataSet = LineChartDataSet(values: currentEntries, label: NSLocalizedString("Current", comment: ""))
    let color = UIColor.Bridgestone.forest.B500
    currentDataSet.setColor(color)
    currentDataSet.circleColors = [color]
    currentDataSet.circleHoleColor = color
    currentDataSet.circleRadius = 5
    currentDataSet.lineWidth = 2.5
    return currentDataSet
}

那么我怎样才能让它显示每个数据点的X轴标签值?谢谢!

1 个答案:

答案 0 :(得分:0)

如果要在xAxis上设置标签数,则需要使用func xAxis.setLabelCount,例如

combinedChart.xAxis.setLabelCount(countOfMyDataPoints, force: true)