2014-06-09 25 views
1

我試圖在我的iOS應用中實施條形圖。我正在使用JBChartView。一切工作正常,但除了使用touchEvent按下它們時,酒吧沒有顏色。在JBChartView中不顯示條形圖的顏色

有沒有人有這個特定的插件的任何經驗,以幫助我得到它正常工作?

感謝

- (void)viewDidLoad 
    { 


     _barChartView = [[JBBarChartView alloc] init]; 
     _barChartView.delegate = self; 
     _barChartView.dataSource = self; 


    _tableView.backgroundColor =[UIColor clearColor]; 
     _barChartView.frame = CGRectMake(0,0,200,200); 
     _barChartView.backgroundColor = [UIColor clearColor]; 
     [_barChartView reloadData]; 
     [_barChart addSubview: _barChartView]; 

    } 


    - (UIColor *)barSelectionColorForBarChartView:(JBBarChartView *)barChartView 
    { 
     return [UIColor greenColor]; // color of selection view 
    } 
    - (BOOL)slideNavigationControllerShouldDisplayLeftMenu 
    { 
     return YES; 
    } 
    - (UIColor *)barChartView:(JBBarChartView *)barChartView colorForBarViewAtIndex:(NSUInteger)index 
    { 
     return [UIColor greenColor]; 
    } 

    - (NSUInteger)numberOfBarsInBarChartView:(JBBarChartView *)barChartView 
    { 
     return 4; 
    } 
    - (CGFloat)barChartView:(JBBarChartView *)barChartView heightForBarViewAtAtIndex:(NSUInteger)index 
    { 
     return 100.0; 
    } 

回答

2

的問題似乎是與如何JBBarChartView 「正常化」 的價值觀。每對JBBarChartView.h頭文件:

/** 
* Height for a bar at a given index (left to right). There is no ceiling on the the height; 
* the chart will automatically normalize all values between the overal min and max heights. 
* 
* @param barChartView  The bar chart object requesting this information. 
* @param index   The 0-based index of a given bar (left to right, x-axis). 
* 
* @return The y-axis height of the supplied bar index (x-axis) 
*/ 

由於這種「正常化」當你所有的價值都是一樣的(100.0f)是他們全部歸0,所以沒有欄中顯示的。幸運的是,它是開源的,所以你只需要打開執行文件,並稍加修改:

- (CGFloat)normalizedHeightForRawHeight:(NSNumber*)rawHeight 
{ 
    CGFloat minHeight = [self minimumValue]; 
    CGFloat maxHeight = [self maximumValue]; 
    CGFloat value = [rawHeight floatValue]; 

    if ((maxHeight - minHeight) <= 0) 
    { 
     return self.availableHeight; // change this line to return the max height instead of 0 
    } 

    return ((value - minHeight)/(maxHeight - minHeight)) * [self availableHeight]; 
} 
+0

作爲一個方面說明,我很驚訝,即使運行,因爲它看起來像'min = max'(又名所有高度相同),你應該得到一個「除以0」的錯誤。 – Stonz2

+0

謝謝你的解釋它的工作原理......但奇怪的問題是最小值的酒吧不顯示..所以我不得不添加一個額外的酒吧,它必須設置0值... –

0

正確的解決問題的方法是在圖表上提供的最小值。

請看看@的文檔JBChartView:

/** 
* The minimum and maxmimum values of the chart. 
* If no value(s) are supplied: 
* 
* minimumValue = chart's data source min value. 
* maxmimumValue = chart's data source max value. 
* 
* If value(s) are supplied, they must be >= 0, otherwise an assertion will be thrown. 
* The min/max values are clamped to the ceiling and floor of the actual min/max values of the chart's data source; 
* for example, if a maximumValue of 20 is supplied & the chart's actual max is 100, then 100 will be used. 
* 
* For min/max modifications to take effect, reloadData must be called. 
*/ 
@property (nonatomic, assign) CGFloat minimumValue; 
@property (nonatomic, assign) CGFloat maximumValue; 

如果所有數據等於單個值(即100),提供的最小值0將確保所有的酒吧都以相同的繪製(可見)高度(相對於0)。

希望這會有所幫助。