Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

objective c - How to achieve trackball and access label above Bar on Bar charts in Swift 3x

Hello Developers I am working on project where I have used danielgindi/Charts a third party library now the challenge here is I need to present a track ball like shown in the image on highest point of the chart.How can I achieve this any suggestions ?? Or how can I access the values above BarChart, I want to print something else on the highest peak(20.0) like "E"??

enter image description here enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unfortunately, there is no way to set an image instead of a String or Double at the value above the bar. but yes we can access the value above bar please visit the mentioned link. 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)
 }
}

add the extension above or beyond the class

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...