核心情节如何绘制2 y轴标签,Graph看起来模糊

时间:2017-07-10 06:40:23

标签: ios swift core-plot

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

class 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轴上使用上偏移,我无法绘制第二个y轴

问题2:在Collectionviewcell中占用Host视图,它有4个图形,所以我使用了collectionview单元格,我不知道为什么图形在真实设备中模糊

1 个答案:

答案 0 :(得分:0)

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

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

很明显,图表托管视图未在单元格内正确定位。我无法从所提供的信息中找出原因。