Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"chartValueSelected" is not getting called in customView #2878

Closed
Schabaani opened this issue Oct 14, 2017 · 4 comments
Closed

"chartValueSelected" is not getting called in customView #2878

Schabaani opened this issue Oct 14, 2017 · 4 comments

Comments

@Schabaani
Copy link

Schabaani commented Oct 14, 2017

I saw a lot issues here, however non of them solve my problem.

class LineChartLandscape : UIViewCustom, ChartViewDelegate {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        if UIDevice.current.orientation.isLandscape == false {
            AppHelper.lockOrientation(UIInterfaceOrientationMask.landscape, andRotateTo : .landscapeRight)
        }
        setupViews()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var datasourceItem: Any? {
        didSet {
            guard let model = datasourceItem as? [AccTransactionTrend] else { return }

            let yse1 = model.map { x in
                return ChartDataEntry(x: Double(x.dayOfMonth)!, y: x.incomeAmount)
            }
            let ys2 = Array(1..<10).map { x in return cos(Double(x) / 2.0 / 3.141) }
            

            let yse2 = ys2.enumerated().map { x, y in return ChartDataEntry(x: Double(x), y: y) }
            
            
            let data = LineChartData()
            data.setDrawValues(true)
            
            let ds1 = LineChartDataSet(values: yse1, label: "Hello")
            ds1.colors = [NSUIColor.red]
            ds1.drawCirclesEnabled = true
            ds1.drawValuesEnabled = true
            ds1.circleHoleRadius = 0.4
            let colorSpace = CGColorSpaceCreateDeviceRGB()
            let ds1colours = [UIColor.red.cgColor, UIColor.white.cgColor] as CFArray
            let ds1Gradient = CGGradient(colorsSpace: colorSpace, colors: ds1colours, locations: [1.0, 0.0])
            ds1.fill = Fill(linearGradient: ds1Gradient!, angle: 90.0)
            
            ds1.drawFilledEnabled = true
            ds1.mode = .cubicBezier
            data.addDataSet(ds1)
            
            let ds2 = LineChartDataSet(values: yse2, label: "World")
            ds2.colors = [NSUIColor.blue]
            ds2.drawCirclesEnabled = false
            ds2.drawValuesEnabled = false
            let ds2colours = [UIColor.blue.cgColor, UIColor.white.cgColor] as CFArray
            let ds2Gradient = CGGradient(colorsSpace: colorSpace, colors: ds2colours, locations: [1.0, 0.0])
            ds2.fill = Fill(linearGradient: ds2Gradient!, angle: 90.0)
            
            
            ds2.drawFilledEnabled = true
            ds2.mode = .cubicBezier
            data.addDataSet(ds2)
            self.lineChartView.data = data
            
            self.lineChartView.gridBackgroundColor = NSUIColor.white
            
            self.lineChartView.chartDescription?.text = "Linechart Demo"
            
            // Grid
            lineChartView.xAxis.drawGridLinesEnabled = false
            lineChartView.leftAxis.drawGridLinesEnabled = false
            lineChartView.rightAxis.drawGridLinesEnabled = false
            
            
            // Number formatting of xAxis
            lineChartView.xAxis.drawLabelsEnabled = true
            lineChartView.leftAxis.drawLabelsEnabled = true
            lineChartView.drawMarkers = false
            
            lineChartView.xAxis.labelPosition = .bottom
            lineChartView.xAxis.labelFont = UIFont(fontType: .H5)!
            lineChartView.xAxis.labelTextColor = UIColor.dark_text
            lineChartView.xAxis.granularity = 1
            lineChartView.xAxis.drawLabelsEnabled = true
            lineChartView.data?.highlightEnabled = true

            lineChartView.data?.setDrawValues(false)
          //lineChartView.delegate = self

        }
    }

    
    func setupViews(){
        
        self.backgroundColor = UIColor.white
        
        addSubview(lineChartView)
        
        lineChartView.anchor(topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, topConstant: 0, leftConstant: 10, bottomConstant: 0, rightConstant: 10, widthConstant: self.bounds.width, heightConstant: self.bounds.height)
        
        self.lineChartView.animate(xAxisDuration: 0.5, yAxisDuration: 0.0)
        
    }
    lazy var lineChartView: LineChartView = {
        let chartView = LineChartView()
        chartView.contentMode = .scaleAspectFit
        chartView.drawBordersEnabled = false
        chartView.legend.enabled = false
        chartView.chartDescription = nil
        chartView.highlighter = nil
        chartView.delegate = self
        chartView.clipsToBounds = true
        chartView.translatesAutoresizingMaskIntoConstraints = false
        return chartView
    }()
    
    public func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
        print("Bar selected")
    }
}

chartValueSelected didn't get call. I click on lineChart and it doesn't do any thing. Where is the issue?

@Schabaani
Copy link
Author

Schabaani commented Oct 15, 2017

I've copied all of necessary codes to sample project. Everything works there. There is only one difference between them:

My project:
class LineChartLandscape : UIViewCustom, ChartViewDelegate
Sample code:
class BarChartViewController: UIViewController, ChartViewDelegate

In my project delegate doesn't work. How should I solve this problem?

@Schabaani Schabaani changed the title "chartValueSelected" is not getting called "chartValueSelected" is not getting called in customView Oct 15, 2017
@krispyk84
Copy link

      //lineChartView.delegate = self

If you uncomment this, does it work?

@Schabaani
Copy link
Author

No, I don't why delegate doesn't work on custom view

@liuxuan30
Copy link
Member

liuxuan30 commented Oct 27, 2017

we don't think it's the library problem. You have made some tweaks in your so called "custom views", so you should start from testing your views hit test and gestures if they get delivered to the real chart view. prerequisite knowledge: responder chain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants