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

Fix horizontal bar chart not drawing values and add unit tests #3906

Merged
merged 8 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Charts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,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 */; };
B66817462241E3CC00017CF1 /* HorizontalBarChartTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B66817452241E3CC00017CF1 /* HorizontalBarChartTests.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 */; };
Expand Down Expand Up @@ -273,6 +274,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>"; };
B66817452241E3CC00017CF1 /* HorizontalBarChartTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = HorizontalBarChartTests.swift; path = Tests/Charts/HorizontalBarChartTests.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>"; };
Expand Down Expand Up @@ -520,6 +522,7 @@
isa = PBXGroup;
children = (
5C3F5E1A69EC06E86505F7B1 /* BarChartTests.swift */,
B66817452241E3CC00017CF1 /* HorizontalBarChartTests.swift */,
224EFF981FBAAC4700CF9B3B /* EquatableTests.swift */,
7AB9062A28AAB9469752A954 /* ChartUtilsTests.swift */,
B6BF9A551F91993A00E62A5D /* CombinedChartTests.swift */,
Expand Down Expand Up @@ -963,6 +966,7 @@
8E1192F7A7152E9DA92C56A9 /* ChartUtilsTests.swift in Sources */,
B6BF9A561F91993A00E62A5D /* CombinedChartTests.swift in Sources */,
2BF85BEA981B359A65E9BF67 /* LineChartTests.swift in Sources */,
B66817462241E3CC00017CF1 /* HorizontalBarChartTests.swift in Sources */,
135F11CE20425AF600D655A3 /* PieChartTests.swift in Sources */,
064989461F5C99C7006E8BB3 /* Snapshot.swift in Sources */,
224EFF991FBAAC4700CF9B3B /* EquatableTests.swift in Sources */,
Expand Down
12 changes: 5 additions & 7 deletions Source/Charts/Renderers/BarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,13 @@ open class BarChartRenderer: BarLineScatterCandleBubbleRenderer
var posOffset: CGFloat
var negOffset: CGFloat
let drawValueAboveBar = dataProvider.isDrawValueAboveBarEnabled

