使用核心图的气泡图、散点图、烛台图
Posted
技术标签:
【中文标题】使用核心图的气泡图、散点图、烛台图【英文标题】:Bubble chart , scatter chart ,candlestick chart using core plot 【发布时间】:2013-06-05 06:14:02 【问题描述】:我的 ipad 项目需要 8 种图形和图表,其中包括 3 个变量气泡图、散点图、烛台图等。快速搜索给了我作为核心图的结果,这是目前最常用的图形和图表解决方案在ios中。但是当我进入那里时,我没有看到任何类型的气泡图或散点图实现。我的问题是..
是否可以在coreplot
中绘制3个可变气泡图、散点图或烛台图?或者我需要使用核心图形绘制整个图形?
提前致谢
【问题讨论】:
【参考方案1】:Core Plot 原生支持散点图和烛台图。通过使用散点图的自定义绘图符号并改变绘图符号的大小来表示第三个变量,您可以轻松制作气泡图。
【讨论】:
【参考方案2】:如果您只想实现 散点图,这里的代码在 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
【讨论】:
【参考方案3】:这是在您的应用中添加气泡图的代码
此代码适用于 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
【讨论】:
以上是关于使用核心图的气泡图、散点图、烛台图的主要内容,如果未能解决你的问题,请参考以下文章
R语言使用symbols函数创建气泡图(bubble plot)使用2D散点图通过添加形状大小来实现3D散点图的效果
Android图表库MPAndroidChart——散点图的孪生兄弟气泡图
Matplotlib学习---用matplotlib画散点图,气泡图(scatterplot, bubble chart)
R语言可视化:散点图散点图和折线图(line charts)3D散点图旋转3D散点图气泡图corrgram包可视化相关性矩阵马赛克图( Mosaic plots)hexbin密度图