CorePlot LineGraph - hover/ click on graph to see values - macOS
Asked Answered
P

1

9

I am using CorePlot to plot a simple Line Graph in my macOS app.

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

I was expecting that the hover/ click to see values would be a default behavior but looks it is not. I have tried searching the forums but no luck. I am assuming it would be really straight forward. Not sure if I am missing something.

Profusion answered 12/9, 2017 at 13:35 Comment(0)
S
4

As far as i know, you're impression is correct, there is no built in data value overlay. However, you can make it yourself. CorePlot has the function indexOfVisiblePointClosestToPlotAreaPoint: that should give you the references needed to add a label w/ point value to your chart.

  • (NSUInteger) indexOfVisiblePointClosestToPlotAreaPoint:

Returns the index of the closest point, or NSNotFound if there is no visible point.

Then you can subclass your graph hostingview, implement a mouse movement event to capture mouse coordinates, and from there to do whatever logic you want to chose how to display the points.

I wouldn't say it's particularly easy to implement, but at least its straight foward. I hope it helps!

References:

http://core-plot.github.io/MacOS/interface_c_p_t_scatter_plot.html#a57eacc8261a4d4a1399f1196be786cff https://mcmap.net/q/1319208/-core-plot-mousemoved

Spacetime answered 19/9, 2017 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.