JBChart查看如何为每个栏添加标签? (迅速)

时间:2015-03-12 17:44:18

标签: ios swift jbchartview

我正在使用JBChartView库在我的iOS应用程序中绘制条形图。每个栏应该有一个标识符,显示在栏的底部。 我尝试使用附加标签定义自定义barView,但不知道如何将标签相对于条形图放置。

以下是代码:

func barChartView(barChartView: JBBarChartView!, barViewAtIndex index: UInt) -> UIView! {
    let barView = UIView()

    // setting up the bar
    let bar: Float = chartData[Int(index)]
    var barColor = UIColorFromRGB(0xE8E8E8)
    if bar >= 1 { barColor = UIColorFromRGB(0xFF6259) }
    if bar > 33 { barColor = UIColorFromRGB(0xFFC02F) }
    if bar > 66 { barColor = UIColorFromRGB(0x28CA41) }
    barView.backgroundColor = barColor

    // setting up the label
    var label = UILabel()
    label.textAlignment = NSTextAlignment.Center
    label.textColor = barColor
    label.text = NSString(format: "%.0f", bar)
    barview.addSubview(label)

    return barView
}

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

好吧,这很令人尴尬:标签和酒吧颜色相同,所以标签一直都在那里。

最终代码(改进并附加了一个数据标签):

func barChartView(barChartView: JBBarChartView!, barViewAtIndex index: UInt) -> UIView! {
    let barView = BarChartBarView()

    let bar: Float = chartData[Int(index)]
    var barColor = UIColorFromRGB(0xE8E8E8)
    if bar >= 1 { barColor = UIColorFromRGB(0xFF6259) }
    if bar > 33 { barColor = UIColorFromRGB(0xFFC02F) }
    if bar > 66 { barColor = UIColorFromRGB(0x28CA41) }
    barView.backgroundColor = barColor

    barView.dataLabel.text = NSString(format: "%.0f", bar)
    barView.legendLabel.text = chartLegend[Int(index)]

    return barView
}

类BarChartBarView:UIView {

let labelFont = UIFont(name:"Raleway-Thin", size:8.0)
var padding = CGFloat(0)
var barWidth = CGFloat(27)
var dataLabel = UILabel()
var legendLabel = UILabel()
var legendLabelWidth = CGFloat(50)
var labelHeight = CGFloat(27)

override convenience init() {
    self.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
}


override init(frame: CGRect) {
    super.init(frame: frame)

    self.backgroundColor = UIColorFromRGB(0xE8E8E8)

    // setting up data Label
    dataLabel.frame = CGRectMake(0, 0, barWidth, labelHeight)
    dataLabel.textAlignment = NSTextAlignment.Center
    dataLabel.textColor = UIColor.whiteColor()
    dataLabel.font = labelFont
    self.addSubview(dataLabel)

    // setting up legend Label
    legendLabel.frame = CGRectMake(0, self.bounds.height, legendLabelWidth, labelHeight)
    legendLabel.textAlignment = NSTextAlignment.Center
    legendLabel.textColor = UIColor.blackColor()
    legendLabel.font = labelFont
    self.addSubview(legendLabel)
}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
    let xOffset = (self.barWidth - self.legendLabelWidth) / 2
    let yOffset:CGFloat = self.bounds.height
    let width = self.legendLabelWidth
    let height = self.labelHeight

    self.legendLabel.frame = CGRectMake(xOffset, yOffset, width, height)
}

}

答案 1 :(得分:1)

@Armin 我得到了实现以下方法的线索:

- (UIView *)barChartView:(JBBarChartView *)barChartView barViewAtIndex:(NSUInteger)index

来自Armin。因为即使JawBone的JBBarChart库令人印象深刻,他们的Demo完全是使用代码完成的,而且它一目了然也有点令人困惑。然而,我添加Label视图的方式与创建UIView并将UILabel添加为子视图并返回它的方式相同。它看起来如下:

- (UIView *)barChartView:(JBBarChartView *)barChartView barViewAtIndex:(NSUInteger)index {
    UIView *barView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 55, (CGFloat)chartData[(int)index])];
    if (index == 0) {
        [barView setBackgroundColor:[UIColor grayColor]];
    } else if (index == 1) {
        [barView setBackgroundColor:[UIColor redColor]];
    } else if (index == 2) {
        [barView setBackgroundColor:[UIColor orangeColor]];
    } else {
        [barView setBackgroundColor:[UIColor blueColor]];
    }

    int roomCount = (int)chartData[(int)index];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(2, 5.0f, 50.0f, 21.0f)];
    [label setTextAlignment:NSTextAlignmentCenter];
    [label setTextColor:[UIColor blackColor]];
    [label setFont:[UIFont fontWithName:@"Raleway-Thin" size:6.0]];
    [label setText:[NSString stringWithFormat:@"%d/56", roomCount]];

    [barView addSubview:label];
    return barView;
}

请注意 chartData 包含我的数据,作为整数。所以我需要根据每个位置的整数来确定每个条视图的高度

希望这个答案对那里的人有帮助,特别是在Objective-C中做这个实现的人。

干杯!