Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* 'master' of https://github.com/danielgindi/Charts:
  add pie chart unit tests (ChartsOrg#3297)
  ChartsOrg#3287: align Objc and Swift demos balloon marker
  update changelog
  Min and Max reset when clearing ChartDataSet (Fixes ChartsOrg#3260)

# Conflicts:
#	Source/Charts/Mark/BalloonMarker.swift
  • Loading branch information
FreddyZeng committed Feb 26, 2018
2 parents 757892b + e29536b commit 09aca8f
Show file tree
Hide file tree
Showing 15 changed files with 568 additions and 72 deletions.
418 changes: 417 additions & 1 deletion CHANGELOG.md

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Charts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
0D8A89398F9BD5DCC8D7F976 /* ICandleChartDataSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18BFB0A14A5C47A302A597D9 /* ICandleChartDataSet.swift */; };
11F68AA2EBF822D7208EE002 /* YAxisRendererRadarChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA5A16F4A382813C4FE8BDF9 /* YAxisRendererRadarChart.swift */; };
1311BEC21E9CC264E971EFAF /* ILineRadarChartDataSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4F7E6A99D82E6AE3804D5A39 /* ILineRadarChartDataSet.swift */; };
135F11CE20425AF600D655A3 /* PieChartTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 135F11CD20425AF600D655A3 /* PieChartTests.swift */; };
146EE16342C2BADC92E45BF2 /* ILineScatterCandleRadarChartDataSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9249AD9AEC8C85772365A128 /* ILineScatterCandleRadarChartDataSet.swift */; };
17E994DA88777AA1D8CCFC58 /* BarChartDataSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = C31AA65EA27776F8C653C7E8 /* BarChartDataSet.swift */; };
203A39685CC96FC625F616E4 /* IHighlighter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 998F2BFE318471AFC05B50AC /* IHighlighter.swift */; };
Expand Down Expand Up @@ -190,6 +191,7 @@
10DD0A02E3CF611BD11EBA9B /* IndexAxisValueFormatter.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = IndexAxisValueFormatter.swift; path = Source/Charts/Formatters/IndexAxisValueFormatter.swift; sourceTree = "<group>"; };
11EF1FE22549E885C8F40738 /* CombinedChartView.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = CombinedChartView.swift; path = Source/Charts/Charts/CombinedChartView.swift; sourceTree = "<group>"; };
12409C3EA15787C11AF0D2BC /* ChartDataEntryBase.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ChartDataEntryBase.swift; path = Source/Charts/Data/Implementations/Standard/ChartDataEntryBase.swift; sourceTree = "<group>"; };
135F11CD20425AF600D655A3 /* PieChartTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = PieChartTests.swift; path = Tests/Charts/PieChartTests.swift; sourceTree = "<group>"; };
18462BFDD9DEE76D51D40503 /* ScatterChartView.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ScatterChartView.swift; path = Source/Charts/Charts/ScatterChartView.swift; sourceTree = "<group>"; };
18BFB0A14A5C47A302A597D9 /* ICandleChartDataSet.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = ICandleChartDataSet.swift; path = Source/Charts/Data/Interfaces/ICandleChartDataSet.swift; sourceTree = "<group>"; };
1C02C3AF5C92FCFC18224C35 /* XAxisRenderer.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = XAxisRenderer.swift; path = Source/Charts/Renderers/XAxisRenderer.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -539,6 +541,7 @@
7AB9062A28AAB9469752A954 /* ChartUtilsTests.swift */,
B6BF9A551F91993A00E62A5D /* CombinedChartTests.swift */,
D2E1819D72CD7B6C4A4E8048 /* LineChartTests.swift */,
135F11CD20425AF600D655A3 /* PieChartTests.swift */,
064989451F5C99C7006E8BB3 /* Snapshot.swift */,
);
name = Charts;
Expand Down Expand Up @@ -998,6 +1001,7 @@
8E1192F7A7152E9DA92C56A9 /* ChartUtilsTests.swift in Sources */,
B6BF9A561F91993A00E62A5D /* CombinedChartTests.swift in Sources */,
2BF85BEA981B359A65E9BF67 /* LineChartTests.swift in Sources */,
135F11CE20425AF600D655A3 /* PieChartTests.swift in Sources */,
064989461F5C99C7006E8BB3 /* Snapshot.swift in Sources */,
C20349C4201344430076234B /* XYMarkerView.swift in Sources */,
224EFF991FBAAC4700CF9B3B /* EquatableTests.swift in Sources */,
Expand Down
47 changes: 29 additions & 18 deletions ChartsDemo/Swift/Components/BalloonMarker.swift
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
//
// BalloonMarker.swift
// ChartsDemo-iOS
// ChartsDemo-Swift
//
// Created by Jacob Christie on 2017-07-09.
// Copyright © 2017 jc. All rights reserved.
// Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
// A port of MPAndroidChart for iOS
// Licensed under Apache License 2.0
//
// https://github.com/danielgindi/Charts
//

