创建透明网格线

时间:2012-09-07 15:19:18

标签: graph sas

我试图让图表的网格线透明,但似乎不支持:

http://support.sas.com/documentation/cdl/en/grstatug/63302/HTML/default/viewer.htm#n1f71f6e9v037an1jy274v66z4r1.htm

我正在尝试将网格线与图表背景颜色混合(在我的代码中可以在颜色之间进行更改),这会使网格线变得微妙而不是在背景颜色发生变化时发生震动。

这是我的代码:

**
** TAKE THE DEFAULT STYLE BEING USED.  MODIFY IT SO THAT 
** GRAPH GRIDLINES WILL BE GREEN AND MOSTLY TRANSPARENT
*;
proc template;
  define style my_style;
    parent = styles.default;
    style GraphGridLines from GraphGridLines / contrastcolor=green transparency=.05;
  end;
run;


**
** LAYOUT TEMPLATE FOR A SIMPLE SERIES CHART
*;
proc template;
 define statgraph mychart;
  begingraph; 
    layout overlay / wallcolor=black
                     xaxisopts=(display=(line) griddisplay=on) 
                     yaxisopts=(display=(line))
                     ;

      seriesplot x=name y=height / lineattrs=(color=white); 

    endlayout;  
  endgraph;
 end;
run;


**
** PLOT SAMPLE DATA USING CUSTOM STYLE AND CHART LAYOUT WE JUST DEFINED
*;
ods graphics / width=640 height=640 ;
ods html style=my_style;

proc sgrender data=sashelp.class template=mychart;
run;

ods html close;

是否有其他方法可以通过将绿色与背景颜色混合来实现此效果?

1 个答案:

答案 0 :(得分:1)

我明白了。下面的代码将灰色阴影与您指定的背景颜色混合在一起。然后使用混合颜色作为网格线的颜色。

我从这些中获取了一些提示/代码/想法:

执行此操作需要一些实用程序宏,它们是:

**
** MACRO TO CONVERT RGB->HEX
*;
%macro rgb_to_hex(iR=,iG=,iB=);
  %local r g b hex;

  %let r=%sysfunc(round(&iR,1));
  %let g=%sysfunc(round(&iG,1));
  %let b=%sysfunc(round(&iB,1)); 

  %let hex = CX%sysfunc(putn(&r,hex2.))%sysfunc(putn(&g,hex2.))%sysfunc(putn(&b,hex2.));
  &hex
%mend;


**
** MACRO TO CONVERT HEX->RGB.  RESULTS ARE SAVED TO THE SPECIFIED MACRO VARIABLES
*;
%macro hex_to_rgb(iHex=, iRedMacroVar=rr, iGreenMacroVar=gg, iBlueMacroVar=bb);

  %global &iRedMacroVar &iGreenMacroVar &iBlueMacroVar;
  %local r g b;

  %let r=%substr(&iHex,3,2);
  %let g=%substr(&iHex,5,2);
  %let b=%substr(&iHex,7,2);

  %let &iRedMacroVar   = %sysfunc(putn(%sysfunc(inputn(&r,hex2.)),z3.));
  %let &iGreenMacroVar = %sysfunc(putn(%sysfunc(inputn(&g,hex2.)),z3.));
  %let &iBlueMacroVar  = %sysfunc(putn(%sysfunc(inputn(&b,hex2.)),z3.));

%mend;


**
** MACRO TO BLEND COLORS TOGETHER.
*;
%macro blend_colors(iHexColor1=, iHexColor2=, iBlendType=average);
  %local blended;

  %hex_to_rgb(iHex=&iHexColor1, iRedMacroVar=r1, iGreenMacroVar=g1, iBlueMacroVar=b1)
  %hex_to_rgb(iHex=&iHexColor2, iRedMacroVar=r2, iGreenMacroVar=g2, iBlueMacroVar=b2)



  %if "%upcase(&iBlendType)" eq "AVERAGE" %then %do;
    %let blended = %rgb_to_hex(iR=%qsysfunc(round((&r1 + &r2) / 2,1)),
                               iG=%qsysfunc(round((&g1 + &g2) / 2,1)),
                               iB=%qsysfunc(round((&b1 + &b2) / 2,1)));
  %end;
  %else %if "%upcase(&iBlendType)" eq "ADDITIVE" %then %do;
    %let blended = %rgb_to_hex(iR=%sysfunc(min(&r1 + &r2, 255)),
                               iG=%sysfunc(min(&g1 + &g2, 255)),
                               iB=%sysfunc(min(&b1 + &b2, 255)));
  %end;
  %else %do;
    %put %str(E)RROR: (MACRO.BLEND_COLORS.SAS): INCORRECT PARAMETER PASSED IN FOR IBLENDTYPE.;
  %end;
  &blended
%mend;

这是代码:

**
** PARAMETERS - SPECIFY A BACKGROUND COLOR AND THE AMOUNT OF GREY TO BLEND AGAINST IT
*;

* EXAMPLE1;
%let bg_color   = CX000000; *BLACK;
%let grid_color = CX202020; *JUST A LITTLE GREY NEEDED;

* EXAMPLE2;
/*%let bg_color   = CX0FF000; *GREEN;*/
/*%let grid_color = CX080808; * VERY LITTLE GREY NEEDED;*/

/** EXAMPLE3;*/
/*%let bg_color   = CXFF0000; *RED;*/
/*%let grid_color = CX363636; *STRONGER GREY NEEDED;*/


**
** YOU COULD ALSO SPECIFY COLORS USING RGB IF YOU PREFER. 
** USING THE BELOW COMMENTED OUT SYNTAX
*;
/*%let bg_color   = %rgb_to_hex(iR=0  ,iG=0  ,iB=0  );*/
/*%let grid_color = %rgb_to_hex(iR=20 ,iG=20 ,iB=20);*/




**
** BLEND THE COLORS TOGETHER
*;
%let blended = %blend_colors(iHexColor1=&bg_color, iHexColor2=&grid_color, iBlendType=additive);




**
** TAKE THE DEFAULT STYLE BEING USED.  MODIFY IT SO THAT 
** GRAPH GRIDLINES WILL BE GREEN AND MOSTLY TRANSPARENT
*;
proc template;
  define style my_style;
    parent = styles.default;
    style GraphGridLines from GraphGridLines / contrastcolor=&blended; * USE THE BLENDED COLOR AS THE GRID COLOR;
  end;
run;


**
** LAYOUT TEMPLATE FOR A SIMPLE SERIES CHART
*;
proc template;
 define statgraph mychart;
  begingraph; 
    layout overlay / wallcolor=&bg_color
                     xaxisopts=(display=(line) griddisplay=on) 
                     yaxisopts=(display=(line))
                     ;

      seriesplot x=name y=height / lineattrs=(color=white); 

    endlayout;  
  endgraph;
 end;
run;


**
** PLOT SAMPLE DATA USING CUSTOM STYLE AND CHART LAYOUT WE JUST DEFINED
*;
ods graphics / width=640 height=640 ;
ods html style=my_style;

proc sgrender data=sashelp.class template=mychart;
run;

ods html close;