ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.52k stars 5.99k forks source link

setViewPortOffsets does not work on Horizontal BarChartView #2853

Open Schabaani opened 6 years ago

Schabaani commented 6 years ago

I want to create offset for horizontalBarChart, However, it does not work.

           let ys1 = [model.totalIncome, abs(model.totalExpense)]

            var yse1 = ys1.enumerated().map { x, y -> BarChartDataEntry in
            return BarChartDataEntry(x: Double(x), y: y)
            }

            let count = 2
            let data = BarChartData()
            let ds1 = BarChartDataSet(values: yse1, label: "")
            ds1.colors = [UIColor.red, UIColor.blue]

            // Number formatting of Values
            ds1.valueFormatter = YAxisValueFormatter()
            ds1.valueFont = UIFont(fontType: .H6)!
            ds1.valueTextColor = UIColor.dark_text
            ds1.iconsOffset = CGPoint(x: 350.0, y: 350.0)

            // Data set
            data.addDataSet(ds1)
            horizontalBarChartView.data = data

            // General setting
            horizontalBarChartView.xAxis.drawLabelsEnabled = false
            horizontalBarChartView.setScaleEnabled(false)
            //horizontalBarChartView.zoomToCenter(scaleX: count > 6 ? CGFloat(count) / 6.0 : 1.0, scaleY: 1.0)

            // Grid
            horizontalBarChartView.xAxis.drawGridLinesEnabled = false
            horizontalBarChartView.leftAxis.gridLineDashLengths = [15.0, 15.0]
            horizontalBarChartView.leftAxis.gridColor = UIColor.palette_28
            horizontalBarChartView.rightAxis.drawGridLinesEnabled = false
            horizontalBarChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: ["A","b"])
            horizontalBarChartView.xAxis.granularityEnabled = true
            horizontalBarChartView.xAxis.granularity = 1
            horizontalBarChartView.xAxis.labelPosition = .bottomInside

            // Disable number formatting of leftAxis and rightAxis
            horizontalBarChartView.leftAxis.enabled = false
            horizontalBarChartView.rightAxis.enabled = false

            // setViewPortOffsets
            let digitCount = Int(data.yMax).digitCount
            let leftOffset: CGFloat = digitCount > 2 ? CGFloat((digitCount - 1) * 10) : 10.0
            horizontalBarChartView.setViewPortOffsets(left: leftOffset, top: 200, right: 100, bottom: 100)

            // https://github.com/danielgindi/Charts/issues/2063
            horizontalBarChartView.leftAxis.axisMinimum = 0

            horizontalBarChartView.data?.notifyDataChanged()

I created horizontalBarChartView programmatically

    lazy var horizontalBarChartView: HorizontalBarChartView = {
        let chartView = HorizontalBarChartView()
        chartView.contentMode = .scaleAspectFit
        chartView.drawBordersEnabled = false
        chartView.drawGridBackgroundEnabled = false
        chartView.gridBackgroundColor = UIColor.clear
        chartView.legend.enabled = false
        chartView.chartDescription = nil
        chartView.highlighter = nil
        chartView.clipsToBounds = true
        chartView.translatesAutoresizingMaskIntoConstraints = false
        return chartView
    }()
kanevP commented 6 years ago

Hi, have you tried calling setViewPortOffsets as last thing you do? In general it should be done right after you set new data, but also any calls to notifyDataChanged() might override your offsets.

Schabaani commented 6 years ago

Hi, I have tried setViewPortOffsets as last line. However, it doesn't work and I don't use notifyDataChanged()

liuxuan30 commented 6 years ago

you called horizontalBarChartView.data?.notifyDataChanged(), but did you call horizontalBarChartView.notifyDataSetChanged()?