我试图根据当月的当前日期为X轴上的标签设置不同的颜色,但是我似乎无法单独配置它们。我还试图使网格线的中线带有破折号,而天花板线则不带破折号。这在左轴上。但是我似乎也不能单独设置它。

目前情况如何:

ios - IOS图表仅在中间突出显示X轴上的特定标签和虚线网格-LMLPHP

我要实现的目标:

ios - IOS图表仅在中间突出显示X轴上的特定标签和虚线网格-LMLPHP

到目前为止,我的代码:

func setBarChart(dataPoints: [String], values: [String]){
    barChartView.noDataTextColor = UIColor.white
    barChartView.noDataText = "No data for the chart"
    barChartView.backgroundColor = UIColor.white

    for i in 0..<dataPoints.count {
        let dataPoint = BarChartDataEntry(x: Double(i), y: Double(values[i])!)
        dataEntry.append(dataPoint)
    }

    let chartDataSet = BarChartDataSet(values: dataEntry, label: "BPM")
    let chartData =  BarChartData()
    chartData.addDataSet(chartDataSet)
    chartData.setDrawValues(false)
    chartData.barWidth = Double(0.5)
    chartDataSet.colors = [UIColor(red: 0, green: 158/255, blue: 237/255, alpha: 1)]


    let formatter: ChartFormatter = ChartFormatter ()
    formatter.setValues(values: dataPoints)
    let floatValue: [CGFloat] = [4,4]
    barChartView.xAxis.valueFormatter = formatter
    barChartView.xAxis.labelTextColor = UIColor(red: 141/255, green: 141/255, blue: 141/255, alpha: 1)
    barChartView.getAxis(.right).labelTextColor = UIColor(red: 141/255, green: 141/255, blue: 141/255, alpha: 1)
    barChartView.xAxis.axisLineColor = UIColor.clear
    barChartView.getAxis(.right).axisLineColor = UIColor.clear
    barChartView.getAxis(.left).axisLineColor = UIColor.clear
    barChartView.xAxis.labelPosition = .bottom
    barChartView.xAxis.drawGridLinesEnabled = false
    barChartView.xAxis.drawLabelsEnabled = true
    barChartView.chartDescription?.enabled = false
    barChartView.legend.enabled = false
    barChartView.rightAxis.enabled = true
    barChartView.rightAxis.labelCount = Int(2)
    barChartView.rightAxis.drawGridLinesEnabled = false
    barChartView.leftAxis.drawGridLinesEnabled = true
    barChartView.leftAxis.gridColor = UIColor(red: 235/255, green: 235/255, blue: 235/255, alpha: 1)
    barChartView.leftAxis.gridLineWidth = CGFloat(1.5)
    barChartView.leftAxis.gridLineDashLengths = floatValue
    barChartView.leftAxis.drawLabelsEnabled = false
    barChartView.data = chartData
}

最佳答案

该功能在库中不可用。

  • https://github.com/danielgindi/Charts/issues/3317
  • https://github.com/danielgindi/Charts/issues/2925


  • 但是,这并不能阻止您自己实现它,甚至不能将其添加到库中。

    首先,看看具有drawLabels(context:pos:anchor:)XAxisRenderer

    在这里,它基本上循环遍历xAxis.entries并通过调用drawLabel(context:formattedLabel:x:y:attributes:constrainedToSize:anchor:angleRadians:)绘制每个标签,并将其传递给labelAttrs,该labelAttrs具有与要应用的颜色有关的信息。
    但是目前... xAxis.labelTextColor是在循环之前设置的,其中一种颜色是从labelAttrs[NSAttributedString.Key.foregroundColor]引用的。

    此处的关键是将xAxis.valueFormatter设置为相对于条目的颜色,例如,在此行附近:
    let label = xAxis.valueFormatter?.stringForValue(xAxis.entries[i], axis: xAxis) ?? ""
    

    如果颜色也要由IAxisValueFormatter提供,那可能是最好的,这反过来又需要更改ojit_code才能提供颜色。

    希望这对您有所帮助。

    关于ios - IOS图表仅在中间突出显示X轴上的特定标签和虚线网格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54778065/

    10-12 04:29