1

我使用 ios-charts 在我的折线图中有负值。显示正值,但在 0 处截断负值。我已将左右轴设置为不从 0 开始,但没有帮助。

    LineChart1.leftAxis.startAtZeroEnabled = false
    LineChart1.rightAxis.startAtZeroEnabled = false

有任何想法吗?TIA。

下面的代码片段。

    override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    self.navigationController?.navigationBar.translucent = false

    self.title = "Line Chart 1"
    LineChart1.bounds = CGRectMake(-20, -20, 275, 250)
    LineChart1.data = getMyData()
    LineChart1.drawBordersEnabled = false
    LineChart1.legend.enabled = false
    LineChart1.leftAxis.startAtZeroEnabled = false
    LineChart1.rightAxis.startAtZeroEnabled = false
    LineChart1.xAxis.setLabelsToSkip(0)
    LineChart1.xAxis.drawGridLinesEnabled = false
    LineChart1.leftAxis.drawGridLinesEnabled = true
    LineChart1.rightAxis.drawLabelsEnabled = false
    LineChart1.animate(xAxisDuration: 0.3)
    LineChart1.animate(yAxisDuration: 0.3)

    LineChart1.setNeedsDisplay()
}

func getMyData() -> LineChartData {

    var xVals = ["2008","2009","2010","2011","2012"]

    var courses: [ChartDataEntry] = []
    courses.append(ChartDataEntry(value: 3, xIndex: 0))
    courses.append(ChartDataEntry(value: -40, xIndex: 1))
    courses.append(ChartDataEntry(value: 4, xIndex: 2))
    courses.append(ChartDataEntry(value: 4, xIndex: 3))
    courses.append(ChartDataEntry(value: 3, xIndex: 4))

    let bcds = LineChartDataSet(yVals: courses, label: "")
    bcds.valueTextColor = UIColor.blackColor()

    return LineChartData(xVals: xVals, dataSet: bcds)
}
4

1 回答 1

2

您应该notifyDataSetChanged()在设置后调用startAtZeroEnabled- 就像在示例应用程序中一样。

图表不知道startAtZeroEnabled内部组件等属性的变化,因此您必须告诉它。并且因为 min/max 是根据 计算的notifyDataSetChangedstartAtZeroEnabled所以在这种情况下您必须通知,并且只需setNeedsDisplay...

于 2015-05-19T08:15:59.923 回答