ios 个图表未在图表中显示 line/bar

ios charts not displaying line/bar in charts

我正在研究 Daniel Gindi 在 swift 3.0 上的 ios-charts。我为条形图编写了以下代码

var dataEntries: [BarChartDataEntry] = []
for i in 0..<dateTime.count {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MM/dd/yyyy hh:mm:ss aa"
    let dateObj = dateFormatter.date(from: dateTime[i])
    let timeIntervalForDate = dateObj!.timeIntervalSince1970
    let dataEntry = BarChartDataEntry(x: Double(timeIntervalForDate), y: Double(readings[i]))
    dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units")
let chartData = BarChartData(dataSet: chartDataSet)
self.ChartsView.data = chartData

我在模拟器中得到以下结果:

你看不到反映我价值观的红线。我什至放大到图表,但什么也没有显示。

我还尝试了带有示例值的折线图。这是代码:

let dollars1 = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
var yValues : [ChartDataEntry] = [ChartDataEntry]()
for i in 0 ..< months.count {
    yValues.append(ChartDataEntry(x: Double(i + 1), y: dollars1[i]))
}
let LineData = LineChartData()
let ds = LineChartDataSet(values: yValues, label: "Months")

LineData.addDataSet(ds)
self.ChartsView.data = LineData

结果更诡异。此图表中也没有显示网格线。这是图片。

我不确定这些图表有什么问题。我错过了什么吗?到目前为止,我无法进一步了解,因为大多数解决方案都使用旧版本的图表(或者可能是我不知道的其他东西)。请指导我正确的方向。谢谢

有一个 bug in ios-charts 尝试使用时间戳索引而不是使用时间戳或日期在 x 轴上显示。

// i is the index of that timestamp in data.    
let dataEntry = BarChartDataEntry(x: i, y: Double(readings[i]))

我正在为版本 3.0 和 Swift 3 而苦苦挣扎,并且缺少文档...我可以为您指出有关该主题的最佳文本吗 here

Xvals 作为参数消失了,取而代之的是在轴

上使用 two-dimensional 数据

关于您的时间值问题..您可以使用 date-object 的 "timeIntervalSince1970" 并使用扩展名

格式化内容
return dateFormatter.string(from: Date(timeIntervalSince1970: value))

所有这些都可以在上面提到的link中找到

嘿,iOS 图表中没有任何东西被禁用,一切正常。这是我在我的应用程序中用来显示水平图表和垂直图表的代码,希望它对任何被卡住的人有所帮助在 IOS 图表上...关注 Whosebug...享受编码 :)

import UIKit

import Charts

class DashboardViewController: UIViewController, ChartViewDelegate
{
 @IBOutlet weak var barview: BarChartView!
 @IBOutlet weak var horizontalview: HorizontalBarChartView!
 var attendencelabels = [String]()
 let Present = [60.0, 60.0]
 let Absent = [20.0,40.0]
 var subjects : [String]!
 var maxUnits : Double!
 override func viewDidLoad() {
    super.viewDidLoad()
    barview.delegate = self
    barview.noDataText = "You need to provide data for the chart."
    barview.chartDescription?.text = ""
    attendencelabels.append("March")
    attendencelabels.append("April")
    subjects = ["Physics","Chemistry","Biology","English","Maths"]
    horizontalview.delegate = self
    var marks = [60.0,70.0,60.0,80.0,90.0]
    maxUnits = marks.max()
    setHorizontalChart(dataPoints: subjects, values: marks)
    //legend
    let legend = barview.legend
    legend.enabled = true
    legend.horizontalAlignment = .center
    legend.verticalAlignment = .bottom
    legend.orientation = .horizontal
    legend.drawInside = false
    legend.yOffset = 10.0;
    legend.xOffset = 10.0;
    legend.yEntrySpace = 0.0;
    let xaxis = barview.xAxis
    xaxis.valueFormatter = DefaultAxisValueFormatter.self as! IAxisValueFormatter
    xaxis.drawGridLinesEnabled = true
    xaxis.labelPosition = .top
    xaxis.centerAxisLabelsEnabled = true
    xaxis.valueFormatter = IndexAxisValueFormatter(values:self.attendencelabels)
    xaxis.granularity = 1
    let leftAxisFormatter = NumberFormatter()
    leftAxisFormatter.maximumFractionDigits = 1
    let yaxis = barview.leftAxis
    yaxis.spaceTop = 0.35
    yaxis.axisMinimum = 0
    yaxis.drawGridLinesEnabled = false
    barview.rightAxis.enabled = false
    setChart()
}

func setChart() {
    barview.noDataText = "You need to provide data for the chart."
    var dataEntries: [BarChartDataEntry] = []
    var dataEntries1: [BarChartDataEntry] = []
    for i in 0..<self.attendencelabels.count 
    {

        let dataEntry = BarChartDataEntry(x: Double(i) , y:     self.Present[i])
        dataEntries.append(dataEntry)

let dataEntry1 = BarChartDataEntry(x: Double(i) , y: self.self.Absent[i])
        dataEntries1.append(dataEntry1)

        //stack barchart
        //let dataEntry = BarChartDataEntry(x: Double(i), yValues:  [self.unitsSold[i],self.unitsBought[i]], label: "groupChart")
    }

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Working Days")
    let chartDataSet1 = BarChartDataSet(values: dataEntries1, label: "Present Days")

    let dataSets: [BarChartDataSet] = [chartDataSet,chartDataSet1]
    chartDataSet.colors = [UIColor(red: 0/255, green: 255/255, blue: 25/255, alpha: 1)]
    chartDataSet1.colors = [UIColor(red: 255/255, green: 0/255, blue: 61/255, alpha: 1)]
    //chartDataSet.colors = ChartColorTemplates.colorful()
    //let chartData = BarChartData(dataSet: chartDataSet)

    let chartData = BarChartData(dataSets: dataSets)


    let groupSpace = 0.3
    let barSpace = 0.05
    let barWidth = 0.3
    // (0.3 + 0.05) * 2 + 0.3 = 1.00 -> interval per "group"

    let groupCount = self.attendencelabels.count
    let startYear = 0


    chartData.barWidth = barWidth;
    barview.xAxis.axisMinimum = Double(startYear)
    let gg = chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)
    print("Groupspace: \(gg)")
    barview.xAxis.axisMaximum = Double(startYear) + gg * Double(groupCount)

    chartData.groupBars(fromX: Double(startYear), groupSpace: groupSpace, barSpace: barSpace)
    //chartData.groupWidth(groupSpace: groupSpace, barSpace: barSpace)
    barview.notifyDataSetChanged()

    barview.data = chartData
    //background color
    barview.backgroundColor = UIColor.white

    //chart animation
    barview.animate(xAxisDuration: 1.5, yAxisDuration: 1.5, easingOption: .linear)

}
func setHorizontalChart(dataPoints: [String], values: [Double]) {
    horizontalview.noDataText = "You need to provide data for the chart."

    let formato: horizontalbarformatter = horizontalbarformatter()
    let xaxiss: XAxis = XAxis()
    var dataEntriesss: [BarChartDataEntry] = []
    for i in 0..<dataPoints.count {
        let dataEntryy = BarChartDataEntry(x: Double(Int(i)), y: values[i])
        dataEntriesss.append(dataEntryy)
        formato.stringForValue(Double(i), axis: xaxiss)
    }
    xaxiss.valueFormatter = formato

    let chartHorizontalDataSet = BarChartDataSet(values: dataEntriesss, label: "Marks")
    let horizontalchartData = BarChartData(dataSet: chartHorizontalDataSet)
    chartHorizontalDataSet.colors = ChartColorTemplates.colorful()

    horizontalview.xAxis.valueFormatter = xaxiss.valueFormatter
    horizontalview.data = horizontalchartData
    horizontalview.backgroundColor = UIColor.white
    horizontalview.descriptionText = ""
    horizontalview.xAxis.labelPosition = .bottom
    horizontalview.leftAxis.axisMinimum = 0
    horizontalview.leftAxis.axisMaximum = maxUnits + 1

    //barChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)
    horizontalview.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce)

    let ll = ChartLimitLine(limit: 10.0, label: "Target")
    horizontalview.rightAxis.addLimitLine(ll)
}

func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
    print("\(entry.y) in \(attendencelabels[Int(entry.x)])")
  }

 }