如何在Swift 3x的条形图上的Bar上方实现轨迹球和访问标签

时间:2017-02-01 12:32:14

标签: objective-c swift charts bar-chart ios-charts

您好开发人员我正在开发项目,我已经使用了danielgindi / Charts第三方库现在这里的挑战是我需要提供一个跟踪球,如图中最高点的图像所示。我怎么能实现这一点有什么建议 ?? 或者我如何访问BarChart上方的值,我想在最高峰(20.0)上打印其他内容,如" E" ??

enter image description here enter image description here

2 个答案:

答案 0 :(得分:7)

不幸的是,没有办法在条形图上方的值处设置图像而不是String或Double。 但是,我们可以访问上面的值,请访问提到的链接。 https://github.com/danielgindi/Charts/issues/2126

class ViewController: UIViewController {
    var chartData = BarChartData()
    var months: [String]!
    var unitsSold = [Double]()
 weak var valueFormatter: IValueFormatter?
   @IBOutlet var viewForChart: BarChartView!
override func viewDidLoad() {
        super.viewDidLoad()
valueFormatter = self

        months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
        unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]

 setChart(dataEntryX: months, dataEntryY: unitsSold)
}

    func setChart(dataEntryX forX:[String],dataEntryY forY: [Double]) {
        viewForChart.noDataText = "You need to provide data for the chart."
        var dataEntries:[BarChartDataEntry] = []
        for i in 0..<forX.count{

            let dataEntry = BarChartDataEntry(x: Double(i), y:  Double(forY[i]) , data: months as AnyObject?)
     dataEntries.append(dataEntry)
        }

        let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
       chartDataSet.setColor(UIColor.brown, alpha: 0.30)
        chartData = BarChartData(dataSet: chartDataSet)
chartData.barWidth = 0.75
        chartData.setDrawValues(true)
        viewForChart.data = chartData
viewForChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)

        viewForChart.drawBordersEnabled = false

  viewForChart.data?.setValueFormatter(valueFormatter)
 }
}

在类

之上或之外添加扩展名
extension ViewController: IValueFormatter {
    func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
        print(value)
        //logic to print string at required point or value
        switch value {

        case 20.0: return ""
        default: return ""
        }
    }
}

答案 1 :(得分:2)

快速2倍 //将此添加到您的班级

class ValueFormator2:NSNumberFormatter{
    override func stringFromNumber(number: NSNumber) -> String? {
        print(number)
        switch number {
        case 3.0: return ""

        default: return "\(number)"
        }
    }

}

然后

 func setChart(xValues: [String], yValuesLineChart: [Double], yValuesBarChart: [Double] , combinedChartView:CombinedChartView) {
    let form = ValueFormator2()
    combinedChartView.barData?.setValueFormatter(form)
}