核心情节线没有出现

Core Plot line not showing up

我创建了 CorePlot 框架的简单实现。图表显示正常,但没有绘制线条。我已经尝试注销我的示例值并且确实有数据。任何关于为什么绘制数据线没有显示的输入都将不胜感激!

头文件:

#import <UIKit/UIKit.h>
#import "PresentedViewControllerDelegate.h"
#import "CorePlot-CocoaTouch.h"

#define NUM_SAMPLES 30

@interface DeviceDetailModal : UIViewController <PresentedViewControllerDelegate, UIWebViewDelegate, UITableViewDataSource, UITableViewDelegate, CPTPlotDataSource>{
CPTXYGraph *graph;
NSMutableArray *samples;

double xxx[NUM_SAMPLES];
double yyy1[NUM_SAMPLES];

}

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, weak) id <PresentedViewControllerDelegate> delegate;


@end

主文件

#import <Foundation/Foundation.h>
#import "DeviceDetailModal.h"
#import "DetailCell.h"
#import "CorePlot-CocoaTouch.h"

#define START_POINT 0 // Start at origin
#define END_POINT 15.0 // TODO time is 15 seconds

#define X_VAL @"X_VAL"
#define Y_VAL @"Y_VAL"

@implementation DeviceDetailModal


-(void)viewDidLoad{
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(closeView)];
self.navigationItem.leftBarButtonItem = closeButton;

[[UIBarButtonItem appearance] setTintColor:[UIColor lightGrayColor]];

[[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];


self.tableView.layer.cornerRadius = 10.0;
self.tableView.layer.borderWidth = 0.7;
self.tableView.layer.borderColor = [UIColor whiteColor].CGColor;

[self generateDataSamples];

NSNumber *xAxisStart = [NSNumber numberWithDouble:START_POINT];
NSNumber *xAxisLength = [NSNumber numberWithDouble:END_POINT - START_POINT];

double maxY = [[samples valueForKeyPath:@"@max.Y_VAL"] doubleValue];
NSNumber *yAxisStart = [NSNumber numberWithDouble:-maxY];
NSNumber *yAxisLength = [NSNumber numberWithDouble:2 *maxY];

CGRect hostingRect = CGRectMake(20, 330, 335, 347);


CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:hostingRect];

[self.view addSubview:hostingView];

graph = [[CPTXYGraph alloc] initWithFrame:hostingView.bounds];

hostingView.hostedGraph = graph;

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:xAxisStart
                                                length:xAxisLength];

plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:yAxisStart
                                                length:yAxisLength];

CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init];
dataSourceLinePlot.delegate = self;

// LINE STYLE
CPTMutableLineStyle *mainPlotLineStyle = [[dataSourceLinePlot dataLineStyle] mutableCopy];
[mainPlotLineStyle setLineWidth:2.0f];
[mainPlotLineStyle setLineColor:[CPTColor colorWithCGColor:[[UIColor whiteColor] CGColor]]];

[dataSourceLinePlot setDataLineStyle:mainPlotLineStyle];

// AXIS STYLE

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)[graph axisSet];

CPTXYAxis *xAxis = [axisSet xAxis];
CPTXYAxis *yAxis = [axisSet yAxis];
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
[axisLineStyle setLineWidth:1];
[axisLineStyle setLineColor:[CPTColor colorWithCGColor:[[UIColor whiteColor] CGColor]]];

[xAxis setAxisLineStyle:axisLineStyle];
[xAxis setMajorTickLineStyle:axisLineStyle];
[yAxis setAxisLineStyle:axisLineStyle];
[yAxis setMajorTickLineStyle:axisLineStyle];


[graph addPlot:dataSourceLinePlot];
NSLog(@"PLOT: %@",dataSourceLinePlot);
[graph reloadData];

}


-(void)generateDataSamples{
double length = (END_POINT - START_POINT);
double delta = length / (NUM_SAMPLES -1);

samples = [[NSMutableArray alloc] initWithCapacity:NUM_SAMPLES];

for (int i = 0; i < NUM_SAMPLES; i++) {
    double x = START_POINT + (delta * i);

    //X^2
    double y = x * x;

    NSDictionary *sample = [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSNumber numberWithDouble:x],X_VAL,
                            [NSNumber numberWithDouble:y],Y_VAL,
                            nil];
    NSLog(@"SAMPLE: %@", sample);
    [samples addObject:sample];
}

[graph reloadData];


}

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot{
return NUM_SAMPLES;
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
NSDictionary *sample = [samples objectAtIndex:index];

if (fieldEnum == CPTScatterPlotFieldX) {
    return [sample valueForKey:X_VAL];
}else{
    return [sample valueForKey:Y_VAL];
}
}

默认背景填充为白色,看起来您没有更改过它。情节线是用白色绘制的,所以它不会出现在白色背景上。尝试使用默认线条样式或此:

mainPlotLineStyle.lineColor = [CPTColor blackColor];

找到问题了!没有设置绘图的数据源,我不得不这样做:

dataSourceLinePlot.dataSource = self;

当我的 numberOfRecordsPerPlot 方法从未被调用时,我发现了这一点。