Saturday, December 3, 2022
HomeiOS Developmentios - danielgindi/Charts the best way to get xLabel place

ios – danielgindi/Charts the best way to get xLabel place


I used to be in a position to get the place of the x Label from xAxisRenderer. However I did not import this into my undertaking. I am attempting to get this place and create a customized x-axis. How can I retailer a price in an exterior module and use it in an inner module?

 @objc open func drawLabels(context: CGContext, pos: CGFloat, anchor: CGPoint)
{
    guard let transformer = self.transformer else { return }
    
    let paraStyle = ParagraphStyle.default.mutableCopy() as! MutableParagraphStyle
    paraStyle.alignment = .heart
    
    let labelAttrs: [NSAttributedString.Key : Any] = [.font: axis.labelFont,
                                                     .foregroundColor: axis.labelTextColor,
                                                     .paragraphStyle: paraStyle]

    let labelRotationAngleRadians = axis.labelRotationAngle.DEG2RAD
    let isCenteringEnabled = axis.isCenterAxisLabelsEnabled
    let valueToPixelMatrix = transformer.valueToPixelMatrix

    var place = CGPoint.zero
    var labelMaxSize = CGSize.zero
    
    if axis.isWordWrapEnabled
    {
        labelMaxSize.width = axis.wordWrapWidthPercent * valueToPixelMatrix.a
    }
    
    let entries = axis.entries
    
    for i in entries.indices
    {
        let px = isCenteringEnabled ? CGFloat(axis.centeredEntries[i]) : CGFloat(entries[i])
        place = CGPoint(x: px, y: 0)
            .making use of(valueToPixelMatrix)

        guard viewPortHandler.isInBoundsX(place.x) else { proceed }
        
        let label = axis.valueFormatter?.stringForValue(axis.entries[i], axis: axis) ?? ""
        let labelns = label as NSString
        
        if axis.isAvoidFirstLastClippingEnabled
        {
            // keep away from clipping of the final
            if i == axis.entryCount - 1 && axis.entryCount > 1
            {
                let width = labelns.boundingRect(with: labelMaxSize, choices: .usesLineFragmentOrigin, attributes: labelAttrs, context: nil).measurement.width
                
                if width > viewPortHandler.offsetRight * 2.0,
                    place.x + width > viewPortHandler.chartWidth
                {
                    place.x -= width / 2.0
                }
            }
            else if i == 0
            { // keep away from clipping of the primary
                let width = labelns.boundingRect(with: labelMaxSize, choices: .usesLineFragmentOrigin, attributes: labelAttrs, context: nil).measurement.width
                place.x += width / 2.0
            }
        }
        
        print(place.x)
        
        drawLabel(context: context,
                  formattedLabel: label,
                  x: place.x,
                  y: pos,
                  attributes: labelAttrs,
                  constrainedTo: labelMaxSize,
                  anchor: anchor,
                  angleRadians: labelRotationAngleRadians)
    }
}

I attempted to get the place worth by creating an open class referred to as XAxisPosition, however the worth is initialized to 0 within the strategy of importing it into my undertaking module. I’ve entry to it, however how do I get the values ​​absolutely into my undertaking?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments