问题
One my ipad project needs to have 8 kinds graph and chart which include 3 variables bubble chart,scatter chart , candlestick chart etc.A quick search gave me result as a core plot which is currently the most used solution for graph and chart in ios.But when i went in there page i didn't see any kind bubble chart or scatter chart implementation. My Question is..
- Is it possible to draw 3 variable bubble chart,scatter chart or candlestick chart in
coreplot
??or i need to draw the whole graph using core graphics?
Thanks in advance
回答1:
Core Plot natively supports scatter plots and candlestick charts. You can make a bubble chart easily by using custom plot symbols for a scatter plot and varying the size of the plot symbols to represent the third variable.
回答2:
if you want to implement only the scatter chart here is the code which works well in 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:
Here is the code to add Bubble chart inside your app
This code works in Swift 3.1 and 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
}
来源:https://stackoverflow.com/questions/16932761/bubble-chart-scatter-chart-candlestick-chart-using-core-plot