import Foundation
import Charts

public class BalloonMarker: MarkerImage {
public var color: UIColor
public var arrowSize = CGSize(width: 15, height: 11)
public var font: UIFont
public var textColor: UIColor
public var insets: UIEdgeInsets
public var minimumSize = CGSize()
open class BalloonMarker: MarkerImage
{
open var color: UIColor
open var arrowSize = CGSize(width: 15, height: 11)
open var font: UIFont
open var textColor: UIColor
open var insets: UIEdgeInsets
open var minimumSize = CGSize()

fileprivate var label: String?
fileprivate var _labelSize: CGSize = CGSize()
fileprivate var _paragraphStyle: NSMutableParagraphStyle?
fileprivate var _drawAttributes = [NSAttributedStringKey : AnyObject]()

public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets) {
public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets)
{
self.color = color
self.font = font
self.textColor = textColor
Expand All @@ -33,7 +38,8 @@ public class BalloonMarker: MarkerImage {
super.init()
}

public override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint {
open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint
{
var offset = self.offset
var size = self.size

Expand Down Expand Up @@ -77,7 +83,8 @@ public class BalloonMarker: MarkerImage {
return offset
}

public override func draw(context: CGContext, point: CGPoint) {
open override func draw(context: CGContext, point: CGPoint)
{
guard let label = label else { return }

let offset = self.offsetForDrawing(atPoint: point)
Expand All @@ -95,7 +102,8 @@ public class BalloonMarker: MarkerImage {

context.setFillColor(color.cgColor)

if offset.y > 0 {
if offset.y > 0
{
context.beginPath()
context.move(to: CGPoint(
x: rect.origin.x,
Expand Down Expand Up @@ -123,7 +131,9 @@ public class BalloonMarker: MarkerImage {
x: rect.origin.x,
y: rect.origin.y + arrowSize.height))
context.fillPath()
} else {
}
else
{
context.beginPath()
context.move(to: CGPoint(
x: rect.origin.x,
Expand Down Expand Up @@ -170,11 +180,13 @@ public class BalloonMarker: MarkerImage {
context.restoreGState()
}

public override func refreshContent(entry: ChartDataEntry, highlight: Highlight) {
open override func refreshContent(entry: ChartDataEntry, highlight: Highlight)
{
setLabel(String(entry.y))
}

public func setLabel(_ newLabel: String) {
open func setLabel(_ newLabel: String)
{
label = newLabel

_drawAttributes.removeAll()
Expand All @@ -191,5 +203,4 @@ public class BalloonMarker: MarkerImage {
size.height = max(minimumSize.height, size.height)
self.size = size
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,22 @@ open class ChartDataSet: ChartBaseDataSet

open override func calcMinMax()
{
guard !values.isEmpty else { return }

_yMax = -Double.greatestFiniteMagnitude
_yMin = Double.greatestFiniteMagnitude
_xMax = -Double.greatestFiniteMagnitude
_xMin = Double.greatestFiniteMagnitude

guard !values.isEmpty else { return }

values.forEach { calcMinMax(entry: $0) }
}

open override func calcMinMaxY(fromX: Double, toX: Double)
{
guard !values.isEmpty else { return }

_yMax = -Double.greatestFiniteMagnitude
_yMin = Double.greatestFiniteMagnitude

guard !values.isEmpty else { return }

let indexFrom = entryIndex(x: fromX, closestToY: Double.nan, rounding: .down)
let indexTo = entryIndex(x: toX, closestToY: Double.nan, rounding: .up)
Expand Down
90 changes: 41 additions & 49 deletions Source/Charts/Mark/BalloonMarker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,29 @@ import Foundation

open class BalloonMarker: MarkerImage
{
@objc open var color: UIColor?
@objc open var color: UIColor
@objc open var arrowSize = CGSize(width: 15, height: 11)
@objc open var font: UIFont?
@objc open var textColor: UIColor?
@objc open var insets = UIEdgeInsets()
@objc open var font: UIFont
@objc open var textColor: UIColor
@objc open var insets: UIEdgeInsets
@objc open var minimumSize = CGSize()

fileprivate var attLabel: NSAttributedString?
fileprivate var label: String?
fileprivate var _labelSize: CGSize = CGSize()
fileprivate var _paragraphStyle: NSMutableParagraphStyle?
fileprivate var _drawAttributes = [NSAttributedStringKey : Any]()
fileprivate var _drawAttributes = [NSAttributedStringKey : AnyObject]()

@objc public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets)
{
super.init()

self.color = color
self.font = font
self.textColor = textColor
self.insets = insets

_paragraphStyle = NSParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle
_paragraphStyle?.alignment = .center
super.init()
}

open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint
Expand Down Expand Up @@ -98,31 +97,27 @@ open class BalloonMarker: MarkerImage
rect.origin.y -= size.height

context.saveGState()

if label.count == 0 {
context.restoreGState()
return;
}

if let color = color
{
context.setFillColor(color.cgColor)

let path = UIBezierPath.init(roundedRect: CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width, height: rect.size.height - arrowSize.height), cornerRadius: 5);

let sanJiao = UIBezierPath();
sanJiao.move(to: CGPoint(
x: rect.origin.x + (rect.size.width - arrowSize.width) / 2.0,
y: rect.origin.y + rect.size.height - arrowSize.height))
sanJiao.addLine(to: CGPoint(
x: rect.origin.x + rect.size.width / 2.0,
y: rect.origin.y + rect.size.height))
sanJiao.addLine(to: CGPoint(
x: rect.origin.x + (rect.size.width + arrowSize.width) / 2.0,
y: rect.origin.y + rect.size.height - arrowSize.height))
path.append(sanJiao)
path.fill()
}
context.setFillColor(color.cgColor)

let path = UIBezierPath.init(roundedRect: CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width, height: rect.size.height - arrowSize.height), cornerRadius: 5);

let sanJiao = UIBezierPath();
sanJiao.move(to: CGPoint(
x: rect.origin.x + (rect.size.width - arrowSize.width) / 2.0,
y: rect.origin.y + rect.size.height - arrowSize.height))
sanJiao.addLine(to: CGPoint(
x: rect.origin.x + rect.size.width / 2.0,
y: rect.origin.y + rect.size.height))
sanJiao.addLine(to: CGPoint(
x: rect.origin.x + (rect.size.width + arrowSize.width) / 2.0,
y: rect.origin.y + rect.size.height - arrowSize.height))
path.append(sanJiao)
path.fill()

rect.origin.y += self.insets.top
rect.size.height -= self.insets.top + self.insets.bottom
Expand Down Expand Up @@ -156,25 +151,22 @@ open class BalloonMarker: MarkerImage
return;
}

if let color = color
{
context.setFillColor(color.cgColor)

let path = UIBezierPath.init(roundedRect: CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width, height: rect.size.height - arrowSize.height), cornerRadius: 5);

let sanJiao = UIBezierPath();
sanJiao.move(to: CGPoint(
x: rect.origin.x + (rect.size.width - arrowSize.width) / 2.0,
y: rect.origin.y + rect.size.height - arrowSize.height))
sanJiao.addLine(to: CGPoint(
x: rect.origin.x + rect.size.width / 2.0,
y: rect.origin.y + rect.size.height))
sanJiao.addLine(to: CGPoint(
x: rect.origin.x + (rect.size.width + arrowSize.width) / 2.0,
y: rect.origin.y + rect.size.height - arrowSize.height))
path.append(sanJiao)
path.fill()
}
context.setFillColor(color.cgColor)

let path = UIBezierPath.init(roundedRect: CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.size.width, height: rect.size.height - arrowSize.height), cornerRadius: 5);

let sanJiao = UIBezierPath();
sanJiao.move(to: CGPoint(
x: rect.origin.x + (rect.size.width - arrowSize.width) / 2.0,
y: rect.origin.y + rect.size.height - arrowSize.height))
sanJiao.addLine(to: CGPoint(
x: rect.origin.x + rect.size.width / 2.0,
y: rect.origin.y + rect.size.height))
sanJiao.addLine(to: CGPoint(
x: rect.origin.x + (rect.size.width + arrowSize.width) / 2.0,
y: rect.origin.y + rect.size.height - arrowSize.height))
path.append(sanJiao)
path.fill()

rect.origin.y += self.insets.top
rect.origin.x += self.insets.left
Expand Down Expand Up @@ -222,9 +214,9 @@ open class BalloonMarker: MarkerImage
label = newLabel

_drawAttributes.removeAll()
_drawAttributes[NSAttributedStringKey.font] = self.font
_drawAttributes[NSAttributedStringKey.paragraphStyle] = _paragraphStyle
_drawAttributes[NSAttributedStringKey.foregroundColor] = self.textColor
_drawAttributes[.font] = self.font
_drawAttributes[.paragraphStyle] = _paragraphStyle
_drawAttributes[.foregroundColor] = self.textColor

_labelSize = label?.size(withAttributes: _drawAttributes) ?? CGSize.zero

Expand Down
73 changes: 73 additions & 0 deletions Tests/Charts/PieChartTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import XCTest
import FBSnapshotTestCase
@testable import Charts

class PieChartTests: FBSnapshotTestCase
{

var chart: PieChartView!
var dataSet: PieChartDataSet!

override func setUp()
{
super.setUp()

// Set to `true` to re-capture all snapshots
self.recordMode = false

// Sample data
let values: [Double] = [11, 33, 81, 52, 97, 101, 75]

var entries: [PieChartDataEntry] = Array()

for value in values
{
entries.append(PieChartDataEntry(value: value, icon: UIImage(named: "icon", in: Bundle(for: self.classForCoder), compatibleWith: nil)))
}

dataSet = PieChartDataSet(values: entries, label: "First unit test data")
dataSet.drawIconsEnabled = false
dataSet.iconsOffset = CGPoint(x: 0, y: 20.0)

dataSet.colors = ChartColorTemplates.vordiplom()
+ ChartColorTemplates.joyful()
+ ChartColorTemplates.colorful()
+ ChartColorTemplates.liberty()
+ ChartColorTemplates.pastel()
+ [UIColor(red: 51/255, green: 181/255, blue: 229/255, alpha: 1)]

chart = PieChartView(frame: CGRect(x: 0, y: 0, width: 480, height: 350))
chart.backgroundColor = NSUIColor.clear
chart.centerText = "PieChart Unit Test"
chart.data = PieChartData(dataSet: dataSet)
}

override func tearDown()
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}

func testDefaultValues()
{
FBSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), tolerance: Snapshot.tolerance)
}

func testHidesValues()
{
dataSet.drawValuesEnabled = false
FBSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), tolerance: Snapshot.tolerance)
}

func testDrawIcons()
{
dataSet.drawIconsEnabled = true
FBSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), tolerance: Snapshot.tolerance)
}

func testHideCenterLabel()
{
chart.drawCenterTextEnabled = false
FBSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), tolerance: Snapshot.tolerance)
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 09aca8f

Please sign in to comment.