如何使用带Swift 3.0的danielgindi/Charts库绘制条形图
问题陈述:无法运行,因为在swift 3.0语法错误中使用“danielgindi/Charts library”实现条形图时出现了一些错误 Swift 3.0中的实际错误:如何使用带Swift 3.0的danielgindi/Charts库绘制条形图,swift,charts,swift3,Swift,Charts,Swift3,问题陈述:无法运行,因为在swift 3.0语法错误中使用“danielgindi/Charts library”实现条形图时出现了一些错误 Swift 3.0中的实际错误: import Charts class ChartViewController: UIViewController { @IBOutlet var barChartView: BarChartView! var months: [String]! var dataEntries: [BarCh
import Charts
class ChartViewController: UIViewController {
@IBOutlet var barChartView: BarChartView!
var months: [String]!
var dataEntries: [BarChartDataEntry] = []
override func viewDidLoad() {
super.viewDidLoad()
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
setChart(dataPoints: months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), yValues: [values[i]])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
barChartView.data = chartData
}
更正上述错误后,它在下一行中给出另一个错误,如下所示
编码内容:
import Charts
class ChartViewController: UIViewController {
@IBOutlet var barChartView: BarChartView!
var months: [String]!
var dataEntries: [BarChartDataEntry] = []
override func viewDidLoad() {
super.viewDidLoad()
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
setChart(dataPoints: months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), yValues: [values[i]])
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
barChartView.data = chartData
}
导入图表
类ChartViewController:UIViewController{
@IBVAR barChartView:barChartView!
变量月份:[字符串]!
var数据项:[BarChartDataEntry]=[]
重写func viewDidLoad(){
super.viewDidLoad()
月份=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”]
让unitsSold=[20.0,4.0,6.0,3.0,12.0,16.0,4.0,18.0,2.0,4.0,5.0,4.0]
设置图(数据点:月,值:单位)
}
func集合图表(数据点:[字符串],值:[双精度]){
barChartView.NodeataText=“您需要为图表提供数据。”
对于0..中的i,您可以将月份和单位销售值迭代为xVal和yVal
var barChartValues: [BarChartDataEntry] = []
for (key,val) in self.barChartDataval.enumerated() {
let xVal = Double(key)
let yVal = Double(val.cost)
let dataEntry = BarChartDataEntry(x: xVal, y: yVal!)
barChartValues.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: barChartValues, label: "Expenses Summary")
chartDataSet.colors = [Constants.k_COLORS.BLUE]
var dataSets = [IChartDataSet]()
dataSets.append(chartDataSet)
let chartData = BarChartData(dataSets: dataSets)
self.barChartView.data = chartData
self.barChartView.chartDescription?.text = ""
self.barChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .easeInBounce)
我也遇到过这个问题。这里是这样的:
let dataEntry = BarChartDataEntry(x: Double(i), yValues: [values[i]])
无法将数据点:[String]转换为类型Double
我的解决方案是删除Double。然后这不满足我的数据类型,所以我将它们都设置为[Int]类型,如下所示:
func setChart(dataPoints: [Int], values: [Int]) {
self.noDataText = "You need to provide data for the chart."
var dataEntries: [BarChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(values[i]) )
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
let chartData = BarChartData(dataSet: chartDataSet)
self.data = chartData
对于折线图,我们可以看到以下内容
import UIKit
import Charts
class LineChartVC: UIViewController , ChartViewDelegate{
@IBOutlet var lineChartView: LineChartView!
var months: [String]!
override func viewDidLoad() {
super.viewDidLoad()
self.lineChartSetUp()
}
}
extension LineChartVC{
func lineChartSetUp(){
self.lineChartView.delegate = self
self.lineChartView.chartDescription?.enabled = false//true
self.lineChartView.dragEnabled = true
self.lineChartView.setScaleEnabled(true)
self.lineChartView.pinchZoomEnabled = true
self.lineChartView.drawGridBackgroundEnabled = false
// x-axis limit line
/*
let llXAxis = ChartLimitLine(limit: 10.0, label: "Index 10")
llXAxis.lineWidth = 4.0
llXAxis.lineDashLengths = [(10.0), (10.0), (0.0)]
llXAxis.labelPosition = ChartLimitLine.LabelPosition.rightBottom//ChartLimitLabelPositionRightBottom
llXAxis.valueFont = UIFont.systemFont(ofSize: 10.0)
//[_chartView.xAxis addLimitLine:llXAxis];
self.lineChartView.xAxis.gridLineDashLengths = [10.0, 10.0]
self.lineChartView.xAxis.gridLineDashPhase = 0.0
let ll1 = ChartLimitLine(limit: 150.0, label: "Upper Limit")
ll1.lineWidth = 4.0
ll1.lineDashLengths = [5.0, 5.0]
ll1.labelPosition = ChartLimitLine.LabelPosition.rightTop //ChartLimitLabelPositionRightTop
ll1.valueFont = UIFont.systemFont(ofSize: 10.0)
let ll2 = ChartLimitLine(limit: -30.0, label: "Lower Limit")
ll2.lineWidth = 4.0
ll2.lineDashLengths = [5.0, 5.0]
ll2.labelPosition = ChartLimitLine.LabelPosition.rightBottom//ChartLimitLabelPositionRightBottom
ll2.valueFont = UIFont.systemFont(ofSize: 10.0)
let leftAxis: YAxis? = self.lineChartView.leftAxis
leftAxis?.removeAllLimitLines()
// leftAxis?.addLimitLine(ll1)
// leftAxis?.addLimitLine(ll2)
// leftAxis?.axisMaximum = 200.0
// leftAxis?.axisMinimum = -50.0
// leftAxis?.axisMaximum = 100.0
// leftAxis?.axisMinimum = 0.0
//
leftAxis?.gridLineDashLengths = [5.0, 5.0]
leftAxis?.drawZeroLineEnabled = false
leftAxis?.drawLimitLinesBehindDataEnabled = true
self.lineChartView.rightAxis.enabled = false
//[_chartView.viewPortHandler setMaximumScaleY: 2.f];
//[_chartView.viewPortHandler setMaximumScaleX: 2.f];
*/
let marker = BalloonMarker(color: UIColor(white: 180 / 255.0, alpha: 1.0), font: UIFont.systemFont(ofSize: 12.0), textColor: UIColor.white, insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0))
marker.chartView = self.lineChartView
marker.minimumSize = CGSize(width: 80.0, height: 40.0)
self.lineChartView.marker = marker
self.lineChartView.legend.form = Legend.Form(rawValue: 5)!
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]
self.setChart(months, values: unitsSold)
}
func setChart(_ dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x:Double(i) , y: values[i])
dataEntries.append(dataEntry)
}
print(dataEntries )
let data = LineChartData()
let ds1 = LineChartDataSet(values: dataEntries, label: "Units Sold")
ds1.colors = [UIColor.red]
data.addDataSet(ds1)
self.lineChartView.data = data
}
func chartValueSelected(chartView: ChartViewBase, entry: ChartDataEntry, dataSetIndex: Int, highlight: Highlight) {
// print("\(entry.value) in \(months[dataSetIndex])")
print(entry )
}
}
导入UIKit
进口图表
类LineChartVC:UIViewController、ChartViewDelegate{
@IBVAR lineChartView:lineChartView!
变量月份:[字符串]!
重写func viewDidLoad(){
super.viewDidLoad()
self.lineChartSetUp()
}
}
延长线图表{
func lineChartSetUp(){
self.lineChartView.delegate=self
self.lineChartView.chartDescription?.enabled=false//true
self.lineChartView.dragEnabled=true
self.lineChartView.setScaleEnabled(真)
self.lineChartView.pinchZoomEnabled=true
self.lineChartView.drawGridBackgroundEnabled=false
//x轴极限线
/*
设llXAxis=ChartLimitLine(限制:10.0,标签:“索引10”)
llXAxis.lineWidth=4.0
llXAxis.lineDashLength=[(10.0)、(10.0)、(0.0)]
llXAxis.labelPosition=ChartLimitLine.labelPosition.rightBottom//ChartLimitLabelPositionRightBottom
llXAxis.valueFont=UIFont.systemFont(大小:10.0)
//[_chartView.xAxis addLimitLine:llXAxis];
self.lineChartView.xAxis.GridlineDashLength=[10.0,10.0]
self.lineChartView.xAxis.gridLineDashPhase=0.0
设ll1=ChartLimitLine(限值:150.0,标签:“上限”)
ll1.0线宽=4.0
ll1.LineDashLength=[5.0,5.0]
ll1.labelPosition=ChartLimitLine.labelPosition.rightTop//ChartLimitLabelPositionRightTop
ll1.valueFont=UIFont.systemFont(大小:10.0)
设ll2=ChartLimitLine(限值:-30.0,标签:“下限”)
ll2.0线宽=4.0
ll2.LineDashLength=[5.0,5.0]
ll2.labelPosition=ChartLimitLine.labelPosition.rightBottom//ChartLimitLabelPositionRightBottom
ll2.valueFont=UIFont.systemFont(大小:10.0)
让leftAxis:YAxis?=self.lineChartView.leftAxis
leftAxis?.removeAllLimitLines()
//leftAxis?.addLimitLine(ll1)
//leftAxis?.addLimitLine(ll2)
//leftAxis?.axisMaximum=200.0
//leftAxis?.Axis最小值=-50.0
//leftAxis?.axisMaximum=100.0
//leftAxis?.Axis最小值=0.0
//
leftAxis?.GridlineDashLength=[5.0,5.0]
leftAxis?.drawZeroLineEnabled=false
leftAxis?.drawLimitLinesBehindDataEnabled=true
self.lineChartView.rightAxis.enabled=false
//[_chartView.viewPortHandler setMaximumScaleY:2.f];
//[_chartView.viewPortHandler setMaximumScaleX:2.f];
*/
let marker=BallookMarker(颜色:UIColor(白色:180/255.0,alpha:1.0),字体:UIFont.systemFont(字体大小:12.0),文本颜色:UIColor.white,插图:UIEdgeInsetsMake(8.0,8.0,20.0,8.0))
marker.chartView=self.lineChartView
marker.minimumSize=CGSize(宽度:80.0,高度:40.0)
self.lineChartView.marker=标记
self.lineChartView.legend.form=legend.form(原始值:5)!
月份=[“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”]
让unitsSold=[20.0,4.0,6.0,3.0,12.0,16.0,4.0,18.0,2.0,4.0,5.0,4.0]
self.setChart(月,值:unitsSold)
}
func集合图表(u数据点:[String],值:[Double]){
变量数据项:[ChartDataEntry]=[]
对于0中的i..导入UIKit
进口图表
类ViewController:UIViewController、ChartViewDelegate{
@IBVAR barChartView:barChartView!
弱var axisFormatDelegate:IAxisValueFormatter?
变量月份:[字符串]!
var unitsSold=[Double]()
重写func viewDidLoad(){
super.viewDidLoad()
barChartView.delegate=self
axisFormatDelegate=self
月份=[“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”]
unitsSold=[20.0,54.0,6.0,30.0,92.0,16.0114.0]
设置图(数据点:月,值:单位)
}
func集合图表(数据点:[字符串],值:[双精度]){
barChartView.NodeataText=“您需要为图表提供数据。”
//防止在图表中设置空数据集(崩溃)
guard dataPoints.count>0 else{return}
var dataEntries=[BarChartDataEntries]()
import UIKit
import Charts
class ViewController: UIViewController, ChartViewDelegate {
@IBOutlet weak var barChartView: BarChartView!
weak var axisFormatDelegate: IAxisValueFormatter?
var months: [String]!
var unitsSold = [Double]()
override func viewDidLoad() {
super.viewDidLoad()
barChartView.delegate = self
axisFormatDelegate = self
months = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
unitsSold = [20.0, 54.0, 6.0, 30.0, 92.0, 16.0,114.0]
setChart(dataPoints: months, values: unitsSold)
}
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataText = "You need to provide data for the chart."
// Prevent from setting an empty data set to the chart (crashes)
guard dataPoints.count > 0 else { return }
var dataEntries = [BarChartDataEntry]()
for i in 0..<dataPoints.count {
let entry = BarChartDataEntry(x: Double(i), y: values[i], data: months as AnyObject?)
dataEntries.append(entry)
}
let chartDataSet = BarChartDataSet(entries: dataEntries, label: "Units Sold")
chartDataSet.drawValuesEnabled = false
chartDataSet.colors = [UIColor.red]
chartDataSet.colors = [UIColor.lightGray]
chartDataSet.highlightColor = UIColor.orange.withAlphaComponent(0.3)
chartDataSet.highlightAlpha = 1
let chartData = BarChartData(dataSet: chartDataSet)
barChartView.data = chartData
let xAxisValue = barChartView.xAxis
xAxisValue.valueFormatter = axisFormatDelegate
// chartDataSet.colors = ChartColorTemplates.colorful() //multiple colors
//Animation bars
barChartView.animate(xAxisDuration: 0.0, yAxisDuration: 1.0, easingOption: .easeInCubic)
// X axis configurations
barChartView.xAxis.granularityEnabled = true
barChartView.xAxis.granularity = 1
barChartView.xAxis.drawAxisLineEnabled = true
barChartView.xAxis.drawGridLinesEnabled = false
barChartView.xAxis.labelFont = UIFont.systemFont(ofSize: 15.0)
barChartView.xAxis.labelTextColor = UIColor.black
barChartView.xAxis.labelPosition = .bottom
// Right axis configurations
barChartView.rightAxis.drawAxisLineEnabled = false
barChartView.rightAxis.drawGridLinesEnabled = false
barChartView.rightAxis.drawLabelsEnabled = false
// Other configurations
barChartView.highlightPerDragEnabled = false
barChartView.chartDescription?.text = ""
barChartView.legend.enabled = false
barChartView.pinchZoomEnabled = false
barChartView.doubleTapToZoomEnabled = false
barChartView.scaleYEnabled = false
barChartView.drawMarkers = true
let l = barChartView.legend
l.horizontalAlignment = .left
l.verticalAlignment = .bottom
l.orientation = .horizontal
l.drawInside = false
l.form = .circle
l.formSize = 9
l.font = UIFont(name: "HelveticaNeue-Light", size: 11)!
l.xEntrySpace = 4
// On tapped bar marker before adding BalloonMarker.swift
let marker = BalloonMarker(color: UIColor.orange, font: UIFont.boldSystemFont(ofSize: 13), textColor: UIColor.white, insets: UIEdgeInsets(top: 7.0, left: 7.0, bottom: 15.0, right: 7.0))
marker.chartView = barChartView
barChartView.marker = marker
}
}
extension ViewController: IAxisValueFormatter {
func stringForValue(_ value: Double, axis: AxisBase?) -> String {
return months[Int(value)]
}
}