That's great Thomas.

Some comments:
Do you need to special case when edgecolor is _1 ? Could you just use an 
edgesize of 0 to get the same result? That simplifies your makedrawbar verb.

Note that you can define an object in the class jzplot from a script other than 
jzplot.ijs. So you can get away with quite a few less (or no) edits to 
jzplot.ijs by using Oleg's typereg verb to register your new plot type.  The 
script below should work without any changes to jzplot.ijs. 

However I think your generalization of getdatarange and addition of a new plot 
type category (SumHalfYTypes) to jzplot that is special-cased by getmin and 
getmax might be a good addition to jzplot.
Maybe typereg or similar should be added to the base library (jzplot.ijs ?) too?

Regards,
Ric

NB.==========Custom Plot script ===============================

require 'plot'

coclass 'pzplot'
coinsert 'jzplot'

NB.*typereg v register plot type (framework)
typereg=: 4 : 0
  ('plot_',y,'_jzplot_')=: ('plot_',y,'_',(>coname''),'_')~ f.
  for_i. x do.
    ((>i),'_jzplot_')=: (((>i),'_jzplot_')~ -. <y),<y
  end.
  PlotTypes_jzplot_=: ~. PlotTypes2d_jzplot_,PlotTypes3d_jzplot_

  empty''
)

plot_sbarn=: 3 : 0
'x y'=. 2 {. y { Data
dat=. citemize y
pos=. makestackbarn dat
pos makedrawbar dat
)

makedrawbar_jzplot_=: 4 : 0
pos=. x
dat=. y
'r c'=. $dat
if. r=1 do.
  drawrect iDATA;EDGECOLOR;EDGESIZE;(ITEMCOLOR {~ 0 >, dat);pos
else.
  clr=. r $ ITEMCOLOR
  pos=. (-c) ,\ pos
  for_p. pos do.
   drawrect iDATA;EDGECOLOR;EDGESIZE;(p_index{clr);_4 ]\ p
  end.
end.
)

makestackbarn_jzplot_=: 3 : 0
'r c'=. $y
wid=. BARWIDTH
w=. Gw * wid % c
t=. Gw * -: (-.wid) % c 
o=. |YMin % YMax-YMin
uh=. (1-o) * Gh
lh=. o * Gh
m=. 0&<:y
y=. m (* ,: -...@[*]) y
y=. +/\."2 y
h=. (uh,lh) * y % YMax,|YMin
h=. (-.m)} h
y=. Gy + lh + ,h
x=. Gx + ($y) $ t + Gw * }: int01 c
x,.y,.(x+w),.y-,h
)

(;:'PlotTypes2d') typereg 'sbarn'


Note 'Usage'
   'sbarn' plot (],:|.)@:(,|.@:-) i.5

   dat=: |:(+0.5-[:?0$~#)^:(<100) 0 _4 4 0
   pd 'new'
   pd 'type sbarn; edgesize 0; barwidth 1;xtic 10 2'
   NB. next 2 lines in lieu of getmin, getmax edits to jzplot.ijs
   minmax=. (<./@:({:"1) , >./@:({."1)) (0&< +//. ])"1 |:dat
   pd 'yrange ',": _2.5 2.5 <.@:+ minmax
   pd  dat
   pd 'type line;color 0 0 0; pensize 2;xtic 10 2'
   pd +/dat
   pd 'show'    )

NB.=========================================

> From: Thomas Costigliola
> 
> Thank you for the information. Option 1 led me to great example code
> so I went ahead and added a new chart type. There had to be some
> changes to other functions within the jzplot class so I updated the
> whole class. I attempted to make a subclass but was unsuccessful. All
> the results can be found here:
> 
>  http://www.jsoftware.com/jwiki/Improved%20Stacked%20Bar%20Graph
> 
> On Sun, Nov 8, 2009 at 5:08 AM, Sherlock, Ric wrote:
> >> From: Thomas Costigliola
> >>
> >> I would like to create a chart like the one in the link below. It is
> a
> >> stacked bar chart with no space between the bars. Plot doesn't seem
> to
> >> allow negative values in stacked bar charts. Is there any work
> around
> >> for this? There would also need to be a way to draw the bars next to
> >> each other as in a histogram.
> >>
> >> The given plot was produced with R; it has an incredibly flexible
> >> plotting package.
> >>
> >> http://www.jsoftware.com/jwiki/ThomasCostigliola
> >
> > The gap between bars can be adjusted using the "barwidth" option.
> > "http://www.jsoftware.com/jwiki/Plot/Options";
> >
> >   'bar;barwidth 1;' plot 3 9 8 _3 9 _7
> >
> > However I don't think you're going to to get the output you're after
> using existing plot types and options in J.
> > I see two main avenues of choices:
> >
> > 0. Use the stats/r/rserve addon (interface to R) to create your chart
> from J data using R.
> > Below is a very basic example of how you could create an R chart as a
> PDF from J.
> >
> >   load 'stats/r/rserve task'
> >   Rcmd 'x<- seq(-10,10,length=50)'
> >   Rcmd 'y<- x'
> >   Rcmd 'foo<- function(x,y){r<-sqrt(x^2+y^2);10*sin(r)/r}'
> >   Rcmd 'z<- outer(x , y , foo)'
> >   Rcmd 'pdf(file="c:\\users\\rsherloc\\documents\\j602-
> user\\temp\\RicRplot001.pdf")'
> >   Rcmd
> 'persp(x,y,z,theta=30,phi=30,expand=0.5,xlab="X",ylab="Y",zlab="Z")'
> >   Rcmd 'dev.off()'
> >   launch jpath '~temp/RicRplot001.pdf'
> >
> > For your purposes you'd want to send the raw data from J to R for
> plotting rather than create in R as in the above example (otherwise you
> might as well just do the whole thing in R!?!
> >
> > 1. Create a new custom chart type
> > There was a good thread on modifying and combining existing chart
> types in the forums about a year ago where Oleg gave an example of how
> to write a custom plot type.
> > "http://www.jsoftware.com/pipermail/programming/2008-
> November/013009.html"
> >
> > I've got a draft of a wiki page summarizing the thread somewhere that
> I must get around to adding to the wiki.
> >
> >
> >
> > ---------------------------------------------------------------------
> -
> > For information about J forums see
> http://www.jsoftware.com/forums.htm
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to