【问题标题】:CorePlot LineGraph - hover/ click on graph to see values - macOSCorePlot LineGraph - 悬停/单击图表以查看值 - macOS
【发布时间】:2017-09-12 13:35:21
【问题描述】:

我正在使用 CorePlot 在我的 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];

我原以为悬停/单击以查看值将是默认行为,但看起来并非如此。我试过搜索论坛,但没有运气。我假设这真的很简单。不确定我是否遗漏了什么。

【问题讨论】:

    标签: objective-c macos core-plot


    【解决方案1】:

    据我所知,您的印象是正确的,没有内置的数据值叠加。但是,您可以自己制作。 CorePlot 具有 indexOfVisiblePointClosestToPlotAreaPoint: 函数,它应该为您提供在图表中添加带有点值的标签所需的参考。

    • (NSUInteger) indexOfVisiblePointClosestToPlotAreaPoint:

    返回最近点的索引,如果没有可见点,则返回 NSNotFound。

    然后您可以子类化您的图形托管视图,实现鼠标移动事件以捕获鼠标坐标,并从那里执行您想要选择如何显示点的任何逻辑。

    我不会说它特别容易实现,但至少它是直截了当的。希望对你有帮助!

    参考资料:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 2019-07-26
      • 1970-01-01
      相关资源
      最近更新 更多