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

Line Chart with callouts #2058

Closed
HPRaval opened this issue Jan 10, 2017 · 7 comments
Closed

Line Chart with callouts #2058

HPRaval opened this issue Jan 10, 2017 · 7 comments

Comments

@HPRaval
Copy link

HPRaval commented Jan 10, 2017

I want to show only max values in multiple line charts, like below. But i don't know how can i achieve this.

screen shot 2017-01-10 at 6 39 41 pm

@liuxuan30
Copy link
Member

I don't know what's max value from your screenshot. But you can override drawValues() to only draw the 'max value'

@HPRaval
Copy link
Author

HPRaval commented Jan 11, 2017

Thanx for replay.Sorry but i can't find darValues(). Can you please give me some example? I am using Charts 2.3.0.

@liuxuan30
Copy link
Member

liuxuan30 commented Jan 12, 2017

you would find it if you simply search drawValues in LineChartRenderer

@HPRaval
Copy link
Author

HPRaval commented Jan 12, 2017

Thanx...i have created a custom class CustomLineLinear which extends LineChartRenderer

import UIKit
import Charts

class CustomLineLinear : LineChartRenderer {
    
    
    
    override func drawValues(context context: CGContext) {
        guard let
            dataProvider = dataProvider,
            lineData = dataProvider.lineData,
            animator = animator
            else { return }
        
        if (CGFloat(lineData.yValCount) < CGFloat(dataProvider.maxVisibleValueCount) * viewPortHandler.scaleX)
        {
            var dataSets = lineData.dataSets
            
            let phaseX = max(0.0, min(1.0, animator.phaseX))
            let phaseY = animator.phaseY
            
            var pt = CGPoint()
            
            for i in 0 ..< dataSets.count
            {
                guard let dataSet = dataSets[i] as? ILineChartDataSet else { continue }
                
                if !dataSet.isDrawValuesEnabled || dataSet.entryCount == 0
                {
                    continue
                }
                
                let valueFont = dataSet.valueFont
                
                guard let formatter = dataSet.valueFormatter else { continue }
                
                let trans = dataProvider.getTransformer(dataSet.axisDependency)
                let valueToPixelMatrix = trans.valueToPixelMatrix
                
                // make sure the values do not interfear with the circles
                var valOffset = Int(dataSet.circleRadius * 1.75)
                
                if (!dataSet.isDrawCirclesEnabled)
                {
                    valOffset = valOffset / 2
                }
                
                let entryCount = dataSet.entryCount
                
                guard let
                    entryFrom = dataSet.entryForXIndex(self.minX < 0 ? 0 : self.minX, rounding: .Down),
                    entryTo = dataSet.entryForXIndex(self.maxX, rounding: .Up)
                    else { continue }
                
                var diff = (entryFrom == entryTo) ? 1 : 0
                if dataSet.mode == .CubicBezier
                {
                    diff += 1
                }
                
                let minx = max(dataSet.entryIndex(entry: entryFrom) - diff, 0)
                let maxx = min(max(minx + 2, dataSet.entryIndex(entry: entryTo) + 1), entryCount)
                
                for j in minx.stride(to: Int(ceil(CGFloat(maxx - minx) * phaseX + CGFloat(minx))), by: 1)
                {
                    guard let e = dataSet.entryForIndex(j) else { break }
                    
                    pt.x = CGFloat(e.xIndex)
                    pt.y = CGFloat(e.value) * phaseY
                    pt = CGPointApplyAffineTransform(pt, valueToPixelMatrix)
                    
                    if (!viewPortHandler.isInBoundsRight(pt.x))
                    {
                        break
                    }
                    
                    if (!viewPortHandler.isInBoundsLeft(pt.x) || !viewPortHandler.isInBoundsY(pt.y))
                    {
                        continue
                    }
                    
                    ChartUtils.drawText(context: context,
                                        text: formatter.stringFromNumber(e.value)!,
                                        point: CGPoint(
                                            x: pt.x,
                                            y: pt.y - CGFloat(valOffset) - valueFont.lineHeight),
                                        align: .Center,
                                        attributes: [NSFontAttributeName: valueFont, NSForegroundColorAttributeName: dataSet.valueTextColorAt(j)])
                  
                }
            }
        }

    }
}

In this i have removed my custom login its just copy and paste, and in my line chart view controller

self.lineChartView.renderer = CustomLineLinear(dataProvider: self.lineChartView,animator:nil,viewPortHandler: self.lineChartView.viewPortHandler)
        self.lineChartView.data = data

But when i run this code, it shows me nothing

@liuxuan30
Copy link
Member

Are you sure your CustomLineLinear is being used and work?
I would normally replace it in subclassing LineChartView

@HPRaval
Copy link
Author

HPRaval commented May 26, 2017

Hi, Now i have moved to swift 3.0, but i am facing some issue with customise CustomLineLinear.

lineChart.renderer = CustomLineLinear(dataProvider: lineChart, animator: nil, viewPortHandler: lineChart.viewPortHandler)

And in my custom class

guard
let dataProvider = dataProvider,
let lineData = dataProvider.lineData,
let animator = animator, //this is nil always
let viewPortHandler = self.viewPortHandler
else { return }

This is working fine with swift 2.3.

@liuxuan30
Copy link
Member

I suggest add breakpoints to debug, I am not sure what's going on

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

2 participants