1

我正在尝试在我的 iOS 应用程序中实现条形图。我正在使用 JBChartView。一切正常,但除了我使用 touchEvent 按下它们时,条形图没有颜色。

有没有人对这个特定的插件有任何经验来帮助我让它正常工作?

谢谢

  - (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;
    }
4

2 回答 2

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];
}
于 2014-06-09T16:48:37.053 回答
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)绘制。

希望这可以帮助。

于 2015-08-10T18:52:15.220 回答