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

Posted

技术标签:

【中文标题】如何在 TableView Cell 上使用 iOSCharts (https://github.com/danielgindi/Charts)?【英文标题】:How to use iOSCharts (https://github.com/danielgindi/Charts) on TableView Cell? 【发布时间】:2016-04-01 09:14:36 【问题描述】:

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

[自行设置BarLineChartView: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];

【问题讨论】:

你能解释一下你的确切问题吗?什么不工作 我自己解决了! 【参考方案1】:

//我们必须在 TableView 类中声明以下属性

@property (nonatomic, strong) CampaignInfoCellTableViewCell *cell;

【讨论】:

【参考方案2】:

**** 连接您的表格视图单元格,就像您通常在自定义 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

                

【讨论】:

以上是关于如何在 TableView Cell 上使用 iOSCharts (https://github.com/danielgindi/Charts)?的主要内容,如果未能解决你的问题,请参考以下文章

iOS 如何设置tableview列表的cell上的的设置删除键 置顶按钮 未读信息按钮

iOS 怎么避免tableview刷新数据错乱

没有情节提要的 TableView Cell 的 iOS Swift Segue

iOS 杂笔-如何解决tableview显示错乱问题

iOS Tableview点击cell 会往上跳

iOS 8 自适应 Cell