2017-09-12 12 views
9

Tôi đang sử dụng CorePlot để vẽ biểu đồ đường thẳng đơn giản trong ứng dụng macOS của tôi.CorePlot LineGraph - di chuột/click vào biểu đồ để xem giá trị - 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]; 

Tôi đã hy vọng rằng di chuột/nhấp để xem giá trị sẽ là hành vi mặc định nhưng có vẻ như không phải. Tôi đã thử tìm kiếm trên diễn đàn nhưng không may mắn. Tôi giả định nó sẽ thực sự thẳng về phía trước. Không chắc chắn nếu tôi đang thiếu một cái gì đó.

Trả lời

4

Theo tôi biết, bạn là hiển thị chính xác, không có lớp phủ giá trị dữ liệu được tích hợp sẵn. Tuy nhiên, bạn có thể tự làm. CorePlot có chức năng indexOfVisiblePointClosestToPlotAreaPoint: sẽ cung cấp cho bạn các tham chiếu cần thiết để thêm giá trị nhãn w/điểm vào biểu đồ của bạn.

  • (NSUInteger) indexOfVisiblePointClosestToPlotAreaPoint:

Trả về chỉ số của các điểm gần nhất, hoặc NSNotFound nếu không có điểm nhìn thấy được.

Sau đó, bạn có thể phân lớp lưu trữ biểu đồ, triển khai sự kiện di chuyển chuột để nắm bắt tọa độ chuột và từ đó thực hiện bất kỳ logic nào bạn muốn chọn cách hiển thị điểm.

Tôi sẽ không nói nó đặc biệt dễ thực hiện, nhưng ít nhất là con chim thẳng của nó. Tôi hy vọng nó sẽ giúp!

Tài liệu tham khảo:

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

Các vấn đề liên quan