【问题标题】:Core plot how to plot 2 y axis label, Graph looks blured核心图如何绘制 2 个 y 轴标签,图形看起来模糊
【发布时间】:2017-07-10 06:40:23
【问题描述】:

我有几个问题:>>>>

类 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 视图,它有 4 个图形,所以我使用了 collectionview 单元,我不知道为什么图形在真实设备中变得模糊

【问题讨论】:

  • #1:不清楚你在问什么——请澄清。 #2:你如何将图形渲染到单元格中?请提供更详细的描述和相关代码。
  • 完成@EricSkroch
  • 完成@Eric Skroch
  • 仍然不清楚您对第 1 部分的要求。从红色条覆盖在另一个图上的方式来看,它看起来像是另一个图的一部分。对于第 2 部分,请发布用于设置集合视图单元格的代码,包括图形托管视图。这个截图来自什么设备?是模拟器还是真机?
  • 嗨@EricSkroch,我已经更新了我的代码并清楚地说明了问题,你能帮我解决一下吗

标签: ios swift core-plot


【解决方案1】:

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

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

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

【讨论】:

  • 是的,我试过了,它为 y 轴保存了相同的内存位置
  • 确保您正在创建一个新轴,而不仅仅是重用旧轴。 y2 = CPTXYAxis()
  • 谢谢 Eric 现在两边 Y 轴都来了。现在 y 轴的间隔是 0 -100 并且对于 y2 0 -6 如何在 configureaxes 方法中执行,如何区分 bot y 轴。 @Eric Skroch
  • 要对轴使用不同的比例,请在图形中添加另一个绘图空间并配置 y2 轴以使用它。您可能还希望将至少一个地块添加到新的地块空间中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多