//-------------------------------------------------------------------
//
//  S7GraphView.h
//  S7Touch
//
//  Created by Aleks Nesterow on 9/27/09.
//  aleks.nesterow@gmail.com
//  
//  Thanks to http://snobit.habrahabr.ru/ for releasing sources for his
//  Cocoa component named GraphView.
//  
//  Copyright © 2009, 7touchGroup, Inc.
//  All rights reserved.
//  
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//  * Redistributions of source code must retain the above copyright
//  notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above copyright
//  notice, this list of conditions and the following disclaimer in the
//  documentation and/or other materials provided with the distribution.
//  * Neither the name of the 7touchGroup, Inc. nor the
//  names of its contributors may be used to endorse or promote products
//  derived from this software without specific prior written permission.
//  
//  THIS SOFTWARE IS PROVIDED BY 7touchGroup, Inc. "AS IS" AND ANY
//  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//  DISCLAIMED. IN NO EVENT SHALL 7touchGroup, Inc. BE LIABLE FOR ANY
//  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
//  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
//  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//  
//-------------------------------------------------------------------

#import <UIKit/UIKit.h>
#import "S7Macros.h"

@class S7GraphView;

@protocol S7GraphViewDataSource <NSObject>

@required

/** 
  * Returns the number of plots your want to be rendered in the view.
  * 
  * @params
  * graphView Component that is asking for the number of plots.
  * 
  * @return Number of plots your want to be rendered in the view.
  */
- (NSUInteger)graphViewNumberOfPlots:(S7GraphView *)graphView;

/** 
  * Returns an array with objects of any type you can provide a formatter for via xValueFormatter property
  * or stay happy with default formatting.
  * 
  * @params
  * graphView Component that is asking for the values.
  * 
  * @return Array with objects of any type you can provide a formatter for via xValueFormatter property
  * or stay happy with default formatting.
  */
- (NSMutableArray *)graphViewXValues:(S7GraphView *)graphView;

/** 
  * Returns an array with objects of type NSNumber.
  * 
  * @params
  * graphView Component that is asking for the values.
  * plotIndex Index of the plot that you should provide an array of values for.
  * 
  * @return Array with objects of type NSNumber.
  */
// 指定位置のデータ内容を返す
- (float)graphView:(S7GraphView *)graphView yValuesForPlot:(NSUInteger)plotIndex index:(NSUInteger)index;
// データ数を返す
- (NSUInteger)graphView:(S7GraphView *)graphView yValuesCount:(NSUInteger)plotIndex;

// Peak値を返す
- (float)graphViewPeakMax:(S7GraphView *)graphView gno:(NSUInteger)plot;
- (float)graphViewPeakMin:(S7GraphView *)graphView gno:(NSUInteger)plot;

@optional

/** 
  * Returns the value indicating whether the component should render the plot with the specified index as filled.
  *
  * @param
  * graphView Component that is asking whether the plot should be filled.
  * plotIndex Index of the plot that you should decide whether it is filled or not.
  * 
  * @return YES if the plot should be rendered as filled; otherwise, NO.
  */
- (BOOL)graphView:(S7GraphView *)graphView shouldFillPlot:(NSUInteger)plotIndex;


// 描画終了時に呼び出す
-(void)graphViewEndOfPlots:(S7GraphView *)graphView;

// グラフ別描画制御(一時的なON/OFF)
-(BOOL)graphViewOnOff:(S7GraphView *)graphView_ gno:(NSUInteger)plotIndex;

// グラフの縦軸数値表示用文字列を返す
-(NSString *)graphViewYFormatter:(S7GraphView *)graphView_ cnt:(NSUInteger)cnt step:(float)step offset:(float)offset;
	// value=((float)i * yValueStep)-zeroOffsetYValue;

@end

//-------------------------------------------------------------------

@interface S7GraphView : UIView
{
@private
	id<S7GraphViewDataSource> __weak _dataSource;
	
	NSFormatter	*_xValuesFormatter;
	
	BOOL		_drawAxisX;
	BOOL		_drawAxisY;
	BOOL		_drawGridX;
	BOOL		_drawGridY;
	
	UIColor		*_xValuesColor;
	UIColor		*_yValuesColor;
	
	UIColor		*_gridXColor;
	UIColor		*_gridYColor;
	
	BOOL		_drawInfo;
	NSString	*_info;
	UIColor		*_infoColor;
	
	// 軸とグラフの分離に伴うメンバー化
	// float(*Value)は即値、CGFloatは座標
	// ここの値は、全グラフで共通となる
	float		minYValue;	// y最小値
	float		maxYValue;	// y最大値
	float		zeroOffsetYValue;	// 正数しかないときは0,0、負数があるときは+zeroOffsetYValueで>=0になる;y軸のみ
	CGFloat		stepX;		// x1マスの幅
	CGFloat		stepY;		// y1マスの幅
	CGFloat		offsetX,offsetXr; // 左、右側余白
	CGFloat		offsetY,offsetYu; // 下、上側余白
	NSUInteger	xstep;
	NSUInteger	maxStep;
	NSUInteger	ycount;			// 縦軸方向の格子分割数
	//
	NSUInteger	xValuesCount;	// x軸個数
	CGFloat		wx;				// グラフx実有効幅
	float		yValueStep;		// y方向の１メモリ分の即値量
	// しきい値;（同時）１グラフのみ
	float		 threshold1, threshold2;		
	BOOL		fthreshold1Hidden,fthreshold2Hidden;	// thresholdを表示/処理しないかどうか
	UIColor		*colThreshold;	// しきい値グラフ描画色
	// peak制御
	BOOL		fpeak;
}

//-------------------------------------------------------------------

// グラフの格子分割数
#define XCOUNT	10	// x方向

@property (nonatomic, weak) IBOutlet	id<S7GraphViewDataSource> dataSource;

@property (nonatomic, strong) IBOutlet	NSFormatter	*xValuesFormatter;	// x軸表示のフォーマッター

@property (nonatomic, assign) BOOL		drawAxisX;
@property (nonatomic, assign) BOOL		drawAxisY;
@property (nonatomic, assign) BOOL		drawGridX;
@property (nonatomic, assign) BOOL		drawGridY;

@property (nonatomic, strong) UIColor	*xValuesColor;
@property (nonatomic, strong) UIColor	*yValuesColor;

@property (nonatomic, strong) UIColor	*gridXColor;
@property (nonatomic, strong) UIColor	*gridYColor;

@property (nonatomic, assign) BOOL		drawInfo;
@property (nonatomic, copy  ) NSString	*info;
@property (nonatomic, strong) UIColor	*infoColor;

@property (nonatomic, assign) float		minYValue;	// y最小値
@property (nonatomic, assign) float		maxYValue;	// y最大値
@property (nonatomic, assign) NSUInteger	ycount;

@property (nonatomic, strong) UIColor	*colThreshold;
@property (nonatomic, assign) BOOL		fpeak;

//-------------------------------------------------------------------

// 関数
UIColor *colorByIndex(NSInteger index);

// メソッド
-(BOOL)calcAxisGrid:(float)minY_ maxYValue:(float)maxY_;
-(float)calcPoint2Yvalue:(CGPoint)point frame:(CGRect)frame;
-(BOOL)checkThresholdArea:(float)threshold value:(float)value;
-(CGPoint)checkPoint:(CGPoint)point frame:(CGRect)frame;
-(void)setThreshold:(float)value1 value2:(float)value2 fValue1Hidden:(BOOL)fValue1Hidden
													   fValue2Hidden:(BOOL)fValue2Hidden color:(UIColor *)col;

//-------------------------------------------------------------------

@end
