如何在 Swift 3x 中实现条形图上条形图上方的轨迹球和访问标签
How to achieve trackball and access label above Bar on Bar charts in Swift 3x
你好开发者我正在做一个项目,我使用了 danielgindi/Charts 第三方库现在这里的挑战是我需要在 [=19 的最高点呈现如图所示的轨迹球=] 我可以实现这个任何建议吗?
或者我如何访问条形图上方的值,我想在最高峰 (20.0) 上打印其他内容,例如 "E"??
遗憾的是,无法在栏上方的值处设置图像而不是 String 或 Double。
但是是的,我们可以访问栏上方的值,请访问提到的 link。
https://github.com/danielgindi/Charts/issues/2126
class ViewController: UIViewController {
var chartData = BarChartData()
var months: [String]!
var unitsSold = [Double]()
weak var valueFormatter: IValueFormatter?
@IBOutlet var viewForChart: BarChartView!
override func viewDidLoad() {
super.viewDidLoad()
valueFormatter = self
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
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(dataEntryX: months, dataEntryY: unitsSold)
}
func setChart(dataEntryX forX:[String],dataEntryY forY: [Double]) {
viewForChart.noDataText = "You need to provide data for the chart."
var dataEntries:[BarChartDataEntry] = []
for i in 0..<forX.count{
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(forY[i]) , data: months as AnyObject?)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
chartDataSet.setColor(UIColor.brown, alpha: 0.30)
chartData = BarChartData(dataSet: chartDataSet)
chartData.barWidth = 0.75
chartData.setDrawValues(true)
viewForChart.data = chartData
viewForChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
viewForChart.drawBordersEnabled = false
viewForChart.data?.setValueFormatter(valueFormatter)
}
}
在 class
之上或之外添加扩展
extension ViewController: IValueFormatter {
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
print(value)
//logic to print string at required point or value
switch value {
case 20.0: return ""
default: return ""
}
}
}
对于swift 2x
//将此添加到您的 class
class ValueFormator2:NSNumberFormatter{
override func stringFromNumber(number: NSNumber) -> String? {
print(number)
switch number {
case 3.0: return ""
default: return "\(number)"
}
}
}
然后
func setChart(xValues: [String], yValuesLineChart: [Double], yValuesBarChart: [Double] , combinedChartView:CombinedChartView) {
let form = ValueFormator2()
combinedChartView.barData?.setValueFormatter(form)
}
你好开发者我正在做一个项目,我使用了 danielgindi/Charts 第三方库现在这里的挑战是我需要在 [=19 的最高点呈现如图所示的轨迹球=] 我可以实现这个任何建议吗? 或者我如何访问条形图上方的值,我想在最高峰 (20.0) 上打印其他内容,例如 "E"??
遗憾的是,无法在栏上方的值处设置图像而不是 String 或 Double。 但是是的,我们可以访问栏上方的值,请访问提到的 link。 https://github.com/danielgindi/Charts/issues/2126
class ViewController: UIViewController {
var chartData = BarChartData()
var months: [String]!
var unitsSold = [Double]()
weak var valueFormatter: IValueFormatter?
@IBOutlet var viewForChart: BarChartView!
override func viewDidLoad() {
super.viewDidLoad()
valueFormatter = self
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
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(dataEntryX: months, dataEntryY: unitsSold)
}
func setChart(dataEntryX forX:[String],dataEntryY forY: [Double]) {
viewForChart.noDataText = "You need to provide data for the chart."
var dataEntries:[BarChartDataEntry] = []
for i in 0..<forX.count{
let dataEntry = BarChartDataEntry(x: Double(i), y: Double(forY[i]) , data: months as AnyObject?)
dataEntries.append(dataEntry)
}
let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
chartDataSet.setColor(UIColor.brown, alpha: 0.30)
chartData = BarChartData(dataSet: chartDataSet)
chartData.barWidth = 0.75
chartData.setDrawValues(true)
viewForChart.data = chartData
viewForChart.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
viewForChart.drawBordersEnabled = false
viewForChart.data?.setValueFormatter(valueFormatter)
}
}
在 class
之上或之外添加扩展extension ViewController: IValueFormatter {
func stringForValue(_ value: Double, entry: ChartDataEntry, dataSetIndex: Int, viewPortHandler: ViewPortHandler?) -> String {
print(value)
//logic to print string at required point or value
switch value {
case 20.0: return ""
default: return ""
}
}
}
对于swift 2x //将此添加到您的 class
class ValueFormator2:NSNumberFormatter{
override func stringFromNumber(number: NSNumber) -> String? {
print(number)
switch number {
case 3.0: return ""
default: return "\(number)"
}
}
}
然后
func setChart(xValues: [String], yValuesLineChart: [Double], yValuesBarChart: [Double] , combinedChartView:CombinedChartView) {
let form = ValueFormator2()
combinedChartView.barData?.setValueFormatter(form)
}