泡沫图,散点图,使用核心阴谋的烛台图
一个我的ipad项目需要有8种graphics和图表,其中包括3个variables的气泡图,散点图,烛台图等。一个快速search给我的结果是一个核心图,这是目前ios中graphics和图表最常用的解决scheme。但是当我去那里的页面,我没有看到任何一种气泡图或散布图实施。 我的问题是..
- 是否有可能在
coreplot
绘制3个可变气泡图,散点图或阴阳烛图?或者我需要使用核心graphics绘制整个graphics?
提前致谢
核心图原生支持散点图和烛台图表。 您可以通过使用散点图的自定义绘图符号和改变绘图符号的大小以表示第三个variables来轻松制作气泡图。
如果你只想实现散点图,那么这个代码在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 }
这里是在您的应用程序内添加气泡图表的代码
此代码在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 }