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

Having the area below a line graph be gradient layer instead of background color #186

Closed
BrandonShega opened this issue Jul 2, 2015 · 21 comments
Labels

Comments

@BrandonShega
Copy link

Is it possible to have the area underneath a line chart to be a gradient layer as opposed to just a solid color? We were also wondering if on a line chart if we could add a drop shadow below the line. Thanks!

@danielgindi
Copy link
Collaborator

@BrandonShega Set the backgroundColor to UIColor.transparentColor and opaque to false

@BrandonShega
Copy link
Author

But won't that just leave it entirely transparent rather than fading from a color to transparent?

@danielgindi
Copy link
Collaborator

@BrandonShega The background gradient is your job. You can even put your girlfriend's face there if you want. Put whatever you want behind the chart.

@BrandonShega
Copy link
Author

No, no, I don't mean behind the chart, I just mean the area below the actual line graph. It's hard to explain here are some images that maybe will help.

Gradient Example:
gradient

Shadow Example:
shadow

@danielgindi
Copy link
Collaborator

@BrandonShega You mean the fill color. That's not supported yet, but it's planned.

@BrandonShega
Copy link
Author

Ok great, thank you so much! Loving the library, I added drop shadow to the circles and lines for line graph, I'll have to make a pull request here some day.

@Halkyonbird
Copy link

Nice library, but really it would be cool to add fill color gradient support!

@fkosec
Copy link

fkosec commented Jan 18, 2016

It would be awesome if this got added to the project.

@danielgindi
Copy link
Collaborator

Yes friends, this happened :-)

@rubenwardy
Copy link

For future readers, this is the fill property on LineDataSets (sorry for bumping, this is the only result I found)

Swift

datset.fill = ChartFill.fill(withColor: color.withAlphaComponent(0.8))

Objective C

dataset.fill = [ChartFill fillWithColor:[color colorWithAlphaComponent:0.8]];

@timotiuserick
Copy link

is it added yet ?
need it so much :(

@romanbaitaliuk
Copy link

romanbaitaliuk commented May 16, 2018

@timotiuserick
let coloTop = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.8).cgColor
let colorBottom = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0.1).cgColor
let gradientColors = [coloTop, colorBottom] as CFArray // Colors of the gradient
let colorLocations:[CGFloat] = [0.7, 0.0] // Positioning of the gradient
let gradient = CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: gradientColors, locations: colorLocations) // Gradient Object
lineChartDataSet.fill = Fill.fillWithLinearGradient(gradient!, angle: 90.0) // Set the Gradient
lineChartDataSet.drawFilledEnabled = true // Draw the Gradient

@timotiuserick
Copy link

@Davigr wow Thank you so much!!

@getabhishekified
Copy link

let gradientColors = [UIColor(red: 141, green: 133, blue: 220, alpha: 0), UIColor(red: 230, green: 155, blue: 210, alpha: 0)] as CFArray // Colors of the gradient
let colorLocations:[CGFloat] = [1.0, 0.0] // Positioning of the gradient
let gradient = CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: gradientColors, locations: colorLocations)
line1.fill = Fill.fillWithLinearGradient(gradient!, angle: 90.0) // Set the Gradient
line1.drawFilledEnabled = true /

this is not working!

@romanbaitaliuk
Copy link

Hey @abhiccc5
Check it out my chart view with all configs
https://github.com/davigr/BLEDemo/blob/master/BLEDemo/Application/Details/RSSIChartView.swift

There are 2 methods that sets gradients 'getGradientFilling', 'setGradientBackground'

@getabhishekified
Copy link

// Setting fill gradient color
let coloTop = UIColor(red: 141/255, green: 133/255, blue: 220/255, alpha: 0.8).cgColor
let colorBottom = UIColor(red: 230/255, green: 155/255, blue: 210/255, alpha: 0.1).cgColor
// Colors of the gradient
let gradientColors = [coloTop, colorBottom] as CFArray
// Positioning of the gradient
let colorLocations: [CGFloat] = [0.7, 0.0]
// Gradient Object
let gradient = CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: gradientColors, locations: colorLocations)!
line1.fill = Fill.fillWithLinearGradient(gradient, angle: 90.0)
line1.drawFilledEnabled = true

Got it working correctly! Thanks to @Davigr 👍

@Aanchal96
Copy link

@BrandonShega Hey! Can you please help me in adding the drop shadow under the line and circles as shown in the pictures you shared.

@getabhishekified
Copy link

Hey @Aanchal96 , use this code snippet to get shadow under the line and circles in your graph.


func setChart(dataPoints: [String], values: [Double]) {
mChart.noDataText = "No data available!"
for i in 0..<values.count {
print("chart point : (values[i])")
let dataEntry = ChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let line1 = LineChartDataSet(values: dataEntries, label: "Units Consumed")
line1.colors = [NSUIColor.blue]
line1.mode = .cubicBezier
line1.cubicIntensity = 0.2

    let gradient = getGradientFilling()
    line1.fill = Fill.fillWithLinearGradient(gradient, angle: 90.0)
    line1.drawFilledEnabled = true

    let data = LineChartData()
    data.addDataSet(line1)
    mChart.data = data
    mChart.setScaleEnabled(false)
    mChart.animate(xAxisDuration: 1.5)
    mChart.drawGridBackgroundEnabled = false
    mChart.xAxis.drawAxisLineEnabled = false
    mChart.xAxis.drawGridLinesEnabled = false
    mChart.leftAxis.drawAxisLineEnabled = false
    mChart.leftAxis.drawGridLinesEnabled = false
    mChart.rightAxis.drawAxisLineEnabled = false
    mChart.rightAxis.drawGridLinesEnabled = false
    mChart.legend.enabled = false
    mChart.xAxis.enabled = false
    mChart.leftAxis.enabled = false
    mChart.rightAxis.enabled = false
    mChart.xAxis.drawLabelsEnabled = false
    
}

/// Creating gradient for filling space under the line chart
private func getGradientFilling() -> CGGradient {
    // Setting fill gradient color
    let coloTop = UIColor(red: 141/255, green: 133/255, blue: 220/255, alpha: 1).cgColor
    let colorBottom = UIColor(red: 230/255, green: 155/255, blue: 210/255, alpha: 1).cgColor
    // Colors of the gradient
    let gradientColors = [coloTop, colorBottom] as CFArray
    // Positioning of the gradient
    let colorLocations: [CGFloat] = [0.7, 0.0]
    // Gradient Object
    return CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: gradientColors, locations: colorLocations)!
}

@cerupcat
Copy link

@abhishekgautam101 is this supposed to create a drop shadow? I copied your code but it justs created a basic gradient - not a drop shadow. Are drop shadows supported?

@pradweep
Copy link

@danielgindi Hi daniel I need some help regarding the filling gradient color for the linecharts. I am struggling hard to how to fill gradient colors for the different data sets as shown in the below screenshot. Can you please provide me the resolution asap.
Screenshot 2020-05-30 at 10 03 21 PM

@dattatrayParthenon
Copy link

@pradweep you got any solution to your problem?

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

No branches or pull requests