jpgraph绘图之Y轴刻度倒排序

来源:互联网 发布:淘宝网男生西装 编辑:程序博客网 时间:2024/06/09 16:34
需求:
    jpgraph默认的图片为Y轴从上到下是从大到小的顺序,但是项目中要把Y轴的刻度反转过来,并设置Y轴刻度为最小值到最大值之间,本例为了美观故增加2个刻度。
    我参考例子:inyaxisex2.php 有这个需求的朋友可以参考这个实例.

    核心代码:

//生成趋势图function generatePhoto($xdata, $ydata, $type, $bgcolor = '#F0FBFF', $width = 680, $height = 210, $l_margin = 30, $r_margin = 8, $t_margin = 5, $d_margin = 25, $boxcolor = array (224, 235, 237 )) {    require_once (CORE_DIR . '../jpgraph/jpgraph.php');    require_once (CORE_DIR . '../jpgraph/jpgraph_line.php');    $IsOnlyDot = 1;//是否在有值的地方进行描点    $DotCount = 0;    $max = max ( $ydata ) + 2;    for($i = 0; $i < sizeof ( $ydata ); $i ++) {        if ($ydata [$i] != '-' && ( int ) $ydata [$i] > 0) {            $DotCount ++;            if (! isset ( $min )) {                //此处是一个过滤掉'-'值取得数组内最小值的函数,由于字符串默认会被当成0(本例是Y轴没有负值的,如果有的话可以不用考虑)                $vt = $this->getMin ( $ydata, '-' ) - 2;                $min = $vt > 0 ? $vt : 0;            }            $ydata [$i] = round ( - $ydata [$i] );        }    }    //当只有一个值的时候描点    if ($DotCount == 1) {        $IsOnlyDot = 1;    }    //有时候可能会出现取出来的值大小颠倒,会报错,这里设置默认值    if (- $max > -$min) {        $max = 4;        $min = 0;    }    $graph = new Graph ( $width, $height );    $graph->SetScale ( 'textint', - $max, - $min );    $graph->yscale->SetGrace ( 10 );    //不设置此项的话数据会显示的不正常,目前不知为何原因,知道的朋友可以tell me.    $graph->SetAxisStyle ( AXSTYLE_BOXIN );    //边距:左 右 上 下,此图形会显示上下X轴,左右的Y轴,通过更改margin掩盖上面和右边的X和Y轴    $graph->SetMargin ( $l_margin, $r_margin, $t_margin, $d_margin );    $graph->SetBox ( true, $boxcolor );    $graph->SetFrame ( true, $bgcolor, 3 );    $graph->SetMarginColor ( $bgcolor );    // Stroke the left Y-axis    //$graph->yaxis->SetPos ( 'min' );    $graph->yaxis->SetLabelSide ( SIDE_LEFT );    // Use Ariel font    $graph->yaxis->SetLabelFormatCallback ( array ($this, "_cb_negate" ) );    $graph->xaxis->SetTickLabels ( $xdata );    $graph->xaxis->SetFont ( FF_ARIAL, FS_NORMAL, 9 );    $graph->yaxis->SetFont ( FF_ARIAL, FS_NORMAL, 9 );    $graph->xgrid->Show (); //是否显示格子    //$graph->yaxis->HideZeroLabel ();    $graph->yaxis->HideTicks ( false, true );    $graph->ygrid->SetFill ( true, '#ffffff@0.5', '#FFFBFC@0' );    $graph->ygrid->SetColor ( '#F7DFE2@0.5' );    $graph->xgrid->SetColor ( '#F7DFE2@0.5' ); //设置格子颜色和粗细    // Create the plot line    $p1 = new LinePlot ( $ydata );    if ($IsOnlyDot) {        $p1->mark->SetType ( MARK_FILLEDCIRCLE ); //MARK_STAR 设置圆圈的样式        $p1->mark->SetFillColor ( "#ffeeff" ); //设置圆圈填充的颜色        $p1->SetWeight ( 3 );        $p1->mark->SetWidth ( 4 ); //设置圆圈的直径        $p1->mark->SetColor ( "red" ); //设置圆圈边框的颜色    }    $graph->Add ( $p1 );    $p1->value->SetColor ( "blue" ); //设置值的颜色    $p1->SetStyle ( "solid" ); //设置线条样式    $p1->SetColor ( 'red' ); //设置线条颜色    //$graph->yaxis->scale->SetGrace ( 20 ); //设置y轴更优美一些    //$graph->xaxis->SetLabelAngle(20);//设置X轴的显示值的角度;    //$p1->value->Show();//显示值    //$graph->img->SetAngle(180);//将图翻转多少度    //$graph->xaxis->title->Set("queries"); //设置坐标轴名称     $graph->xaxis->SetColor ( "#885680" ); //设置x轴坐标文字的颜色    $graph->yaxis->SetColor ( "#417AD2" ); //设置Y轴坐标文字的颜色    // Output graph    $graph->Stroke ();} // 对Y轴的刻度进行否定function _cb_negate($aVal) {    return round ( - $aVal );}

反转需要注意的地方:
 第一、使用回调函数_cb_negate对Y轴的刻度进行否定,用到的函数SetLabelFormatCallback
 第二、对Y轴的值进行否定。即本例的$ydata [$i] = round ( - $ydata [$i] );

原文地址:http://www.yzswyl.cn/blread-1461.html

原创粉丝点击