gpt4 book ai didi

ios - 核心图如何绘制 2 y 轴标签,图形看起来模糊

转载 作者:行者123 更新时间:2023-11-30 12:19:12 25 4
gpt4 key购买 nike

我有几个问题:>>>> enter image description here

类 ScatteredCell:UICollectionViewCell、CPTScatterPlotDataSource、CPTPlotDataSource、CPTScatterPlotDelegate{ @IBOutlet var hostView:CPTGraphHostingView!

var plot1: CPTBarPlot!
var plot2: CPTBarPlot!
var plot3: CPTScatterPlot!
var indexOfCell:Int? = nil
var selectedComponent: String!
let BarWidth = 0.8
let BarInitialX = 1.0
let yArray = [0,0,3,0,2,1,0,1,5,0,0,5,8,0,0,1,0,0,6,0,7,0,3,5,5,0,2,0,0,1,0]
var valueBarPlot:Array<Any> = []
var fullBarPlot:Array<Any> = []
var yIntervalLength = NSNumber()

var yAxisScale = 0

override func awakeFromNib()
{
super.awakeFromNib()
}
override func draw(_ rect: CGRect) {
self.layer.borderWidth = 1
self.layer.borderColor = UIColor(red: 204.0/255.0, green: 204.0/255.0, blue: 204.0/255.0, alpha: 1).cgColor
}
func initWithPlot()
{
configureHostView()
configureGraph()
configureChart()
configureAxes()
configureLegend()
}

func configureHostView()
{
hostView.allowPinchScaling = false
}

func configureGraph()
{

// 1 - Create the graph
let graph = CPTXYGraph(frame: hostView.bounds)
graph.plotAreaFrame?.masksToBorder = false
hostView.hostedGraph = graph
// 2 - Configure the graph
graph.fill = CPTFill(color: CPTColor.clear())
graph.paddingBottom = 40.0
graph.paddingLeft = 20.0
graph.paddingTop = 20.0
graph.paddingRight = 15.0



let yLabelFormatter = NumberFormatter()
yLabelFormatter.generatesDecimalNumbers = false

// 4 - Set up plot space
guard let plotSpace = graph.defaultPlotSpace as? CPTXYPlotSpace else { return }
plotSpace.yRange = CPTPlotRange(location: 0, length:yAxisScale as NSNumber)
var yAxisLength:NSNumber = 0
if selectedComponent == "month"{
yAxisLength = 31
}else {
yAxisLength = 12
}

plotSpace.xRange = CPTPlotRange(location:0.5, length:yAxisLength)
let axisSet = (graph.axisSet as? CPTXYAxisSet)
let axisSet1 = (graph.axisSet as? CPTXYAxisSet)
let y: CPTXYAxis? = axisSet?.yAxis
y?.labelFormatter = yLabelFormatter
y?.axisConstraints = CPTConstraints.init(lowerOffset: -25.0)
y?.plotSpace = plotSpace

let x: CPTXYAxis? = axisSet?.xAxis
x?.labelFormatter = yLabelFormatter
graph.masksToBorder = true
}

func configureChart()
{

// 1 - take plot
plot1 = CPTBarPlot()
plot1.fill = CPTFill(color: CPTColor.init(componentRed: 25.0/255.0, green: 100.0/255.0, blue: 150.0/255.0, alpha: 1))

plot2 = CPTBarPlot()
plot2.fill = CPTFill(color: CPTColor.init(componentRed: 230.0/255.0, green: 230.0/255.0, blue: 230.0/255.0, alpha: 1))



// 2 - Set up line style
let barLineStyle = CPTMutableLineStyle()
barLineStyle.lineColor = CPTColor.clear()
barLineStyle.lineWidth = 1

// 3 - configure plots to graph
let barX = BarInitialX
let plot:CPTBarPlot = plot1!
plot.dataSource = self
//plot.delegate = self
plot.barWidth = NSNumber(value: BarWidth)
plot.barOffset = NSNumber(value: barX)
plot.lineStyle = barLineStyle
// barX += BarWidth
let splot:CPTBarPlot = plot2!
splot.dataSource = self

//Add plots to graph
guard let graph = hostView.hostedGraph else { return }
graph.add(splot, to: graph.defaultPlotSpace)
graph.add(plot, to: graph.defaultPlotSpace)
let myBorderLineStyle = CPTMutableLineStyle()
myBorderLineStyle.lineColor = CPTColor.clear()
plot2.lineStyle = myBorderLineStyle
plot2.barWidth = NSNumber(value: BarWidth)
plot2.barOffset = NSNumber(value: barX)
plot3 = CPTScatterPlot()
let scatplot:CPTScatterPlot = plot3!
scatplot.dataSource = self
//Add plots to graph
guard let graph1 = hostView.hostedGraph else { return }
graph1.add(scatplot, to: graph.defaultPlotSpace)

plot1.attributedTitle = attributedString("DISTANCE TRAVELLED(MILES)",fontSize: 7.0)
plot3.title = "" //attributedString("TIME(HOURS)",fontSize: 8.0)
}
func configureAxes()
{
// 1 - Configure styles
let axisLineStyle = CPTMutableLineStyle()
//let axisLineStyle = CPTMutableLineStyle()
axisLineStyle.lineWidth = 2.0
axisLineStyle.lineColor = CPTColor.clear()

// 2 - Get the graph's axis set
guard let axisSet = hostView.hostedGraph?.axisSet as? CPTXYAxisSet else { return }

let axisTextStyle = CPTMutableTextStyle()
axisTextStyle.fontName = "Reg"

let axisFormatter = NumberFormatter()
axisFormatter.minimumIntegerDigits = 1

// 3 - Configure the x-axis
if let x = axisSet.xAxis {
var xAxisInterval:NSNumber = 0

if selectedComponent == "month"{
xAxisInterval = 5
}else {
xAxisInterval = 1
}
x.majorIntervalLength = xAxisInterval
x.orthogonalPosition = 0
x.minorTicksPerInterval = 2
x.majorTickLength = 5
x.attributedTitle = attributedString("MONTH",fontSize: 9.0)
axisTextStyle.color = CPTColor.gray()
axisTextStyle.fontSize = 6.0
x.labelTextStyle = axisTextStyle


}

//if 4 - Configure the y-axis
if let y = axisSet.yAxis{
let identi = y.identifier
print(identi)
y.majorIntervalLength = yIntervalLength
y.majorTickLength = 5
y.minorTicksPerInterval = 1
y.orthogonalPosition = 0
y.axisConstraints = CPTConstraints.init(lowerOffset: 5.0)
axisTextStyle.color = CPTColor.init(componentRed: 25.0/255.0, green: 100.0/255.0, blue: 150.0/255.0, alpha: 1)
axisTextStyle.fontSize = 6.0
y.labelTextStyle = axisTextStyle
y.labelFormatter = axisFormatter
}
}
func configureLegend() {
guard let graph = hostView.hostedGraph else { return }
let legend = CPTLegend(graph: graph)
graph.legend = legend
graph.legendAnchor = .topLeft
graph.legendDisplacement = CGPoint(x: 25.0, y: 0.0)
legend.fill = CPTFill(color: CPTColor.white())
legend.swatchSize = CGSize(width: 10.0, height: 10.0)
let titleStyle = CPTMutableTextStyle()
titleStyle.color = CPTColor.black()
titleStyle.fontSize = 11.0
legend.textStyle = titleStyle
legend.delegate = self
}
func attributedString(_ title:String, fontSize:CGFloat) -> NSAttributedString? {
let attributes = [
NSFontAttributeName : UIFont(name: Font.Bold, size: fontSize) ?? UIFont.systemFont(ofSize: 17.0),
NSForegroundColorAttributeName : UIColor(red: 127.0/255.0, green: 137.0/255.0, blue: 147.0/255.0, alpha: 1),
] as [String : Any]
let attributedString = NSAttributedString(string: title, attributes: attributes)
return attributedString
}
// MARK: - Plot Data Source Methods
func numberOfRecords(for plot: CPTPlot) -> UInt
{

return UInt(valueBarPlot.count)
}

func number(for plot: CPTPlot, field fieldEnum: UInt, record idx: UInt) -> Any?
{
if fieldEnum == UInt(CPTBarPlotField.barTip.rawValue)
{
if plot == plot1
{
return self.valueBarPlot[Int(idx)] as! NSNumber;
}
if plot == plot2
{
return self.fullBarPlot[Int(idx)] as! NSNumber;
}
if plot == plot3
{
return self.yArray[Int(idx)] as NSNumber;
}
}
return idx
}

}

问题 1:如果我在 Y 轴上使用 upperoffset,我将无法绘制第二个 y 轴

问题2:在Collectionviewcell中填充Host View ,它有4个图表,所以我使用了collectionview单元,我不知道为什么图表在真实设备中变得模糊

最佳答案

提供的代码中没有提及第二个 y 轴。您需要创建并配置一个新的轴对象并将其添加到轴集中。有关示例代码,请参阅Plot Gallery示例应用程序中的“Axis Demo”。

graph.axisSet.axes = [x, y, y2]

很明显,图形托管 View 在单元格内的位置不正确。从提供的信息中我无法判断原因。

关于ios - 核心图如何绘制 2 y 轴标签,图形看起来模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45005276/

25 4 0