2016-11-19 100 views
1

我试图将DatePlot核心绘图示例添加到UIVisualEffectView。我有图形渲染,但由于某种原因,我没有看到实际的数据行。我想知道如果有人能帮我看看我做错了什么。以下是我的代码。我打电话set_up_chart,然后add_chart_data,如果有帮助。 (我也打过电话add_chart_dataset_up_chart之前,但得到相同的结果。)Core绘图DatePlot示例的执行示例不绘制线条

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 

回答

0

xRange的情节空间被初始化为[0,1],而数据time - 125time - 75之间初始化值。所有的数据点都在xRange以外。

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

+0

啊,非常感谢! – tvalent2