0

I'm trying to get the chart's gridlines to be transparent but it doesn't seem to be supported:

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

I'm doing to try and blend the gridlines with the chart background color (which in my code can change between colors) which would make the gridlines subtle rather than jarring when background colors change.

Here is my code:

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

Is there another way to achieve this effect by blending the green color with the background color?

Jay Corbett
  • 28,091
  • 21
  • 57
  • 74
Robert Penridge
  • 8,424
  • 2
  • 34
  • 55

1 Answers1

1

I figured it out. The below code will blend together a shade of gray with the background color you specify. It then uses the blended color as the color for the gridlines.

I took some tips/code/ideas from these:

Some utility macros are needed to do this, here they are:

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

And here's the code:

**
** 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;
Community
  • 1
  • 1
Robert Penridge
  • 8,424
  • 2
  • 34
  • 55