ios-charts的水平条形图

时间:2015-08-30 09:47:57

标签: xcode ios-charts

如何在xcode中通过ios-charts创建水平条形图? 我用类Horizo​​ntalBarChartView

创建了View
@IBOutlet weak var horChart: HorizontalBarChartView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
    let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]

    setChart(months, values: unitsSold)

}

func setChart(dataPoints: [String], values: [Double]) {

    var dataEntries: [ChartDataEntry] = []

    for i in 0..<dataPoints.count {
        let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
    }

    let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Units Sold")
    let chartData = BarChartData(xVals: dataPoints, dataSet: chartDataSet)
    horChart.data = chartData

}

没有在互联网上找到任何例子

2 个答案:

答案 0 :(得分:6)

检查mainStoryboard中的horChart视图。 class是“Horizo​​ntalBarChartView”,模块是“Charts”, 我面临同样的问题但我的问题通过设置模块解决了“图表”

答案 1 :(得分:0)

ChartsDemo文件夹中有exmaples,您可以查看很多图表。但是我会在这里粘贴代码,它是从@interface HorizontalBarChartViewController复制的:

- (void)setDataCount:(int)count range:(double)range
{
    NSMutableArray *xVals = [[NSMutableArray alloc] init];

    for (int i = 0; i < count; i++)
    {
        [xVals addObject:months[i % 12]];
    }

    NSMutableArray *yVals = [[NSMutableArray alloc] init];

    for (int i = 0; i < count; i++)
    {
        double mult = (range + 1);
        double val = (double) (arc4random_uniform(mult));
        [yVals addObject:[[BarChartDataEntry alloc] initWithValue:val xIndex:i]];
    }

    BarChartDataSet *set1 = [[BarChartDataSet alloc] initWithYVals:yVals label:@"DataSet"];
    set1.barSpace = 0.35;

    NSMutableArray *dataSets = [[NSMutableArray alloc] init];
    [dataSets addObject:set1];

    BarChartData *data = [[BarChartData alloc] initWithXVals:xVals dataSets:dataSets];
    [data setValueFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:10.f]];

    _chartView.data = data;
}