在danielgindi / Charts ios中显示x轴值

时间:2017-05-17 12:35:53

标签: ios iphone swift charts ios-charts

我需要在ios中创建一个折线图,所以我搜索并发现 danielgindi / Charts 是最好的选择之一。

我开始使用它并绘制图表,但我需要在UI中进行一些更改。我试图给danielgindi LineChart看起来我想要但我没有得到它。

我希望我为x轴提供的值应该按原样反映但是它没有反映?

我想绘制的图表: - Line chart needed

我创建的图表: - Line chart made using ios charts

事情需要改变: -

  1. 左轴值 - >我试图通过lineChart.rightAxis.enabled = false将其删除。它删除了y轴线(与x轴平行的线)。我想要y轴线。

  2. 虚线轴而不是完整。

  3. X轴应该正确显示值,Jan,jan,feb显示而不是jan,feb,march,这也不正确。

  4. 需要移除方形黑匣子。

  5. 这是代码: -

        override func viewDidLoad() {
            super.viewDidLoad()
    
            self.lineChart.delegate = self
            self.lineChart.chartDescription?.textColor = UIColor.white
    
            let months = ["Jan" , "Feb", "Mar"]
            let dollars1 = [1453.0,2352,5431]
            setChart(months, values: dollars1)
        }
    
        func setChart(_ dataPoints: [String], values: [Double]) {
    
            var dataEntries: [ChartDataEntry] = []
    
            for i in 0 ..< dataPoints.count {
                dataEntries.append(ChartDataEntry(x: Double(i), y: values[i]))
            }
    
            let lineChartDataSet = LineChartDataSet(values: dataEntries, label: nil)
            lineChartDataSet.axisDependency = .left
            lineChartDataSet.setColor(UIColor.black)
            lineChartDataSet.setCircleColor(UIColor.black) // our circle will be dark red
            lineChartDataSet.lineWidth = 1.0
            lineChartDataSet.circleRadius = 3.0 // the radius of the node circle
            lineChartDataSet.fillAlpha = 1
            lineChartDataSet.fillColor = UIColor.black
            lineChartDataSet.highlightColor = UIColor.white
            lineChartDataSet.drawCircleHoleEnabled = true
    
            var dataSets = [LineChartDataSet]()
            dataSets.append(lineChartDataSet)
    
    
            let lineChartData = LineChartData(dataSets: dataSets)
            lineChart.data = lineChartData
            lineChart.rightAxis.enabled = false
            lineChart.xAxis.drawGridLinesEnabled = false
            lineChart.xAxis.labelPosition = .bottom
            lineChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
        }
    

2 个答案:

答案 0 :(得分:1)

每个日期都需要存储为字符串并添加到xvalues数组中。这就是我解决这个问题的方法:

 override func viewDidLoad() {
    super.viewDidLoad()

    var xvalues: [String] = [String]()
    xvalues.append("3 Sept")
    xvalues.append("16 Sept")
    xvalues.append("15 Oct")
    xvalues.append("15 Oct")
    xvalues.append("15 Oct")

    let xAxis = lineChart.xAxis
    xAxis.labelPosition = .bothSided
    xAxis.axisMinimum = 0.0
    xAxis.granularity = 1.0

    lineChart.leftAxis.enabled = false
    lineChart.rightAxis.enabled = false
    lineChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: xvalues)

    let data = generateLineData()
    lineChart.data = data


}

func generateLineData() -> LineChartData {
    let data: LineChartData = LineChartData()
    var entries: [ChartDataEntry] = []
    var dataArr: [Int] = []
    dataArr.append(10)
    dataArr.append(5)
    dataArr.append(8)
    dataArr.append(12)
    dataArr.append(10)

    for index in 0..<5 {
        entries.append(ChartDataEntry(x: Double(index),  y: Double(dataArr[index]) ) )
    }

    let set: LineChartDataSet = LineChartDataSet(values: entries, label: "My label")
    set.setCircleColor(UIColor.blue)
    set.lineWidth = 1
    set.circleRadius = 5
    set.drawCircleHoleEnabled = false
    set.valueTextColor = UIColor.blue
    set.valueFont = UIFont(name: "Verdana", size: 12.0)!
    set.drawFilledEnabled = true
    set.mode = Charts.LineChartDataSet.Mode.linear
    set.axisDependency = Charts.YAxis.AxisDependency.left

    data.addDataSet(set)

    return data
}

答案 1 :(得分:1)

在swift 3中: 答案1:

chartView.leftAxis.enabled = false

答案4:

chartView.legend.enabled = false