ScatterPlot无法正常工作 - 点未连接

时间:2017-12-06 21:25:18

标签: ios charts frameworks core-plot scatter-plot

我正在使用带有Objective-C的Core-Plot开发一个项目。客户要求创建一个折线图(使用Scatter Plot为此),其中绘制了时间的数量×时间。

嗯,经过近一个星期的研究,这是行不通的。我的点没有连接,我不知道为什么。

Plots doesn't work

这条蓝线应该已连接,因为它是一个图。

这是我的代码:

-(void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme animated:(BOOL)animated{
CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:bounds];
[self addGraph:graph toHostingView:layerHostingView];
[self applyTheme:theme toGraph:graph withDefault:[CPTTheme themeNamed:kCPTDarkGradientTheme]];

graph.paddingTop = 20.0;
graph.paddingRight = 20.0;
graph.paddingLeft = 20.0;

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.delegate              = self;

CPTMutableLineStyle *axesLineStyle = [CPTMutableLineStyle lineStyle];
axesLineStyle.lineWidth            = 1.0f;
axesLineStyle.miterLimit           = 1.0f;
axesLineStyle.lineColor            = [CPTColor grayColor];

CPTTextStyle *labelTextStyle = [CPTTextStyle textStyleWithAttributes:@{ NSForegroundColorAttributeName: BoardGraphValueColor, NSFontAttributeName: BoardGraphValueFont}];

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
NSNumberFormatter * axisFormat = [[NSNumberFormatter alloc] init];
[axisFormat setNumberStyle:NSNumberFormatterNoStyle];
CPTXYAxis *x          = axisSet.xAxis;{
    x.visibleRange                  = [CPTPlotRange plotRangeWithLocation:@(-1.0) length:@(100)];
    x.labelingPolicy                = CPTAxisLabelingPolicyAutomatic;
    x.majorIntervalLength           = @2;
    x.minorTicksPerInterval         = 1;
    x.majorTickLineStyle            = axesLineStyle;
    x.minorTickLineStyle            = axesLineStyle;
    x.axisLineStyle                 = axesLineStyle;

    x.plotSpace                     = plotSpace;
    x.labelTextStyle                = labelTextStyle;
    x.labelFormatter                = axisFormat;
    x.labelOffset                   = 5.0;
}

CPTXYAxis *y = axisSet.yAxis;{
    y.visibleRange   = [CPTPlotRange plotRangeWithLocation:@(-1.0) length:@(200)];
    y.labelingPolicy                = CPTAxisLabelingPolicyAutomatic;
    y.majorIntervalLength           = @30;
    y.minorTicksPerInterval         = 6;
    y.majorTickLineStyle            = axesLineStyle;
    y.minorTickLineStyle            = axesLineStyle;
    y.axisLineStyle                 = axesLineStyle;

    y.plotSpace                     = plotSpace;
    y.labelTextStyle                = labelTextStyle;
    y.labelFormatter                = axisFormat;
    y.labelOffset                   = 5.0;
}

graph.axisSet.axes = @[x, y];

CPTMutableLineStyle *lineStyle = [[CPTMutableLineStyle alloc] init];
lineStyle.lineWidth = 2.0;

CPTScatterPlot *scatterLinePlot = [[CPTScatterPlot alloc] init];
scatterLinePlot.areaBaseValue = @(0.0);

scatterLinePlot.identifier = @(1);
scatterLinePlot.title = @"Title";

lineStyle.lineColor = [CPTColor blueColor];
scatterLinePlot.dataLineStyle = lineStyle;
scatterLinePlot.histogramOption = self.histogramOption;

scatterLinePlot.areaBaseValue = @(0);

scatterLinePlot.delegate                        = self;
scatterLinePlot.plotSymbolMarginForHitDetection = 5.0;
scatterLinePlot.dataSource = self;

[graph addPlot:scatterLinePlot];

CPTPlotRange *globalYRange = [CPTPlotRange plotRangeWithLocation:@0.0
                                                          length:@12.0];
plotSpace.globalYRange = globalYRange;

[plotSpace scaleToFitEntirePlots:[graph allPlots]];}

这是创建一个情节的代码,但我需要创建六个。我做了一个循环,但它也是这样。

这是数据源:

-(NSUInteger)numberOfRecordsForPlot:(nonnull CPTPlot *)plot{
return [self.plotData objectAtIndex:0].count;}

-(NSNumber *)numberForPlot:(nonnull CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
NSNumber *num = nil;
NSString *key;
NSArray *currentDataPoint;

NSString *plotIdentifier = (NSString *)plot.identifier;

if ([plotIdentifier isEqual:@(1)]){
    currentDataPoint = [self.plotData objectAtIndex:0];
} else if ([plotIdentifier isEqual:@(2)]){
    currentDataPoint = [self.plotData objectAtIndex:1];
} else if ([plotIdentifier isEqual:@(3)]){
    currentDataPoint = [self.plotData objectAtIndex:2];
} else if ([plotIdentifier isEqual:@(4)]){
    currentDataPoint = [self.plotData objectAtIndex:3];
} else if ([plotIdentifier isEqual:@(5)]){
    currentDataPoint = [self.plotData objectAtIndex:4];
} else if ([plotIdentifier isEqual:@(6)]){
    currentDataPoint = [self.plotData objectAtIndex:5];
}

if (fieldEnum == CPTScatterPlotFieldX) {

    return @(index);

} else if (fieldEnum == CPTScatterPlotFieldY) {

    key = @"count";
    int number = [[[currentDataPoint objectAtIndex:index] valueForKey:key] intValue];
    NSLog(@"Y value for index %lu is %d", index, number);
    num = [[currentDataPoint objectAtIndex:index] valueForKey:key];
    return num;}

return num;
}

我做错了什么?

PS:嗯,另一个错误是条形图的颜色。每个都应该有不同的颜色,但它也不起作用。提供的颜色是传说中的颜色。

提前致谢。

1 个答案:

答案 0 :(得分:0)

要更改每个条形的条形颜色,请在数据源中实施-barFillForBarPlot:recordIndex:方法,并为每个索引返回正确的填充。您还可以使用-barLineStyleForBarPlot:recordIndex:数据源方法为每个条形设置边框线样式(例如,使用不同的颜色)。

看起来数据线被截断到可见区域之外。调用-scaleToFitEntirePlots:时,请确保数据源中的绘图数据可用。