如何绘制与BarChartView的Bar平行的文本?

时间:2018-12-07 10:12:31

标签: ios iphone swift charts bar-chart

我正在尝试实现平行于酒吧显示每月获利和损失百分比的功能。请提供任何建议。

我正在使用danielgindi/Charts

Bar Chart with parallel Label

我正在尝试实现这种功能。 这是图表设置代码:

    chartView.legend.enabled = true
    chartView.chartDescription?.enabled = false
    chartView.maxVisibleCount = 12
    chartView.drawBarShadowEnabled = false
    chartView.drawValueAboveBarEnabled = false
    chartView.delegate = self
    chartView.dragEnabled = true
    chartView.doubleTapToZoomEnabled = false
    chartView.pinchZoomEnabled = true
    chartView.noDataTextColor  = .black
    chartView.highlightFullBarEnabled = true
    chartView.drawGridBackgroundEnabled = false

    chartView.leftAxis.enabled = false
    chartView.leftAxis.drawGridLinesEnabled = false

    chartView.rightAxis.enabled = false
    chartView.rightAxis.drawGridLinesEnabled = false

    chartView.xAxis.enabled = true
    chartView.xAxis.drawGridLinesEnabled = false
    chartView.xAxis.drawAxisLineEnabled = false
    chartView.xAxis.labelPosition = .bottom
    chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
    chartView.xAxis.setLabelCount(months.count, force: false)
    chartView.xAxis.granularity = 1

    let l = chartView.legend
    l.horizontalAlignment = .center
    l.verticalAlignment = .bottom
    l.orientation = .horizontal
    l.drawInside = false
    l.xEntrySpace = 5
    l.yEntrySpace = 2
    l.yOffset = 5
    l.xOffset = 20

在这里,我将值分配给图表

 let yVals = (0..<data.count).map { (i) -> BarChartDataEntry in
        let val1 = Double(data[i].ownopportunityOwn.roundToOneDigitCroreValue)
        let val2 = Double(data[i].ownopportunityAssistby.roundToOneDigitCroreValue)
        return BarChartDataEntry(x: Double(i), yValues: [val1, val2])
    }

    let set = BarChartDataSet(values: yVals, label: "")
    set.drawIconsEnabled = false
    set.colors = [AppColors.Graph_LightGreenColor,AppColors.Graph_PurpleColor]
    set.roundedCorners = [.topLeft,.topRight]

    let data = BarChartData(dataSet: set)
    data.setValueFont(.systemFont(ofSize: 7, weight: .bold))
    data.setValueFormatter(DefaultValueFormatter(formatter: formatter))
    data.setValueTextColor(.white)
    chartView.fitBars = true

    chartView.data = data

    var array = [LegendEntry]()

    var legendValueArray = [(title: DashboardConstant.K_OWN_WINS.value, Color: AppColors.Graph_LightGreenColor),(title: DashboardConstant.K_ASSISTED_BY_WIN.value, Color: AppColors.Graph_PurpleColor)]

    for object in legendValueArray {
        array.append( LegendEntry(label: object.title, form: .square, formSize: 10, formLineWidth: 1, formLineDashPhase: 1, formLineDashLengths: nil, formColor: object.Color))
    }
    self.chartView.legend.setCustom(entries: array)
    self.chartView.fitScreen()
    self.chartView.notifyDataSetChanged()
    chartView.animate(xAxisDuration: 3, yAxisDuration: 3)

0 个答案:

没有答案