【问题标题】:core plot custom Theme?核心情节自定义主题?
【发布时间】:2011-02-27 08:45:21
【问题描述】:

我在 iPhone 应用程序中使用 Core-Plot 绘制趋势图,但还没有找到自定义背景的方法。我可以使用内置主题创建主题,例如 kCPPlainWhiteTheme,但我该如何更改它们?还是创建一个新的?

我基本上需要做的是使背景透明。

编辑/更新

我刚刚测试了这段代码,但它似乎不起作用:

    //CPTheme *theme = [CPTheme themeNamed:kCPPlainWhiteTheme];
    CPTheme *theme = [[CPTheme alloc]init];
    chartTrend5 = (CPXYGraph *)[theme newGraph];    
    chartView.hostedGraph = chartTrend5;
    chartTrend5.paddingLeft = 0.0;
    chartTrend5.paddingTop = 0.0;
    chartTrend5.paddingRight = 0.0;
    chartTrend5.paddingBottom = 0.0;
    chartTrend5.fill = nil;
    chartTrend5.borderLineStyle = nil;
    chartView.hostedGraph.fill = nil;

    chartTrend5PlotSpace = (CPXYPlotSpace *)chartTrend5.defaultPlotSpace;
    chartTrend5PlotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
                                                              length:CPDecimalFromFloat(5)];

    // range is 0-125, but since the frame heights is 90, 
    // we need to convert the points to this adjusted scale. The factor is 0.72 => (90/125)
    chartTrend5PlotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0)
                                                              length:CPDecimalFromFloat(90)];



    CPXYAxisSet *axisSet = (CPXYAxisSet *)chartTrend5.axisSet;

    CPXYAxis *x = axisSet.xAxis;
    x.majorIntervalLength =  CPDecimalFromFloat(100);
    //x.constantCoordinateValue = CPDecimalFromFloat(2);
    x.minorTicksPerInterval = 2;
    x.borderWidth = 0;
    x.labelExclusionRanges = [NSArray arrayWithObjects:
                              [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-1) 
                                                          length:CPDecimalFromFloat(800)], 
                              nil];;

    CPXYAxis *y = axisSet.yAxis;
    y.majorIntervalLength = CPDecimalFromFloat(100);
    y.minorTicksPerInterval = 1;
    //y.constantCoordinateValue = length:CPDecimalFromFloat(2);
    y.labelExclusionRanges = [NSArray arrayWithObjects:
                              [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-26) 
                                                          length:CPDecimalFromFloat(100)], 
                              nil];





    CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease];
    dataSourceLinePlot.identifier = @"TrendChart";
    dataSourceLinePlot.dataLineStyle.lineWidth = 2.f;
    dataSourceLinePlot.dataLineStyle.lineColor = [CPColor colorWithComponentRed:(16/255.f) 
                                                                          green:(101/255.f) 
                                                                           blue:(122/255.f) 
                                                                          alpha:1];
    dataSourceLinePlot.dataSource = self;
    [chartTrend5 addPlot:dataSourceLinePlot];
    chartTrend5.fill = nil;
    // Put an area gradient under the plot above

    CPColor *areaColor = [CPColor colorWithComponentRed:(212/255.f) 
                                                  green:(233/255.f)
                                                   blue:(216/255.f)
                                                  alpha:1];

    CPGradient *areaGradient = [CPGradient gradientWithBeginningColor:areaColor 
                                                          endingColor:areaColor];
    areaGradient.angle = -90.0f;
    CPFill *areaGradientFill = [CPFill fillWithGradient:areaGradient];
    dataSourceLinePlot.areaFill = areaGradientFill;
    dataSourceLinePlot.areaBaseValue = CPDecimalFromString(@"5.25");

这里我将 CPXYGraph *chartTrend5 和 *CPXYPlotSpace *chartTrend5PlotSpace 的填充属性设置为 nil。

【问题讨论】:

    标签: iphone core-plot


    【解决方案1】:

    除了 Eric 的建议之外,您还可以尝试将填充设置为清晰的颜色。例如,我过去曾使用它为图表提供透明背景:

    CPTTheme *theme = [CPTTheme themeNamed:kCPPlainWhiteTheme];
    graph = (CPTXYGraph *)[theme newGraph];
    
    graph.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
    graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
    

    【讨论】:

    • 如果您出于任何原因(滚动、调整大小等)担心绘图性能,将这些填充属性设置为 nil 将告诉 Core Plot 没有可绘制的内容。这样它就知道跳过设置剪辑区域和绘制不会显示在屏幕上的像素。
    • @Eric - 感谢您指出这一点。我替换了一些我有清晰填充的代码,只是将填充设置为 nil,并在不断重新渲染绘图时在我的 Mac 上节省了大约 2-3% 的 CPU 负载。
    • 嗨,我也是这样做的,但是我遇到了一个奇怪的问题,即图形页面上的所有控件都被镜像了。我发了一个问题:stackoverflow.com/questions/9927031/…
    • 这似乎不适用于最新版本。即:对于 CPTXYGraph..Brad 你能对此发表评论吗?
    • CPFill 和 CPColor 现在是 CPTFill 和 CPTColor
    【解决方案2】:

    主题只是一次设置大量样式属性的便捷方式。您可以单独设置其中任何一个以自定义外观。任何时候你想让一个区域透明,你可以将它的填充设置为nil。与线条样式相同 - 将它们设置为 nil 以防止绘制线条。

    您可能会关注两个“背景”区域。图形有填充,绘图区域也有填充。绘图区域是绘制绘图的区域。将这两个上的fill 属性设置为nil 以使背景透明。

    【讨论】:

    • 我做不到 chartTrend5PlotSpace.fill = nil;
    • 嗨,我也是这样做的,但是我遇到了一个奇怪的问题,即图形页面上的所有控件都被镜像了。我发了一个问题:stackoverflow.com/questions/9927031/…
    【解决方案3】:

    我试过了,它对我有用:

    // Here 'hostingView' is your CPTGraphHostingView to
    // which you add your graph
    UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
    [hostingView addSubview:backgorundImage];
    [hostingView sendSubviewToBack:backgroundImage];
    

    【讨论】:

      【解决方案4】:

      我使用了.fill 和.plot.AreaFrame.fill 属性:

      plot = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
      CPTTheme *theme = [CPTTheme themeNamed:kCPTSlateTheme];
      [plot applyTheme:theme];
      self.graph.collapsesLayers = NO;
      self.graph.hostedGraph = plot;
      
      plot.paddingLeft = 1.0;
      plot.paddingTop = 1.0;
      plot.paddingRight = 1.0;
      plot.paddingBottom = 1.0;
      plot.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
      plot.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor clearColor]];
      

      这对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多