Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

different Bar color based on values #2342

Closed
gsolanki1509 opened this issue Apr 9, 2017 · 10 comments
Closed

different Bar color based on values #2342

gsolanki1509 opened this issue Apr 9, 2017 · 10 comments

Comments

@gsolanki1509
Copy link

Dear all,

I want to show different colors for bars based on its values.

screenshot_2017-04-09-09-03-06

Can anyone help me please...!!

@gsolanki1509
Copy link
Author

Currently, im doing as below:

func loadBarChart(_ count: Int, range: Double)
{
var yVals = BarChartDataEntry
for i in 0..<count //).reversed()
{
let val = Double((arrBarValues[i]))
yVals.append(BarChartDataEntry(x: Double(i), y: val!))
}

    var set1 = BarChartDataSet()
    
    if barChartView.data == nil
    {
   
        set1.colors = [UIColor(red: 255.0/255.0, green: 245.0/255.0, blue: 118.0/255.0, alpha: 1.0)  ]
        set1.drawValuesEnabled = false
 
        
        var dataSets = [ChartDataSet]()
        dataSets.append(set1)
        let data = BarChartData(dataSets: dataSets)
        
        let groupSpace = 0.1                 // space between bar groups
        let barSpace = 0.09                  // space between bars within a group
        
        data.barWidth = (1.0 - groupSpace) / 1.75 - barSpace         // 2 = number of bars within a group
        data.groupBars(fromX: 0.0, groupSpace: groupSpace, barSpace: barSpace)
        
        let  marker = YMarkerView( color: NSUIColor.black,
                                   font:  NSUIFont.systemFont(ofSize: 12.0),
                                   textColor: NSUIColor.white,
                                   insets: UIEdgeInsets(top: 8.0, left: 0.0, bottom: 8.0, right: 0.0),
                                   yAxisValueFormatter: HourValueFormatter())

        
        
        marker.minimumSize = CGSize(width: CGFloat(80.0), height: CGFloat(40.0))
        barChartView.marker = marker
        
        barChartView.data = data
        barChartView.fitBars = true
        
    }
    else
    {
        set1 = (barChartView.data!.dataSets[0] as! BarChartDataSet )
        set1.values = yVals
        
        barChartView.data?.notifyDataChanged()
        barChartView.notifyDataSetChanged()
        
    }
    
    barChartView.setNeedsDisplay()
}

@aelam
Copy link
Contributor

aelam commented Apr 9, 2017 via email

@gsolanki1509
Copy link
Author

can you please elaborate ???

@gsolanki1509
Copy link
Author

My concern is to show bar color as blue if its timing is after 9:00 AM. how can i get the value of that particular bar in that function?

@gsolanki1509
Copy link
Author

Got it... thanks ..!! :)

@anasimtiaz
Copy link

@gsolanki1509 Would be nice if you can share the solution

@thierryH91200
Copy link
Contributor

@anasimtiaz look at #2334 It is the same principle
func colorPicker(value : Double) -> NSUIColor etc....

@anasimtiaz
Copy link

@thierryH91200 Thanks!

@gsolanki1509
Copy link
Author

Hi @anasimtiaz ,

Please find my code as below:

I'm using an Array for colors "arrBarColors". And then adding colors to it based on chart Values.

func loadBarChart(_ count: Int, range: Double)
{
var yVals = BarChartDataEntry
for i in 0..<count //).reversed()
{
let val = Double((arrBarValues[i]["time"])!)
print(val!)
yVals.append(BarChartDataEntry(x: Double(i), y: val!))
if (Int(arrBarValues[i]["time"]!)!) > 34200 { // 09:30 AM
arrBarColors.append(UIColor(red: 234.0/255.0, green: 23.0/255.0, blue: 38.0/255.0, alpha: 1.0))

        }else if (Int(arrBarValues[i]["time"]!)!) == 28801 { // 08:00 AM
            arrBarColors.append(UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.0))
            
        }else{
            arrBarColors.append(UIColor(red: 255.0/255.0, green: 245.0/255.0, blue: 118.0/255.0, alpha: 1.0))
        }
    }
    
    var set1 = BarChartDataSet()
    
    if barChartView.data == nil
    {
        print(yVals)
        
        set1 = BarChartDataSet(values: yVals, label: "DataSet")
        set1.colors = arrBarColors //[UIColor(red: 255.0/255.0, green: 245.0/255.0, blue: 118.0/255.0, alpha: 1.0)  ]//ChartColorTemplates.vordiplom() 255,245,118
        set1.drawValuesEnabled = false
      
        
        var dataSets = [ChartDataSet]()
        dataSets.append(set1)
        let data = BarChartData(dataSets: dataSets)
        
        let groupSpace = 0.1                 // space between bar groups
        let barSpace = 0.09                  // space between bars within a group
        
        data.barWidth = (1.0 - groupSpace) / 1.75 - barSpace         // 2 = number of bars within a group
        data.groupBars(fromX: 0.0, groupSpace: groupSpace, barSpace: barSpace)
        
        let  marker = YMarkerView( color: NSUIColor.black,
                                   font:  NSUIFont.systemFont(ofSize: 12.0),
                                   textColor: NSUIColor.white,
                                   insets: UIEdgeInsets(top: 8.0, left: 0.0, bottom: 8.0, right: 0.0),
                                   yAxisValueFormatter: HourValueFormatter())
 
        
        
        marker.minimumSize = CGSize(width: CGFloat(80.0), height: CGFloat(40.0))
        barChartView.marker = marker
        
        barChartView.data = data
        barChartView.fitBars = true
        
    }
    else
    {
        set1 = (barChartView.data!.dataSets[0] as! BarChartDataSet )
        set1.values = yVals
        
        barChartView.data?.notifyDataChanged()
        barChartView.notifyDataSetChanged()
        
    }
    
    barChartView.setNeedsDisplay()
}

Hope it helps us :)

@anasimtiaz
Copy link

Thank you. That helped a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants