2017-12-06 7 views
0

Objective-CでCore-Plotを使ってプロジェクトを開発しています。クライアントは、xの時間数をプロットした折れ線グラフ(これについては散布図を使用)を作成するよう求めます。ScatterPlotは正しく動作しません - ドットは接続されていません

まあ、ほぼ一週間勉強した後、これは動作していません。私のドットは結ばれておらず、私は理由を知らない。

Plots doesn't work

それは1つのプロットしたものであるため、この青い線は、接続してきたはずです。

は、ここに私のコードです:

-(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]];} 

これは単なる1プロットを作成するためのコードがあるが、私はを作成する必要があります。私はループをしましたが、それもそのように働いています。

は、ここにデータソースです:

-(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:もう一つのバグは、棒グラフの色です。それぞれの色は異なるはずですが、うまくいきません。提供される色は、伝説の色です。

ありがとうございます。

答えて

0

各バーのバーの色を変更するには、データソースに-barFillForBarPlot:recordIndex:メソッドを実装し、各インデックスの正しい塗りを返します。 -barLineStyleForBarPlot:recordIndex:データソースメソッドでは、各バーの境界線のスタイルを設定することもできます(別の色を使用するなど)。

データラインが可視領域外にクリップされているようです。 -scaleToFitEntirePlots:を呼び出すときに、プロットデータがデータソースから利用可能であることを確認してください。

+0

@Eric Skrochありがとうございます。行の問題は、グラフのglobalYrangeです。それを変更し、働いた。 バーについて、私はこれを使用しています: '[CPTFill fillWithColor:[CPTColor colorWithCGColor:[UIColor colorWithHexString:color] .CGColor]]'とは動作しません。 (可変色は16進値を持つ) {CPTFill fillWithColor:[CPTColor redColor]]に変更すると、すべてのバーが赤に変わります。 何か間違っていますか? もう一度おねがいします! –

+0

'-colorWithHexString:'が有効な色を返すことを確認してください。 –

+0

です!それは絵の底にある伝説の色を戻します。 @@Eric Skroch、私を助けることができます:散布図で領域を選択しようとしていますが、 'plotAreaWasSelected:'を理解できません。この方法は私が使っているのですか? –

関連する問題