for dataSetIndex in 0 ..< barData.dataSetCount
{
guard let dataSet = dataSets[dataSetIndex] as? IBarChartDataSet else { continue }

if !shouldDrawValues(forDataSet: dataSet)
{
continue
}
guard let
dataSet = dataSets[dataSetIndex] as? IBarChartDataSet,
shouldDrawValues(forDataSet: dataSet)
else { continue }

let isInverted = dataProvider.isInverted(axis: dataSet.axisDependency)

Expand Down
9 changes: 3 additions & 6 deletions Source/Charts/Renderers/CandleStickChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,11 @@ open class CandleStickChartRenderer: LineScatterCandleRadarRenderer

for i in 0 ..< dataSets.count
{
guard let dataSet = dataSets[i] as? IBarLineScatterCandleBubbleChartDataSet
guard let
dataSet = dataSets[i] as? IBarLineScatterCandleBubbleChartDataSet,
shouldDrawValues(forDataSet: dataSet)
else { continue }

if !shouldDrawValues(forDataSet: dataSet)
{
continue
}

let valueFont = dataSet.valueFont

guard let formatter = dataSet.valueFormatter else { continue }
Expand Down
10 changes: 4 additions & 6 deletions Source/Charts/Renderers/HorizontalBarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,10 @@ open class HorizontalBarChartRenderer: BarChartRenderer

for dataSetIndex in 0 ..< barData.dataSetCount
{
guard let dataSet = dataSets[dataSetIndex] as? IBarChartDataSet else { continue }

if !shouldDrawValues(forDataSet: dataSet) || !(dataSet.isDrawIconsEnabled && dataSet.isVisible)
{
continue
}
guard let
dataSet = dataSets[dataSetIndex] as? IBarChartDataSet,
shouldDrawValues(forDataSet: dataSet)
else { continue }

let isInverted = dataProvider.isInverted(axis: dataSet.axisDependency)

Expand Down
10 changes: 4 additions & 6 deletions Source/Charts/Renderers/LineChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,10 @@ open class LineChartRenderer: LineRadarRenderer

for i in 0 ..< dataSets.count
{
guard let dataSet = dataSets[i] as? ILineChartDataSet else { continue }

if !shouldDrawValues(forDataSet: dataSet)
{
continue
}
guard let
dataSet = dataSets[i] as? ILineChartDataSet,
shouldDrawValues(forDataSet: dataSet)
else { continue }

let valueFont = dataSet.valueFont

Expand Down
10 changes: 4 additions & 6 deletions Source/Charts/Renderers/RadarChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,10 @@ open class RadarChartRenderer: LineRadarRenderer

for i in 0 ..< data.dataSetCount
{
let dataSet = data.getDataSetByIndex(i) as! IRadarChartDataSet

if !shouldDrawValues(forDataSet: dataSet)
{
continue
}
guard let
dataSet = data.getDataSetByIndex(i) as? IRadarChartDataSet,
shouldDrawValues(forDataSet: dataSet)
else { continue }

let entryCount = dataSet.entryCount

Expand Down
11 changes: 4 additions & 7 deletions Source/Charts/Renderers/ScatterChartRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,13 @@ open class ScatterChartRenderer: LineScatterCandleRadarRenderer
for i in 0 ..< scatterData.dataSetCount
{
let dataSet = dataSets[i]

if !shouldDrawValues(forDataSet: dataSet)
{
continue
}
guard let
formatter = dataSet.valueFormatter,
shouldDrawValues(forDataSet: dataSet)
else { continue }

let valueFont = dataSet.valueFont

guard let formatter = dataSet.valueFormatter else { continue }

let trans = dataProvider.getTransformer(forAxis: dataSet.axisDependency)
let valueToPixelMatrix = trans.valueToPixelMatrix

Expand Down
63 changes: 63 additions & 0 deletions Tests/Charts/BarChartTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ class BarChartTests: FBSnapshotTestCase
return setupCustomValuesDataEntries(values: values)
}

func setupStackedValuesDataEntries() -> [ChartDataEntry]
{
var entries: [ChartDataEntry] = Array()
entries.append(BarChartDataEntry(x: 0, yValues: [28, 50, 60, 30, 42], icon: UIImage(named: "icon")))
entries.append(BarChartDataEntry(x: 1, yValues: [-20, -36, -52, -40, -15], icon: UIImage(named: "icon")))
entries.append(BarChartDataEntry(x: 2, yValues: [10, 30, 40, 90, 72], icon: UIImage(named: "icon")))
entries.append(BarChartDataEntry(x: 3, yValues: [-40, -50, -30, -60, -20], icon: UIImage(named: "icon")))
entries.append(BarChartDataEntry(x: 4, yValues: [10, 40, 60, 45, 62], icon: UIImage(named: "icon")))
return entries
}

func setupDefaultStackedDataSet(chartDataEntries: [ChartDataEntry]) -> BarChartDataSet
{
let dataSet = BarChartDataSet(entries: chartDataEntries, label: "Stacked bar chart unit test data")
dataSet.drawIconsEnabled = false
dataSet.iconsOffset = CGPoint(x: 0, y: -10.0)
dataSet.colors = Array(arrayLiteral:NSUIColor(red: 46/255.0, green: 204/255.0, blue: 113/255.0, alpha: 1.0),
NSUIColor(red: 241/255.0, green: 196/255.0, blue: 15/255.0, alpha: 1.0),
NSUIColor(red: 231/255.0, green: 76/255.0, blue: 60/255.0, alpha: 1.0),
NSUIColor(red: 52/255.0, green: 152/255.0, blue: 219/255.0, alpha: 1.0)
)
return dataSet
}

func setupDefaultDataSet(chartDataEntries: [ChartDataEntry]) -> BarChartDataSet
{
let dataSet = BarChartDataSet(entries: chartDataEntries, label: "Bar chart unit test data")
Expand Down Expand Up @@ -226,6 +250,45 @@ class BarChartTests: FBSnapshotTestCase
chart.notifyDataSetChanged()
ChartsSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), overallTolerance: Snapshot.tolerance)
}

func testNotDrawValueAboveBars()
{
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.drawValueAboveBarEnabled = false
chart.notifyDataSetChanged()
ChartsSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), overallTolerance: Snapshot.tolerance)
}

func testStackedDrawValues()
{
let dataEntries = setupStackedValuesDataEntries()
let dataSet = setupDefaultStackedDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.notifyDataSetChanged()
ChartsSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), overallTolerance: Snapshot.tolerance)
}

func testStackedNotDrawValues()
{
let dataEntries = setupStackedValuesDataEntries()
let dataSet = setupDefaultStackedDataSet(chartDataEntries: dataEntries)
dataSet.drawValuesEnabled = false
let chart = setupDefaultChart(dataSets: [dataSet])
chart.notifyDataSetChanged()
ChartsSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), overallTolerance: Snapshot.tolerance)
}

func testStackedNotDrawValuesAboveBars()
{
let dataEntries = setupStackedValuesDataEntries()
let dataSet = setupDefaultStackedDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.drawValueAboveBarEnabled = false
chart.notifyDataSetChanged()
ChartsSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), overallTolerance: Snapshot.tolerance)
}

