192ceb2003-11-22Henrik Grubbström (Grubba) //! Graph sub-module for drawing bars.
2249372001-11-19Martin Nilsson // These functions were written by Henrik "Hedda" Wallin (hedda@roxen.com) // Create_bars can draw normal bars, sumbars and normalized sumbars.
d697d71999-09-30Henrik Wallin 
a580e12000-09-27Fredrik Hübinette (Hubbe) #pike __REAL_VERSION__
a20af62000-09-26Fredrik Hübinette (Hubbe) 
d697d71999-09-30Henrik Wallin #include "graph.h"
78ee8b2005-11-15Martin Nilsson inherit .create_graph;
d697d71999-09-30Henrik Wallin 
2249372001-11-19Martin Nilsson #ifdef BG_DEBUG # define START_DEBUG(X) diagram_data->bg_timers[(X)]=gethrtime() # define STOP_DEBUG(X) diagram->data->bg_timers[(X)]=gethrtime()-diagram_data->bg_timers[(X)] #else # define START_DEBUG(X) # define STOP_DEBUG(X) #endif
d697d71999-09-30Henrik Wallin  mapping(string:mixed) create_bars(mapping(string:mixed) diagram_data) { #ifdef BG_DEBUG
2249372001-11-19Martin Nilsson  diagram_data->bg_timers = ([]);
d697d71999-09-30Henrik Wallin #endif //Supports only xsize>=100
aaa34a2005-11-15Martin Nilsson  int si=diagram_data->fontsize;
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  //Fix defaultcolors! setinitcolors(diagram_data);
e1767a2005-11-14Martin Nilsson  Image.Image barsdiagram;
d697d71999-09-30Henrik Wallin 
2249372001-11-19Martin Nilsson  START_DEBUG("init_bg");
d697d71999-09-30Henrik Wallin  init_bg(diagram_data);
2249372001-11-19Martin Nilsson  STOP_DEBUG("init_bg");
aaa34a2005-11-15Martin Nilsson  barsdiagram=diagram_data->image;
d697d71999-09-30Henrik Wallin 
2249372001-11-19Martin Nilsson  START_DEBUG("set_legend_size");
d697d71999-09-30Henrik Wallin  set_legend_size(diagram_data);
2249372001-11-19Martin Nilsson  STOP_DEBUG("set_legend_size");
aaa34a2005-11-15Martin Nilsson  //write("ysize:"+diagram_data->ysize+"\n"); diagram_data->ysize-=diagram_data->legend_size; //write("ysize:"+diagram_data->ysize+"\n");
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  //Calculate biggest and smallest datavalues
2249372001-11-19Martin Nilsson  START_DEBUG("init");
d697d71999-09-30Henrik Wallin  init(diagram_data);
2249372001-11-19Martin Nilsson  STOP_DEBUG("init");
d697d71999-09-30Henrik Wallin  //Calculate how many and how big the texts are.
2249372001-11-19Martin Nilsson  START_DEBUG("space");
aaa34a2005-11-15Martin Nilsson  if (!(diagram_data->xspace))
d697d71999-09-30Henrik Wallin  { //Initiate the distance between the texts on the x-axis (Or X-axis?)
aaa34a2005-11-15Martin Nilsson  float range=(diagram_data->xmaxvalue- diagram_data->xminvalue);
d697d71999-09-30Henrik Wallin  float space=pow(10.0, floor(log(range/3.0)/log(10.0))); if (range/space>5.0) { if(range/(2.0*space)>5.0) { space=space*5.0; } else space=space*2.0; } else if (range/space<2.5) space*=0.5;
3524712015-05-26Martin Nilsson  diagram_data->xspace=space;
d697d71999-09-30Henrik Wallin  }
aaa34a2005-11-15Martin Nilsson  if (!(diagram_data->yspace))
d697d71999-09-30Henrik Wallin  { //Initiate the distance between the texts on the y-axis (Or X-axis?)
aaa34a2005-11-15Martin Nilsson  float range=(diagram_data->ymaxvalue- diagram_data->yminvalue);
d697d71999-09-30Henrik Wallin  float space=pow(10.0, floor(log(range/3.0)/log(10.0))); if (range/space>5.0) { if(range/(2.0*space)>5.0) space *= 5.0; else space *= 2.0; } else if (range/space<2.5) space *= 0.5;
3524712015-05-26Martin Nilsson  diagram_data->yspace=space;
d697d71999-09-30Henrik Wallin  }
2249372001-11-19Martin Nilsson  STOP_DEBUG("space");
d697d71999-09-30Henrik Wallin 
2249372001-11-19Martin Nilsson  START_DEBUG("text");
d697d71999-09-30Henrik Wallin  float start;
aaa34a2005-11-15Martin Nilsson  start=diagram_data->xminvalue+diagram_data->xspace/2.0;
e2b07b2010-01-25Henrik Grubbström (Grubba)  if (!diagram_data->values_for_xnames) { diagram_data->values_for_xnames=allocate(sizeof(diagram_data->xnames)); if (diagram_data->subtype=="line") { for(int i=0; i<sizeof(diagram_data->xnames); i++) { diagram_data->values_for_xnames[i]=start*2*i; } } else { for(int i=0; i<sizeof(diagram_data->xnames); i++) { diagram_data->values_for_xnames[i]=start+start*2*i; } } }
d697d71999-09-30Henrik Wallin 
aaa34a2005-11-15Martin Nilsson  if (!(diagram_data->values_for_ynames))
d697d71999-09-30Henrik Wallin  {
aaa34a2005-11-15Martin Nilsson  if ((diagram_data->yspace<LITET) && (diagram_data->yspace>-LITET))
484a742002-03-09Martin Nilsson  error( "Very bad error because yspace is zero!\n" );
d697d71999-09-30Henrik Wallin  float start;
aaa34a2005-11-15Martin Nilsson  start=diagram_data->yminvalue; start=diagram_data->yspace*ceil((start)/diagram_data->yspace); diagram_data->values_for_ynames=({start}); while(start < diagram_data->ymaxvalue) { diagram_data->values_for_ynames+=({start+=diagram_data->yspace});
96e3462004-03-16Henrik Grubbström (Grubba)  }
d697d71999-09-30Henrik Wallin  }
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  function fun;
aaa34a2005-11-15Martin Nilsson  if (diagram_data->eng)
d697d71999-09-30Henrik Wallin  fun=diagram_eng; else fun=diagram_neng;
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  //Draw the text if it does not exist
aaa34a2005-11-15Martin Nilsson  if (!(diagram_data->ynames)) if (diagram_data->eng||diagram_data->neng)
d697d71999-09-30Henrik Wallin  {
aaa34a2005-11-15Martin Nilsson  diagram_data->ynames= allocate(sizeof(diagram_data->values_for_ynames)); array(mixed) v=diagram_data->values_for_ynames; mixed m=diagram_data->ymaxvalue; mixed mi=diagram_data->yminvalue;
d697d71999-09-30Henrik Wallin  for(int i=0; i<sizeof(v); i++) if (abs(v[i]*1000)<max(m, abs(mi)))
aaa34a2005-11-15Martin Nilsson  diagram_data->ynames[i]="0";
3524712015-05-26Martin Nilsson  else
aaa34a2005-11-15Martin Nilsson  diagram_data->ynames[i]=
d697d71999-09-30Henrik Wallin  fun((float)(v[i])); } else {
aaa34a2005-11-15Martin Nilsson  diagram_data->ynames= allocate(sizeof(diagram_data->values_for_ynames));
3524712015-05-26Martin Nilsson 
aaa34a2005-11-15Martin Nilsson  for(int i=0; i<sizeof(diagram_data->values_for_ynames); i++) diagram_data->ynames[i]= no_end_zeros((string)(diagram_data->values_for_ynames[i]));
d697d71999-09-30Henrik Wallin  }
3524712015-05-26Martin Nilsson 
aaa34a2005-11-15Martin Nilsson  if (!(diagram_data->xnames))
d697d71999-09-30Henrik Wallin  {
aaa34a2005-11-15Martin Nilsson  diagram_data->xnames= allocate(sizeof(diagram_data->values_for_xnames));
3524712015-05-26Martin Nilsson 
aaa34a2005-11-15Martin Nilsson  for(int i=0; i<sizeof(diagram_data->values_for_xnames); i++) diagram_data->xnames[i]= no_end_zeros((string)(diagram_data->values_for_xnames[i]));
d697d71999-09-30Henrik Wallin  } //Draw the text-images //calculate xmaxynames, ymaxynames xmaxxnames ymaxxnames create_text(diagram_data);
aaa34a2005-11-15Martin Nilsson  si=diagram_data->fontsize;
2249372001-11-19Martin Nilsson  STOP_DEBUG("text");
d697d71999-09-30Henrik Wallin  //Create the label-texts for the X-axis object labelimg; string label; int labelx=0; int labely=0;
aaa34a2005-11-15Martin Nilsson  if (diagram_data->labels)
d697d71999-09-30Henrik Wallin  {
aaa34a2005-11-15Martin Nilsson  if (diagram_data->labels[2] && sizeof(diagram_data->labels[2])) label=diagram_data->labels[0]+" ["+diagram_data->labels[2]+"]"; //Xstorhet
d697d71999-09-30Henrik Wallin  else
aaa34a2005-11-15Martin Nilsson  label=diagram_data->labels[0];
d697d71999-09-30Henrik Wallin 
b122a72000-10-12Martin Nilsson  object notext=GETFONT(xaxisfont);
d697d71999-09-30Henrik Wallin  if ((label!="")&&(label!=0)) labelimg=notext
aaa34a2005-11-15Martin Nilsson  ->write(UNICODE(label,diagram_data->encoding)) ->scale(0,diagram_data->labelsize);
d697d71999-09-30Henrik Wallin  else
aaa34a2005-11-15Martin Nilsson  labelimg=Image.Image(diagram_data->labelsize,diagram_data->labelsize);
d697d71999-09-30Henrik Wallin  if (labelimg->xsize()<1)
aaa34a2005-11-15Martin Nilsson  labelimg=Image.Image(diagram_data->labelsize,diagram_data->labelsize);
d697d71999-09-30Henrik Wallin  if (labelimg->xsize()>
aaa34a2005-11-15Martin Nilsson  diagram_data->xsize/2) labelimg=labelimg->scale(diagram_data->xsize/2, 0);
d697d71999-09-30Henrik Wallin 
aaa34a2005-11-15Martin Nilsson  labely=diagram_data->labelsize;
d697d71999-09-30Henrik Wallin  labelx=labelimg->xsize(); } else
aaa34a2005-11-15Martin Nilsson  diagram_data->labelsize=0;
d697d71999-09-30Henrik Wallin  labely+=write_name(diagram_data); int ypos_for_xaxis; //Distance from bottom int xpos_for_yaxis; //Distance from right //Decide where the graph can be drawn
aaa34a2005-11-15Martin Nilsson  diagram_data->ystart=(int)ceil(diagram_data->linewidth); diagram_data->ystop=diagram_data->ysize- (int)ceil(diagram_data->linewidth+si)-labely; if (((float)diagram_data->yminvalue>-LITET)&& ((float)diagram_data->yminvalue<LITET)) diagram_data->yminvalue=0.0; if (diagram_data->yminvalue<0)
d697d71999-09-30Henrik Wallin  {
3524712015-05-26Martin Nilsson  //Calculate position for the x-axis.
d697d71999-09-30Henrik Wallin  //If this doesn't work: Draw the y-axis at right or left
aaa34a2005-11-15Martin Nilsson  // and recalculate diagram_data->ystart ypos_for_xaxis=((-diagram_data->yminvalue) * (diagram_data->ystop - diagram_data->ystart)) / (diagram_data->ymaxvalue-diagram_data->yminvalue) + diagram_data->ystart;
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  int minpos;
aaa34a2005-11-15Martin Nilsson  minpos=max(labely, diagram_data->ymaxxnames)+si/2;
d697d71999-09-30Henrik Wallin  if (minpos>ypos_for_xaxis) { ypos_for_xaxis=minpos;
aaa34a2005-11-15Martin Nilsson  diagram_data->ystart=ypos_for_xaxis+ diagram_data->yminvalue*(diagram_data->ystop-ypos_for_xaxis)/ (diagram_data->ymaxvalue);
d697d71999-09-30Henrik Wallin  } else { int maxpos;
aaa34a2005-11-15Martin Nilsson  maxpos=diagram_data->ysize- (int)ceil(diagram_data->linewidth+si*2)
d697d71999-09-30Henrik Wallin  - labely; if (maxpos<ypos_for_xaxis) { ypos_for_xaxis=maxpos;
aaa34a2005-11-15Martin Nilsson  diagram_data->ystop=ypos_for_xaxis + diagram_data->ymaxvalue*(ypos_for_xaxis-diagram_data->ystart) / (0-diagram_data->yminvalue);
d697d71999-09-30Henrik Wallin  } } } else
aaa34a2005-11-15Martin Nilsson  if (diagram_data->yminvalue==0.0)
d697d71999-09-30Henrik Wallin  {
3524712015-05-26Martin Nilsson  // Place the x-axis and diagram_data->ystart at bottom.
aaa34a2005-11-15Martin Nilsson  diagram_data->ystop=diagram_data->ysize - (int)ceil(diagram_data->linewidth+si)-labely; ypos_for_xaxis=max(labely, diagram_data->ymaxxnames)+si/2; diagram_data->ystart=ypos_for_xaxis;
d697d71999-09-30Henrik Wallin  } else {
aaa34a2005-11-15Martin Nilsson  //Place the x-axis at bottom and diagram_data->ystart a
d697d71999-09-30Henrik Wallin  //Little bit higher
aaa34a2005-11-15Martin Nilsson  diagram_data->ystop=diagram_data->ysize - (int)ceil(diagram_data->linewidth+si)-labely; ypos_for_xaxis=max(labely, diagram_data->ymaxxnames)+si/2; diagram_data->ystart=ypos_for_xaxis+si*2;
d697d71999-09-30Henrik Wallin  } //Calculate position for the y-axis
aaa34a2005-11-15Martin Nilsson  diagram_data->xstart=(int)ceil(diagram_data->linewidth); diagram_data->xstop=diagram_data->xsize- (int)ceil(diagram_data->linewidth)-max(si,labelx+si/2)- diagram_data->xmaxxnames/2; if (((float)diagram_data->xminvalue>-LITET)&& ((float)diagram_data->xminvalue<LITET)) diagram_data->xminvalue=0.0;
3524712015-05-26Martin Nilsson 
aaa34a2005-11-15Martin Nilsson  if (diagram_data->xminvalue<0)
d697d71999-09-30Henrik Wallin  {
3524712015-05-26Martin Nilsson  //Calculate position for the y-axis.
d697d71999-09-30Henrik Wallin  //If this doesn't work: Draw the y-axis at right or left
aaa34a2005-11-15Martin Nilsson  // and recalculate diagram_data->xstart xpos_for_yaxis=((-diagram_data->xminvalue) * (diagram_data->xstop-diagram_data->xstart)) / (diagram_data->xmaxvalue-diagram_data->xminvalue) + diagram_data->xstart;
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  int minpos;
aaa34a2005-11-15Martin Nilsson  minpos=diagram_data->xmaxynames+si/2;
d697d71999-09-30Henrik Wallin  if (minpos>xpos_for_yaxis) { xpos_for_yaxis=minpos;
aaa34a2005-11-15Martin Nilsson  diagram_data->xstart=xpos_for_yaxis+ diagram_data->xminvalue*(diagram_data->xstop-xpos_for_yaxis)/ (diagram_data->ymaxvalue);
d697d71999-09-30Henrik Wallin  } else { int maxpos;
aaa34a2005-11-15Martin Nilsson  maxpos=diagram_data->xsize- (int)ceil((float)diagram_data->linewidth+si*2+labelx);
d697d71999-09-30Henrik Wallin  if (maxpos<xpos_for_yaxis) { xpos_for_yaxis=maxpos;
aaa34a2005-11-15Martin Nilsson  diagram_data->xstop=xpos_for_yaxis+ diagram_data->xmaxvalue*(xpos_for_yaxis-diagram_data->xstart)/ (0-diagram_data->xminvalue);
d697d71999-09-30Henrik Wallin  } } } else
aaa34a2005-11-15Martin Nilsson  if (diagram_data->xminvalue==0.0)
d697d71999-09-30Henrik Wallin  {
aaa34a2005-11-15Martin Nilsson  //Place the y-axis at left (?) and diagram_data->xstart at
d697d71999-09-30Henrik Wallin  // the same place
aaa34a2005-11-15Martin Nilsson  diagram_data->xstop=diagram_data->xsize- (int)ceil(diagram_data->linewidth)-max(si,labelx+si/2)- diagram_data->xmaxxnames/2; xpos_for_yaxis=diagram_data->xmaxynames+si/2+2; diagram_data->xstart=xpos_for_yaxis+si/2;
d697d71999-09-30Henrik Wallin  } else {
aaa34a2005-11-15Martin Nilsson  //Place the y-axis at left (?) and diagram_data->xstart a
d697d71999-09-30Henrik Wallin  // little bit more right (?)
aaa34a2005-11-15Martin Nilsson  diagram_data->xstop=diagram_data->xsize- (int)ceil(diagram_data->linewidth)-max(si,labelx+si/2)- diagram_data->xmaxxnames/2; xpos_for_yaxis=diagram_data->xmaxynames+si/2; diagram_data->xstart=xpos_for_yaxis+si*2;
d697d71999-09-30Henrik Wallin  } //Calculate some shit
aaa34a2005-11-15Martin Nilsson  float xstart=(float)diagram_data->xstart; float xmore=(-xstart+diagram_data->xstop)/ (diagram_data->xmaxvalue-diagram_data->xminvalue); float ystart=(float)diagram_data->ystart; float ymore=(-ystart+diagram_data->ystop)/ (diagram_data->ymaxvalue-diagram_data->yminvalue);
3524712015-05-26Martin Nilsson 
2249372001-11-19Martin Nilsson  START_DEBUG("draw_grid"); draw_grid(diagram_data, xpos_for_yaxis, ypos_for_xaxis,
d697d71999-09-30Henrik Wallin  xmore, ymore, xstart, ystart, (float) si);
2249372001-11-19Martin Nilsson  STOP_DEBUG("draw_grid");
d697d71999-09-30Henrik Wallin  //??? int farg=0;
3524712015-05-26Martin Nilsson 
2249372001-11-19Martin Nilsson  START_DEBUG("draw_values");
aaa34a2005-11-15Martin Nilsson  if (diagram_data->type=="sumbars")
d697d71999-09-30Henrik Wallin  {
aaa34a2005-11-15Martin Nilsson  int s=diagram_data->datasize; float barw=diagram_data->xspace*xmore/3.0;
d697d71999-09-30Henrik Wallin  for(int i=0; i<s; i++) { int j=0; float x,y;
aaa34a2005-11-15Martin Nilsson  x=xstart+(diagram_data->xspace/2.0+diagram_data->xspace*i)*
d697d71999-09-30Henrik Wallin  xmore;
3524712015-05-26Martin Nilsson 
aaa34a2005-11-15Martin Nilsson  y=-(-diagram_data->yminvalue)*ymore+
3524712015-05-26Martin Nilsson  diagram_data->ysize-ystart;
d697d71999-09-30Henrik Wallin  float start=y;
3524712015-05-26Martin Nilsson 
aaa34a2005-11-15Martin Nilsson  foreach(column(diagram_data->data, i), float|string d)
d697d71999-09-30Henrik Wallin  { if (d==VOIDSYMBOL) d=0.0; y-=d*ymore;
3524712015-05-26Martin Nilsson 
aaa34a2005-11-15Martin Nilsson  barsdiagram->setcolor(@(diagram_data->datacolors[j++]));
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  barsdiagram->polygone(
3524712015-05-26Martin Nilsson  ({x-barw, y , x+barw, y,
d697d71999-09-30Henrik Wallin  x+barw, start , x-barw, start
3524712015-05-26Martin Nilsson  }));
d697d71999-09-30Henrik Wallin  /* barsdiagram->setcolor(0,0,0);
3524712015-05-26Martin Nilsson  draw(barsdiagram, 0.5,
d697d71999-09-30Henrik Wallin  ({ x-barw, start,
3524712015-05-26Martin Nilsson  x-barw, y , x+barw, y,
d697d71999-09-30Henrik Wallin  x+barw, start
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  }) ); */ start=y; } } } else
aaa34a2005-11-15Martin Nilsson  if (diagram_data->subtype=="line") if (diagram_data->drawtype=="linear") foreach(diagram_data->data, array(float|string) d)
d697d71999-09-30Henrik Wallin  { array(float|string) l=allocate(sizeof(d)*2); for(int i=0; i<sizeof(d); i++) if (d[i]==VOIDSYMBOL) { l[i*2]=VOIDSYMBOL; l[i*2+1]=VOIDSYMBOL; } else {
e2b07b2010-01-25Henrik Grubbström (Grubba)  l[i*2]=xstart+diagram_data->xspace*i * xmore;
aaa34a2005-11-15Martin Nilsson  l[i*2+1]=-(d[i]-diagram_data->yminvalue)*ymore+
3524712015-05-26Martin Nilsson  diagram_data->ysize-ystart;
d697d71999-09-30Henrik Wallin  }
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  //Draw Ugly outlines
aaa34a2005-11-15Martin Nilsson  if ((diagram_data->backdatacolors)&& (diagram_data->backlinewidth))
d697d71999-09-30Henrik Wallin  {
aaa34a2005-11-15Martin Nilsson  barsdiagram->setcolor(@(diagram_data->backdatacolors[farg])); draw(barsdiagram, diagram_data->backlinewidth,l, diagram_data->xspace );
d697d71999-09-30Henrik Wallin  }
aaa34a2005-11-15Martin Nilsson  barsdiagram->setcolor(@(diagram_data->datacolors[farg++])); draw(barsdiagram, diagram_data->graphlinewidth,l);
d697d71999-09-30Henrik Wallin  } else
aaa34a2005-11-15Martin Nilsson  error( "\""+diagram_data->drawtype
484a742002-03-09Martin Nilsson  + "\" is an unknown bars-diagram drawtype!\n" );
d697d71999-09-30Henrik Wallin  else
aaa34a2005-11-15Martin Nilsson  if (diagram_data->subtype=="box") if (diagram_data->drawtype=="2D")
d697d71999-09-30Henrik Wallin  {
aaa34a2005-11-15Martin Nilsson  int s=sizeof(diagram_data->data); float barw=diagram_data->xspace*xmore/1.5;
d697d71999-09-30Henrik Wallin  float dnr=-barw/2.0+ barw/s/2.0; barw/=s; barw/=2.0; farg=-1;
aaa34a2005-11-15Martin Nilsson  float yfoo=(float)(diagram_data->ysize-ypos_for_xaxis);
d697d71999-09-30Henrik Wallin  //"draw_values":3580,
aaa34a2005-11-15Martin Nilsson  foreach(diagram_data->data, array(float|string) d)
d697d71999-09-30Henrik Wallin  { farg++;
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  for(int i=0; i<sizeof(d); i++) if (d[i]!=VOIDSYMBOL) { float x,y;
aaa34a2005-11-15Martin Nilsson  x=xstart+(diagram_data->xspace/2.0+diagram_data->xspace*i)*
d697d71999-09-30Henrik Wallin  xmore;
aaa34a2005-11-15Martin Nilsson  y=-(d[i]-diagram_data->yminvalue)*ymore+
3524712015-05-26Martin Nilsson  diagram_data->ysize-ystart;
aaa34a2005-11-15Martin Nilsson  barsdiagram->setcolor(@(diagram_data->datacolors[farg]));
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  barsdiagram->polygone(
3524712015-05-26Martin Nilsson  ({x-barw+dnr, y , x+barw+dnr, y,
d697d71999-09-30Henrik Wallin  x+barw+dnr, yfoo , x-barw+dnr, yfoo
3524712015-05-26Martin Nilsson  })); /* barsdiagram->setcolor(0,0,0); draw(barsdiagram, 0.5, ({x-barw+dnr, y , x+barw+dnr, y,
aaa34a2005-11-15Martin Nilsson  x+barw+dnr, diagram_data->ysize-ypos_for_xaxis , x-barw+dnr,diagram_data->ysize- ypos_for_xaxis,
3524712015-05-26Martin Nilsson  x-barw+dnr, y
d697d71999-09-30Henrik Wallin  }));*/ } dnr+=barw*2.0;
3524712015-05-26Martin Nilsson  }
d697d71999-09-30Henrik Wallin  } else
aaa34a2005-11-15Martin Nilsson  error( "\""+diagram_data->drawtype
484a742002-03-09Martin Nilsson  + "\" is an unknown bars-diagram drawtype!\n" );
d697d71999-09-30Henrik Wallin  else
aaa34a2005-11-15Martin Nilsson  error( "\""+diagram_data->subtype
484a742002-03-09Martin Nilsson  +"\" is an unknown bars-diagram subtype!\n" );
2249372001-11-19Martin Nilsson  STOP_DEBUG("draw_values");
d697d71999-09-30Henrik Wallin  //Draw X and Y-axis
aaa34a2005-11-15Martin Nilsson  barsdiagram->setcolor(@(diagram_data->axcolor));
d697d71999-09-30Henrik Wallin  //Draw the X-axis
aaa34a2005-11-15Martin Nilsson  if ((diagram_data->xminvalue<=LITET)&& (diagram_data->xmaxvalue>=-LITET))
d697d71999-09-30Henrik Wallin  barsdiagram->
3524712015-05-26Martin Nilsson  polygone(make_polygon_from_line(diagram_data->linewidth,
d697d71999-09-30Henrik Wallin  ({ xpos_for_yaxis,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize- ypos_for_xaxis, diagram_data->xsize-
3524712015-05-26Martin Nilsson  diagram_data->linewidth-labelx/2,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis
3524712015-05-26Martin Nilsson  }),
d697d71999-09-30Henrik Wallin  1, 1)[0]); else
aaa34a2005-11-15Martin Nilsson  if (diagram_data->xmaxvalue<-LITET)
d697d71999-09-30Henrik Wallin  { barsdiagram ->polygone(make_polygon_from_line(
3524712015-05-26Martin Nilsson  diagram_data->linewidth,
d697d71999-09-30Henrik Wallin  ({ xpos_for_yaxis,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis,
3524712015-05-26Martin Nilsson  xpos_for_yaxis-4.0/3.0*si,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis,
3524712015-05-26Martin Nilsson  xpos_for_yaxis-si,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis-si/2.0,
d697d71999-09-30Henrik Wallin  xpos_for_yaxis-si/1.5,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis+si/2.0,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis-si/3.0,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis,
3524712015-05-26Martin Nilsson 
aaa34a2005-11-15Martin Nilsson  diagram_data->xsize-diagram_data->linewidth
3524712015-05-26Martin Nilsson  - labelx/2,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis
d697d71999-09-30Henrik Wallin  }), 1, 1)[0]); } else
aaa34a2005-11-15Martin Nilsson  if (diagram_data->xminvalue>LITET)
d697d71999-09-30Henrik Wallin  { barsdiagram ->polygone(make_polygon_from_line(
3524712015-05-26Martin Nilsson  diagram_data->linewidth,
d697d71999-09-30Henrik Wallin  ({ xpos_for_yaxis,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize- ypos_for_xaxis,
3524712015-05-26Martin Nilsson  xpos_for_yaxis+si/3.0,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis,
3524712015-05-26Martin Nilsson  xpos_for_yaxis+si/1.5,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis-si/2.0,
3524712015-05-26Martin Nilsson  xpos_for_yaxis+si,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis+si/2.0,
3524712015-05-26Martin Nilsson  xpos_for_yaxis+4.0/3.0*si,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis,
3524712015-05-26Martin Nilsson 
aaa34a2005-11-15Martin Nilsson  diagram_data->xsize-diagram_data->linewidth
3524712015-05-26Martin Nilsson  - labelx/2,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis
d697d71999-09-30Henrik Wallin  }), 1, 1)[0]); }
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  //Draw arrow on the X-axis
aaa34a2005-11-15Martin Nilsson  if (diagram_data->subtype=="line")
d697d71999-09-30Henrik Wallin  barsdiagram->polygone( ({
aaa34a2005-11-15Martin Nilsson  diagram_data->xsize-diagram_data->linewidth/2-(float)si-labelx/2, diagram_data->ysize-ypos_for_xaxis-(float)si/4.0,
3524712015-05-26Martin Nilsson 
aaa34a2005-11-15Martin Nilsson  diagram_data->xsize-diagram_data->linewidth/2-labelx/2, diagram_data->ysize-ypos_for_xaxis,
3524712015-05-26Martin Nilsson 
aaa34a2005-11-15Martin Nilsson  diagram_data->xsize-diagram_data->linewidth/2-(float)si-labelx/2, diagram_data->ysize-ypos_for_xaxis+(float)si/4.0
d697d71999-09-30Henrik Wallin  })
3524712015-05-26Martin Nilsson  );
d697d71999-09-30Henrik Wallin  //Draw Y-axis
aaa34a2005-11-15Martin Nilsson  if ((diagram_data->yminvalue<=LITET)&& (diagram_data->ymaxvalue>=-LITET))
d697d71999-09-30Henrik Wallin  {
aaa34a2005-11-15Martin Nilsson  if ((diagram_data->yminvalue<=LITET)&&
3524712015-05-26Martin Nilsson  (diagram_data->yminvalue>=-LITET))
d697d71999-09-30Henrik Wallin  barsdiagram->
3524712015-05-26Martin Nilsson  polygone(make_polygon_from_line(diagram_data->linewidth,
d697d71999-09-30Henrik Wallin  ({ xpos_for_yaxis,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis,
d697d71999-09-30Henrik Wallin  xpos_for_yaxis, si+labely }), 1, 1)[0]); else barsdiagram->
3524712015-05-26Martin Nilsson  polygone(make_polygon_from_line(diagram_data->linewidth,
d697d71999-09-30Henrik Wallin  ({ xpos_for_yaxis,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize - diagram_data->linewidth,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis, si+labely }), 1, 1)[0]);
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  } else
aaa34a2005-11-15Martin Nilsson  if (diagram_data->ymaxvalue<-LITET)
d697d71999-09-30Henrik Wallin  { barsdiagram-> polygone(make_polygon_from_line(
3524712015-05-26Martin Nilsson  diagram_data->linewidth,
d697d71999-09-30Henrik Wallin  ({ xpos_for_yaxis,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-diagram_data->linewidth,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis+si*4.0/3.0,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis-si/2.0,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis+si,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis+si/2.0,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis+si/1.5,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis+si/3.0,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis, si+labely }), 1, 1)[0]); } else
aaa34a2005-11-15Martin Nilsson  if (diagram_data->yminvalue>LITET)
d697d71999-09-30Henrik Wallin  { barsdiagram-> polygone(make_polygon_from_line(
3524712015-05-26Martin Nilsson  diagram_data->linewidth,
d697d71999-09-30Henrik Wallin  ({ xpos_for_yaxis,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-diagram_data->linewidth,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis-si/3.0,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis-si/2.0,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis-si/1.5,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis+si/2.0,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis-si,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis,
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-ypos_for_xaxis-si*4.0/3.0,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis, si+labely }), 1, 1)[0]); }
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  //Draw arrow barsdiagram-> polygone( ({ xpos_for_yaxis-(float)si/4.0,
aaa34a2005-11-15Martin Nilsson  diagram_data->linewidth/2.0+(float)si+labely,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis,
aaa34a2005-11-15Martin Nilsson  diagram_data->linewidth/2.0+labely,
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis+(float)si/4.0,
aaa34a2005-11-15Martin Nilsson  diagram_data->linewidth/2.0+(float)si+labely
3524712015-05-26Martin Nilsson  }) );
d697d71999-09-30Henrik Wallin  //Write the text on the X-axis
aaa34a2005-11-15Martin Nilsson  int s=sizeof(diagram_data->xnamesimg);
d697d71999-09-30Henrik Wallin 
2249372001-11-19Martin Nilsson  START_DEBUG("text_on_axis");
d697d71999-09-30Henrik Wallin  for(int i=0; i<s; i++)
e2b07b2010-01-25Henrik Grubbström (Grubba)  if ((diagram_data->values_for_xnames[i]<=diagram_data->xmaxvalue)&& (diagram_data->values_for_xnames[i]>=diagram_data->xminvalue))
d697d71999-09-30Henrik Wallin  {
e2b07b2010-01-25Henrik Grubbström (Grubba)  float xpos = floor((diagram_data->values_for_xnames[i] - diagram_data->xminvalue)*xmore+xstart);
d697d71999-09-30Henrik Wallin  barsdiagram->paste_alpha_color(
3524712015-05-26Martin Nilsson  diagram_data->xnamesimg[i], @(diagram_data->textcolor), (int)(xpos - diagram_data->xnamesimg[i]->xsize()/2),
aaa34a2005-11-15Martin Nilsson  (int)floor(diagram_data->ysize-ypos_for_xaxis+si/4.0));
e2b07b2010-01-25Henrik Grubbström (Grubba)  if (diagram_data->subtype == "line") { barsdiagram->setcolor(@diagram_data->axcolor); barsdiagram-> polygone(make_polygon_from_line(
3524712015-05-26Martin Nilsson  diagram_data->linewidth,
e2b07b2010-01-25Henrik Grubbström (Grubba)  ({ xpos, (float)diagram_data->ysize-ypos_for_xaxis - si/4.0, xpos, (float)diagram_data->ysize-ypos_for_xaxis + si/4.0, }), 1, 1)[0]); }
d697d71999-09-30Henrik Wallin  } //Write the text on the Y-axis
3524712015-05-26Martin Nilsson  s=min(sizeof(diagram_data->ynamesimg),
aaa34a2005-11-15Martin Nilsson  sizeof(diagram_data->values_for_ynames));
d697d71999-09-30Henrik Wallin  for(int i=0; i<s; i++)
aaa34a2005-11-15Martin Nilsson  if ((diagram_data->values_for_ynames[i]<=diagram_data->ymaxvalue)&& (diagram_data->values_for_ynames[i]>=diagram_data->yminvalue))
d697d71999-09-30Henrik Wallin  {
aaa34a2005-11-15Martin Nilsson  barsdiagram->setcolor(@diagram_data->textcolor);
d697d71999-09-30Henrik Wallin  barsdiagram->paste_alpha_color(
3524712015-05-26Martin Nilsson  diagram_data->ynamesimg[i], @(diagram_data->textcolor),
d697d71999-09-30Henrik Wallin  (int)floor(xpos_for_yaxis-
aaa34a2005-11-15Martin Nilsson  si/4.0-diagram_data->linewidth- diagram_data->ynamesimg[i]->xsize()), (int)floor(-(diagram_data->values_for_ynames[i]- diagram_data->yminvalue) *ymore+diagram_data->ysize-ystart
d697d71999-09-30Henrik Wallin  -
aaa34a2005-11-15Martin Nilsson  diagram_data->ymaxynames/2));
3524712015-05-26Martin Nilsson 
aaa34a2005-11-15Martin Nilsson  barsdiagram->setcolor(@diagram_data->axcolor);
d697d71999-09-30Henrik Wallin  barsdiagram-> polygone(make_polygon_from_line(
3524712015-05-26Martin Nilsson  diagram_data->linewidth,
d697d71999-09-30Henrik Wallin  ({ xpos_for_yaxis-si/4,
aaa34a2005-11-15Martin Nilsson  (-(diagram_data->values_for_ynames[i] - diagram_data->yminvalue)*ymore + diagram_data->ysize-ystart),
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  xpos_for_yaxis+si/4,
aaa34a2005-11-15Martin Nilsson  (-(diagram_data->values_for_ynames[i] - diagram_data->yminvalue)*ymore + diagram_data->ysize-ystart)
d697d71999-09-30Henrik Wallin  }), 1, 1)[0]); } //Write labels ({xstorhet, ystorhet, xenhet, yenhet})
aaa34a2005-11-15Martin Nilsson  if (diagram_data->labelsize)
d697d71999-09-30Henrik Wallin  { barsdiagram
3524712015-05-26Martin Nilsson  ->paste_alpha_color(labelimg, @(diagram_data->labelcolor),
aaa34a2005-11-15Martin Nilsson  diagram_data->xsize-labelx - (int)ceil((float)diagram_data->linewidth), diagram_data->ysize
d697d71999-09-30Henrik Wallin  - (int)ceil((float)(ypos_for_xaxis-si/2)));
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  string label; int x; int y;
aaa34a2005-11-15Martin Nilsson  if (diagram_data->labels[3] && sizeof(diagram_data->labels[3])) label=diagram_data->labels[1]+" ["+diagram_data->labels[3]+"]"; //Yquantity
d697d71999-09-30Henrik Wallin  else
aaa34a2005-11-15Martin Nilsson  label=diagram_data->labels[1];
b122a72000-10-12Martin Nilsson  object notext=GETFONT(yaxisfont);
d697d71999-09-30Henrik Wallin  if ((label!="")&&(label!=0)) labelimg=notext
aaa34a2005-11-15Martin Nilsson  ->write(label)->scale(0,diagram_data->labelsize);
d697d71999-09-30Henrik Wallin  else
aaa34a2005-11-15Martin Nilsson  labelimg=Image.Image(diagram_data->labelsize,diagram_data->labelsize);
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  if (labelimg->xsize()<1)
aaa34a2005-11-15Martin Nilsson  labelimg=Image.Image(diagram_data->labelsize,diagram_data->labelsize);
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  if (labelimg->xsize()>
aaa34a2005-11-15Martin Nilsson  diagram_data->xsize) labelimg=labelimg->scale(diagram_data->xsize, 0);
3524712015-05-26Martin Nilsson 
d697d71999-09-30Henrik Wallin  x=max(2,((int)floor((float)xpos_for_yaxis)-labelimg->xsize()/2)); x=min(x, barsdiagram->xsize()-labelimg->xsize());
3524712015-05-26Martin Nilsson  y=0;
d697d71999-09-30Henrik Wallin  if (label && sizeof(label))
3524712015-05-26Martin Nilsson  barsdiagram->paste_alpha_color(labelimg, @(diagram_data->labelcolor),
d697d71999-09-30Henrik Wallin  x, 2+labely-labelimg->ysize()); }
2249372001-11-19Martin Nilsson  STOP_DEBUG("text_on_axis");
d697d71999-09-30Henrik Wallin 
aaa34a2005-11-15Martin Nilsson  diagram_data->ysize-=diagram_data->legend_size; diagram_data->image=barsdiagram;
d697d71999-09-30Henrik Wallin  return diagram_data; }