2017-09-12 73 views
9

我正在使用CorePlot在我的macOS应用程序中绘制一个简单的线图。CorePlot LineGraph - 悬停/点击图表查看值 - macOS

CPTXYGraph *newGraph = [[CPTXYGraph alloc] initWithFrame:CGRectZero]; 

CPTTheme *theme  = [CPTTheme themeNamed:kCPTDarkGradientTheme]; 
[newGraph applyTheme:theme]; 
self.graph = newGraph; 
self.hostView.hostedGraph = newGraph; 

newGraph.plotAreaFrame.paddingTop = 10.0; 
newGraph.plotAreaFrame.paddingBottom = 30.0; 
newGraph.plotAreaFrame.paddingLeft = 40.0; 
newGraph.plotAreaFrame.paddingRight = 10.0; 

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)newGraph.defaultPlotSpace; 
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:@(1.0) length:[NSNumber numberWithUnsignedInteger:[dataArray count]-1]]; 
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:@0.0 length:@102.0]; 
    plotSpace.allowsUserInteraction = YES; 

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)newGraph.axisSet; 
    CPTXYAxis *x   = axisSet.xAxis; 
    //x.majorIntervalLength = @1; 
    x.majorIntervalLength = [NSNumber numberWithInt:numberOfIntervalsX]; 
    x.orthogonalPosition = @(0); 
    x.minorTicksPerInterval = 0; 
    x.labelOffset = 0; 

    CPTXYAxis *y = axisSet.yAxis; 
    y.majorIntervalLength = @5; 
    y.minorTicksPerInterval = 0; 
    y.orthogonalPosition = @(1.0); 
    y.labelOffset = 0.0; 

CPTScatterPlot *dataSourceLinePlot = [[CPTScatterPlot alloc] init]; 
    CPTMutableLineStyle *lineStyle = [dataSourceLinePlot.dataLineStyle mutableCopy]; 
    lineStyle.lineWidth    = 2.; 
    lineStyle.lineColor    = [CPTColor greenColor]; 
    dataSourceLinePlot.dataLineStyle = lineStyle; 


    dataSourceLinePlot.dataSource = self; 
    [newGraph addPlot:dataSourceLinePlot]; 

我期待悬停/点击看到值将是一个默认行为,但看起来不是。我试过搜索论坛,但没有运气。我认为这将是非常简单的。不知道我是否缺少一些东西。

回答

4

据我所知,你的印象是正确的,没有内置的数据值叠加。但是,你可以自己做。 CorePlot具有功能indexOfVisiblePointClosestToPlotAreaPoint:,它应该为您提供将标记带点值添加到图表所需的参考。

  • (NSUInteger)indexOfVisiblePointClosestToPlotAreaPoint:

如果没有可见点返回最接近点的指数,或NSNotFound。

然后,您可以继承图的hostingview,实现鼠标移动事件捕获鼠标坐标,并从那里做任何你想要选择的逻辑来选择如何显示点。

我不会说这是特别容易实现,但至少它的直向前进。我希望它有帮助!

参考文献:

http://core-plot.github.io/MacOS/interface_c_p_t_scatter_plot.html#a57eacc8261a4d4a1399f1196be786cff https://stackoverflow.com/a/21819342/357288