func testHideLeftAxis()
{
Expand Down
147 changes: 147 additions & 0 deletions Tests/Charts/HorizontalBarChartTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
//
// HorizontalBarChartTests.swift
// ChartsTests
//
// Created by Xuan Liu on 2019/3/20.
//

import XCTest
import FBSnapshotTestCase
@testable import Charts

class HorizontalBarChartTests: FBSnapshotTestCase
{

override func setUp()
{
super.setUp()
// Set to `true` to re-capture all snapshots
self.recordMode = false
}

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

//MARK: Prepare
func setupCustomValuesDataEntries(values: [Double]) -> [ChartDataEntry]
{
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)))
}
return entries
}

func setupStackedvaluesDataEntries() -> [ChartDataEntry]
liuxuan30 marked this conversation as resolved.
Show resolved Hide resolved
{
var entries: [ChartDataEntry] = Array()
entries.append(BarChartDataEntry(x: 0, yValues: [28, 50, 60, 30, 42], icon: UIImage(named: "icon")))
entries.append(BarChartDataEntry(x: 1, yValues: [-20, -36, -52, -40, -15], icon: UIImage(named: "icon")))
entries.append(BarChartDataEntry(x: 2, yValues: [10, 30, 40, 90, 72], icon: UIImage(named: "icon")))
entries.append(BarChartDataEntry(x: 3, yValues: [-40, -50, -30, -60, -20], icon: UIImage(named: "icon")))
entries.append(BarChartDataEntry(x: 4, yValues: [10, 40, 60, 45, 62], icon: UIImage(named: "icon")))
return entries
}

func setupDefaultValuesDataEntries() -> [ChartDataEntry]
{
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]
return setupCustomValuesDataEntries(values: values)
}

func setupDefaultDataSet(chartDataEntries: [ChartDataEntry]) -> BarChartDataSet
{
let dataSet = BarChartDataSet(entries: chartDataEntries, label: "Bar chart unit test data")
dataSet.drawIconsEnabled = false
dataSet.iconsOffset = CGPoint(x: 0, y: -10.0)
return dataSet
}

func setupDefaultStackedDataSet(chartDataEntries: [ChartDataEntry]) -> BarChartDataSet
{
let dataSet = BarChartDataSet(entries: chartDataEntries, label: "Stacked bar chart unit test data")
dataSet.drawIconsEnabled = false
dataSet.iconsOffset = CGPoint(x: 0, y: -10.0)
dataSet.colors = Array(arrayLiteral:NSUIColor(red: 46/255.0, green: 204/255.0, blue: 113/255.0, alpha: 1.0),
NSUIColor(red: 241/255.0, green: 196/255.0, blue: 15/255.0, alpha: 1.0),
NSUIColor(red: 231/255.0, green: 76/255.0, blue: 60/255.0, alpha: 1.0),
NSUIColor(red: 52/255.0, green: 152/255.0, blue: 219/255.0, alpha: 1.0)
)
return dataSet
}

func setupDefaultChart(dataSets: [BarChartDataSet]) -> BarChartView
{
let data = BarChartData(dataSets: dataSets)
data.barWidth = 0.85

let chart = HorizontalBarChartView(frame: CGRect(x: 0, y: 0, width: 480, height: 350))
chart.backgroundColor = NSUIColor.clear
chart.data = data
return chart
}

//MARK: Start Test
func testDefaultValues()
{
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
ChartsSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), overallTolerance: Snapshot.tolerance)
}

func testHidesValues()
{
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
dataSet.drawValuesEnabled = false
chart.notifyDataSetChanged()
ChartsSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), overallTolerance: Snapshot.tolerance)
}

func testNotDrawValueAboveBars()
{
let dataEntries = setupDefaultValuesDataEntries()
let dataSet = setupDefaultDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.drawValueAboveBarEnabled = false
chart.notifyDataSetChanged()
ChartsSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), overallTolerance: Snapshot.tolerance)
}

func testStackedDrawValues()
{
let dataEntries = setupStackedvaluesDataEntries()
let dataSet = setupDefaultStackedDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.notifyDataSetChanged()
ChartsSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), overallTolerance: Snapshot.tolerance)
}

func testStackedNotDrawValues()
{
let dataEntries = setupStackedvaluesDataEntries()
let dataSet = setupDefaultStackedDataSet(chartDataEntries: dataEntries)
dataSet.drawValuesEnabled = false
let chart = setupDefaultChart(dataSets: [dataSet])
chart.notifyDataSetChanged()
ChartsSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), overallTolerance: Snapshot.tolerance)
}

func testStackedNotDrawValuesAboveBars()
{
let dataEntries = setupStackedvaluesDataEntries()
let dataSet = setupDefaultStackedDataSet(chartDataEntries: dataEntries)
let chart = setupDefaultChart(dataSets: [dataSet])
chart.drawValueAboveBarEnabled = false
chart.notifyDataSetChanged()
ChartsSnapshotVerifyView(chart, identifier: Snapshot.identifier(UIScreen.main.bounds.size), overallTolerance: 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.
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.
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.