在 danielgindi/Charts ios 中显示 x 轴实数

Show x axis values real in danielgindi/Charts ios

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

我开始使用它并绘制了图表,但我需要对 UI 进行一些更改。我试着给 danielgindi LineChart 我想要的样子,但我不明白。

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

我想画的图表:-

我创建的图表:-

事情需要改变:-

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

  2. 虚线而不是完整的轴线。

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

  4. 方形黑框需要去掉

这是代码:-

    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)
    }

每个日期都需要存储为字符串并添加到 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
}

在 swift 3 中: 答案一:

chartView.leftAxis.enabled = false

答案 4:

chartView.legend.enabled = false