3

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

Harshit Goel
  • 679
  • 1
  • 6
  • 20
  • what does the original author of _danielgindi/Charts_ say? – holex Feb 01 '17 at 12:37
  • @holex I had a chat with him what u say ? – Harshit Goel Feb 01 '17 at 12:55
  • I suggested you that you should ask him instead about _"what way you could achieve this"_. – holex Feb 01 '17 at 14:16
  • @holex can you guide me how to access the labels of bar charts (on the peaks one ) – Harshit Goel Feb 02 '17 at 04:30
  • @matt Sir I need help can you guide me how to do the same thing in swift 2? – Harshit Goel Feb 03 '17 at 06:22
  • @HarshitGoel have you achieved this? – Ramkumar chintala Mar 02 '17 at 11:16
  • @Ramkumarchintala yes below is the answer I had asked my designer to creat the font then simply installed it and using ASCII value showed it on the chart. – Harshit Goel Mar 02 '17 at 11:27
  • @HarshitGoel in my case is same. but in my case value should be shown in the circle. if the value is minimum shown in red . value is middle show in red, value is max shown in yellow. how to achieve this? – Ramkumar chintala Mar 02 '17 at 14:24
  • Actually in that case you have to hinder the library and customise the label at the top of the bar UILabel *headerLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(185, headerView.frame.size.height/3, 15, 15)]; headerLabel2.text =[NSString stringWithFormat:@"%@",variableName] ; headerLabel2.textAlignment = NSTextAlignmentCenter; headerLabel2.textColor = [UIColor whiteColor] ; headerLabel2.backgroundColor=[UIColor redColor]; – Harshit Goel Mar 03 '17 at 04:18

2 Answers2

7

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 ""
        }
    }
}
Harshit Goel
  • 679
  • 1
  • 6
  • 20
2

For swift 2x //add this to your class

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

        default: return "\(number)"
        }
    }

}

and then

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