1

I'm trying to assign a label to the x axis of a Bar Chart, the label is a normal text string stored in an array under $gsettings['axisXlabel']. Unfortunatly pCharts SetSerieDescription doesn't seem to be working as expected.

Below is the function producing the graphs and an attachment of the current output. The part of interest is the 3 lines under /* Bind a data series to the X axis */

/**
* function to plot bar charts
*/
function cg_graphs_plot_bar_graph($gdata, $gsettings){

  $graph_data = new pData();

  if(isset($gdata['bar_plots2'])){ //if this is set, its a duel bar graph
    $graph_data->addPoints($gdata['bar_plots'],"surgeondata");
    $graph_data->addPoints(array(0,0,0,0),"surgeondatanull");
    $graph_data->addPoints($gdata['bar_plots2'],"everyonedata");
    $graph_data->addPoints(array(0,0,0,0),"everyonedatanull");
    $graph_data->setSerieDrawable(array("everyonedatanull"), FALSE);
    $graph_data->setSerieDescription("surgeondata",$gdata['surgeonname']);
    $graph_data->setSerieDescription("everyonedata","All Surgeons");
    $graph_data->setAxisUnit(0,"%");

    $surgeon = array("R"=>21,"G"=>0,"B"=>0); //surgeon series colour
    $all = array("R"=>191,"G"=>160,"B"=>36); //everyone series colour
    $graph_data->setPalette("surgeondata",$surgeon);
    $graph_data->setPalette("everyonedata",$all);
    $graph_data->setPalette("surgeondatanull",$surgeon);
    $graph_data->setPalette("everyonedatanull",$all);

  } else {
    $graph_data->addPoints($gdata['bar_plots'],"percentiles");
    $graph_data->addPoints($gdata['surgeon_bar'],"surgeonbar");
    $graph_data->setSerieDrawable(array("surgeonbar"), FALSE);
  }
  $graph_data->setAxisName(0,$gsettings['axisYlabel']);
  /* Bind a data serie to the X axis */
  $graph_data->addPoints($gdata['xaxis_names'],"Labels");
  $graph_data->setSerieDescription("Labels",$gsettings['axisXlabel']);
  $graph_data->setAbscissa("Labels");

  $width=540;
  $height=419;

  $chart = new pImage($width,$height,$graph_data);
  $chart->drawFromJPG(0,0,drupal_get_path('module', 'cg_graphs')."/images/graphbg.jpg");

  /* Write the picture title */ 
  $chart->setFontProperties(array("FontName"=>drupal_get_path('module', 'cg_graphs')."/pChart/fonts/ARIAL.TTF","FontSize"=>8));
  $chart->setFontProperties(array("R"=>0,"G"=>0,"B"=>0));
  $chart->drawText(270,70,$gsettings['title'],array("R"=>0,"G"=>0,"B"=>0,"Align"=>TEXT_ALIGN_MIDDLEMIDDLE, "FontSize" => 12));

  /* Set the graph area */
  $chart->setGraphArea(70,120,490,310);

  /* Draw a rectangle */
  $chart->drawFilledRectangle(70,120,489,309,array("R"=>255,"G"=>255,"B"=>255,"Dash"=>FALSE, "BorderR"=>201,"BorderG"=>201,"BorderB"=>201));

  /* Turn on shadow computing */ 
  $chart->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>20));

    $format = array(
        "DisplayValues"=>FALSE,
        "DisplayColor"=>DISPLAY_AUTO,
        "Rounded"=>FALSE,
        "Gradient"=>TRUE,
        "GradientAlpha"=>100,
        "GradientMode"=>GRADIENT_EFFECT_CAN,
        "GradientStartR"=>251,
        "GradientStartG"=>220,
        "GradientStartB"=>96,
        "GradientEndR"=>191,
        "GradientEndG"=>160,
        "GradientEndtB"=>36
    );

  if(isset($gdata['bar_plots2'])){
      /* Draw the scale */
      $chart->drawScale(array("XMargin"=>50, "Mode"=>SCALE_MODE_MANUAL, "ManualScale"=> $gsettings['maxmin'], "Pos" => SCALE_POS_LEFTRIGHT,'DrawXLines' => FALSE, 'GridTicks' => 500,'GridR'=>0,'GridG'=>0,'GridB'=>0, 'LabelRotation'=>0, 'AroundZero' => TRUE, 'Interleave' => 0.1));

      $graph_data->setSerieDrawable(array("surgeondata"), FALSE);
      $graph_data->setSerieDrawable(array("surgeondatanull"), TRUE);
  } else {
    /* Draw the scale*/
  $chart->drawScale(array("XMargin"=>40, "Mode"=>SCALE_MODE_MANUAL, "ManualScale"=> $gsettings['maxmin'], "Pos" => SCALE_POS_LEFTRIGHT,'DrawXLines' => FALSE, 'GridTicks' => 500,'GridR'=>0,'GridG'=>0,'GridB'=>0, 'LabelRotation'=>0, 'AroundZero' => TRUE));
  }

  $chart->drawBarChart($format);

  //draw next bar with new colour.
  $format = array(
        "DisplayValues"=>FALSE,
        "DisplayColor"=>DISPLAY_AUTO,
        "Rounded"=>FALSE,
        "Gradient"=>TRUE,
        "GradientAlpha"=>100,
        "GradientMode"=>GRADIENT_EFFECT_CAN,
        "GradientStartR"=>255,
        "GradientStartG"=>230,
        "GradientStartB"=>126,
        "GradientEndR"=>21,
        "GradientEndG"=>0,
        "GradientEndtB"=>0
  );

  if(!isset($gdata['bar_plots2'])){ //not set? we need to draw the second one.
    //set draw series to false / true here

    $graph_data->setSerieDrawable(array("percentiles"), FALSE);
    $graph_data->setSerieDrawable(array("surgeonbar"), TRUE);

    $chart->drawBarChart($format);
  } else {
    $graph_data->setSerieDrawable(array("surgeondatanull", "everyonedata"), FALSE);
    $graph_data->setSerieDrawable(array("surgeondata", "everyonedatanull"), TRUE);

    $chart->drawBarChart($format);
    $graph_data->setSerieDrawable(array("everyonedatanull"), FALSE);
    $graph_data->setSerieDrawable(array("everyonedata"), TRUE);
    $chart->drawLegend(190,100,array("Style"=>LEGEND_NOBORDER, "Mode" => LEGEND_HORIZONTAL)); //draw legand
  }

  $imagename = str_replace(' ', '-', $gdata['surgeonname']);
  $chart->render(drupal_get_path('module', 'cg_graphs')."/pChart/examples/pictures/".$imagename."-".$gsettings['name'].".png");
}

