Core Plot DatePlot 示例的实现不渲染线

Implementation of Core Plot DatePlot example not rendering lines

我正在尝试将核心情节的 DatePlot 示例添加到 UIVisualEffectView。我有图形渲染,但出于某种原因我没有看到实际的数据行。我想知道是否有人可以帮我看看我做错了什么。下面是我的代码。如果有帮助的话,我正在呼叫 set_up_chart,然后是 add_chart_data。 (我也试过在 set_up_chart 之前调用 add_chart_data 但得到了相同的结果。)

def graph
  @graph ||= CPTXYGraph.alloc.initWithFrame(host_view.bounds)
end

def host_view
  chart_frame = CGRect.new(
    [0, 0],
    [vibrancy_view.frame.size.width, vibrancy_view.frame.size.height]
  )

  @host_view ||= CPTGraphHostingView.alloc.initWithFrame(chart_frame)
end

def set_up_chart
  # graph.applyTheme(CPTTheme.themeNamed(KCPTDarkGradientTheme))
  host_view.backgroundColor = UIColor.clearColor
  host_view.allowPinchScaling = false

  whiteTextStyle = CPTMutableTextStyle.alloc.init
  whiteTextStyle.color    = CPTColor.whiteColor
  whiteTextStyle.fontSize = 12.0

  whiteTickStyle = CPTLineStyle.alloc.init
  whiteTickStyle.lineColor = CPTColor.whiteColor
  whiteTickStyle.lineWidth = 0.5

  axisLineStyle = CPTMutableLineStyle.alloc.init
  axisLineStyle.lineColor = CPTColor.whiteColor
  axisLineStyle.lineWidth = 1.0
  axisLineStyle.lineCap   = KCGLineCapRound

  host_view.hostedGraph = graph

  graph.paddingLeft   = 5.0
  graph.paddingTop    = 5.0
  graph.paddingRight  = 5.0
  graph.paddingBottom = 5.0

  graph.plotAreaFrame.paddingLeft   = 32.0
  graph.plotAreaFrame.paddingTop    = 0.0
  graph.plotAreaFrame.paddingRight  = 0.0
  graph.plotAreaFrame.paddingBottom = 22.0

  # Plot space
  plotSpace = graph.defaultPlotSpace

  start_time = Time.now.to_i
  plotSpace.xRange = CPTPlotRange.plotRangeWithLocation(0.0, length: 1)

  y_range_length = ((event[:range_minimum]).abs + 1) + (event[:range_maximum] + 1)
  y_start = event[:range_minimum] - 1
  plotSpace.yRange = CPTPlotRange.plotRangeWithLocation(y_start, length: y_range_length)

  plotSpace.delegate = self

  axisSet = graph.axisSet

  # X axis
  x = axisSet.xAxis
  x.labelingPolicy        = CPTAxisLabelingPolicyAutomatic
  x.majorIntervalLength   = start_time
  # set where the x-axis aligns itself
  x.orthogonalPosition    = y_start

  x.minorTicksPerInterval = 0
  x.labelOffset           = 0.25
  x.labelTextStyle          = whiteTextStyle
  x.titleTextStyle          = whiteTextStyle
  x.axisLineStyle           = axisLineStyle
  x.majorTickLineStyle      = whiteTickStyle
  x.minorTickLineStyle      = whiteTickStyle
  x.axisConstraints         = CPTConstraints.constraintWithLowerOffset(0.0)
  # x.labelOffset = 16.0
  x.majorTickLength = 4.0
  x.minorTickLength = 2.0
  # x.tickDirection = CPTSignPositive
  x.preferredNumberOfMajorTicks = 4.0
  # x.majorGridLineStyle    = majorGridLineStyle
  # x.minorGridLineStyle    = minorGridLineStyle

  dateFormatter = NSDateFormatter.alloc.init
  dateFormatter.setDateFormat("h:mm")
  timeFormatter = CPTTimeFormatter.alloc.initWithDateFormatter(dateFormatter)
  timeFormatter.referenceDate = reference_date
  x.labelFormatter            = timeFormatter

  # Y axis
  y = axisSet.yAxis
  y.labelingPolicy        = CPTAxisLabelingPolicyAutomatic
  y.orthogonalPosition    = 0.0
  # y.majorGridLineStyle    = majorGridLineStyle
  # y.minorGridLineStyle    = minorGridLineStyle
  y.minorTicksPerInterval = 1
  y.labelOffset           = 0.25
  y.axisConstraints       = CPTConstraints.constraintWithLowerOffset(0.0)
  y.labelTextStyle          = whiteTextStyle
  y.titleTextStyle          = whiteTextStyle
  y.axisLineStyle           = axisLineStyle
  y.majorTickLineStyle      = whiteTickStyle

  # Remove minor ticks on y-axis
  y.minorTickLineStyle      = whiteTickStyle

  # Create the plot
  dataSourceLinePlot = CPTScatterPlot.alloc.init
  dataSourceLinePlot.identifier     = "Test"
  dataSourceLinePlot.cachePrecision = CPTPlotCachePrecisionDouble

  lineStyle = dataSourceLinePlot.dataLineStyle.mutableCopy
  lineStyle.lineWidth              = 3.0
  lineStyle.lineColor              = CPTColor.greenColor
  dataSourceLinePlot.dataLineStyle = lineStyle

  dataSourceLinePlot.dataSource = self
  dataSourceLinePlot.delegate = self

  graph.addPlot(dataSourceLinePlot)
end

def add_chart_data
  if self.chart_data.count == 0
    array = []

    100.times do |n|
      value = [-5,-1,0,1,5].sample
      time = (Time.now.to_i - 100) + (5 * n)
      data = {CPTScatterPlotFieldX => time, CPTScatterPlotFieldY => value}
      array << data

      self.chart_data = array
    end
  end

  graph.reloadData
end

def numberOfRecordsForPlot(plot)
  self.chart_data.count
end

def numberForPlot(plot, field: fieldEnum, recordIndex: index)
  if self.chart_data[index]
    if fieldEnum == CPTScatterPlotFieldX
      self.chart_data[index][fieldEnum]
    else
      self.chart_data[index][fieldEnum]
    end
  end
end

绘图 space 的 xRange 被初始化为 [0, 1],而数据被初始化为 time - 125time - 75 之间的值。所有数据点都在此 xRange.

之外

计算数据点时需要使用当前时间吗?如果是这样,请将其保存在某处并在创建数据和配置绘图时使用相同的值 space。如果不是,请为数据选择一个常量起点并创建 xRange 以匹配。