-2

I am using iOS- Charts, and i created bar chart. I also conformed the delegate so that i can get callbacks when i am tapped on a bar but the problem is when i tapped in the non bar area also the delegate method is getting called and getting highlighted. I am using Swift3.

var mMonths : [String]?
var mAverage : Double?
var mValues : [Double]?
override func viewDidLoad() {
    super.viewDidLoad()
    mMonths = ["A","B","C","D","F","G","H","I","J","K","L","M"]
}

override func viewWillAppear(_ animated: Bool) {
    mAverage = 50
    mValues = [20,40.0,0,50,10,100,55,80,40,10.50,80,35]
    mBarChartView.delegate = self
    setChart(dataPoints: mMonths!, values: mValues!)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// For setting up the data and customizing the Chart
private func setChart(dataPoints :[String] , values : [Double])
{
    var dataEntries : [BarChartDataEntry] = []
    var colors : [UIColor] = []
    let belowAverageColor : UIColor = UIColor.blue
    let aboveAverageColor : UIColor = UIColor.blue.withAlphaComponent(0.5)
    let averageColor : UIColor = UIColor.lightGray

    for i in 0..<dataPoints.count
    {
        let eachValue : Double = values[i]
        var entry : BarChartDataEntry?
        if eachValue == 0
        {
            entry = BarChartDataEntry.init(x: Double(i), yValues: [mAverage!])
            colors.append(averageColor)
        }
        else if eachValue <= mAverage!
        {
            entry = BarChartDataEntry.init(x: Double(i), yValues: [eachValue,mAverage!-eachValue])
            colors.append(belowAverageColor)
            colors.append(averageColor)
        }
        else
        {
            entry = BarChartDataEntry.init(x: Double(i), yValues: [mAverage!,eachValue-mAverage!])
            colors.append(belowAverageColor)
            colors.append(aboveAverageColor)
        }
        dataEntries.append(entry!)
    }

    let dataSet = BarChartDataSet.init(values: dataEntries, label: "")

    // removed value on top of each bar
    dataSet.drawValuesEnabled = false

    // removing the highlight on bar tapped
    dataSet.highlightAlpha = 0


    // assigning colors to bar
    dataSet.colors = colors
    let data = BarChartData(dataSet: dataSet)

    mBarChartView.data = data

    // Skipping labels in between
    mBarChartView.xAxis.setLabelCount(mMonths!.count, force: false)

    // setting data on X axis
    mBarChartView.xAxis.valueFormatter = IndexAxisValueFormatter.init(values: mMonths!)

    // color of labels on xaxis
    mBarChartView.xAxis.labelTextColor = UIColor.black


    // setting maximum value of the graph
    mBarChartView.leftAxis.axisMaximum =  100
    mBarChartView.rightAxis.axisMaximum = 100
    mBarChartView.leftAxis.axisMinimum = 0.0
    mBarChartView.rightAxis.axisMinimum = 0.0

    // removing grid lines
    mBarChartView.leftAxis.drawGridLinesEnabled = false
    mBarChartView.rightAxis.drawGridLinesEnabled = false
    mBarChartView.xAxis.drawGridLinesEnabled = false

    // removing left and right axis
    mBarChartView.leftAxis.enabled = false
    mBarChartView.rightAxis.enabled = false

    // removing bottom line
    mBarChartView.xAxis.drawAxisLineEnabled = false

    // Emptying the description label
    mBarChartView.chartDescription?.text = ""

    // placing the X axis label to bottom
    mBarChartView.xAxis.labelPosition = .bottom

    // bottom information about the bars is hidden
    mBarChartView.legend.enabled = false

    // Disabling the Zooming
    mBarChartView.doubleTapToZoomEnabled = false
    mBarChartView.pinchZoomEnabled = true
    mBarChartView.scaleXEnabled = true
    mBarChartView.scaleYEnabled = false

    mBarChartView.highlightFullBarEnabled = true 
}

Thank you

check this image

  • 1
    you should show your code or third party library if you are using, this is uncompleted information to answer and unclear what are you asking because lake of information you provided! – Ketan Parmar Jun 01 '17 at 06:23

1 Answers1

-1

In the ChartHighlighter.swift file you can change the closestSelectionDetailByPixel function to this:

 internal func closestSelectionDetailByPixel(
    closestValues: [Highlight],
    x: CGFloat,
    y: CGFloat,
    axis: YAxis.AxisDependency?,
    minSelectionDistance: CGFloat) -> Highlight?
{
    var distance = minSelectionDistance
    var closest: Highlight?

    for i in 0 ..< closestValues.count
    {
        let high = closestValues[i]

        if axis == nil || high.axis == axis
        {
            print("high.xPx: \(high.xPx)")
            print("high.x: \(high.x)")
            print("high.yPx: \(high.yPx)")
            print("high.y: \(high.y)")
            print("x: \(x)")
            print("y: \(y)")



            let cDistance = getDistance(x1: x, y1: y, x2: high.xPx, y2: high.yPx)
            print("cDistance: \(cDistance)")
            if cDistance < distance
            {
                closest = high
                distance = cDistance

                //if the y position where user clicks is above the value, return nil
                if(y<high.yPx) {
                    print("returning nil")
                    return nil
                }

            }
        }
    }
    print("closest: \(closest)")
    return closest
}

The only thing I added was this:

 //if the y position where user clicks is above the value, return nil
 if(y<high.yPx) {
    print("returning nil")
    return nil
 }

and the print statements, if you need to do more changes you can use them to understand better how the function works. Otherwise just delete the print statements.

DevB2F
  • 4,674
  • 4
  • 36
  • 60
  • Hi, Thank you for the reply. Is there any way that i cannot change the thing inside the library because multiple people are working on the project so i can't go to every one of them and change the code whenever they install the pods. should i rise a issue in the github so that they modify in the library ? – LOKESH KUMAR PEDDA Jun 03 '17 at 05:42
  • There are two things you can do: 1. Have everyone use the same library files that you are using, but this makes updating the library difficult and is really not a good solution. 2. Create a custom highlighter that includes the changes you need. Then set the highlighter for your mBarChartView as the custom highlighter. Updates won't be a problem and the other coders will only need to add your custom highlighter file to their projects and the charts library it self is not changed in any way. – DevB2F Jun 03 '17 at 16:43