我的一个 ipad 项目需要有 8 种图形和图表,其中包括 3 个变量气泡图、散点图、烛台图等。快速搜索给了我作为核心图的结果,这是目前 ios 中最常用的图形和图表解决方案。但是当我进入那里的页面时,我没有看到任何类型的气泡图或散点图实现。我的问题是..
- 是否可以在中绘制 3 个变量气泡图、散点图或烛台图
coreplot
?或者我需要使用核心图形绘制整个图表?
提前致谢
我的一个 ipad 项目需要有 8 种图形和图表,其中包括 3 个变量气泡图、散点图、烛台图等。快速搜索给了我作为核心图的结果,这是目前 ios 中最常用的图形和图表解决方案。但是当我进入那里的页面时,我没有看到任何类型的气泡图或散点图实现。我的问题是..
coreplot
?或者我需要使用核心图形绘制整个图表?提前致谢
Core Plot 原生支持散点图和烛台图。您可以通过使用散点图的自定义绘图符号并改变绘图符号的大小来表示第三个变量来轻松制作气泡图。
这是在您的应用程序中添加气泡图的代码
此代码适用于Swift 3.1 和 Xcode 8.3.2
func setBubbleChart(dataPoints: [Int], values: [Int], amounts: [Int]) {
var dataEntries: [BubbleChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = BubbleChartDataEntry(x: Double(dataPoints[i]), y: Double(values[i]), size: 15.0)
dataEntries.append(dataEntry)
}
let format = NumberFormatter()
format.generatesDecimalNumbers = false
format.zeroSymbol = ""
_ = DefaultValueFormatter(formatter: format)
let chartDataSet = BubbleChartDataSet(values: dataEntries, label: "Prashant")
let chartData = BubbleChartData(dataSet: chartDataSet)
chartDataSet.colors.append(setColor(value:30))
bubbleChartView.doubleTapToZoomEnabled = true
bubbleChartView.scaleXEnabled = true
bubbleChartView.scaleYEnabled = true
bubbleChartView.highlightPerTapEnabled = true
bubbleChartView.highlightPerDragEnabled = true
let firstLegend = LegendEntry.init(label: "Below 50", form: .default, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: CGFloat.nan, formLineDashLengths: nil, formColor: UIColor.black)
let secondLegend = LegendEntry.init(label: "Between 50 and 75", form: .default, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: CGFloat.nan, formLineDashLengths: nil, formColor: UIColor.black)
let thirdLegend = LegendEntry.init(label: "Over 75", form: .default, formSize: CGFloat.nan, formLineWidth: CGFloat.nan, formLineDashPhase: CGFloat.nan, formLineDashLengths: nil, formColor: UIColor.black)
bubbleChartView.chartDescription = nil
bubbleChartView.legend.entries = [firstLegend, secondLegend, thirdLegend]
bubbleChartView.data = chartData
bubbleChartView.drawBordersEnabled = true
bubbleChartView.animate(xAxisDuration: 5.0, yAxisDuration: 5.0)
let xAxis: XAxis = bubbleChartView.xAxis
xAxis.drawAxisLineEnabled = true
xAxis.drawGridLinesEnabled = true
xAxis.drawLabelsEnabled = true
xAxis.axisMinimum = 0
xAxis.axisMaximum = 10
let leftAxis: YAxis = bubbleChartView.leftAxis
leftAxis.drawAxisLineEnabled = true
leftAxis.drawGridLinesEnabled = true
leftAxis.setLabelCount(2, force: true)
leftAxis.axisMinimum = 0
leftAxis.axisMaximum = 120
let rightAxis: YAxis = bubbleChartView.rightAxis
rightAxis.drawAxisLineEnabled = false
rightAxis.drawGridLinesEnabled = true
rightAxis.drawLabelsEnabled = false
}
如果您只想实现散点图,这里的代码在swift 3.1 Xcode 8.3.2中运行良好
func setChart(dataPoints:[String] , value1 :[Double] , value2:[Double])
{
var dataEntries1:[ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x:value1[i] , y : Double(i))
dataEntries1.append(dataEntry)
}
var dataEntries2:[ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(x:value2[i] , y : Double(i))
dataEntries2.append(dataEntry)
}
let dataSet1 = ScatterChartDataSet(values: dataEntries1, label: "Value1" )
dataSet1 .setColor(UIColor.blue)
let dataSet2 = ScatterChartDataSet(values: dataEntries2 ,label: "Value2")
dataSet2.setColor(UIColor.green)
var dataSets = [ScatterChartDataSet]()
dataSets.append(dataSet1)
dataSets.append(dataSet2)
let barChartData = ScatterChartData(dataSets: dataSets)
scatterChart.xAxis.labelPosition = .bottom
scatterChart.rightAxis.enabled=false
scatterChart.legend.enabled=false
scatterChart.descriptionText = ""
scatterChart.data = barChartData
}