如何在TableView Cell上使用iOSCharts(https://github.com/danielgindi/Charts)?

时间:2016-04-01 09:14:37

标签: ios objective-c iphone swift cocoa-touch

我想在表格单元格中使用水平条形图,因为每个单元格值都会动态分配。谢谢 这是cellForRowAtIndexPath中的代码 强文

[self setupBarLineChartView:cell.chartView];     cell.chartView.delegate = self;

cell.chartView.drawBarShadowEnabled = NO;
cell.chartView.drawValueAboveBarEnabled = YES;

cell.chartView.maxVisibleValueCount = 60;

ChartXAxis *xAxis = cell.chartView.xAxis;
xAxis.labelPosition = XAxisLabelPositionBottom;
xAxis.labelFont = [UIFont systemFontOfSize:10.f];
xAxis.drawAxisLineEnabled = YES;
xAxis.drawGridLinesEnabled = YES;
xAxis.gridLineWidth = .3;

ChartYAxis *leftAxis = cell.chartView.leftAxis;
leftAxis.labelFont = [UIFont systemFontOfSize:10.f];
leftAxis.drawAxisLineEnabled = YES;
leftAxis.drawGridLinesEnabled = YES;
leftAxis.gridLineWidth = .3;
leftAxis.axisMinValue = 0.0; // this replaces startAtZero = YES

ChartYAxis *rightAxis = cell.chartView.rightAxis;
rightAxis.enabled = YES;
rightAxis.labelFont = [UIFont systemFontOfSize:10.f];
rightAxis.drawAxisLineEnabled = YES;
rightAxis.drawGridLinesEnabled = NO;
rightAxis.axisMinValue = 0.0; // this replaces startAtZero = YES

cell.chartView.legend.position = ChartLegendPositionBelowChartLeft;
cell.chartView.legend.form = ChartLegendFormSquare;
cell.chartView.legend.formSize = 8.0;
cell.chartView.legend.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:11.f];
cell.chartView.legend.xEntrySpace = 4.0;

 _sliderX.value = 11.0;
 _sliderY.value = 50.0;

[self slidersValueChanged:nil];

[cell.chartView animateWithYAxisDuration:2.5];

2 个答案:

答案 0 :(得分:0)

//我们必须在TableView类

中声明以下属性

@property(非原子,强)CampaignInfoCellTableViewCell * cell;

答案 1 :(得分:0)

**** 连接您的表格视图单元格,就像在自定义UITableViewCell类中一样,确保身份检查器中的类设置为Horizo​​ntalBarChart。     在“表格视图”控制器中,在“cellforRowAt”功能中。正如您通常所做的那样设置水平条形图,在带有单元格的所有对水平图表视图的引用之前......如下所示**

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if indexPath.row % 2 == 1 { let cell = tableView2.dequeueReusableCell(withIdentifier: "cellist", for: indexPath) as! AccordionCustomCellTableViewCell
                cell.barView.backgroundColor = UIColor .white


                //-------------------------------------------------
                cell.barView.maxVisibleCount = 60

                let xAxis  = cell.barView.xAxis
                xAxis.labelPosition = .bottom
                xAxis.drawAxisLineEnabled = false 
                xAxis.drawGridLinesEnabled = false
                xAxis.granularity = 1.0

                let leftAxis = cell.barView.leftAxis;
                leftAxis.drawAxisLineEnabled = false;
                leftAxis.drawGridLinesEnabled = false;
                leftAxis.axisMinimum = 0.0; // this replaces startAtZero = YES
                leftAxis.enabled = false

                let rightAxis = cell.barView.rightAxis
                rightAxis.enabled = false;
                rightAxis.drawAxisLineEnabled = true;
                rightAxis.drawGridLinesEnabled = false;
                rightAxis.axisMinimum = 0.0; // this replaces startAtZero = YES

                let l = cell.barView.legend
                l.enabled =  false

                cell.barView.fitBars = true;
                cell.barView.chartDescription?.enabled = false
                cell.barView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce)
                cell.barView.highlightPerTapEnabled = false

                func updateChartWithData() {
                    var dataEntries: [BarChartDataEntry] = []
                    let visitorCounts = names
                    for i in 0..<visitorCounts.count {
                        let dataEntry = BarChartDataEntry(x: Double(i), y: Double(values[i]), data: names as AnyObject?)
                        dataEntries.append(dataEntry)
                    }
                    let chartDataSet = BarChartDataSet(values: dataEntries, label: "")
//                    chartDataSet.setColor(UIColor.brown, alpha: 0.30)
                    chartDataSet.setColor(UIColor.blue, alpha: 0.30)
                    let chartData = BarChartData(dataSet: chartDataSet)
                    cell.barView.xAxis.valueFormatter = IndexAxisValueFormatter(values: names)
                    cell.barView.drawBordersEnabled = false
                    cell.barView.data?.setValueFormatter(valueFormatter)
                    cell.barView.data = chartData

                }