gpt4 book ai didi

iOS 图表气泡图颜色

转载 作者:行者123 更新时间:2023-11-28 06:28:59 25 4
gpt4 key购买 nike

如何根据 y 值为 iOS Charts 气泡图中的单个气泡设置特定颜色?就像 iOS Charts GitHub 网站上的其中一张截图所示 here .所以所有顶层的气泡应该是相同的颜色,所有中间的都是另一个,等等......

我已经试过这样改变颜色了:

chartDataSet.colors = [UIColor.red, UIColor.orange, UIColor.green]

但这只会改变每个 x 值的颜色。

完整代码

这是我设置气泡图的完整代码:

func getDetailedAnalytics() {

let currentTeacherDetailedAnalyticsUrl = TEACHER_DETAILED_ANALYTICS_URL + currentTeacherCourseId

Alamofire.request(currentTeacherDetailedAnalyticsUrl).responseJSON { response in
let result = response.result

if let dict = result.value as? Dictionary<String, AnyObject> {

let firstDateValue = dict["0"] as? Dictionary<String, AnyObject>
let secondDateValue = dict["1"] as? Dictionary<String, AnyObject>
let thirdDateValue = dict["2"] as? Dictionary<String, AnyObject>
let fourthDateValue = dict["3"] as? Dictionary<String, AnyObject>
let fifthDateValue = dict["4"] as? Dictionary<String, AnyObject>

let firstDateLow = firstDateValue?["numberValuesUnder50"] as! Int
let firstDateMiddle = firstDateValue?["numberValuesInMiddle"] as! Int
let firstDateHigh = firstDateValue?["numberValuesOver75"] as! Int

let secondDateLow = secondDateValue?["numberValuesUnder50"] as! Int
let secondDateMiddle = secondDateValue?["numberValuesInMiddle"] as! Int
let secondDateHigh = secondDateValue?["numberValuesOver75"] as! Int

let thirdDateLow = thirdDateValue?["numberValuesUnder50"] as! Int
let thirdDateMiddle = thirdDateValue?["numberValuesInMiddle"] as! Int
let thirdDateHigh = thirdDateValue?["numberValuesOver75"] as! Int

let fourthDateLow = fourthDateValue?["numberValuesUnder50"] as! Int
let fourthDateMiddle = fourthDateValue?["numberValuesInMiddle"] as! Int
let fourthDateHigh = fourthDateValue?["numberValuesOver75"] as! Int

let fifthDateLow = fifthDateValue?["numberValuesUnder50"] as! Int
let fifthDateMiddle = fifthDateValue?["numberValuesInMiddle"] as! Int
let fifthDateHigh = fifthDateValue?["numberValuesOver75"] as! Int

self.analyticsData = [25, 62, 88, 25, 62, 88, 25, 62, 88, 25, 62, 88, 25, 62, 88]
self.analyticsAmountsData = [firstDateLow, firstDateMiddle, firstDateHigh, secondDateLow, secondDateMiddle, secondDateHigh, thirdDateLow, thirdDateMiddle, thirdDateHigh, fourthDateLow, fourthDateMiddle, fourthDateHigh, fifthDateLow, fifthDateMiddle, fifthDateHigh]
self.setBubbleChart(dataPoints: self.dateBubble, values: self.analyticsData, amounts: self.analyticsAmountsData)
}
}
}

func setBubbleChart(dataPoints: [Int], values: [Int], amounts: [Int]) {

var dataEntries: [BubbleChartDataEntry] = []

for i in 0..<dataPoints.count {
let dataEntry = BubbleChartDataEntry(x: Double(dataPoints[i]), y: Double(values[i]), size: (CGFloat(amounts[i])))
dataEntries.append(dataEntry)
}

let format = NumberFormatter()
format.generatesDecimalNumbers = false
format.zeroSymbol = ""
let formatter = DefaultValueFormatter(formatter: format)

let chartDataSet = BubbleChartDataSet(values: dataEntries, label: "")
let chartData = BubbleChartData(dataSet: chartDataSet)

bubbleView.doubleTapToZoomEnabled = false
bubbleView.scaleXEnabled = false
bubbleView.scaleYEnabled = false
bubbleView.highlightPerTapEnabled = false
bubbleView.highlightPerDragEnabled = false

let firstLegend = LegendEntry.init(label: "Below 50", form: .default, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: CGFloat.nan, formLineDashLengths: nil, formColor: UIColor.black)
let secondLegend = LegendEntry.init(label: "Between 50 and 75", form: .default, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: CGFloat.nan, formLineDashLengths: nil, formColor: UIColor.black)
let thirdLegend = LegendEntry.init(label: "Over 75", form: .default, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: CGFloat.nan, formLineDashLengths: nil, formColor: UIColor.black)

bubbleView.chartDescription = nil
bubbleView.legend.entries = [firstLegend, secondLegend, thirdLegend]
bubbleView.data = chartData

bubbleView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0)

let xAxis: XAxis = bubbleView.xAxis
xAxis.drawAxisLineEnabled = false
xAxis.drawGridLinesEnabled = false
xAxis.drawLabelsEnabled = false
xAxis.axisMinimum = 0
xAxis.axisMaximum = 10

let leftAxis: YAxis = bubbleView.leftAxis
leftAxis.drawAxisLineEnabled = false
leftAxis.drawGridLinesEnabled = false
leftAxis.setLabelCount(2, force: true)
leftAxis.axisMinimum = 0
leftAxis.axisMaximum = 100

let rightAxis: YAxis = bubbleView.rightAxis
rightAxis.drawAxisLineEnabled = false
rightAxis.drawGridLinesEnabled = false
rightAxis.drawLabelsEnabled = false
}

所有值为 25 的气泡应该是相同的颜色(例如红色),所有值为 62 的气泡应该是相同的颜色(例如橙色),所有值为 88 的气泡应该是相同的颜色(例如绿色) ).

最佳答案

在这里看看我的回答:Different colors for bars in BarChart depend on value

但本质上,您需要编写一个自定义函数,根据值返回 UIColor。

func setColor(value: Double) -> UIColor{

if(value < 30){
return UIColor.red
}
else if(value <= 70 && value >= 30){
return UIColor.orange
}
else if(value > 70){
return UIColor.green
}

else { //In case anything goes wrong
return UIColor.black
}
}

然后像这样设置颜色

chartDataSet.colors.append(setColor(dataValue))

希望这对您有所帮助!

关于iOS 图表气泡图颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40957590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com