【问题标题】:Creating two X-axis for 2 different plot in Core plot on a singe view page在单个视图页面上的核心图中为 2 个不同的图创建两个 X 轴
【发布时间】:2012-01-27 19:41:00
【问题描述】:

我试图在同一页面上绘制两组数据到时间,因此我需要两个不同的 X 和 Y 轴。这是我正在使用的示例代码:

 -(void)initialisePlot
{
// Start with some simple sanity checks before we kick off
if ( (self.hostingView == nil) || (self.graphData == nil) ) {
    NSLog(@"TUTSimpleScatterPlot: Cannot initialise plot without hosting view or data.");
    return;
}

if ( self.graph != nil ) {
    NSLog(@"TUTSimpleScatterPlot: Graph object already exists.");
    return;
}

// Create a graph object which we will use to host just one scatter plot.
CGRect frame = [self.hostingView bounds];
self.graph = [[CPTXYGraph alloc] initWithFrame:frame];

// Add some padding to the graph, with more at the bottom for axis labels.
self.graph.plotAreaFrame.paddingTop = 20.0f;
self.graph.plotAreaFrame.paddingRight = 20.0f;
self.graph.plotAreaFrame.paddingBottom = 50.0f;
self.graph.plotAreaFrame.paddingLeft= 20.0f;

// Tie the graph we've created with the hosting view.
self.hostingView.hostedGraph = self.graph;

// If you want to use one of the default themes - apply that here.
[self.graph applyTheme:[CPTTheme themeNamed:kCPTPlainWhiteTheme]];

// Create a line style that we will apply to the axis and data line.
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.lineColor = [CPTColor redColor];
lineStyle.lineWidth = 2.0f;

// Create a text style that we will use for the axis labels.
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
textStyle.fontName = @"Helvetica";
textStyle.fontSize = 14;
textStyle.color = [CPTColor blackColor];

// Create the plot symbol we're going to use.
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol hexagonPlotSymbol];
plotSymbol.lineStyle = lineStyle;
plotSymbol.size = CGSizeMake(8.0, 8.0);


// Setup some floats that represent the min/max values on our axis.
float xAxisMin = -10;
float xAxisMax = 10;
float yAxisMin = 0;
float yAxisMax = 100;

// We modify the graph's plot space to setup the axis' min / max values.
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xAxisMin) length:CPTDecimalFromFloat(xAxisMax - xAxisMin)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yAxisMin) length:CPTDecimalFromFloat(yAxisMax - yAxisMin)];

// Modify the graph's axis with a label, line style, etc.
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;


axisSet.xAxis.title = @"Data X";
axisSet.xAxis.titleTextStyle = textStyle;
axisSet.xAxis.titleOffset = 30.0f;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.labelTextStyle = textStyle;
axisSet.xAxis.labelOffset = 3.0f;
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(2.0f);
axisSet.xAxis.minorTicksPerInterval = 1;
axisSet.xAxis.minorTickLength = 5.0f;
axisSet.xAxis.majorTickLength = 7.0f;

axisSet.yAxis.title = @"Data Y";
axisSet.yAxis.titleTextStyle = textStyle;
axisSet.yAxis.titleOffset = 40.0f;
axisSet.yAxis.axisLineStyle = lineStyle;
axisSet.yAxis.majorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.labelTextStyle = textStyle;
axisSet.yAxis.labelOffset = 3.0f;
axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(20.0f);
axisSet.yAxis.minorTicksPerInterval = 1;
axisSet.yAxis.minorTickLength = 5.0f;
axisSet.yAxis.majorTickLength = 7.0f;

// Add a plot to our graph and axis. We give it an identifier so that we
// could add multiple plots (data lines) to the same graph if necessary.
CPTScatterPlot *plot = [[CPTScatterPlot alloc] init];



plot.dataSource = self;
plot.identifier = @"mainplot";

plot.dataLineStyle = lineStyle;
plot.plotSymbol = plotSymbol;
[_graph reloadData];
[self.graph addPlot:plot];
}

我应该做出哪些改变?任何的想法? 谢谢

【问题讨论】:

    标签: ios graph core-plot


    【解决方案1】:

    这一行

    CGRect frame = [self.hostingView bounds];
    

    表示我们将在所有的hostingView上绘制我们的情节。

    1. 怎么用

      -(void)initialisePlot:(UIInteger)indexOfPlot

      代替:

      -(void)initialisePlot

    2. 仅使用部分托管视图(相应地更改框架 indexOfPlot):

      CGRect frame = [self.hostingView bounds]; //change this to:

      只是方案:

      CGRect frame;

      if (indexOfPlot == 0) {frame = ...(set upper part here)} else {frame = ...(lower)}

    3. 然后在源码中找到initialisePlot,改成initialezePlot:0和initializePlot:1这两个方法。

    看起来很简单。

    【讨论】:

    • 你好卡斯帕图斯。我有点迷路了。 -(void)initialisePlot:(UIInteger)indexOfPlot 我应该在哪里声明这个?和 CGRect frame = [self.hostingView bounds];改变这个?
    • 在这里,可能在 .h 文件中。而且,是的,更改CGRect frame = [self.hostingView bounds]; 您必须使用 indexOfPlot 相应地设置框架。例如,第一个图的上半部分和第二个图的下半部分。
    • 你好 Kaspartus,你的建议工作正常,但如果我需要同时使用两个不同的数据集怎么办?
    • hm,看起来,您只发布了初始化图(只是没有图的轴)。您必须在所有函数中执行相同的技巧(使用 indexOfPlot 相应地绘制绘图)。另一种方法:将函数更改为具有绘图框架参数的函数(例如:-(void)initialisePlot:(NSFrame *)plotFrame 而不是 -(void)initialisePlot)。这真的很简单。但是您必须更改与框架链接的所有绘图功能。
    • 那么我可以在 initialiseplot() 中分别调用 frame1 和 frame 2,从而让最终用户可以看到这两个图吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多