如何根據y值爲iOS圖表氣泡圖中的單個氣泡設置特定顏色?就像它在iOS Charts GitHub網站here上的屏幕截圖中顯示的一樣。 所以所有頂級氣泡應該是相同的顏色,所有中間的人的另一個,等等iOS圖表氣泡圖顏色
我已經嘗試改變顏色那樣:
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的氣泡應該是相同的顏色(例如綠色)。
你需要展示你已經完成了一些代碼 – RyanM
@RyanM請看編輯 – OSX55