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

Description of Bar doesn't appear in Horizontal BarChartView #3756

Closed
vitormeds opened this issue Nov 22, 2018 · 1 comment
Closed

Description of Bar doesn't appear in Horizontal BarChartView #3756

vitormeds opened this issue Nov 22, 2018 · 1 comment

Comments

@vitormeds
Copy link

vitormeds commented Nov 22, 2018

What did you do?

Hi, I'm implementing a HorizontalBarChartView but when I use currency values ​​it does not display the description in front of the bar, when I use values ​​of other types it works normally, does anyone know what might be happening?

What did you expect to happen?

Should display the description in front of the bar

captura de tela 2018-11-22 as 13 59 55

What happened instead?

Does not display description on front of bar

captura de tela 2018-11-22 as 14 00 15

Charts Environment

**Charts version/Branch/Commit Number: 3.2.1
**Xcode version: 10.1
**Swift version: 4.0
**Platform(s) running Charts: iOS 12.1
**macOS version running Xcode: 10.14.1

Demo Project

`class ChartView: UIView, ChartViewDelegate, IAxisValueFormatter {

var chart: Chart!
var isAnimation: Bool!

func fillWith(chart: Chart, isAnimation: Bool) {
    self.chart = chart
    self.isAnimation = isAnimation
    titleLabel.text = chart.title
    setChart()
}

lazy var titleLabel: UILabel = {
    let label = UILabel()
    label.backgroundColor = UIColor.clear
    label.textColor = UIColor.blue

    label.sizeToFit()
    label.numberOfLines = 2
    label.font = UIFont.boldSystemFont(ofSize: 22)
    label.textAlignment = .left
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
}()

let barChartView: HorizontalBarChartView = {
    let barChartView = HorizontalBarChartView()
    barChartView.isUserInteractionEnabled = false
    barChartView.gridBackgroundColor = UIColor.lightGray
    barChartView.tintColor = UIColor.lightGray
    barChartView.xAxis.labelTextColor = UIColor.lightGray
    barChartView.leftAxis.gridColor = UIColor.lightGray
    barChartView.leftAxis.gridLineWidth = 0.2
    barChartView.leftAxis.labelTextColor = UIColor.lightGray
    barChartView.translatesAutoresizingMaskIntoConstraints = false
    return barChartView
}()



override init(frame: CGRect) {
    super.init(frame: frame)
    
    translatesAutoresizingMaskIntoConstraints = false
    backgroundColor = UIColor.clear
    setupView()
    setupChart()
}

fileprivate func setupView() {
    
    let lineView = UIView()
    lineView.backgroundColor = UIColor(hex: "F0F1F6")
    lineView.translatesAutoresizingMaskIntoConstraints = false
    
    addSubview(lineView)
    lineView.heightAnchor.constraint(equalToConstant: 3).isActive = true
    lineView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
    lineView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
    lineView.topAnchor.constraint(equalTo: topAnchor).isActive = true
    
    addSubview(titleLabel)
    titleLabel.heightAnchor.constraint(equalTo: titleLabel.heightAnchor).isActive = true
    titleLabel.topAnchor.constraint(equalTo: topAnchor, constant: 16).isActive = true
    titleLabel.leftAnchor.constraint(equalTo: leftAnchor, constant: 32).isActive = true
    titleLabel.rightAnchor.constraint(equalTo: rightAnchor, constant: -16).isActive = true
    
    addSubview(barChartView)
    barChartView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
    barChartView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
    barChartView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 16).isActive = true
    barChartView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
}

fileprivate func setupChart() {
    
    barChartView.extraRightOffset = CGFloat(90.0)
    
    barChartView.delegate = self
    barChartView.drawBarShadowEnabled = false
    barChartView.xAxis.valueFormatter = self
    
    barChartView.chartDescription?.text = ""
    barChartView.xAxis.labelPosition = .bottom
    barChartView.legend.enabled = false
    barChartView.legend.horizontalAlignment = .center
    barChartView.legend.verticalAlignment = .top
    
    barChartView.xAxis.granularity = 1
    barChartView.drawValueAboveBarEnabled = true
    
    barChartView.scaleYEnabled = true
    barChartView.scaleXEnabled = true
    
    barChartView.pinchZoomEnabled = false
    barChartView.doubleTapToZoomEnabled = false
    
    barChartView.rightAxis.enabled = false
    barChartView.drawMarkers = true
    
    barChartView.leftAxis.enabled = true
    barChartView.leftAxis.granularityEnabled = true
    barChartView.leftAxis.granularity = 1.0
    barChartView.leftAxis.axisLineColor = UIColor.clear
    
    barChartView.xAxis.drawGridLinesEnabled = false
    
    barChartView.xAxis.spaceMin = 0.5
    barChartView.xAxis.spaceMax = 0.5
    barChartView.leftAxis.valueFormatter = OtherValueFormatter()
}

func stringForValue(_ value: Double, axis: AxisBase?) -> String {
    return chart.data[Int(value)].label
}

fileprivate func setChart() {
    
    var resultEntries = [BarChartDataEntry]()
    var x = 0.0
    for chartData in chart.data {
        
        let result = BarChartDataEntry(x: x, y: chartData.value)
        resultEntries.append(result)
        x += 1.0
    }
    let barChartDataSet = BarChartDataSet(values: resultEntries, label: nil)
    barChartDataSet.colors = [chart.color]
    barChartDataSet.valueTextColor = UIColor.black
    barChartDataSet.valueFont = UIFont.systemFont(ofSize: 11)
    barChartDataSet.highlightEnabled = false
    
    barChartView.xAxis.labelCount = chart.data.count
    
    let barChartData = BarChartData(dataSet: barChartDataSet)
    let baseValueFormatter = BaseValueFormatter(chartDatas: chart.data)
    barChartDataSet.valueFormatter = baseValueFormatter
    
    barChartView.data = barChartData
    
    if isAnimation {
        barChartView.animate(xAxisDuration: 1.0, yAxisDuration: 1.0)
    }
}


required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

class BaseValueFormatter: NSObject, IValueFormatter {

var chartDatas: [ChartData]!

convenience init(chartDatas: [ChartData]) {
    self.init()
    self.chartDatas = chartDatas
}

public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
    
    return chartDatas.filter( { $0.value == value }).first!.formattedValue
}

}

open class OtherValueFormatter: NSObject, IAxisValueFormatter, IValueFormatter {

public func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
    
    return formatDouble(value: value)
}

public func stringForValue(_ value: Double, axis: AxisBase?) -> String{
    
    return formatDouble(value: value)
}

func formatDouble(value:Double) -> String {
    let newValue = abs(value)
    let sinal = value < 0 ? "-" : ""
    
    if newValue == 0 {
        
        return ""
    }else if newValue < 1000 {
        if newValue.truncatingRemainder(dividingBy: 1) == 0 {
            return sinal + String(Int(newValue))
        }else{
            return sinal + String(Int(newValue.roundTo(places: 0)))
        }
    }else if newValue < 1000000 {
        
        if newValue.truncatingRemainder(dividingBy: 1000) == 0 {
            return sinal + String(Int(newValue / 1000)) + "K"
        }else{
            return sinal + String(Int((newValue/1000).roundTo(places: 0))) + "K"
        }
    } else if newValue.truncatingRemainder(dividingBy: 1000000) == 0 {
        return sinal + String(Int(newValue / 1000000)) + "M"
    }else {
        return sinal + String(Int((newValue/1000000).roundTo(places: 0))) + "M"
    }
}

}
`

`class Chart {

var color: UIColor
var title: String
var type: String
var data = [ChartData]()

init(json: JSON, color: UIColor) {
    
    self.color = color
    self.title = json["title"].stringValue
    self.type = json["type"].stringValue
    
    for dataJson in json["data"].arrayValue.reversed() {
        
        let chartData = ChartData(json: dataJson)
        data.append(chartData)
    }
}

}

struct ChartData {

var label: String
var value: Double
var formattedValue: String

init(json: JSON) {
    
    self.label = json["label"].stringValue
    self.value = json["value"].doubleValue
    self.formattedValue = json["formattedValue"].stringValue
}

}
`

@liuxuan30
Copy link
Member

please try out ChartsDemo first. I cannot assure if it's your setup issue or a bug.

        chartView.chartDescription?.enabled = true
        chartView.chartDescription?.text = "hello world"

I do see a small text with ChartsDemo

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