And here's the output, I want to label the Xaxis, currently the label isn't showing. (sorry for removed title etc, data isn't in the public domain yet and names needed removing)

Current output

Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
tobynew
  • 337
  • 1
  • 3
  • 17
  • Your labels are actually in `$gdata['xaxis_names']`. Care to give us the output of a `print_r()` on that? It looks like it should be `Array( "0", "1", "2", ">2")`. – BLaZuRE Jun 17 '13 at 11:26
  • yep, they are and that all works fine. if you take a look at the output, they are labeling the bars as they should. the issue is the missing Xaxis label, rather than the data labels. Like the Y axis has '' Percentage of Ops – tobynew Jun 17 '13 at 11:28
  • just a note, edited the above, since i hit return too soon – tobynew Jun 17 '13 at 11:29
  • So, are you actually talking about `setSerieDescription()`? Have you made sure `$gsettings['axisXlabel']` actually contains the (nonempty) value you want? – BLaZuRE Jun 17 '13 at 11:37
  • nope, see: http://wiki.pchart.net/doc.dataset.setabscissa.html and yes the array possition does contain what im expecting, and i have also tried it just by hard coding a value :) – tobynew Jun 17 '13 at 12:31
  • `setAbscissa()` is in charge of setting up labels for each "tick" (0, 1, 2, >2). `setSerieDescription()`, see http://wiki.pchart.net/doc.dataset.setseriedescription.html, sets the label for the entire series on the axis (similar to your *Percentage of Ops* label, but on a legend). Did you want to add a label similar to *Percentage of Ops*, but horizontally? – BLaZuRE Jun 17 '13 at 12:34
  • so it does... sorry, been up most of the night - so i guess the issue is serie description then. i will edit the question to reflect that as well – tobynew Jun 17 '13 at 12:38

2 Answers2

3

You need to use the function setAbscissaName as defined in http://wiki.pchart.net/doc.dataset.setabscissaname.html

Example:

$MyData->setAbscissaName("Months");

This will display Months in the X axis under ticks.

shamittomar
  • 46,210
  • 12
  • 74
  • 78
  • This worked for me, even though many of the examples in the docs omit this call in the example code and yet display example images containing the label/name. – ekerner Sep 28 '14 at 18:45
0

The pChart documentation is a mess. I've found an error or two where the documentation differs from the source code. Their naming scheme also needs work. It's unintuitive to anyone besides the creator.

I believe you are trying to add a label to the X-Axis. In order to do this, you need to use setXAxisName().

The "Temperature" that appears in the code for setSerieDescription() is a string that is used to tie that set of series data to that name. It is not actually the label that appears on the chart (which is the same name). It is the name that is used again in setAbscissa().

BLaZuRE
  • 2,356
  • 2
  • 26
  • 43
  • 1
    i can't seem to get setXaxis name working ether, changing that section of code to /* Bind a data serie to the X axis */ $graph_data->addPoints($gdata['xaxis_names'],"Labels"); $graph_data->setSerieDescription("Labels",$gsettings['axisXlabel']); $graph_data->setAbscissa("Labels"); $graph_data->setXAxisName($gsettings['axisXlabel']); still has the same result. that is to say, nothing shows up – tobynew Jun 17 '13 at 14:32