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 gradient line drawing to LineChartRenderer. based on PR #3142 #3415

Merged
merged 12 commits into from
Jun 10, 2018

Conversation

larryonoff
Copy link
Contributor

@larryonoff larryonoff commented Apr 24, 2018

Some stabilization and code cleanup.

@jjatie could you please take a look at this PR. This PR stabilises and improves your PR #3142

@jjatie
Copy link
Collaborator

jjatie commented Apr 25, 2018

First glance looks okay. Please fix build failures. What do you mean by "stabilization"?

I'll review thoroughly once fixed.

context.setStrokeColor(drawingColor.cgColor)
context.strokePath()
}
drawLine(context: context, dataSet: dataSet, spline: cubicPath, matrix: valueToPixelMatrix, drawingColor: drawingColor)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I thought about doing this. I think this correct way to do it is

if dataSet.isDrawLineWithGradientEnabled
{
    drawGradientLine(context: context, dataSet: dataSet, spline: cubicPath, matrix: valueToPixelMatrix)
}
else
{
    drawLine(in: ...)
}

Copy link
Contributor Author

@larryonoff larryonoff Apr 25, 2018

Choose a reason for hiding this comment

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

I exposed this logic into a separate function with the name drawLine.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm aware. I'm saying that drawLine and drawGradientLine should be separate methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just pushed a change. I think that this's correct now.

@@ -795,6 +806,12 @@ open class LineChartRenderer: LineRadarRenderer

func drawGradientLine(context: CGContext, dataSet: LineChartDataSetProtocol, spline: CGPath, matrix: CGAffineTransform)
{
guard let gradientPositions = dataSet.gradientPositions else
{
assertionFailure("Must set `gradientPositions if `dataSet.isDrawLineWithGradientEnabled` is true")
Copy link
Collaborator

@jjatie jjatie Apr 25, 2018

Choose a reason for hiding this comment

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

👍 I think we could use more of this.

@larryonoff
Copy link
Contributor Author

@jjatie

First glance looks okay. Please fix build failures. What do you mean by "stabilization"?

  • fixed out of bounds crash in generateGradientLinePath at for i in (from + 1)..<to by adding guard (from + 1) < to.
  • fixed how gradient colors are picked, see drawGradientLine, i.e. the first data set color was always selected for points in the middle.

@larryonoff
Copy link
Contributor Author

I'll fix the crash. It's because different return parameters of the func getRed(red:, green:, blue: , alpha:) for NSColor and UIColor.

@codecov-io
Copy link

codecov-io commented Apr 25, 2018

Codecov Report

Merging #3415 into 4.0.0 will decrease coverage by 0.15%.
The diff coverage is 37.41%.

Impacted file tree graph

@@            Coverage Diff             @@
##            4.0.0    #3415      +/-   ##
==========================================
- Coverage   29.42%   29.26%   -0.16%     
==========================================
  Files         114      114              
  Lines       12605    12683      +78     
==========================================
+ Hits         3709     3712       +3     
- Misses       8896     8971      +75
Impacted Files Coverage Δ
...ta/Implementations/Standard/LineChartDataSet.swift 32.65% <ø> (ø) ⬆️
Source/Charts/Utils/Platform.swift 12.9% <0%> (-2.49%) ⬇️
Source/Charts/Renderers/LineChartRenderer.swift 51.97% <40.31%> (-5.25%) ⬇️

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 08bd20c...aee20f0. Read the comment docs.

@larryonoff
Copy link
Contributor Author

It looks that gradient locations are calculated wrong. I hope that I'll fix it tomorrow.

context: CGContext,
dataSet: LineChartDataSetProtocol,
spline: CGMutablePath,
matrix: CGAffineTransform,
Copy link
Collaborator

Choose a reason for hiding this comment

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

unused parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed


private func drawLine(
context: CGContext,
dataSet: LineChartDataSetProtocol,
Copy link
Collaborator

Choose a reason for hiding this comment

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

unused parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@larryonoff larryonoff changed the title Stabilize and clean the code Stabilize and clean the code for draw gradient line PR Apr 26, 2018
@larryonoff
Copy link
Contributor Author

What anyone thing about implementing similar approach like for fill, i.e.

open class ChartGradient {
  var start: CGPoint
  var end: CGPoint
  var positions: [CGFloat]?
  var colors: [UIColor]?
}

@jjatie
Copy link
Collaborator

jjatie commented Apr 27, 2018

That sounds like a great idea. I think the properties can all be let decorations. There are many areas I'd like to introduce this sort of data type.

@larryonoff
Copy link
Contributor Author

@jjatie what do you think about class name? is it fine? or better LineChartGradient, LineChartLineGradient?

@jjatie
Copy link
Collaborator

jjatie commented Apr 27, 2018

Hold off on doing it in this PR, a separate one would be greatly appreciated where you implement this across all gradient usage in the framework.

@@ -785,6 +796,11 @@ open class LineChartRenderer: LineRadarRenderer

// create a new path
let to = Int(ceil(CGFloat(to - from) * phaseX + CGFloat(from)))

guard (from + 1) < to else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need this?
Maybe let from = from + 1 instead?
Would be nice to have a comment for why we use + 1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why do we need this?

this's because path generations starts from command move for the first point, and then add line.

{
fatalError("Must set `gradientPositions if `dataSet.isDrawLineWithGradientEnabled` is true")
let boundingBox = spline.boundingBox
guard !boundingBox.isNull && !boundingBox.isInfinite && !boundingBox.isEmpty else {
Copy link
Collaborator

Choose a reason for hiding this comment

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

comma instead of ampersand. We're listing requirements, not performing Boolean logic.

@@ -304,6 +320,26 @@ types are aliased to either their UI* implementation (on iOS) or their NS* imple
}
}

extension NSUIColor
{
var nsuiRGBA: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)? {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rename to rgba


extension NSUIColor
{
var nsuiRGBA: (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)? {
Copy link
Collaborator

Choose a reason for hiding this comment

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

rgba

@larryonoff
Copy link
Contributor Author

larryonoff commented Apr 28, 2018

@jjatie I think it would be great to rebase this PR into 4.0.0 branch. Already did this.

@larryonoff larryonoff changed the base branch from draw-gradient-line to 4.0.0 April 28, 2018 10:17
@larryonoff larryonoff changed the title Stabilize and clean the code for draw gradient line PR Added gradient line drawing to LineChartRenderer v2. base on PR #3142 Apr 28, 2018
@larryonoff larryonoff changed the title Added gradient line drawing to LineChartRenderer v2. base on PR #3142 Added gradient line drawing to LineChartRenderer. based on PR #3142 Apr 28, 2018
@larryonoff larryonoff force-pushed the draw-gradient-line branch from 3c20de9 to aee20f0 Compare May 2, 2018 10:26
@pmairoldi pmairoldi merged commit 1b387d8 into ChartsOrg:4.0.0 Jun 10, 2018
@pmairoldi
Copy link
Collaborator

Thanks 👍

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

Successfully merging this pull request may close these issues.

4 participants