├── .DS_Store
├── YKLineChartViewExample
├── YKLineChartView.gif
├── YKLineChartView.xcodeproj
│ ├── project.xcworkspace
│ │ └── contents.xcworkspacedata
│ └── project.pbxproj
├── YKLineChartView
│ ├── ViewController.h
│ ├── KLineChartViewController.h
│ ├── TimeLineChartViewController.h
│ ├── AppDelegate.h
│ ├── main.m
│ ├── Assets.xcassets
│ │ └── AppIcon.appiconset
│ │ │ └── Contents.json
│ ├── ViewController.m
│ ├── Info.plist
│ ├── Base.lproj
│ │ ├── LaunchScreen.storyboard
│ │ └── Main.storyboard
│ ├── AppDelegate.m
│ ├── TimeLineChartViewController.m
│ └── KLineChartViewController.m
├── YKLineChartViewTests
│ ├── Info.plist
│ └── YKLineChartViewTests.m
└── YKLineChartViewUITests
│ ├── Info.plist
│ └── YKLineChartViewUITests.m
├── YKlineChartView
├── YKLineChart.h
├── YKLineDataSet.m
├── YKLineEntity.m
├── YKTimeLineView.h
├── YKLineChartView.h
├── YKLineEntity.h
├── YKViewBase.h
├── YKLineDataSet.h
├── YKLineChartViewBase.h
├── YKViewBase.m
├── YKLineChartViewBase.m
├── YKTimeLineView.m
└── YKLineChartView.m
├── .gitignore
├── YKLineChartView.podspec
├── LICENSE
└── README.md
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chenyk0317/YKLineChartView/HEAD/.DS_Store
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/chenyk0317/YKLineChartView/HEAD/YKLineChartViewExample/YKLineChartView.gif
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/YKlineChartView/YKLineChart.h:
--------------------------------------------------------------------------------
1 | //
2 | // YKLineChart.h
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/11.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 |
9 |
10 | #import "YKLineChartView.h"
11 | #import "YKLineEntity.h"
12 | #import "YKLineDataSet.h"
13 | #import "YKTimeLineView.h"
14 |
15 |
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView/ViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.h
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/8.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 | //
9 |
10 | #import
11 | @interface ViewController : UIViewController
12 |
13 |
14 | @end
15 |
16 |
--------------------------------------------------------------------------------
/YKlineChartView/YKLineDataSet.m:
--------------------------------------------------------------------------------
1 | //
2 | // YKLineDataSet.m
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/9.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 |
9 | #import "YKLineDataSet.h"
10 |
11 | @implementation YKLineDataSet
12 |
13 | @end
14 |
15 | @implementation YKTimeDataset
16 |
17 |
18 |
19 | @end
--------------------------------------------------------------------------------
/YKlineChartView/YKLineEntity.m:
--------------------------------------------------------------------------------
1 | //
2 | // YKLineEntity.m
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/9.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 |
9 | #import "YKLineEntity.h"
10 |
11 | @implementation YKLineEntity
12 |
13 | @end
14 |
15 | @implementation YKTimeLineEntity
16 |
17 |
18 |
19 | @end
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView/KLineChartViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // KLineChartViewController.h
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/12.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 | //
9 |
10 | #import
11 |
12 | @interface KLineChartViewController : UIViewController
13 |
14 | @end
15 |
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView/TimeLineChartViewController.h:
--------------------------------------------------------------------------------
1 | //
2 | // TimeLineChartViewController.h
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/12.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 | //
9 |
10 | #import
11 |
12 | @interface TimeLineChartViewController : UIViewController
13 |
14 | @end
15 |
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView/AppDelegate.h:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.h
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/8.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 | //
9 |
10 | #import
11 |
12 | @interface AppDelegate : UIResponder
13 |
14 | @property (strong, nonatomic) UIWindow *window;
15 |
16 |
17 | @end
18 |
19 |
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView/main.m:
--------------------------------------------------------------------------------
1 | //
2 | // main.m
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/8.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 | //
9 |
10 | #import
11 | #import "AppDelegate.h"
12 |
13 | int main(int argc, char * argv[]) {
14 | @autoreleasepool {
15 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Xcode
2 | #
3 | build/
4 | *.pbxuser
5 | !default.pbxuser
6 | *.mode1v3
7 | !default.mode1v3
8 | *.mode2v3
9 | !default.mode2v3
10 | *.perspectivev3
11 | !default.perspectivev3
12 | xcuserdata
13 | *.xccheckout
14 | *.moved-aside
15 | DerivedData
16 | *.hmap
17 | *.ipa
18 | *.xcuserstate
19 |
20 | # CocoaPods
21 | #
22 | # We recommend against adding the Pods directory to your .gitignore. However
23 | # you should judge for yourself, the pros and cons are mentioned at:
24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control
25 | #
26 | #Pods/
27 |
--------------------------------------------------------------------------------
/YKlineChartView/YKTimeLineView.h:
--------------------------------------------------------------------------------
1 | //
2 | // YKTimeLineView.h
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/10.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 |
9 | #import "YKLineChartViewBase.h"
10 | #import "YKLineDataSet.h"
11 |
12 | @interface YKTimeLineView : YKLineChartViewBase
13 |
14 |
15 | @property (nonatomic,assign)CGFloat offsetMaxPrice;
16 | @property (nonatomic,assign)NSInteger countOfTimes;
17 |
18 | @property (nonatomic,assign)BOOL endPointShowEnabled;
19 | @property (nonatomic,assign)BOOL isDrawAvgEnabled;
20 |
21 | - (void)setupData:(YKTimeDataset *)dataSet;
22 | @end
23 |
--------------------------------------------------------------------------------
/YKLineChartView.podspec:
--------------------------------------------------------------------------------
1 | #
2 | Pod::Spec.new do |s|
3 | s.name = 'YKLineChartView'
4 | s.summary = 'chart framework for iOS.'
5 | s.version = '0.3'
6 | s.license = { :type => 'MIT', :file => 'LICENSE' }
7 | s.authors = { 'chenyk0317' => 'chenyk0317@163.com' }
8 | s.social_media_url = 'http://chenyk.com'
9 | s.homepage = 'https://github.com/chenyk0317/YKLineChartView'
10 | s.platform = :ios, '7.0'
11 | s.ios.deployment_target = '7.0'
12 | s.source = { :git => 'https://github.com/chenyk0317/YKLineChartView.git', :tag => s.version.to_s }
13 |
14 | s.requires_arc = true
15 | s.source_files = 'YKLineChartView/*.{h,m}'
16 | s.public_header_files = 'YKLineChartView/*.{h}'
17 | s.frameworks = 'UIKit', 'CoreFoundation'
18 |
19 | end
20 |
--------------------------------------------------------------------------------
/YKlineChartView/YKLineChartView.h:
--------------------------------------------------------------------------------
1 | //
2 | // YKLineChartView.h
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/9.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 |
9 | #import "YKLineChartViewBase.h"
10 |
11 | @class YKLineDataSet;
12 | @interface YKLineChartView : YKLineChartViewBase
13 |
14 |
15 | @property (nonatomic,assign)CGFloat candleWidth;
16 | @property (nonatomic,assign)CGFloat candleMaxWidth;
17 | @property (nonatomic,assign)CGFloat candleMinWidth;
18 |
19 | @property (nonatomic,assign)BOOL isShowAvgMarkerEnabled;
20 |
21 | @property (nonatomic,strong)NSDictionary * avgLabelAttributedDic;
22 |
23 |
24 | - (void)setupData:(YKLineDataSet *)dataSet;
25 |
26 | - (void)addDataSetWithArray:(NSArray *)array;
27 | @end
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "29x29",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "29x29",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "40x40",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "40x40",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "60x60",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "60x60",
31 | "scale" : "3x"
32 | }
33 | ],
34 | "info" : {
35 | "version" : 1,
36 | "author" : "xcode"
37 | }
38 | }
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartViewTests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleSignature
20 | ????
21 | CFBundleVersion
22 | 1
23 |
24 |
25 |
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartViewUITests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleSignature
20 | ????
21 | CFBundleVersion
22 | 1
23 |
24 |
25 |
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView/ViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.m
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/8.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 | //
9 |
10 | #import "ViewController.h"
11 |
12 | @interface ViewController ()
13 |
14 | @property (nonatomic,assign)BOOL isScrollLeft;
15 | @property (nonatomic,strong)NSMutableArray * addArray;
16 | @end
17 |
18 | @implementation ViewController
19 |
20 | - (void)viewDidLoad {
21 | [super viewDidLoad];
22 |
23 | // Do any additional setup after loading the view, typically from a nib.
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | }
33 |
34 |
35 |
36 |
37 |
38 | - (void)didReceiveMemoryWarning {
39 | [super didReceiveMemoryWarning];
40 | // Dispose of any resources that can be recreated.
41 | }
42 |
43 | @end
44 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 chenyk0317
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartViewTests/YKLineChartViewTests.m:
--------------------------------------------------------------------------------
1 | //
2 | // YKLineChartViewTests.m
3 | // YKLineChartViewTests
4 | //
5 | // Created by chenyk on 15/12/8.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 | //
9 |
10 | #import
11 |
12 | @interface YKLineChartViewTests : XCTestCase
13 |
14 | @end
15 |
16 | @implementation YKLineChartViewTests
17 |
18 | - (void)setUp {
19 | [super setUp];
20 | // Put setup code here. This method is called before the invocation of each test method in the class.
21 | }
22 |
23 | - (void)tearDown {
24 | // Put teardown code here. This method is called after the invocation of each test method in the class.
25 | [super tearDown];
26 | }
27 |
28 | - (void)testExample {
29 | // This is an example of a functional test case.
30 | // Use XCTAssert and related functions to verify your tests produce the correct results.
31 | }
32 |
33 | - (void)testPerformanceExample {
34 | // This is an example of a performance test case.
35 | [self measureBlock:^{
36 | // Put the code you want to measure the time of here.
37 | }];
38 | }
39 |
40 | @end
41 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | YKLineChartView
2 | ==============
3 |
4 | [](https://github.com/chenyk0317/YKLineChartView/blob/master/LICENSE)
5 | [](http://cocoapods.org/?q= YKLineChartView)
6 | [](http://cocoapods.org/?q= YKLineChartView)
7 | [](https://www.apple.com/nl/ios/)
8 | [](https://www.apple.com/)
9 |
10 |
11 | Stock chart framework for iOS.
12 |
13 | 
14 |
15 |
16 |
17 | Installation
18 | ==============
19 |
20 | ### CocoaPods
21 |
22 | 1. Add `pod "YKLineChartView"` to your Podfile.
23 | 2. Run `pod install` or `pod update`.
24 | 3. Import \.
25 |
26 |
27 | ### Manually
28 |
29 | 1. Download all the files in the YKLineChartView subdirectory.
30 | 2. Add the source files to your Xcode project.
31 | 3. Import `YKLineChart.h`.
32 |
33 |
--------------------------------------------------------------------------------
/YKlineChartView/YKLineEntity.h:
--------------------------------------------------------------------------------
1 | //
2 | // YKLineEntity.h
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/9.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 |
9 | #import
10 | #import
11 | @interface YKLineEntity : NSObject
12 | @property (nonatomic,assign)CGFloat open;
13 | @property (nonatomic,assign)CGFloat high;
14 | @property (nonatomic,assign)CGFloat low;
15 | @property (nonatomic,assign)CGFloat close;
16 | @property (nonatomic,assign)NSInteger index;
17 | @property (nonatomic,strong)NSString * date;
18 |
19 | @property (nonatomic,assign)CGFloat volume;
20 | @property (nonatomic,assign)CGFloat ma5;
21 | @property (nonatomic,assign)CGFloat ma10;
22 | @property (nonatomic,assign)CGFloat ma20;
23 | @property (nonatomic,assign)CGFloat preClosePx;
24 | @property (nonatomic,strong)NSString * rate;
25 |
26 | @end
27 |
28 |
29 | @interface YKTimeLineEntity : NSObject
30 | @property (nonatomic,strong)NSString * currtTime;
31 | @property (nonatomic,assign)CGFloat preClosePx;
32 | @property (nonatomic,assign)CGFloat avgPirce;
33 | @property (nonatomic,assign)CGFloat lastPirce;
34 | @property (nonatomic,assign)CGFloat totalVolume;
35 | @property (nonatomic,assign)CGFloat volume;
36 | @property (nonatomic,assign)CGFloat trade;
37 | @property (nonatomic,strong)NSString * rate;
38 |
39 | @end
40 |
--------------------------------------------------------------------------------
/YKlineChartView/YKViewBase.h:
--------------------------------------------------------------------------------
1 | //
2 | // YKViewBase.h
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/10.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 |
9 | #import
10 | /**
11 | * @author LiuK, 16-05-16 17:05:55
12 | *
13 | * 1、修复view在autolayout下计算Frame的BUG
14 | */
15 | @interface YKViewBase : UIView
16 | @property (nonatomic,assign) CGRect contentRect;
17 | @property (nonatomic,assign) CGFloat chartHeight;
18 | @property (nonatomic,assign) CGFloat chartWidth;
19 |
20 |
21 |
22 |
23 | - (void)setupChartOffsetWithLeft:(CGFloat)left
24 | top:(CGFloat)top
25 | right:(CGFloat)right
26 | bottom:(CGFloat)bottom;
27 |
28 | - (void)notifyDataSetChanged;
29 |
30 | - (void)notifyDeviceOrientationChanged;
31 | - (BOOL)isInBoundsX:(CGFloat)x;
32 |
33 | - (BOOL)isInBoundsY:(CGFloat)y;
34 |
35 | - (BOOL)isInBoundsX:(CGFloat)x
36 | y:(CGFloat)y;
37 |
38 | - (BOOL)isInBoundsLeft:(CGFloat)x;
39 | - (BOOL)isInBoundsRight:(CGFloat)x;
40 |
41 | - (BOOL)isInBoundsTop:(CGFloat)y;
42 |
43 | - (BOOL)isInBoundsBottom:(CGFloat)y;
44 |
45 | - (CGFloat)contentTop;
46 | - (CGFloat)contentLeft;
47 | - (CGFloat)contentRight;
48 | - (CGFloat)contentBottom;
49 | - (CGFloat)contentWidth;
50 | - (CGFloat)contentHeight;
51 | @end
52 |
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | en
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleSignature
20 | ????
21 | CFBundleVersion
22 | 1
23 | LSRequiresIPhoneOS
24 |
25 | UILaunchStoryboardName
26 | LaunchScreen
27 | UIMainStoryboardFile
28 | Main
29 | UIRequiredDeviceCapabilities
30 |
31 | armv7
32 |
33 | UISupportedInterfaceOrientations
34 |
35 | UIInterfaceOrientationLandscapeRight
36 | UIInterfaceOrientationPortrait
37 | UIInterfaceOrientationPortraitUpsideDown
38 | UIInterfaceOrientationLandscapeLeft
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartViewUITests/YKLineChartViewUITests.m:
--------------------------------------------------------------------------------
1 | //
2 | // YKLineChartViewUITests.m
3 | // YKLineChartViewUITests
4 | //
5 | // Created by chenyk on 15/12/8.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 | //
9 |
10 | #import
11 |
12 | @interface YKLineChartViewUITests : XCTestCase
13 |
14 | @end
15 |
16 | @implementation YKLineChartViewUITests
17 |
18 | - (void)setUp {
19 | [super setUp];
20 |
21 | // Put setup code here. This method is called before the invocation of each test method in the class.
22 |
23 | // In UI tests it is usually best to stop immediately when a failure occurs.
24 | self.continueAfterFailure = NO;
25 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
26 | [[[XCUIApplication alloc] init] launch];
27 |
28 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
29 | }
30 |
31 | - (void)tearDown {
32 | // Put teardown code here. This method is called after the invocation of each test method in the class.
33 | [super tearDown];
34 | }
35 |
36 | - (void)testExample {
37 | // Use recording to get started writing UI tests.
38 | // Use XCTAssert and related functions to verify your tests produce the correct results.
39 | }
40 |
41 | @end
42 |
--------------------------------------------------------------------------------
/YKlineChartView/YKLineDataSet.h:
--------------------------------------------------------------------------------
1 | //
2 | // YKLineDataSet.h
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/9.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 |
9 | #import
10 | #import
11 | @interface YKLineDataSet : NSObject
12 | @property (nonatomic,strong)NSMutableArray * data;
13 | @property (nonatomic,assign)CGFloat highlightLineWidth;
14 | @property (nonatomic,strong)UIColor * highlightLineColor;
15 | @property (nonatomic,strong)UIColor * candleRiseColor;
16 | @property (nonatomic,strong)UIColor * candleFallColor;
17 | @property (nonatomic,strong)UIColor * avgMA5Color;
18 | @property (nonatomic,strong)UIColor * avgMA10Color;
19 | @property (nonatomic,strong)UIColor * avgMA20Color;
20 | @property (nonatomic,assign)CGFloat avgLineWidth;
21 | @property (nonatomic,assign)CGFloat candleTopBottmLineWidth;
22 |
23 |
24 | @end
25 |
26 |
27 | @interface YKTimeDataset : NSObject
28 | @property (nonatomic,strong)NSMutableArray * data;
29 | @property (nonatomic,assign)CGFloat highlightLineWidth;
30 | @property (nonatomic,strong)UIColor * highlightLineColor;
31 | @property (nonatomic,assign)CGFloat lineWidth;
32 | @property (nonatomic,strong)UIColor * priceLineCorlor;
33 | @property (nonatomic,strong)UIColor * avgLineCorlor;
34 |
35 | @property (nonatomic,strong)UIColor * volumeRiseColor;
36 | @property (nonatomic,strong)UIColor * volumeFallColor;
37 | @property (nonatomic,strong)UIColor * volumeTieColor;
38 |
39 | @property (nonatomic,assign)BOOL drawFilledEnabled;
40 | @property (nonatomic,strong)UIColor * fillStartColor;
41 | @property (nonatomic,strong)UIColor * fillStopColor;
42 | @property (nonatomic,assign)CGFloat fillAlpha;
43 |
44 |
45 | @end
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView/AppDelegate.m:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.m
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/8.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 | //
9 |
10 | #import "AppDelegate.h"
11 |
12 | @interface AppDelegate ()
13 |
14 | @end
15 |
16 | @implementation AppDelegate
17 |
18 |
19 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
20 | // Override point for customization after application launch.
21 | return YES;
22 | }
23 |
24 | - (void)applicationWillResignActive:(UIApplication *)application {
25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
26 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
27 | }
28 |
29 | - (void)applicationDidEnterBackground:(UIApplication *)application {
30 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
31 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
32 | }
33 |
34 | - (void)applicationWillEnterForeground:(UIApplication *)application {
35 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
36 | }
37 |
38 | - (void)applicationDidBecomeActive:(UIApplication *)application {
39 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
40 | }
41 |
42 | - (void)applicationWillTerminate:(UIApplication *)application {
43 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
44 | }
45 |
46 | @end
47 |
--------------------------------------------------------------------------------
/YKlineChartView/YKLineChartViewBase.h:
--------------------------------------------------------------------------------
1 | //
2 | // YKLineChartViewBase.h
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/9.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 |
9 | #import
10 | #import "YKViewBase.h"
11 |
12 |
13 | @protocol YKLineChartViewDelegate
14 |
15 | @optional
16 | - (void)chartValueSelected:(YKViewBase *)chartView entry:(id)entry entryIndex:(NSInteger)entryIndex;
17 | - (void)chartValueNothingSelected:(YKViewBase *)chartView;
18 |
19 | - (void)chartKlineScrollLeft:(YKViewBase *)chartView;
20 |
21 |
22 | @end
23 |
24 | @interface YKLineChartViewBase : YKViewBase
25 |
26 |
27 | @property (nonatomic,assign) CGFloat uperChartHeightScale;
28 | @property (nonatomic,assign) CGFloat xAxisHeitht;
29 |
30 | @property (nonatomic,strong) UIColor *gridBackgroundColor;
31 | @property (nonatomic,strong) UIColor *borderColor;
32 | @property (nonatomic,assign) CGFloat borderWidth;
33 |
34 |
35 | @property (nonatomic,assign)CGFloat maxPrice;
36 | @property (nonatomic,assign)CGFloat minPrice;
37 | @property (nonatomic,assign)CGFloat maxVolume;
38 | @property (nonatomic,assign)CGFloat candleCoordsScale;
39 | @property (nonatomic,assign)CGFloat volumeCoordsScale;
40 |
41 | @property (nonatomic,assign)NSInteger highlightLineCurrentIndex;
42 | @property (nonatomic,assign)CGPoint highlightLineCurrentPoint;
43 | @property (nonatomic,assign)BOOL highlightLineCurrentEnabled;
44 |
45 | @property (nonatomic,strong)NSDictionary * leftYAxisAttributedDic;
46 | @property (nonatomic,strong)NSDictionary * xAxisAttributedDic;
47 | @property (nonatomic,strong)NSDictionary * highlightAttributedDic;
48 | @property (nonatomic,strong)NSDictionary * defaultAttributedDic;
49 |
50 | @property (nonatomic,assign)BOOL highlightLineShowEnabled;
51 | @property (nonatomic,assign)BOOL scrollEnabled;
52 | @property (nonatomic,assign)BOOL zoomEnabled;
53 |
54 | @property (nonatomic,assign)BOOL leftYAxisIsInChart;
55 | @property (nonatomic,assign)BOOL rightYAxisDrawEnabled;
56 |
57 | @property (nonatomic,assign)id delegate;
58 |
59 |
60 | @property (nonatomic,assign)BOOL isETF;
61 |
62 |
63 | - (void)drawline:(CGContextRef)context
64 | startPoint:(CGPoint)startPoint
65 | stopPoint:(CGPoint)stopPoint
66 | color:(UIColor *)color
67 | lineWidth:(CGFloat)lineWitdth;
68 |
69 | - (void)drawLabelPrice:(CGContextRef)context;
70 |
71 | //圆点
72 | -(void)drawCiclyPoint:(CGContextRef)context
73 | point:(CGPoint)point
74 | radius:(CGFloat)radius
75 | color:(UIColor*)color;
76 | - (void)drawHighlighted:(CGContextRef)context
77 | point:(CGPoint)point
78 | idex:(NSInteger)idex
79 | value:(id)value
80 | color:(UIColor *)color
81 | lineWidth:(CGFloat)lineWidth;
82 |
83 |
84 | - (void)drawLabel:(CGContextRef)context
85 | attributesText:(NSAttributedString *)attributesText
86 | rect:(CGRect)rect;
87 |
88 | - (void)drawRect:(CGContextRef)context
89 | rect:(CGRect)rect
90 | color:(UIColor*)color;
91 |
92 |
93 | - (void)drawGridBackground:(CGContextRef)context
94 | rect:(CGRect)rect;
95 |
96 |
97 |
98 | @end
99 |
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView/TimeLineChartViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // TimeLineChartViewController.m
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/12.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 | //
9 |
10 | #import "TimeLineChartViewController.h"
11 | #import "YKLineChart.h"
12 |
13 | @interface TimeLineChartViewController ()
14 |
15 | @property (weak, nonatomic) IBOutlet YKTimeLineView *timeLineView;
16 |
17 |
18 | @end
19 |
20 | @implementation TimeLineChartViewController
21 |
22 | - (void)viewDidLoad {
23 | [super viewDidLoad];
24 | // Do any additional setup after loading the view.
25 |
26 | NSString * path =[[NSBundle mainBundle]pathForResource:@"data.plist" ofType:nil];
27 |
28 | NSArray * sourceArray = [[NSDictionary dictionaryWithContentsOfFile:path] objectForKey:@"data3"];
29 | NSMutableArray * timeArray = [NSMutableArray array];
30 | for (NSDictionary * dic in sourceArray) {
31 | YKTimeLineEntity * e = [[YKTimeLineEntity alloc]init];
32 | e.currtTime = dic[@"curr_time"];
33 | e.preClosePx = [dic[@"pre_close_px"] doubleValue];
34 | e.avgPirce = [dic[@"avg_pirce"] doubleValue];
35 | e.lastPirce = [dic[@"last_px"]doubleValue];
36 | e.volume = [dic[@"last_volume_trade"]doubleValue];
37 | e.rate = dic[@"rise_and_fall_rate"];
38 | [timeArray addObject:e];
39 | }
40 |
41 | [self.timeLineView setupChartOffsetWithLeft:50 top:10 right:10 bottom:10];
42 | self.timeLineView.gridBackgroundColor = [UIColor whiteColor];
43 | self.timeLineView.borderColor = [UIColor colorWithRed:203/255.0 green:215/255.0 blue:224/255.0 alpha:1.0];
44 | self.timeLineView.borderWidth = .5;
45 | self.timeLineView.uperChartHeightScale = 0.7;
46 | self.timeLineView.xAxisHeitht = 25;
47 | self.timeLineView.countOfTimes = 242;
48 | self.timeLineView.endPointShowEnabled = YES;
49 |
50 | YKTimeDataset * set = [[YKTimeDataset alloc]init];
51 | set.data = timeArray;
52 | set.avgLineCorlor = [UIColor colorWithRed:253/255.0 green:179/255.0 blue:8/255.0 alpha:1.0];
53 | set.priceLineCorlor = [UIColor colorWithRed:24/255.0 green:96/255.0 blue:254/255.0 alpha:1.0];
54 | set.lineWidth = 1.f;
55 | set.highlightLineWidth = .8f;
56 | set.highlightLineColor = [UIColor colorWithRed:60/255.0 green:76/255.0 blue:109/255.0 alpha:1.0];
57 |
58 | set.volumeTieColor = [UIColor grayColor];
59 | set.volumeRiseColor = [UIColor colorWithRed:233/255.0 green:47/255.0 blue:68/255.0 alpha:1.0];
60 | set.volumeFallColor = [UIColor colorWithRed:33/255.0 green:179/255.0 blue:77/255.0 alpha:1.0];
61 |
62 | set.fillStartColor = [UIColor colorWithRed:24/255.0 green:96/255.0 blue:254/255.0 alpha:1.0];
63 | set.fillStopColor = [UIColor whiteColor];
64 | set.fillAlpha = .5f;
65 | set.drawFilledEnabled = YES;
66 | self.timeLineView.delegate = self;
67 | self.timeLineView.highlightLineShowEnabled = YES;
68 | [self.timeLineView setupData:set];
69 |
70 | }
71 |
72 | -(void)chartValueSelected:(YKViewBase *)chartView entry:(id)entry entryIndex:(NSInteger)entryIndex
73 | {
74 |
75 | }
76 |
77 | - (void)chartValueNothingSelected:(YKViewBase *)chartView
78 | {
79 | }
80 |
81 | - (void)chartKlineScrollLeft:(YKViewBase *)chartView
82 | {
83 |
84 | }
85 |
86 | - (void)didReceiveMemoryWarning {
87 | [super didReceiveMemoryWarning];
88 | // Dispose of any resources that can be recreated.
89 | }
90 |
91 | /*
92 | #pragma mark - Navigation
93 |
94 | // In a storyboard-based application, you will often want to do a little preparation before navigation
95 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
96 | // Get the new view controller using [segue destinationViewController].
97 | // Pass the selected object to the new view controller.
98 | }
99 | */
100 |
101 | @end
102 |
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView/KLineChartViewController.m:
--------------------------------------------------------------------------------
1 | //
2 | // KLineChartViewController.m
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/12.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 | //
9 |
10 | #import "KLineChartViewController.h"
11 | #import "YKLineChart.h"
12 | @interface KLineChartViewController ()
13 | @property (weak, nonatomic) IBOutlet YKLineChartView *klineView;
14 |
15 | @end
16 |
17 | @implementation KLineChartViewController
18 |
19 | - (void)viewDidLoad {
20 | [super viewDidLoad];
21 | // Do any additional setup after loading the view.
22 |
23 | NSString * path =[[NSBundle mainBundle]pathForResource:@"data.plist" ofType:nil];
24 | NSArray * sourceArray = [[NSDictionary dictionaryWithContentsOfFile:path] objectForKey:@"data"];
25 | NSMutableArray * array = [NSMutableArray array];
26 | for (NSDictionary * dic in sourceArray) {
27 |
28 | YKLineEntity * entity = [[YKLineEntity alloc]init];
29 | entity.high = [dic[@"high_px"] doubleValue];
30 | entity.open = [dic[@"open_px"] doubleValue];
31 |
32 | entity.low = [dic[@"low_px"] doubleValue];
33 |
34 | entity.close = [dic[@"close_px"] doubleValue];
35 |
36 | entity.date = dic[@"date"];
37 | entity.ma5 = [dic[@"avg5"] doubleValue];
38 | entity.ma10 = [dic[@"avg10"] doubleValue];
39 | entity.ma20 = [dic[@"avg20"] doubleValue];
40 | entity.volume = [dic[@"total_volume_trade"] doubleValue];
41 | [array addObject:entity];
42 | //YTimeLineEntity * entity = [[YTimeLineEntity alloc]init];
43 | }
44 | [array addObjectsFromArray:array];
45 | YKLineDataSet * dataset = [[YKLineDataSet alloc]init];
46 | dataset.data = array;
47 | dataset.highlightLineColor = [UIColor colorWithRed:60/255.0 green:76/255.0 blue:109/255.0 alpha:1.0];
48 | dataset.highlightLineWidth = 0.7;
49 | dataset.candleRiseColor = [UIColor colorWithRed:233/255.0 green:47/255.0 blue:68/255.0 alpha:1.0];
50 | dataset.candleFallColor = [UIColor colorWithRed:33/255.0 green:179/255.0 blue:77/255.0 alpha:1.0];
51 | dataset.avgLineWidth = 1.f;
52 | dataset.avgMA10Color = [UIColor colorWithRed:252/255.0 green:85/255.0 blue:198/255.0 alpha:1.0];
53 | dataset.avgMA5Color = [UIColor colorWithRed:67/255.0 green:85/255.0 blue:109/255.0 alpha:1.0];
54 | dataset.avgMA20Color = [UIColor colorWithRed:216/255.0 green:192/255.0 blue:44/255.0 alpha:1.0];
55 | dataset.candleTopBottmLineWidth = 1;
56 |
57 | [self.klineView setupChartOffsetWithLeft:50 top:10 right:10 bottom:10];
58 | self.klineView.gridBackgroundColor = [UIColor whiteColor];
59 | self.klineView.borderColor = [UIColor colorWithRed:203/255.0 green:215/255.0 blue:224/255.0 alpha:1.0];
60 | self.klineView.borderWidth = .5;
61 | self.klineView.candleWidth = 8;
62 | self.klineView.candleMaxWidth = 30;
63 | self.klineView.candleMinWidth = 1;
64 | self.klineView.uperChartHeightScale = 0.7;
65 | self.klineView.xAxisHeitht = 25;
66 | self.klineView.delegate = self;
67 | self.klineView.highlightLineShowEnabled = YES;
68 | self.klineView.zoomEnabled = YES;
69 | self.klineView.scrollEnabled = YES;
70 | [self.klineView setupData:dataset];
71 | }
72 |
73 | -(void)chartValueSelected:(YKViewBase *)chartView entry:(id)entry entryIndex:(NSInteger)entryIndex
74 | {
75 |
76 | }
77 |
78 | - (void)chartValueNothingSelected:(YKViewBase *)chartView
79 | {
80 | }
81 |
82 | - (void)chartKlineScrollLeft:(YKViewBase *)chartView
83 | {
84 |
85 | }
86 |
87 |
88 |
89 | - (void)didReceiveMemoryWarning {
90 | [super didReceiveMemoryWarning];
91 | // Dispose of any resources that can be recreated.
92 | }
93 |
94 | /*
95 | #pragma mark - Navigation
96 |
97 | // In a storyboard-based application, you will often want to do a little preparation before navigation
98 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
99 | // Get the new view controller using [segue destinationViewController].
100 | // Pass the selected object to the new view controller.
101 | }
102 | */
103 |
104 | @end
105 |
--------------------------------------------------------------------------------
/YKlineChartView/YKViewBase.m:
--------------------------------------------------------------------------------
1 | //
2 | // YKViewBase.m
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/10.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 |
9 | #import "YKViewBase.h"
10 |
11 | @interface YKViewBase()
12 | @property (nonatomic,assign)CGFloat offsetLeft;
13 | @property (nonatomic,assign)CGFloat offsetTop;
14 | @property (nonatomic,assign)CGFloat offsetRight;
15 | @property (nonatomic,assign)CGFloat offsetBottom;
16 | @end
17 | @implementation YKViewBase
18 |
19 | - (id)initWithCoder:(NSCoder *)aDecoder {
20 | self = [super initWithCoder:aDecoder];
21 | if (self) {
22 | [self addObserver];
23 | // [self commonInit];
24 | }
25 | return self;
26 | }
27 |
28 | - (id)initWithFrame:(CGRect)frame {
29 | self = [super initWithFrame:frame];
30 | if (self) {
31 | [self addObserver];
32 | // [self commonInit];
33 |
34 | }
35 | return self;
36 | }
37 | - (void)addObserver
38 | {
39 | //@2016-5-16 by Liuk autolayout不会触发bounds, frame
40 | // [self addObserver:self forKeyPath:@"bounds" options:NSKeyValueObservingOptionNew context:nil];
41 | // [self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
42 |
43 | [[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications];
44 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
45 | }
46 | //- (void)commonInit {
47 | //
48 | // //[self setChartDimens:self.bounds.size.width height:self.bounds.size.height];
49 | //
50 | //}
51 | - (void)dealloc
52 | {
53 | // [self removeObserver:self forKeyPath:@"bounds"];
54 | // [self removeObserver:self forKeyPath:@"frame"];
55 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
56 | }
57 | //@2016-5-16 by Liuk autolayout不会触发bounds, frame
58 | //- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
59 | //{
60 | // if ([keyPath isEqualToString:@"bounds"] || [keyPath isEqualToString: @"frame"])
61 | // {
62 | // CGRect bounds = self.bounds;
63 | //
64 | // if ((bounds.size.width != self.chartWidth ||
65 | // bounds.size.height != self.chartHeight))
66 | // {
67 | // [self setChartDimens:bounds.size.width height:bounds.size.height];
68 | // [self notifyDataSetChanged];
69 | //
70 | // }
71 | // }
72 | //
73 | //}
74 |
75 | -(void)layoutSubviews{
76 | //@2016-5-16 byliukai, 布局调整
77 | CGRect bounds = self.bounds;
78 | if ((bounds.size.width != self.chartWidth ||
79 | bounds.size.height != self.chartHeight))
80 | {
81 | [self setChartDimens:bounds.size.width height:bounds.size.height];
82 | [self notifyDataSetChanged];
83 |
84 | }
85 | }
86 | -(void) deviceOrientationDidChange:(NSNotification *) notification
87 | {
88 | if([UIDevice currentDevice].orientation!=UIDeviceOrientationUnknown)
89 | {
90 | [self notifyDeviceOrientationChanged];
91 | }
92 | }
93 |
94 | - (void)notifyDeviceOrientationChanged
95 | {
96 |
97 | }
98 | - (void)notifyDataSetChanged
99 | {
100 | [self setNeedsDisplay];
101 |
102 | }
103 |
104 | - (void)setupChartOffsetWithLeft:(CGFloat)left top:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom
105 | {
106 | self.offsetLeft = left;
107 | self.offsetRight = right;
108 | self.offsetTop = top;
109 | self.offsetBottom = bottom;
110 | //by LiuK 需要设置_contentRect
111 | [self restrainViewPort:left top:top right:right bottom:bottom];
112 |
113 | }
114 | - (void)setChartDimens:(CGFloat)width
115 | height:(CGFloat)height
116 | {
117 | CGFloat offsetLeft = self.offsetLeft;
118 | CGFloat offsetTop = self.offsetTop;
119 | CGFloat offsetRight = self.offsetRight;
120 | CGFloat offsetBottom = self.offsetBottom;
121 | self.chartHeight = height;
122 | self.chartWidth = width;
123 | [self restrainViewPort:offsetLeft top:offsetTop right:offsetRight bottom:offsetBottom];
124 | }
125 | - (void)restrainViewPort:(CGFloat)offsetLeft
126 | top:(CGFloat)offsetTop
127 | right:(CGFloat)offsetRight
128 | bottom:(CGFloat)offsetBottom
129 | {
130 | _contentRect.origin.x = offsetLeft;
131 | _contentRect.origin.y = offsetTop;
132 | _contentRect.size.width = self.chartWidth - offsetLeft - offsetRight;
133 | _contentRect.size.height = self.chartHeight - offsetBottom - offsetTop;
134 | }
135 |
136 |
137 | - (BOOL)isInBoundsX:(CGFloat)x
138 | {
139 | if ([self isInBoundsLeft:x] && [self isInBoundsRight:x])
140 | {
141 | return YES;
142 | }
143 | else
144 | {
145 | return NO;
146 | }
147 | }
148 | - (BOOL)isInBoundsY:(CGFloat)y
149 | {
150 | if ([self isInBoundsTop:y] &&[self isInBoundsBottom:y])
151 | {
152 | return YES;
153 | }
154 | else
155 | {
156 | return NO;
157 | }
158 | }
159 |
160 | - (BOOL)isInBoundsX:(CGFloat)x y:(CGFloat)y
161 | {
162 | if ([ self isInBoundsX:x] && [self isInBoundsY:y])
163 | {
164 | return YES;
165 | }
166 | else
167 | {
168 | return NO;
169 | }
170 | }
171 | - (BOOL)isInBoundsLeft:(CGFloat)x
172 | {
173 | return _contentRect.origin.x <= x ? YES : NO;
174 | }
175 | - (BOOL)isInBoundsRight:(CGFloat)x
176 | {
177 | CGFloat normalizedX = ((NSInteger)(x * 100.f))/100.f;
178 | return (_contentRect.origin.x + _contentRect.size.width) >= normalizedX ? YES : NO;
179 | }
180 | - (BOOL)isInBoundsTop:(CGFloat)y
181 | {
182 | return _contentRect.origin.y <= y ? YES : NO;
183 | }
184 | - (BOOL)isInBoundsBottom:(CGFloat)y
185 | {
186 | CGFloat normalizedY = ((NSInteger)(y * 100.f))/100.f;
187 | return (_contentRect.origin.y + _contentRect.size.height) >= normalizedY ? YES : NO;
188 |
189 | }
190 | - (CGFloat)contentTop
191 | {
192 | return _contentRect.origin.y;
193 | }
194 |
195 | - (CGFloat)contentLeft
196 | {
197 | return _contentRect.origin.x;
198 | }
199 | - (CGFloat)contentRight
200 | {
201 | return _contentRect.origin.x + _contentRect.size.width;
202 | }
203 | - (CGFloat)contentBottom
204 | {
205 | return _contentRect.origin.y + _contentRect.size.height;
206 | }
207 | - (CGFloat)contentWidth
208 | {
209 | return _contentRect.size.width;
210 | }
211 | - (CGFloat)contentHeight
212 | {
213 | return _contentRect.size.height;
214 | }
215 |
216 | /*
217 | // Only override drawRect: if you perform custom drawing.
218 | // An empty implementation adversely affects performance during animation.
219 | - (void)drawRect:(CGRect)rect {
220 | // Drawing code
221 | }
222 | */
223 |
224 | @end
225 |
--------------------------------------------------------------------------------
/YKLineChartViewExample/YKLineChartView/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
27 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
--------------------------------------------------------------------------------
/YKlineChartView/YKLineChartViewBase.m:
--------------------------------------------------------------------------------
1 | //
2 | // YKLineChartViewBase.m
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/9.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 |
9 | #import "YKLineChartViewBase.h"
10 | #import "YKLineEntity.h"
11 | @interface YKLineChartViewBase()
12 |
13 |
14 | @end
15 | @implementation YKLineChartViewBase
16 | - (id)initWithCoder:(NSCoder *)aDecoder {
17 | self = [super initWithCoder:aDecoder];
18 | if (self) {
19 | // [self commonInit];
20 | }
21 | return self;
22 | }
23 |
24 | - (id)initWithFrame:(CGRect)frame {
25 | self = [super initWithFrame:frame];
26 | if (self) {
27 | // [self commonInit];
28 |
29 | }
30 | return self;
31 | }
32 |
33 | //- (void)commonInit {
34 | //
35 | //
36 | //}
37 |
38 | - (void)drawRect:(CGRect)rect
39 | {
40 | [super drawRect:rect];
41 | }
42 | - (void)drawGridBackground:(CGContextRef)context
43 | rect:(CGRect)rect;
44 | {
45 | UIColor * backgroundColor = self.gridBackgroundColor?:[UIColor whiteColor];
46 | CGContextSetFillColorWithColor(context, backgroundColor.CGColor);
47 | CGContextFillRect(context, rect);
48 |
49 | //画边框
50 | CGContextSetLineWidth(context, self.borderWidth);
51 | CGContextSetStrokeColorWithColor(context, self.borderColor.CGColor);
52 | CGContextStrokeRect(context, CGRectMake(self.contentLeft, self.contentTop, self.contentWidth, (self.uperChartHeightScale * self.contentHeight)));
53 |
54 | CGContextStrokeRect(context, CGRectMake(self.contentLeft, (self.uperChartHeightScale * self.contentHeight)+self.xAxisHeitht, self.contentWidth, (self.contentBottom - (self.uperChartHeightScale * self.contentHeight)-self.xAxisHeitht)));
55 |
56 | //画中间的线
57 | [self drawline:context startPoint:CGPointMake(self.contentLeft,(self.uperChartHeightScale * self.contentHeight)/2.0 + self.contentTop) stopPoint:CGPointMake(self.contentRight, (self.uperChartHeightScale * self.contentHeight)/2.0 + self.contentTop) color:self.borderColor lineWidth:self.borderWidth/2.0];
58 |
59 | }
60 |
61 | - (void)drawLabelPrice:(CGContextRef)context
62 | {
63 |
64 | UIColor * labelBGColor = [UIColor colorWithWhite:1.0 alpha:0.3];
65 | NSDictionary * drawAttributes = self.leftYAxisAttributedDic?:self.defaultAttributedDic;
66 | //@2016-5-12 by Liuk, 价格统一往左移2个像素,价格标签把图标的线遮挡了
67 | NSString * maxPriceStr = [self handleStrWithPrice:self.maxPrice];
68 | NSMutableAttributedString * maxPriceAttStr = [[NSMutableAttributedString alloc]initWithString:maxPriceStr attributes:drawAttributes];
69 | CGSize sizeMaxPriceAttStr = [maxPriceAttStr size];
70 | CGRect maxPriceRect = CGRectMake(self.contentLeft - (self.leftYAxisIsInChart?0:sizeMaxPriceAttStr.width+2), self.contentTop, sizeMaxPriceAttStr.width, sizeMaxPriceAttStr.height);
71 | [self drawRect:context rect:maxPriceRect color:labelBGColor];
72 | [self drawLabel:context attributesText:maxPriceAttStr rect:maxPriceRect];
73 |
74 | NSString * midPriceStr = [self handleStrWithPrice:(self.maxPrice+self.minPrice)/2.0];
75 | NSMutableAttributedString * midPriceAttStr = [[NSMutableAttributedString alloc]initWithString:midPriceStr attributes:drawAttributes];
76 | CGSize sizeMidPriceAttStr = [midPriceAttStr size];
77 | CGRect midPriceRect = CGRectMake(self.contentLeft - (self.leftYAxisIsInChart?0:sizeMidPriceAttStr.width+2), ((self.uperChartHeightScale * self.contentHeight)/2.0 + self.contentTop)-sizeMidPriceAttStr.height/2.0, sizeMidPriceAttStr.width, sizeMidPriceAttStr.height);
78 | [self drawRect:context rect:midPriceRect color:labelBGColor];
79 | [self drawLabel:context attributesText:midPriceAttStr rect:midPriceRect];
80 |
81 | NSString * minPriceStr = [self handleStrWithPrice:self.minPrice];
82 | NSMutableAttributedString * minPriceAttStr = [[NSMutableAttributedString alloc]initWithString:minPriceStr attributes:drawAttributes];
83 | CGSize sizeMinPriceAttStr = [minPriceAttStr size];
84 | CGRect minPriceRect = CGRectMake(self.contentLeft - (self.leftYAxisIsInChart?0:sizeMinPriceAttStr.width+2), ((self.uperChartHeightScale * self.contentHeight) + self.contentTop - sizeMinPriceAttStr.height ), sizeMinPriceAttStr.width, sizeMinPriceAttStr.height);
85 | [self drawRect:context rect:minPriceRect color:labelBGColor];
86 | [self drawLabel:context attributesText:minPriceAttStr rect:minPriceRect];
87 |
88 | NSMutableAttributedString * zeroVolumeAttStr = [[NSMutableAttributedString alloc]initWithString:[self handleShowWithVolume:self.maxVolume] attributes:drawAttributes];
89 | CGSize zeroVolumeAttStrSize = [zeroVolumeAttStr size];
90 | CGRect zeroVolumeRect = CGRectMake(self.contentLeft - (self.leftYAxisIsInChart?0:zeroVolumeAttStrSize.width+2), self.contentBottom - zeroVolumeAttStrSize.height, zeroVolumeAttStrSize.width, zeroVolumeAttStrSize.height);
91 | [self drawRect:context rect:zeroVolumeRect color:labelBGColor];
92 | [self drawLabel:context attributesText:zeroVolumeAttStr rect:zeroVolumeRect];
93 |
94 | NSString * maxVolumeStr = [self handleShowNumWithVolume:self.maxVolume];
95 | NSMutableAttributedString * maxVolumeAttStr = [[NSMutableAttributedString alloc]initWithString:maxVolumeStr attributes:drawAttributes];
96 | CGSize maxVolumeAttStrSize = [maxVolumeAttStr size];
97 | CGRect maxVolumeRect = CGRectMake(self.contentLeft - (self.leftYAxisIsInChart?0:maxVolumeAttStrSize.width+2), (self.uperChartHeightScale * self.contentHeight)+self.xAxisHeitht, maxVolumeAttStrSize.width, maxVolumeAttStrSize.height);
98 | [self drawRect:context rect:maxVolumeRect color:labelBGColor];
99 | [self drawLabel:context attributesText:maxVolumeAttStr rect:maxVolumeRect];
100 |
101 |
102 | if (self.rightYAxisDrawEnabled) {
103 | NSString * maxRateStr = [self handleRateWithPrice:self.maxPrice originPX:(self.maxPrice+self.minPrice)/2.0];
104 | NSMutableAttributedString * maxRateAttStr = [[NSMutableAttributedString alloc]initWithString:maxRateStr attributes:drawAttributes];
105 | CGSize sizeMaxRateAttStr = [maxRateAttStr size];
106 | CGRect maxRateRect = CGRectMake(self.contentRight- (self.leftYAxisIsInChart?sizeMaxRateAttStr.width:0), self.contentTop, sizeMaxRateAttStr.width, sizeMaxRateAttStr.height);
107 | [self drawRect:context rect:maxRateRect color:labelBGColor];
108 | [self drawLabel:context attributesText:maxRateAttStr rect:maxRateRect];
109 |
110 | NSString * minRateStr = [self handleRateWithPrice:self.minPrice originPX:(self.maxPrice+self.minPrice)/2.0];
111 | NSMutableAttributedString * minRateAttStr = [[NSMutableAttributedString alloc]initWithString:minRateStr attributes:drawAttributes];
112 | CGSize sizeMinRateAttStr = [minRateAttStr size];
113 | CGRect minRateRect = CGRectMake(self.contentRight-(self.leftYAxisIsInChart?sizeMinRateAttStr.width:0), ((self.uperChartHeightScale * self.contentHeight) + self.contentTop - sizeMinRateAttStr.height ), sizeMinRateAttStr.width, sizeMinRateAttStr.height);
114 | [self drawRect:context rect:minRateRect color:labelBGColor];
115 | [self drawLabel:context attributesText:minRateAttStr rect:minRateRect];
116 | }
117 |
118 | }
119 |
120 |
121 | - (void)drawHighlighted:(CGContextRef)context
122 | point:(CGPoint)point
123 | idex:(NSInteger)idex
124 | value:(id)value
125 | color:(UIColor *)color
126 | lineWidth:(CGFloat)lineWidth
127 | {
128 |
129 | NSString * leftMarkerStr = @"";
130 | NSString * bottomMarkerStr = @"";
131 | NSString * rightMarkerStr = @"";
132 | NSString * volumeMarkerStr = @"";
133 |
134 |
135 | if ([value isKindOfClass:[YKTimeLineEntity class]]) {
136 | YKTimeLineEntity * entity = value;
137 | leftMarkerStr = [self handleStrWithPrice:entity.lastPirce];
138 | bottomMarkerStr = entity.currtTime;
139 | rightMarkerStr = entity.rate;
140 |
141 | }else if([value isKindOfClass:[YKLineEntity class]]){
142 | YKLineEntity * entity = value;
143 | leftMarkerStr = [self handleStrWithPrice:entity.close];
144 | bottomMarkerStr = entity.date;
145 | volumeMarkerStr = [NSString stringWithFormat:@"%@%@",[self handleShowNumWithVolume:entity.volume],[self handleShowWithVolume:entity.volume]];
146 | }else{
147 | return;
148 | }
149 |
150 | if (nil == leftMarkerStr || nil == bottomMarkerStr || nil == rightMarkerStr) {
151 | return;
152 | }
153 | bottomMarkerStr = [[@" " stringByAppendingString:bottomMarkerStr] stringByAppendingString:@" "];
154 | CGContextSetStrokeColorWithColor(context,color.CGColor);
155 | CGContextSetLineWidth(context, lineWidth);
156 | CGContextBeginPath(context);
157 | CGContextMoveToPoint(context, point.x, self.contentTop);
158 | CGContextAddLineToPoint(context, point.x, self.contentBottom);
159 | CGContextStrokePath(context);
160 |
161 |
162 | CGContextBeginPath(context);
163 | CGContextMoveToPoint(context, self.contentLeft, point.y);
164 | CGContextAddLineToPoint(context, self.contentRight, point.y);
165 | CGContextStrokePath(context);
166 |
167 | CGFloat radius = 3.0;
168 | CGContextSetFillColorWithColor(context, color.CGColor);
169 | CGContextFillEllipseInRect(context, CGRectMake(point.x-(radius/2.0), point.y-(radius/2.0), radius, radius));
170 |
171 |
172 | NSDictionary * drawAttributes = self.highlightAttributedDic?:self.defaultAttributedDic;
173 |
174 |
175 | NSMutableAttributedString * leftMarkerStrAtt = [[NSMutableAttributedString alloc]initWithString:leftMarkerStr attributes:drawAttributes];
176 |
177 | CGSize leftMarkerStrAttSize = [leftMarkerStrAtt size];
178 | [self drawLabel:context attributesText:leftMarkerStrAtt rect:CGRectMake(self.contentLeft - leftMarkerStrAttSize.width,point.y - leftMarkerStrAttSize.height/2.0, leftMarkerStrAttSize.width, leftMarkerStrAttSize.height)];
179 |
180 |
181 | NSMutableAttributedString * bottomMarkerStrAtt = [[NSMutableAttributedString alloc]initWithString:bottomMarkerStr attributes:drawAttributes];
182 |
183 | CGSize bottomMarkerStrAttSize = [bottomMarkerStrAtt size];
184 | CGRect rect = CGRectMake(point.x - bottomMarkerStrAttSize.width/2.0, ((self.uperChartHeightScale * self.contentHeight) + self.contentTop), bottomMarkerStrAttSize.width, bottomMarkerStrAttSize.height);
185 | if (rect.size.width + rect.origin.x > self.contentRight) {
186 | rect.origin.x = self.contentRight -rect.size.width;
187 | }
188 | if (rect.origin.x < self.contentLeft) {
189 | rect.origin.x = self.contentLeft;
190 | }
191 | [self drawLabel:context attributesText:bottomMarkerStrAtt rect:rect];
192 |
193 |
194 | NSMutableAttributedString * rightMarkerStrAtt = [[NSMutableAttributedString alloc]initWithString:rightMarkerStr attributes:drawAttributes];
195 | CGSize rightMarkerStrAttSize = [rightMarkerStrAtt size];
196 | [self drawLabel:context attributesText:rightMarkerStrAtt rect:CGRectMake(self.contentRight, point.y - rightMarkerStrAttSize.height/2.0, rightMarkerStrAttSize.width, rightMarkerStrAttSize.height)];
197 |
198 | NSMutableAttributedString * volumeMarkerStrAtt = [[NSMutableAttributedString alloc]initWithString:volumeMarkerStr attributes:drawAttributes];
199 | CGSize volumeMarkerStrAttSize = [volumeMarkerStrAtt size];
200 | [self drawLabel:context attributesText:volumeMarkerStrAtt rect:CGRectMake(self.contentLeft, self.contentHeight * self.uperChartHeightScale+self.xAxisHeitht, volumeMarkerStrAttSize.width, volumeMarkerStrAttSize.height)];
201 |
202 |
203 |
204 | }
205 |
206 | - (void)drawLabel:(CGContextRef)context
207 | attributesText:(NSAttributedString *)attributesText
208 | rect:(CGRect)rect
209 | {
210 | [attributesText drawInRect:rect];
211 | //[self drawRect:context rect:rect color:[UIColor clearColor]];
212 | }
213 |
214 | - (void)drawRect:(CGContextRef)context
215 | rect:(CGRect)rect
216 | color:(UIColor*)color
217 | {
218 | if ((rect.origin.x + rect.size.width) > self.contentRight) {
219 | return;
220 | }
221 | CGContextSetFillColorWithColor(context, color.CGColor);
222 | CGContextFillRect(context, rect);
223 | }
224 | -(void)drawCiclyPoint:(CGContextRef)context
225 | point:(CGPoint)point
226 | radius:(CGFloat)radius
227 | color:(UIColor*)color{
228 | CGContextSetFillColorWithColor(context, color.CGColor);//填充颜色
229 | CGContextSetStrokeColorWithColor(context, color.CGColor);
230 | CGContextSetLineWidth(context, 1.0);//线的宽度
231 | CGContextAddArc(context, point.x, point.y, radius, 0, 2*M_PI, 0); //添加一个圆
232 | CGContextDrawPath(context, kCGPathFillStroke); //绘制路径加填充
233 | }
234 | - (void)drawline:(CGContextRef)context
235 | startPoint:(CGPoint)startPoint
236 | stopPoint:(CGPoint)stopPoint
237 | color:(UIColor *)color
238 | lineWidth:(CGFloat)lineWitdth
239 | {
240 | if (startPoint.x < self.contentLeft ||stopPoint.x >self.contentRight || startPoint.y 0){
261 | return [NSString stringWithFormat:@"+%.2f%@",rate,@"%"];
262 |
263 | }
264 | return [NSString stringWithFormat:@"%.2f%@",rate,@"%"];
265 | }
266 |
267 | - (NSString *)handleStrWithPrice:(CGFloat)price
268 | {
269 | if (self.isETF) {
270 | return [NSString stringWithFormat:@"%.3f ",price];
271 | }
272 | return [NSString stringWithFormat:@"%.2f ",price];
273 | }
274 | - (NSString *)handleShowWithVolume:(CGFloat)volume
275 | {
276 | volume = volume/100.0;
277 |
278 | if (volume < 10000.0) {
279 | return @"手 ";
280 | }else if (volume > 10000.0 && volume < 100000000.0){
281 | return @"万手 ";
282 | }else{
283 | return @"亿手 ";
284 | }
285 | }
286 | - (NSString *)handleShowNumWithVolume:(CGFloat)volume
287 | {
288 | volume = volume/100.0;
289 | if (volume < 10000.0) {
290 | return [NSString stringWithFormat:@"%.0f ",volume];
291 | }else if (volume > 10000.0 && volume < 100000000.0){
292 | return [NSString stringWithFormat:@"%.2f ",volume/10000.0];
293 | }else{
294 | return [NSString stringWithFormat:@"%.2f ",volume/100000000.0];
295 | }
296 | }
297 |
298 | - (NSDictionary *)defaultAttributedDic
299 | {
300 | if (!_defaultAttributedDic) {
301 | _defaultAttributedDic = @{NSFontAttributeName:[UIFont systemFontOfSize:10],NSBackgroundColorAttributeName:[UIColor clearColor]};
302 | }
303 | return _defaultAttributedDic;
304 | }
305 |
306 | - (void)setHighlightLineCurrentEnabled:(BOOL)highlightLineCurrentEnabled
307 | {
308 |
309 | if (_highlightLineCurrentEnabled != highlightLineCurrentEnabled) {
310 | _highlightLineCurrentEnabled = highlightLineCurrentEnabled;
311 | if ( NO == highlightLineCurrentEnabled) {
312 | if ([self.delegate respondsToSelector:@selector(chartValueNothingSelected:)]) {
313 | [self.delegate chartValueNothingSelected:self];
314 | }
315 | }
316 | }
317 | }
318 |
319 | /*
320 | // Only override drawRect: if you perform custom drawing.
321 | // An empty implementation adversely affects performance during animation.
322 | - (void)drawRect:(CGRect)rect {
323 | // Drawing code
324 | }
325 | */
326 |
327 | @end
328 |
--------------------------------------------------------------------------------
/YKlineChartView/YKTimeLineView.m:
--------------------------------------------------------------------------------
1 | //
2 | // YKTimeLineView.m
3 | // YKLineChartView
4 | //
5 | // Created by chenyk on 15/12/10.
6 | // Copyright © 2015年 chenyk. All rights reserved.
7 | // https://github.com/chenyk0317/YKLineChartView
8 |
9 | #import "YKTimeLineView.h"
10 | #import "YKLineEntity.h"
11 | @interface YKTimeLineView()
12 |
13 | @property (nonatomic,assign) CGFloat volumeWidth;
14 | @property (nonatomic,strong)YKTimeDataset * dataset;
15 | @property (nonatomic,strong)UILongPressGestureRecognizer * longPressGesture;
16 | @property (nonatomic,strong)UITapGestureRecognizer * tapGesture;
17 | @property (nonatomic,strong)CALayer * breathingPoint;
18 |
19 | @end
20 | @implementation YKTimeLineView
21 |
22 | - (id)initWithCoder:(NSCoder *)aDecoder {
23 | self = [super initWithCoder:aDecoder];
24 | if (self) {
25 | [self commonInit];
26 | }
27 | return self;
28 | }
29 |
30 | - (id)initWithFrame:(CGRect)frame {
31 | self = [super initWithFrame:frame];
32 | if (self) {
33 | [self commonInit];
34 |
35 | }
36 | return self;
37 | }
38 |
39 | - (CGFloat)volumeWidth
40 | {
41 | return self.contentWidth/self.countOfTimes;
42 | }
43 | - (void)setupData:(YKTimeDataset *)dataSet
44 | {
45 |
46 | self.dataset = dataSet;
47 | [self notifyDataSetChanged];
48 | }
49 | - (void)commonInit {
50 |
51 | self.candleCoordsScale = 0.f;
52 | [self addGestureRecognizer:self.longPressGesture];
53 | [self addGestureRecognizer:self.tapGesture];
54 | }
55 |
56 |
57 | - (void)setCurrentDataMaxAndMin
58 | {
59 |
60 |
61 | if (self.dataset.data.count > 0) {
62 | self.maxPrice = CGFLOAT_MIN;
63 | self.minPrice = CGFLOAT_MAX;
64 | self.maxVolume = CGFLOAT_MIN;
65 | self.offsetMaxPrice = CGFLOAT_MIN;
66 | for (NSInteger i = 0; i < self.dataset.data.count; i++) {
67 | YKTimeLineEntity * entity = [self.dataset.data objectAtIndex:i];
68 |
69 | self.offsetMaxPrice = self.offsetMaxPrice >fabs(entity.lastPirce-entity.preClosePx)?self.offsetMaxPrice:fabs(entity.lastPirce-entity.preClosePx);
70 | if (entity.avgPirce) {
71 | self.offsetMaxPrice = self.offsetMaxPrice > fabs(entity.avgPirce - entity.preClosePx)?self.offsetMaxPrice:fabs(entity.avgPirce - entity.preClosePx);
72 | }
73 | self.maxVolume = self.maxVolume >entity.volume ? self.maxVolume : entity.volume;
74 | }
75 | self.maxPrice =((YKTimeLineEntity *)[self.dataset.data firstObject]).preClosePx + self.offsetMaxPrice;
76 | self.minPrice =((YKTimeLineEntity *)[self.dataset.data firstObject]).preClosePx - self.offsetMaxPrice;
77 |
78 | if (self.maxPrice == self.minPrice) {
79 | self.maxPrice = self.maxPrice + 0.01;
80 | self.minPrice = self.minPrice - 0.01;
81 | }
82 | for (NSInteger i = 0; i < self.dataset.data.count; i++) {
83 | YKTimeLineEntity * entity = [self.dataset.data objectAtIndex:i];
84 | if (entity.avgPirce != 0) {
85 | entity.avgPirce = entity.avgPirce < self.minPrice ? self.minPrice :entity.avgPirce;
86 | entity.avgPirce = entity.avgPirce > self.maxPrice ? self.maxPrice :entity.avgPirce;
87 | }
88 | }
89 |
90 |
91 | }
92 | }
93 |
94 | - (void)drawRect:(CGRect)rect
95 | {
96 | [super drawRect:rect];
97 |
98 | [self setCurrentDataMaxAndMin];
99 |
100 |
101 | CGContextRef context = UIGraphicsGetCurrentContext();
102 |
103 |
104 |
105 | [self drawGridBackground:context rect:rect];
106 | [self drawTimeLabel:context];
107 | if (self.dataset.data.count>0) {
108 | [self drawTimeLine:context];
109 | }
110 | [self drawLabelPrice:context];
111 |
112 |
113 | }
114 |
115 | - (void)drawGridBackground:(CGContextRef)context rect:(CGRect)rect
116 | {
117 | [super drawGridBackground:context rect:rect];
118 | [self drawline:context startPoint:CGPointMake(self.contentWidth/2.0 + self.contentLeft, self.contentTop) stopPoint:CGPointMake(self.contentWidth/2.0 + self.contentLeft,(self.uperChartHeightScale * self.contentHeight)+self.contentTop) color:self.borderColor lineWidth:self.borderWidth/2.0];
119 | }
120 | - (void)drawTimeLabel:(CGContextRef)context
121 | {
122 | NSDictionary * drawAttributes = self.xAxisAttributedDic ?: self.defaultAttributedDic;
123 |
124 | NSMutableAttributedString * startTimeAttStr = [[NSMutableAttributedString alloc]initWithString:@"9:30" attributes:drawAttributes];
125 | CGSize sizeStartTimeAttStr = [startTimeAttStr size];
126 | [self drawLabel:context attributesText:startTimeAttStr rect:CGRectMake(self.contentLeft, (self.uperChartHeightScale * self.contentHeight+self.contentTop), sizeStartTimeAttStr.width, sizeStartTimeAttStr.height)];
127 |
128 | NSMutableAttributedString * midTimeAttStr = [[NSMutableAttributedString alloc]initWithString:@"11:30/13:00" attributes:drawAttributes];
129 | CGSize sizeMidTimeAttStr = [midTimeAttStr size];
130 | [self drawLabel:context attributesText:midTimeAttStr rect:CGRectMake(self.contentWidth/2.0 + self.contentLeft - sizeMidTimeAttStr.width/2.0, (self.uperChartHeightScale * self.contentHeight+self.contentTop), sizeMidTimeAttStr.width, sizeMidTimeAttStr.height)];
131 |
132 | NSMutableAttributedString * stopTimeAttStr = [[NSMutableAttributedString alloc]initWithString:@"15:00" attributes:drawAttributes];
133 | CGSize sizeStopTimeAttStr = [stopTimeAttStr size];
134 | [self drawLabel:context attributesText:stopTimeAttStr rect:CGRectMake(self.contentRight -sizeStopTimeAttStr.width, (self.uperChartHeightScale * self.contentHeight+self.contentTop), sizeStopTimeAttStr.width, sizeStopTimeAttStr.height)];
135 |
136 |
137 | }
138 | - (void)drawTimeLine:(CGContextRef)context
139 | {
140 | CGContextSaveGState(context);
141 |
142 | self.candleCoordsScale = (self.uperChartHeightScale * self.contentHeight)/(self.maxPrice-self.minPrice);
143 | self.volumeCoordsScale = (self.contentHeight - (self.uperChartHeightScale * self.contentHeight)-self.xAxisHeitht)/(self.maxVolume - 0);
144 |
145 | CGMutablePathRef fillPath = CGPathCreateMutable();
146 |
147 | for (NSInteger i = 0 ; i< self.dataset.data.count; i ++) {
148 |
149 | YKTimeLineEntity * entity = [self.dataset.data objectAtIndex:i];
150 |
151 | CGFloat left = (self.volumeWidth * i + self.contentLeft) + self.volumeWidth / 6.0;
152 |
153 | CGFloat candleWidth = self.volumeWidth - self.volumeWidth / 6.0;
154 | CGFloat startX = left + candleWidth/2.0 ;
155 | CGFloat yPrice = 0;
156 |
157 | UIColor * color = self.dataset.volumeRiseColor;
158 |
159 | if (i > 0){
160 | YKTimeLineEntity * lastEntity = [self.dataset.data objectAtIndex:i -1];
161 | CGFloat lastX = startX - self.volumeWidth;
162 |
163 | CGFloat lastYPrice = (self.maxPrice - lastEntity.lastPirce)*self.candleCoordsScale + self.contentTop;
164 |
165 | yPrice = (self.maxPrice - entity.lastPirce)*self.candleCoordsScale + self.contentTop;
166 |
167 | [self drawline:context startPoint:CGPointMake(lastX, lastYPrice) stopPoint:CGPointMake(startX, yPrice) color:self.dataset.priceLineCorlor lineWidth:self.dataset.lineWidth];
168 |
169 | if (self.isDrawAvgEnabled) {
170 | if (lastEntity.avgPirce > 0 && entity.avgPirce > 0) {
171 | CGFloat lastYAvg = (self.maxPrice - lastEntity.avgPirce)*self.candleCoordsScale + self.contentTop;
172 | CGFloat yAvg = (self.maxPrice - entity.avgPirce)*self.candleCoordsScale + self.contentTop;
173 |
174 | [self drawline:context startPoint:CGPointMake(lastX, lastYAvg) stopPoint:CGPointMake(startX, yAvg) color:self.dataset.avgLineCorlor lineWidth:self.dataset.lineWidth];
175 | }
176 | }
177 |
178 |
179 | if (entity.lastPirce > lastEntity.lastPirce) {
180 | color = self.dataset.volumeRiseColor;
181 | }else if (entity.lastPirce < lastEntity.lastPirce){
182 | color = self.dataset.volumeFallColor;
183 | }else{
184 | color = self.dataset.volumeTieColor;
185 | }
186 |
187 | if (1 == i) {
188 | CGPathMoveToPoint(fillPath, NULL, self.contentLeft, (self.uperChartHeightScale * self.contentHeight) + self.contentTop);
189 | CGPathAddLineToPoint(fillPath, NULL, self.contentLeft,lastYPrice);
190 | CGPathAddLineToPoint(fillPath, NULL, lastX, lastYPrice);
191 | }else{
192 | CGPathAddLineToPoint(fillPath, NULL, startX, yPrice);
193 | }
194 | if ((self.dataset.data.count - 1) == i) {
195 | CGPathAddLineToPoint(fillPath, NULL, startX, yPrice);
196 | CGPathAddLineToPoint(fillPath, NULL, startX, (self.uperChartHeightScale * self.contentHeight) + self.contentTop);
197 | CGPathCloseSubpath(fillPath);
198 | }
199 | }
200 |
201 |
202 | //成交量
203 | CGFloat volume = ((entity.volume - 0) * self.volumeCoordsScale);
204 | [self drawRect:context rect:CGRectMake(left, self.contentBottom - volume , candleWidth, volume) color:color];
205 |
206 | //十字线
207 | if (self.highlightLineCurrentEnabled) {
208 | if (i == self.highlightLineCurrentIndex) {
209 | if (i == 0) {
210 | yPrice = (self.maxPrice - entity.lastPirce)*self.candleCoordsScale + self.contentTop;
211 | }
212 |
213 | YKTimeLineEntity * entity;
214 | if (i < self.dataset.data.count) {
215 | entity = [self.dataset.data objectAtIndex:i];
216 | }
217 | [self drawHighlighted:context point:CGPointMake(startX, yPrice) idex:i value:entity color:self.dataset.highlightLineColor lineWidth:self.dataset.highlightLineWidth ];
218 |
219 | if ([self.delegate respondsToSelector:@selector(chartValueSelected:entry:entryIndex:) ]) {
220 | [self.delegate chartValueSelected:self entry:entity entryIndex:i];
221 | }
222 | }
223 | }
224 |
225 | if (self.endPointShowEnabled) {
226 | if (i == self.dataset.data.count - 1 && i != self.countOfTimes-1) {
227 | self.breathingPoint.frame = CGRectMake(startX-4/2, yPrice-4/2,4,4);
228 | }
229 | }
230 |
231 | }
232 |
233 |
234 | if (self.dataset.drawFilledEnabled && self.dataset.data.count > 0) {
235 | [self drawLinearGradient:context path:fillPath alpha:self.dataset.fillAlpha startColor:self.dataset.fillStartColor.CGColor endColor:self.dataset.fillStopColor.CGColor];
236 | }
237 | CGPathRelease(fillPath);
238 |
239 | CGContextRestoreGState(context);
240 |
241 |
242 | for (NSInteger i = 0 ; i< self.dataset.data.count; i ++) {
243 | YKTimeLineEntity * entity = [self.dataset.data objectAtIndex:i];
244 | CGFloat left = (self.volumeWidth * i + self.contentLeft) + self.volumeWidth / 6.0;
245 | CGFloat candleWidth = self.volumeWidth - self.volumeWidth / 6.0;
246 | CGFloat startX = left + candleWidth/2.0 ;
247 | if (i > 0){
248 | YKTimeLineEntity * lastEntity = [self.dataset.data objectAtIndex:i -1];
249 | CGFloat lastX = startX - self.volumeWidth;
250 | if (self.isDrawAvgEnabled) {
251 | if (lastEntity.avgPirce > 0 && entity.avgPirce > 0) {
252 | CGFloat lastYAvg = (self.maxPrice - lastEntity.avgPirce)*self.candleCoordsScale + self.contentTop;
253 | CGFloat yAvg = (self.maxPrice - entity.avgPirce)*self.candleCoordsScale + self.contentTop;
254 |
255 | [self drawline:context startPoint:CGPointMake(lastX, lastYAvg) stopPoint:CGPointMake(startX, yAvg) color:self.dataset.avgLineCorlor lineWidth:self.dataset.lineWidth];
256 | }
257 | }
258 | }
259 | }
260 | }
261 |
262 |
263 | - (UILongPressGestureRecognizer *)longPressGesture
264 | {
265 | if (!_longPressGesture) {
266 | _longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestureAction:)];
267 | _longPressGesture.minimumPressDuration = 0.5;
268 | }
269 | return _longPressGesture;
270 | }
271 | - (void)handleLongPressGestureAction:(UIPanGestureRecognizer *)recognizer
272 | {
273 | if (!self.highlightLineShowEnabled) {
274 | return;
275 | }
276 | if (recognizer.state == UIGestureRecognizerStateBegan) {
277 | CGPoint point = [recognizer locationInView:self];
278 |
279 | if (point.x > self.contentLeft && point.x < self.contentRight && point.y >self.contentTop && point.y self.contentLeft && point.x < self.contentRight && point.y >self.contentTop && point.y self.dataSet.data.count) {
71 | _startDrawIndex = 0;
72 | }
73 | _startDrawIndex = startDrawIndex;
74 | }
75 |
76 | -(void)setupData:(YKLineDataSet *)dataSet
77 | {
78 | self.dataSet = dataSet;
79 | [self notifyDataSetChanged];
80 |
81 | }
82 | - (void)addDataSetWithArray:(NSArray *)array
83 | {
84 | NSArray * tempArray = [self.dataSet.data mutableCopy];
85 | [self.dataSet.data removeAllObjects];
86 | [self.dataSet.data addObjectsFromArray:array];
87 | [self.dataSet.data addObjectsFromArray:tempArray];
88 | self.startDrawIndex += array.count;
89 | [self setCurrentDataMaxAndMin];
90 | [self setNeedsDisplay];
91 | }
92 | - (void)setCurrentDataMaxAndMin
93 | {
94 |
95 | if (self.dataSet.data.count > 0) {
96 | self.maxPrice = CGFLOAT_MIN;
97 | self.minPrice = CGFLOAT_MAX;
98 | self.maxVolume = CGFLOAT_MIN;
99 |
100 | NSInteger idx = self.startDrawIndex;
101 | for (NSInteger i = idx; i < self.startDrawIndex + self.countOfshowCandle && i < self.dataSet.data.count; i++) {
102 | YKLineEntity * entity = [self.dataSet.data objectAtIndex:i];
103 | self.minPrice = self.minPrice < entity.low ? self.minPrice : entity.low;
104 | self.maxPrice = self.maxPrice > entity.high ? self.maxPrice : entity.high;
105 | self.maxVolume = self.maxVolume >entity.volume ? self.maxVolume : entity.volume;
106 | if(entity.ma5>0){
107 | self.minPrice = self.minPrice < entity.ma5 ? self.minPrice:entity.ma5;
108 | self.maxPrice = self.maxPrice > entity.ma5 ? self.maxPrice : entity.ma5;
109 | }
110 | if (entity.ma10 >0) {
111 | self.minPrice = self.minPrice < entity.ma10 ? self.minPrice:entity.ma10;
112 | self.maxPrice = self.maxPrice > entity.ma10 ? self.maxPrice : entity.ma10;
113 | }
114 | if (entity.ma20>0) {
115 | self.minPrice = self.minPrice < entity.ma20 ? self.minPrice:entity.ma20;
116 | self.maxPrice = self.maxPrice > entity.ma20 ? self.maxPrice : entity.ma20;
117 | }
118 |
119 | }
120 |
121 | if (self.maxPrice - self.minPrice < 0.3) {
122 | self.maxPrice +=0.5;
123 | self.minPrice -=0.5;
124 | }
125 | }
126 | }
127 |
128 | - (void)drawRect:(CGRect)rect
129 | {
130 | [super drawRect:rect];
131 |
132 | [self setCurrentDataMaxAndMin];
133 |
134 | CGContextRef optionalContext = UIGraphicsGetCurrentContext();
135 |
136 |
137 |
138 | [self drawGridBackground:optionalContext rect:rect];
139 |
140 |
141 | if (self.dataSet.data.count) {
142 | [self drawCandle:optionalContext];
143 |
144 | }
145 | [self drawLabelPrice:optionalContext];
146 |
147 |
148 | }
149 | - (void)drawGridBackground:(CGContextRef)context rect:(CGRect)rect
150 | {
151 | [super drawGridBackground:context rect:rect];
152 |
153 | }
154 |
155 |
156 |
157 |
158 | - (void)drawAvgMarker:(CGContextRef)context
159 | idex:(NSInteger)idex
160 | isDrawRight:(BOOL)isDrawRight
161 | {
162 |
163 | if (!self.isShowAvgMarkerEnabled) {
164 | return;
165 | }
166 |
167 | YKLineEntity * entity;
168 | if (0 == idex) {
169 | entity = [self.dataSet.data lastObject];
170 | }else{
171 | entity = self.dataSet.data[idex];
172 | }
173 | NSDictionary * drawAttributes = self.avgLabelAttributedDic ?:self.defaultAttributedDic;
174 |
175 |
176 | NSString * ma5Str = [NSString stringWithFormat:@"MA5 %.2f",entity.ma5];
177 | NSMutableAttributedString * ma5StrAtt = [[NSMutableAttributedString alloc]initWithString:ma5Str attributes:drawAttributes];
178 | CGSize ma5StrAttSize = [ma5StrAtt size];
179 |
180 | NSString * ma10Str = [NSString stringWithFormat:@"MA10 %.2f",entity.ma10];
181 | NSMutableAttributedString * ma10StrAtt = [[NSMutableAttributedString alloc]initWithString:ma10Str attributes:drawAttributes];
182 | CGSize ma10StrAttSize = [ma10StrAtt size];
183 |
184 | NSString * ma20Str = [NSString stringWithFormat:@"MA20 %.2f",entity.ma20];
185 | NSMutableAttributedString * ma20StrAtt = [[NSMutableAttributedString alloc]initWithString:ma20Str attributes:drawAttributes];
186 | CGSize ma20StrAttSize = [ma20StrAtt size];
187 |
188 |
189 | CGFloat radius = ma5StrAttSize.height/2;
190 | CGFloat length = ma5StrAttSize.width+ma20StrAttSize.width+ma10StrAttSize.width+radius*8;
191 | CGFloat space = radius;
192 |
193 | CGPoint startP = CGPointMake(self.contentLeft, self.contentTop);
194 | if (isDrawRight) {
195 | startP.x = self.contentRight - length - 4;
196 | }
197 |
198 | startP.y = startP.y+(radius/2.0)+2;
199 | CGFloat labelY = self.contentTop+(radius/4.0);
200 | //Background
201 | UIColor * bgColor = [UIColor colorWithWhite:1 alpha:0.5];
202 | [self drawRect:context rect:CGRectMake(startP.x, self.contentTop+1,length, ma5StrAttSize.height) color:bgColor];
203 |
204 | //=====
205 | CGContextSetFillColorWithColor(context, self.dataSet.avgMA5Color.CGColor);
206 | CGContextFillEllipseInRect(context, CGRectMake(startP.x+(radius/2.0),startP.y , radius, radius));
207 |
208 | startP.x += (radius+space);
209 | [self drawLabel:context attributesText:ma5StrAtt rect:CGRectMake(startP.x,labelY, ma5StrAttSize.width, ma5StrAttSize.height)];
210 | startP.x += (ma5StrAttSize.width + space);
211 |
212 | //=====
213 |
214 | CGContextSetFillColorWithColor(context, self.dataSet.avgMA10Color.CGColor);
215 | CGContextFillEllipseInRect(context, CGRectMake(startP.x+(radius/2.0),startP.y, radius, radius));
216 | startP.x += (radius+space);
217 |
218 | [self drawLabel:context attributesText:ma10StrAtt rect:CGRectMake(startP.x,labelY, ma10StrAttSize.width, ma10StrAttSize.height)];
219 | startP.x += (ma10StrAttSize.width + space);
220 |
221 | //=====
222 | CGContextSetFillColorWithColor(context, self.dataSet.avgMA20Color.CGColor);
223 | CGContextFillEllipseInRect(context, CGRectMake(startP.x+(radius/2.0), startP.y, radius, radius));
224 |
225 | startP.x += (radius+space);
226 |
227 | [self drawLabel:context attributesText:ma20StrAtt rect:CGRectMake(startP.x, labelY, ma20StrAttSize.width, ma20StrAttSize.height)];
228 |
229 | }
230 | - (void)drawCandle:(CGContextRef)context
231 | {
232 | CGContextSaveGState(context);
233 |
234 | NSInteger idex = self.startDrawIndex;
235 | self.candleCoordsScale = (self.uperChartHeightScale * self.contentHeight)/(self.maxPrice-self.minPrice);
236 | self.volumeCoordsScale = (self.contentHeight - (self.uperChartHeightScale * self.contentHeight)-self.xAxisHeitht)/(self.maxVolume - 0);
237 | for (NSInteger i = idex ; i< self.dataSet.data.count; i ++) {
238 |
239 | YKLineEntity * entity = [self.dataSet.data objectAtIndex:i];
240 |
241 | CGFloat open = ((self.maxPrice - entity.open) * self.candleCoordsScale) + self.contentTop;
242 | CGFloat close = ((self.maxPrice - entity.close) * self.candleCoordsScale) + self.contentTop;
243 | CGFloat high = ((self.maxPrice - entity.high) * self.candleCoordsScale) + self.contentTop;
244 | CGFloat low = ((self.maxPrice - entity.low) * self.candleCoordsScale) + self.contentTop;
245 | CGFloat left = (self.candleWidth * (i - idex) + self.contentLeft) + self.candleWidth / 6.0;
246 |
247 | CGFloat candleWidth = self.candleWidth - self.candleWidth / 6.0;
248 | CGFloat startX = left + candleWidth/2.0 ;
249 |
250 |
251 |
252 | //date
253 | //日期
254 | if (i > self.startDrawIndex+5 && i < self.dataSet.data.count - 2) {
255 | if (i % (NSInteger)(180/self.candleWidth) == 0) {
256 | [self drawline:context startPoint:CGPointMake(startX, self.contentTop) stopPoint:CGPointMake(startX, (self.uperChartHeightScale * self.contentHeight)+ self.contentTop) color:self.borderColor lineWidth:0.5];
257 | [self drawline:context startPoint:CGPointMake(startX, (self.uperChartHeightScale * self.contentHeight)+ self.xAxisHeitht) stopPoint:CGPointMake(startX,self.contentBottom) color:self.borderColor lineWidth:0.5];
258 | NSString * date = entity.date;
259 | NSDictionary * drawAttributes = self.xAxisAttributedDic?:self.defaultAttributedDic;
260 | NSMutableAttributedString * dateStrAtt = [[NSMutableAttributedString alloc]initWithString:date attributes:drawAttributes];
261 | CGSize dateStrAttSize = [dateStrAtt size];
262 | [self drawLabel:context attributesText:dateStrAtt rect:CGRectMake(startX - dateStrAttSize.width/2,((self.uperChartHeightScale * self.contentHeight)+ self.contentTop), dateStrAttSize.width, dateStrAttSize.height)];
263 |
264 | }
265 | }
266 |
267 | UIColor * color = self.dataSet.candleRiseColor;
268 | if (open < close) {
269 | color = self.dataSet.candleFallColor;
270 | CGFloat hight = close-open < 1.0?1.0:close-open;
271 | [self drawRect:context rect:CGRectMake(left, open, candleWidth, hight) color:color];
272 | [self drawline:context startPoint:CGPointMake(startX, high) stopPoint:CGPointMake(startX, low) color:color lineWidth:self.dataSet.candleTopBottmLineWidth];
273 | }
274 | else if (open == close) {
275 | if (i > 1) {
276 | YKLineEntity * lastEntity = [self.dataSet.data objectAtIndex:i-1];
277 | if (lastEntity.close > entity.close) {
278 | color = self.dataSet.candleFallColor;
279 | }
280 | }
281 | [self drawRect:context rect:CGRectMake(left, open, candleWidth, 1.5) color:color];
282 | [self drawline:context startPoint:CGPointMake(startX, high) stopPoint:CGPointMake(startX, low) color:color lineWidth:self.dataSet.candleTopBottmLineWidth];
283 | } else {
284 | color = self.dataSet.candleRiseColor;
285 | CGFloat hight = open-close < 1.0?1.0:open-close;
286 | [self drawRect:context rect:CGRectMake(left, close, candleWidth, hight) color:color];
287 | [self drawline:context startPoint:CGPointMake(startX, high) stopPoint:CGPointMake(startX, low) color:color lineWidth:self.dataSet.candleTopBottmLineWidth];
288 | }
289 |
290 |
291 |
292 | if (i > 0){
293 | YKLineEntity * lastEntity = [self.dataSet.data objectAtIndex:i -1];
294 | CGFloat lastX = startX - self.candleWidth;
295 |
296 | CGFloat lastY5 = (self.maxPrice - lastEntity.ma5)*self.candleCoordsScale + self.contentTop;
297 | CGFloat y5 = (self.maxPrice - entity.ma5)*self.candleCoordsScale + self.contentTop;
298 | if (entity.ma5 > 0 && lastEntity.ma5>0) {
299 | [self drawline:context startPoint:CGPointMake(lastX, lastY5) stopPoint:CGPointMake(startX, y5) color:self.dataSet.avgMA5Color lineWidth:self.dataSet.avgLineWidth];
300 | }
301 |
302 |
303 | CGFloat lastY10 = (self.maxPrice - lastEntity.ma10)*self.candleCoordsScale + self.contentTop;
304 | CGFloat y10 = (self.maxPrice - entity.ma10)*self.candleCoordsScale + self.contentTop;
305 | if (entity.ma10 > 0 && lastEntity.ma10 > 0) {
306 | [self drawline:context startPoint:CGPointMake(lastX, lastY10) stopPoint:CGPointMake(startX, y10) color:self.dataSet.avgMA10Color lineWidth:self.dataSet.avgLineWidth];
307 | }
308 |
309 | CGFloat lastY20 = (self.maxPrice - lastEntity.ma20)*self.candleCoordsScale + self.contentTop;
310 | CGFloat y20 = (self.maxPrice - entity.ma20)*self.candleCoordsScale + self.contentTop;
311 | if (entity.ma20 > 0 && lastEntity.ma20 >0) {
312 | [self drawline:context startPoint:CGPointMake(lastX, lastY20) stopPoint:CGPointMake(startX, y20) color:self.dataSet.avgMA20Color lineWidth:self.dataSet.avgLineWidth];
313 | }
314 |
315 | }
316 |
317 | //成交量
318 | CGFloat volume = ((entity.volume - 0) * self.volumeCoordsScale);
319 | [self drawRect:context rect:CGRectMake(left, self.contentBottom - volume , candleWidth, volume) color:color];
320 |
321 | }
322 | for (NSInteger i = idex ; i< self.dataSet.data.count; i ++) {
323 | YKLineEntity * entity = [self.dataSet.data objectAtIndex:i];
324 |
325 | CGFloat close = ((self.maxPrice - entity.close) * self.candleCoordsScale) + self.contentTop;
326 | CGFloat left = (self.candleWidth * (i - idex) + self.contentLeft) + self.candleWidth / 6.0;
327 | CGFloat candleWidth = self.candleWidth - self.candleWidth / 6.0;
328 | CGFloat startX = left + candleWidth/2.0 ;
329 | //十字线
330 | if (self.highlightLineCurrentEnabled) {
331 | if (i == self.highlightLineCurrentIndex) {
332 |
333 | YKLineEntity * entity;
334 | if (i < self.dataSet.data.count) {
335 | entity = [self.dataSet.data objectAtIndex:i];
336 | }
337 | [self drawHighlighted:context point:CGPointMake(startX, close)idex:idex value:entity color:self.dataSet.highlightLineColor lineWidth:self.dataSet.highlightLineWidth];
338 |
339 | BOOL isDrawRight = startX < (self.contentRight)/2.0;
340 | [self drawAvgMarker:context idex:i isDrawRight:isDrawRight];
341 | if ([self.delegate respondsToSelector:@selector(chartValueSelected:entry:entryIndex:) ]) {
342 | [self.delegate chartValueSelected:self entry:entity entryIndex:i];
343 | }
344 | }
345 | }
346 | }
347 |
348 |
349 | if (!self.highlightLineCurrentEnabled) {
350 | [self drawAvgMarker:context idex:0 isDrawRight:NO];;
351 | }
352 | CGContextRestoreGState(context);
353 | }
354 |
355 |
356 | - (UIPanGestureRecognizer *)panGesture
357 | {
358 | if (!_panGesture) {
359 | _panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGestureAction:)];
360 | }
361 | return _panGesture;
362 | }
363 | - (void)handlePanGestureAction:(UIPanGestureRecognizer *)recognizer
364 | {
365 | if (!self.scrollEnabled) {
366 | return;
367 | }
368 |
369 | self.highlightLineCurrentEnabled = NO;
370 |
371 | BOOL isPanRight = NO;
372 |
373 | CGPoint point = [recognizer translationInView:self];
374 |
375 | if (recognizer.state == UIGestureRecognizerStateBegan) {
376 | }
377 | if (recognizer.state == UIGestureRecognizerStateChanged) {
378 | }
379 |
380 | CGFloat offset = point.x;
381 |
382 | NSLog(@"%ld=======,%.2f,%.2f",(long)self.startDrawIndex,offset,[recognizer velocityInView:self].x);
383 |
384 |
385 | if (offset > 0) {
386 |
387 | NSInteger offsetIndex = offset/8.0 ;
388 | NSLog(@"%ld",(long)offsetIndex);
389 |
390 | self.startDrawIndex -= offsetIndex;
391 | if ( self.startDrawIndex < 2) {
392 | if ([self.delegate respondsToSelector:@selector(chartKlineScrollLeft:)]) {
393 | [self.delegate chartKlineScrollLeft:self];
394 | }
395 | }
396 | }else{
397 |
398 | if (self.startDrawIndex + self.countOfshowCandle - (+offset)/self.candleWidth > self.dataSet.data.count) {
399 | isPanRight = YES;
400 | }
401 | NSInteger offsetIndex = (-offset)/8.0;
402 | self.startDrawIndex += offsetIndex;
403 | if (self.startDrawIndex +10 > self.dataSet.data.count) {
404 | self.startDrawIndex = 0;
405 | }
406 |
407 |
408 | }
409 | if (recognizer.state == UIGestureRecognizerStateEnded) {
410 | if (isPanRight) {
411 | [self notifyDataSetChanged];
412 | }
413 | }
414 |
415 | [self setNeedsDisplay];
416 | [recognizer setTranslation:CGPointMake(0, 0) inView:self];
417 |
418 | }
419 |
420 | - (UIPinchGestureRecognizer *)pinGesture
421 | {
422 | if (!_pinGesture) {
423 | _pinGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinGestureAction:)];
424 | }
425 | return _pinGesture;
426 | }
427 |
428 |
429 | - (void)handlePinGestureAction:(UIPinchGestureRecognizer *)recognizer
430 | {
431 | if (!self.zoomEnabled) {
432 | return;
433 | }
434 |
435 | self.highlightLineCurrentEnabled = NO;
436 |
437 | recognizer.scale= recognizer.scale-self.lastPinScale + 1;
438 |
439 | ;
440 | self.candleWidth = recognizer.scale * self.candleWidth;
441 |
442 | if(self.candleWidth > self.candleMaxWidth){
443 | self.candleWidth = self.candleMaxWidth;
444 | }
445 | if(self.candleWidth < self.candleMinWidth){
446 | self.candleWidth = self.candleMinWidth;
447 | }
448 |
449 | //self.startDrawIndex = self.dataSet.data.count - self.countOfshowCandle;
450 | NSInteger offset = (NSInteger)((self.lastPinCount -self.countOfshowCandle)/2);
451 |
452 | if (labs(offset)) {
453 | NSLog(@"offset %ld",(long)offset);
454 | self.lastPinCount = self.countOfshowCandle;
455 | self.startDrawIndex = self.startDrawIndex + offset;
456 | [self setNeedsDisplay];
457 | }
458 |
459 | NSLog(@"%ld",(long)self.startDrawIndex);
460 |
461 | self.lastPinScale = recognizer.scale;
462 |
463 | }
464 |
465 | - (UILongPressGestureRecognizer *)longPressGesture
466 | {
467 | if (!_longPressGesture) {
468 | _longPressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestureAction:)];
469 | _longPressGesture.minimumPressDuration = 0.5;
470 | }
471 | return _longPressGesture;
472 | }
473 | - (void)handleLongPressGestureAction:(UIPanGestureRecognizer *)recognizer
474 | {
475 | if (!self.highlightLineShowEnabled) {
476 | return;
477 | }
478 | if (recognizer.state == UIGestureRecognizerStateBegan) {
479 | CGPoint point = [recognizer locationInView:self];
480 |
481 | if (point.x > self.contentLeft && point.x < self.contentRight && point.y >self.contentTop && point.y self.contentLeft && point.x < self.contentRight && point.y >self.contentTop && point.y