2016-12-04 93 views
1

如何根据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的气泡应该是相同的颜色(例如绿色)。

+0

你需要展示你已经完成了一些代码 – RyanM

+0

@RyanM请看编辑 – OSX55

回答

1

看看我的答案在这里: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)) 

希望这有助于!

+0

这只是改变每列的颜色而不是每行 – OSX55

+0

你能告诉我你如何设置你的完整代码吗?气泡图? –

+0

请看看编辑 – OSX55