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

Added bar gradient #3225

Closed
wants to merge 16 commits into from
Closed

Added bar gradient #3225

wants to merge 16 commits into from

Conversation

alekzuz
Copy link

@alekzuz alekzuz commented Jan 29, 2018

Added gradients for the vertical bars #2722 #1065

Usage:

dataSet.barGradientColors = [[color1, color2], [color3, color4]]

simulator screen shot - iphone x - 2018-01-29 at 16 58 00

At the same time made minor refactoring: removed isSingleColor in BarChartRenderer.
Test case: iPhone 6s, iOS 11.1, 100 xValues, 100 yValues, single color.
Average execution time of the drawDataSet of 10 attempts:
0.00160739421844482 seconds with isSingleColor
0.00167040824890137 seconds without

@codecov-io
Copy link

codecov-io commented Jan 29, 2018

Codecov Report

Merging #3225 into 4.0.0 will decrease coverage by 0.01%.
The diff coverage is 31.91%.

Impacted file tree graph

@@            Coverage Diff             @@
##            4.0.0    #3225      +/-   ##
==========================================
- Coverage   31.49%   31.47%   -0.02%     
==========================================
  Files         112      112              
  Lines       10050    10091      +41     
==========================================
+ Hits         3165     3176      +11     
- Misses       6885     6915      +30
Impacted Files Coverage Δ
Source/Charts/Utils/Platform.swift 0% <ø> (ø) ⬆️
.../Charts/Renderers/HorizontalBarChartRenderer.swift 0% <0%> (ø) ⬆️
Source/Charts/Utils/ChartColorTemplates.swift 25.31% <0%> (-0.83%) ⬇️
Source/Charts/Renderers/BarChartRenderer.swift 51.56% <35.13%> (-1.15%) ⬇️
...ata/Implementations/Standard/BarChartDataSet.swift 67.39% <66.66%> (-0.06%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5689a1c...a65a3e7. Read the comment docs.

@Trinitron
Copy link

@alekzuz works fine, thanx!

@jjatie jjatie added this to the 4.0.0 milestone Feb 3, 2018
@liuxuan30
Copy link
Member

liuxuan30 commented Feb 16, 2018

Thanks for your help!
Are you able to apply gradient to horizontal bar charts?

Currently master won't accept any feature PRs until 3.1 is released.
can we target this to 4.0 branch? @jjatie

@alekzuz alekzuz changed the base branch from master to 4.0.0 February 16, 2018 06:46
@alekzuz alekzuz changed the base branch from 4.0.0 to master February 16, 2018 06:52
@alekzuz
Copy link
Author

alekzuz commented Feb 16, 2018

Are you able to apply gradient to horizontal bar charts?

Yes, I can do this.

Currently master won't accept any feature PRs until 3.1 is released.
can we target this to 4.0 branch?

I have to open new pull request or to change target branch and resolve conflicts ?

@liuxuan30
Copy link
Member

I have to open new pull request or to change target branch and resolve conflicts ?

@alekzuz seems you don't have to, just edit the title to see if you can change the base branch, at least I can see it.

@jjatie jjatie changed the base branch from master to 4.0.0 February 20, 2018 23:30
@jjatie
Copy link
Collaborator

jjatie commented Feb 20, 2018

The issue with this is there are two ways to draw the gradient, option 1 is how you have it where the gradient is stretched to the length of the bar. Option 2 is to have the gradient fixed, and the longer the bar, the more of the gradient is revealed. I'm not sure which is better.

@jjatie
Copy link
Collaborator

jjatie commented Feb 20, 2018

This also restricts the gradient, what if you want it on a different angle?

@jjatie
Copy link
Collaborator

jjatie commented Feb 20, 2018

Note that I changed the branch to 4.0.0. You will need to rebase onto 4.0.0 branch

@jjatie
Copy link
Collaborator

jjatie commented Feb 21, 2018

#1751

@ChanchalW
Copy link

When this will be merged and the new version released??

@alekzuz
Copy link
Author

alekzuz commented Mar 4, 2018

Note that I changed the branch to 4.0.0. You will need to rebase onto 4.0.0 branch

Done

@Lukafin
Copy link

Lukafin commented Mar 13, 2018

Hey @alekzuz , i tried to use your fork because i need gradient support for bar charts (pod 'Charts' , :git => 'https://github.com/alekzuz/Charts.git', :branch => 'bar-gradient' ) and it does not compile.
I get the following errors:

screen shot 2018-03-13 at 11 30 15

Any idea?

@alekzuz
Copy link
Author

alekzuz commented Mar 13, 2018

Hello @Lukafin , check you build scheme, it seems that you selected wrong one

@Lukafin
Copy link

Lukafin commented Mar 13, 2018

@alekzuz Build scheme is for app that uses your fork of Charts lib. I dont have any other schemes.
Did i miss some important step?
The only thing i changed in my project is this line:
pod 'Charts' , :git => 'https://github.com/alekzuz/Charts.git', :branch => 'bar-gradient'
Before it was:
pod 'Charts'

@alekzuz
Copy link
Author

alekzuz commented Mar 13, 2018

@Lukafin Take a look at the demo project, it works fine. May be you have to clean your project, I do not know.

@Lukafin
Copy link

Lukafin commented Mar 13, 2018

@alekzuz Now checking your demo project and it works there.
So currently the gradient is stretched to the length of the bar.
Is it possible to have the gradient fixed, and the longer the bar, the more of the gradient is revealed?
Also i would like to set some positions for gradient, because i would like some gradient colors to start at some values (thresholds).

@@ -15,6 +15,13 @@ import CoreGraphics

open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartDataSetProtocol
{
@objc
public enum BarGradientOrientation: Int
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't make sense to nest this in BarChartDataSet

/// This prevents out-of-bounds by performing a modulus on the gradient color index, so colours will repeat themselves.
open func barGradientColor(atIndex index: Int) -> [NSUIColor]?
{
guard let gradientColors = barGradientColors
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one line

@@ -149,6 +156,26 @@ open class BarChartDataSet: BarLineScatterCandleBubbleChartDataSet, BarChartData
/// the alpha value (transparency) that is used for drawing the highlight indicator bar. min = 0.0 (fully transparent), max = 1.0 (fully opaque)
open var highlightAlpha = CGFloat(120.0 / 255.0)

/// array of gradient colors [[color1, color2], [color3, color4]]
open var barGradientColors: [[NSUIColor]]?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this a nested array?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested array is array of colors for each bar.


/// - returns: The gradient colors at the given index of the DataSet's gradient color array.
/// This prevents out-of-bounds by performing a modulus on the gradient color index, so colours will repeat themselves.
open func barGradientColor(atIndex index: Int) -> [NSUIColor]?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swift naming convention would be func barGradientColor(at index: Int) -> [NSUIColor]?

else { return nil }

var index = index
if index < 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't necessary, it is already common place in Swift and Obj-C that collections require a positive index. Array doesn't perform these checks, we don't need to either.

open func drawBar(context: CGContext, dataSet: BarChartDataSetProtocol, index: Int, barRect: CGRect)
{
context.saveGState()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer { context.restoreGState() }

@objc open class func gradients () -> [[NSUIColor]]
{
return [
[NSUIColor(red: 146/255.0, green: 132/255.0, blue: 240/255.0, alpha: 1.0),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where did these colours come from? If they are from other templates, you should directly call them instead of redefining them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's custom colours especially for the gradients.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but where did the values for the colours come from? We already have a list of themes in ChartColorTemplates. If this adds to that, it should be done there. It probably should just use the already existing theme colours though.

@@ -17,6 +17,7 @@ types are aliased to either their UI* implementation (on iOS) or their NS* imple
public typealias NSUIGestureRecognizerDelegate = UIGestureRecognizerDelegate
public typealias NSUITapGestureRecognizer = UITapGestureRecognizer
public typealias NSUIPanGestureRecognizer = UIPanGestureRecognizer
public typealias NSUIBezierPath = UIBezierPath
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed.

@@ -229,6 +230,7 @@ types are aliased to either their UI* implementation (on iOS) or their NS* imple
public typealias NSUIPinchGestureRecognizer = NSMagnificationGestureRecognizer
public typealias NSUIRotationGestureRecognizer = NSRotationGestureRecognizer
public typealias NSUIScreen = NSScreen
public typealias NSUIBezierPath = NSBezierPath
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed.

@@ -610,4 +612,33 @@ types are aliased to either their UI* implementation (on iOS) or their NS* imple
return NSUIScreen.main
}

extension NSBezierPath
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this only for OSX/AppKit instead of universal with UIKit?

@ChartsOrg ChartsOrg deleted a comment from alekzuz Mar 29, 2018
@zq190693370
Copy link

Hey @Lukafin now i have the same errors, how did you fix them? thanks!

@zq190693370
Copy link

@alekzuz pardon where can i get your demo?

@terrypanghoming
Copy link

any update? I saw it didn't passed the CI for tvOS

Copy link

@gaming-hacker gaming-hacker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the horizontal bar charts, did you forget to implement the gradient?

is there a way to use the radial gradient with the bubble chart?

@pmairoldi
Copy link
Collaborator

I believe that the gradient should be in the other direction for horizontal bar charts
simulator screen shot - iphone x - 2018-06-10 at 19 10 49

@forbidden404
Copy link

Hello there,

I would appreciate this feature being introduced in the Framework. Is there any way I can help with the process? (e.g.: Continue developing the feature and fix merge issues, or maybe with CC and tests)

@liuxuan30
Copy link
Member

liuxuan30 commented Mar 5, 2019

there is #3533 same as this feature. Is it possible you guys can work together to make this one PR?

@alekzuz
Copy link
Author

alekzuz commented Mar 6, 2019

I will try to finish bar gradients issues the next few days

alekzuz added 2 commits March 6, 2019 23:45
# Conflicts:
#	Source/Charts/Renderers/BarChartRenderer.swift
#	Source/Charts/Renderers/HorizontalBarChartRenderer.swift
@roMummy
Copy link

roMummy commented Jul 3, 2019

thanks

@zdmjerry
Copy link

zdmjerry commented Dec 9, 2019

Can I set rounded corners ?

@jjatie jjatie closed this Jan 26, 2021
@jjatie jjatie deleted the branch ChartsOrg:4.0.0 January 26, 2021 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.