iOS Charts - single values not showing Swift

a 夏天 提交于 2019-12-06 06:12:23

The only way I could get around this was to add an additional invisible line.

I created a clear line that started the day before and ended the day after my single values.

As long as there is a line on the chart that goes from one point to another, the other single values show.

  var singleValue = false
    for i in 0...(dataSets.count - 1) {
        if dataSets[i].values.count > 1{
            singleValue = true
        }
    }
    var data = dataSets
    if singleValue == false {
        let minNS = Calendar.current.date(byAdding: .day, value: -1, to: minNSDate as! Date)
        let maxNS =  Calendar.current.date(byAdding: .day, value: 1, to: maxNSDate as! Date)

        var dataEntries: [ChartDataEntry] = []

        let dataEntry1 = ChartDataEntry(x:Double(String(format: "%.2f",Double((minNS?.timeIntervalSince1970)!)))!,y:00.00)
        let dataEntry2 = ChartDataEntry(x:Double(String(format: "%.2f",Double((maxNS?.timeIntervalSince1970)!)))!,y:00.00)
        dataEntries.append(dataEntry1)
        dataEntries.append(dataEntry2)
        let set = LineChartDataSet(values: dataEntries, label: "")
        set.setCircleColor(UIColor.clear)
        set.circleHoleColor = UIColor.clear
        set.setColor(UIColor.white, alpha: 0.0)
        set.drawValuesEnabled = false
        data.append(set)

    }
    chart.chartDescription?.text = ""
    let chartData = LineChartData(dataSets: data)
    chart.data = chartData

I think I found a better solution. Single point is not enough to draw a line (you need at least two points) so LineChartView can't render your data. You can fix that by replace LineChartView with CombinedChartView. CombinedChartView give a possibility to mix different types of data on one chart. You can check how many data entires do you have and decide which type of DataSet will be proper.

Code example:

    if dataEntry.count == 1 {
        let scatterDataSet = ScatterChartDataSet(values: dataEntry, label: title)
        scatterDataSet.setColor(UIColor.pmd_darkBlue)
        scatterDataSet.setScatterShape(.circle)
        scatterDataSet.drawValuesEnabled = false

        combinedChartData.scatterData = ScatterChartData(dataSets: [scatterDataSet])
    }
    else {
        let lineDataSet = LineChartDataSet(values: dataEntry, label: title)
        lineDataSet.setColor(UIColor.pmd_darkBlue)
        lineDataSet.lineWidth = 3.0
        lineDataSet.drawCirclesEnabled = false
        lineDataSet.drawValuesEnabled = false

        combinedChartData.lineData = LineChartData(dataSets: [lineDataSet])
    }

    combinedChart.data = combinedChartData 

You can also combine two and more types DataSets in one chart.

Important

Don't forget to add this line:

combinedChart.drawOrder = [DrawOrder.line.rawValue, DrawOrder.scatter.rawValue]

You must to write types of data types you use otherwise data will not render.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!