Skip to content

Commit

Permalink
fix #1830. credit from ChartsOrg/Charts#2049 (#2874)
Browse files Browse the repository at this point in the history
* fix #1830. credit from ChartsOrg/Charts#2049

* add combined chart unit tests for iOS, tvOS (macOS only have build process)

* use iterater rather than index
  • Loading branch information
rcblue committed Dec 25, 2017
1 parent 6e10075 commit b93f2a5
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 20 deletions.
4 changes: 4 additions & 0 deletions Charts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
B0D28C68BB9A958DC56EB214 /* DefaultValueFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 107D8F8163EE54D6D9E916B0 /* DefaultValueFormatter.swift */; };
B13C74B4FF705D7B595D01EF /* IAxisValueFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0BD9DF16AF59680A3BB49452 /* IAxisValueFormatter.swift */; };
B539114951455C35BADAE3F3 /* PieChartDataSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4FB5E3761EF8B4D1E1E1014 /* PieChartDataSet.swift */; };
B6BF9A561F91993A00E62A5D /* CombinedChartTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6BF9A551F91993A00E62A5D /* CombinedChartTests.swift */; };
B6C9F450D937B87224D29D5C /* IFillFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 818AC6B12505B7C0A53D62F9 /* IFillFormatter.swift */; };
B6DCC229615EFE706F64A37D /* LineScatterCandleRadarRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 923206233CA89FD03565FF87 /* LineScatterCandleRadarRenderer.swift */; };
B85DEB06B4C1AFFC8A0E3295 /* CircleShapeRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECE1B1623D3AF69CECAE8562 /* CircleShapeRenderer.swift */; };
Expand Down Expand Up @@ -266,6 +267,7 @@
B137428B41C143D5115726C4 /* Description.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = Description.swift; path = Source/Charts/Components/Description.swift; sourceTree = "<group>"; };
B1BA6B21CBDF77A15848994F /* RadarChartDataSet.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = RadarChartDataSet.swift; path = Source/Charts/Data/Implementations/Standard/RadarChartDataSet.swift; sourceTree = "<group>"; };
B44829AF0ADA583F1F0279B7 /* BubbleChartDataSet.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = BubbleChartDataSet.swift; path = Source/Charts/Data/Implementations/Standard/BubbleChartDataSet.swift; sourceTree = "<group>"; };
B6BF9A551F91993A00E62A5D /* CombinedChartTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = CombinedChartTests.swift; path = Tests/Charts/CombinedChartTests.swift; sourceTree = "<group>"; };
BA157EFF2F952192C11DF937 /* AnimatedMoveViewJob.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = AnimatedMoveViewJob.swift; path = Source/Charts/Jobs/AnimatedMoveViewJob.swift; sourceTree = "<group>"; };
BA1A58428DC4780BAB4EAADC /* CandleStickChartView.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = CandleStickChartView.swift; path = Source/Charts/Charts/CandleStickChartView.swift; sourceTree = "<group>"; };
BC19DC2434D65FFB446A61B7 /* XAxisRendererRadarChart.swift */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = sourcecode.swift; name = XAxisRendererRadarChart.swift; path = Source/Charts/Renderers/XAxisRendererRadarChart.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -514,6 +516,7 @@
5C3F5E1A69EC06E86505F7B1 /* BarChartTests.swift */,
224EFF981FBAAC4700CF9B3B /* EquatableTests.swift */,
7AB9062A28AAB9469752A954 /* ChartUtilsTests.swift */,
B6BF9A551F91993A00E62A5D /* CombinedChartTests.swift */,
D2E1819D72CD7B6C4A4E8048 /* LineChartTests.swift */,
064989451F5C99C7006E8BB3 /* Snapshot.swift */,
);
Expand Down Expand Up @@ -946,6 +949,7 @@
files = (
3B11556EB7DC034E2FC958E4 /* BarChartTests.swift in Sources */,
8E1192F7A7152E9DA92C56A9 /* ChartUtilsTests.swift in Sources */,
B6BF9A561F91993A00E62A5D /* CombinedChartTests.swift in Sources */,
2BF85BEA981B359A65E9BF67 /* LineChartTests.swift in Sources */,
064989461F5C99C7006E8BB3 /* Snapshot.swift in Sources */,
224EFF991FBAAC4700CF9B3B /* EquatableTests.swift in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions ChartsDemo/Objective-C/Demos/CombinedChartViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ - (BarChartData *)generateBarData
[set1 setColor:[UIColor colorWithRed:60/255.f green:220/255.f blue:78/255.f alpha:1.f]];
set1.valueTextColor = [UIColor colorWithRed:60/255.f green:220/255.f blue:78/255.f alpha:1.f];
set1.valueFont = [UIFont systemFontOfSize:10.f];
set1.axisDependency = AxisDependencyLeft;
set1.axisDependency = AxisDependencyRight;

BarChartDataSet *set2 = [[BarChartDataSet alloc] initWithValues:entries2 label:@""];
set2.stackLabels = @[@"Stack 1", @"Stack 2"];
Expand All @@ -216,7 +216,7 @@ - (BarChartData *)generateBarData
];
set2.valueTextColor = [UIColor colorWithRed:61/255.f green:165/255.f blue:255/255.f alpha:1.f];
set2.valueFont = [UIFont systemFontOfSize:10.f];
set2.axisDependency = AxisDependencyLeft;
set2.axisDependency = AxisDependencyRight;

float groupSpace = 0.06f;
float barSpace = 0.02f; // x2 dataset
Expand Down
42 changes: 24 additions & 18 deletions Source/Charts/Data/Implementations/Standard/CombinedChartData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,25 +136,31 @@ open class CombinedChartData: BarLineScatterCandleBubbleChartData
{
_xMin = data.xMin
}

if data.yMax > _leftAxisMax
{
_leftAxisMax = data.yMax
}

if data.yMin < _leftAxisMin
{
_leftAxisMin = data.yMin
}

if data.yMax > _rightAxisMax
{
_rightAxisMax = data.yMax
}

if data.yMin < _rightAxisMin

for dataset in sets
{
_rightAxisMin = data.yMin
if dataset.axisDependency == .left
{
if dataset.yMax > _leftAxisMax
{
_leftAxisMax = dataset.yMax
}
if dataset.yMin < _leftAxisMin
{
_leftAxisMin = dataset.yMin
}
}
else
{
if dataset.yMax > _rightAxisMax
{
_rightAxisMax = dataset.yMax
}
if dataset.yMin < _rightAxisMin
{
_rightAxisMin = dataset.yMin
}
}
}
}
}
Expand Down
104 changes: 104 additions & 0 deletions Tests/Charts/CombinedChartTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//
// CombinedChartTests.swift
// ChartsTests
//
// Created by Xuan Liu on 14/10/2017.
//

import XCTest
import FBSnapshotTestCase
@testable import Charts

class CombinedChartTests: FBSnapshotTestCase
{
var chart: CombinedChartView!
var lineDataSet: LineChartDataSet!
var barDataSet: BarChartDataSet!

override func setUp()
{
super.setUp()

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

// Sample data
let combinedData = CombinedChartData()
combinedData.barData = generateBarData()
combinedData.lineData = generateLineData()
chart = CombinedChartView(frame: CGRect(x: 0, y: 0, width: 480, height: 350))
chart.backgroundColor = NSUIColor.clear
chart.leftAxis.axisMinimum = 0.0
chart.rightAxis.axisMinimum = 0.0
chart.data = combinedData
}

func generateBarData() -> BarChartData
{
let values: [Double] = [8, 104, 81, 93, 52, 44, 97, 101, 75, 28,
76, 25, 20, 13, 52, 44, 57, 23, 45, 91,
99, 14, 84, 48, 40, 71, 106, 41, 45, 61]

var entries: [ChartDataEntry] = Array()

for (i, value) in values.enumerated()
{
entries.append(BarChartDataEntry(x: Double(i), y: value, icon: UIImage(named: "icon", in: Bundle(for: self.classForCoder), compatibleWith: nil)))
}

barDataSet = BarChartDataSet(values: entries, label: "Bar chart unit test data")
barDataSet.drawIconsEnabled = false

let data = BarChartData(dataSet: barDataSet)
data.barWidth = 0.85
return data
}

func generateLineData() -> LineChartData
{
let values: [Double] = [0, 254, 81, 93, 52, 44, 97, 101, 75, 28,
76, 25, 20, 13, 52, 44, 57, 23, 45, 91,
99, 14, 84, 48, 40, 71, 106, 41, 45, 61]

var entries: [ChartDataEntry] = Array()

for (i, value) in values.enumerated()
{
entries.append(ChartDataEntry(x: Double(i), y: value, icon: UIImage(named: "icon", in: Bundle(for: self.classForCoder), compatibleWith: nil)))
}

lineDataSet = LineChartDataSet(values: entries, label: "Line chart unit test data")
lineDataSet.drawIconsEnabled = false
return LineChartData(dataSet: lineDataSet)
}

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

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

func testLeftRightAxisDependency()
{
lineDataSet.axisDependency = .left
barDataSet.axisDependency = .right
chart.data?.notifyDataChanged()
chart.notifyDataSetChanged()
FBSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), tolerance: Snapshot.tolerance)
}

func testAllRightAxisDependency()
{
lineDataSet.axisDependency = .right
barDataSet.axisDependency = .right
chart.data?.notifyDataChanged()
chart.notifyDataSetChanged()
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.

0 comments on commit b93f2a5

Please sign in to comment.