在iOS图表中点击堆积条形图条形图的一侧

时间:2017-06-01 06:11:08

标签: ios charts swift3 ios-charts

我正在使用iOS-图表,我创建了条形图。我也符合代表,这样当我点击一个栏时我就可以得到回调,但问题是当我点击非栏区域时,委托方法也会被调用并突出显示。我正在使用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 
}

谢谢

check this image

1 个答案:

答案 0 :(得分:-1)

在ChartHighlighter.swift文件中,您可以将nearestSelectionDetailByPixel函数更改为:

 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
}

我添加的唯一内容是:

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

和print语句,如果需要进行更多更改,可以使用它们来更好地理解函数的工作原理。否则只需删除打印语句。