通过jpgrph在x轴上添加自定义直线

时间:2017-07-05 09:23:40

标签: charts scaling jpgraph

Add customized straight line on each Sunday 我想问一下如何使用jpgraph将自定义行添加到图表中。例如,要识别从数据库中选择的日期SUNDAY,jpgraph将在每个星期日绘制一条红色的直线,将在x轴上显示。

有没有人遇到相关问题并且已经解决了?请告诉我,谢谢。

我的情况代码:     `

$dateLocale = new DateLocale(); 
$dateLocale->Set(''); 
$file_date = date("Ymd");
$dateArray = array();
$dataSuccessful = array(); //get from db 
$dataUser_not_found = array();
$dataAcc_not_activated = array();
$dataUnsuccess_others = array();

$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
//Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "example";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $dateArray[] = date("d/m/Y (D)", strtotime($row["date"]));
        $dataSuccessful[] = $row["login_success_count"];
        $dataUser_not_found[] = $row["unsuccess_not_found"];
        $dataAcc_not_activated[] = $row["unsuccess_not_activated"];
        $dataUnsuccess_others[] = $row["unsuccess_others"];
    }
} else {
    echo "No results in this table";
}

function strBefore($string, $substring) {
    $pos = strpos($string, $substring);
    if($pos === false){
        return $string;
    }else{
        return(substr($string, 0, $pos));
    }
}

function strAfter($string, $substring) {
    $pos = strpos($string, $substring);
    if($pos === false){
        return $string;
    }else{ 
        return(substr($string, $pos+strlen($substring)));
    }
}

JpgraphError::SetImageFlag(false);
JpGraphError::SetLogFile('syslog');

// Create the graph. 
$graph = new Graph(2560, 1320);  
//initialization of the default theme
$graph->ClearTheme();

//$graph->SetScale('datlin',0,$x_max);
$graph->SetScale('datlin');
$graph->img->SetMargin(60,150,50,60);
$graph->SetShadow();

// Create the linear plot (SUCCESSFUL)
$l1plot=new LinePlot($dataSuccessful);
$l1plot->SetColor('lightblue:0.4');
$l1plot->SetFillColor("lightblue:0.7");
$l1plot->SetWeight(2);
$l1plot->SetLegend('The total number of visit (SUCCESSFUL)');

// Create the linear plot (UNSUCCESSFUL)
$user_not_found_plot = new LinePlot($dataUser_not_found);
$user_not_found_plot->SetColor('orange:1.2');
$user_not_found_plot->SetFillColor('orange@0.2');
$user_not_found_plot->SetLegend('(UNSUCCESSFUL) User not found');
$acc_not_activated_plot = new LinePlot($dataAcc_not_activated);
$acc_not_activated_plot->SetColor('green:0.8');
$acc_not_activated_plot->SetFillColor('green@0.4');
$acc_not_activated_plot->SetLegend('(UNSUCCESSFUL) Account not activated');
$others_plot = new LinePlot($dataUnsuccess_others);
$others_plot->SetColor('lightred:1.2');
$others_plot->SetFillColor('lightred@0.4');
$others_plot->SetLegend('(UNSUCCESSFUL) Others');

$graph->title->Set('log report');
$graph->xaxis->title->Set('Last 30 days');

$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->setYScale(0, 'lin', 0, 2000);

/* Add the plots to the graph */
//SUCCESSFUL numbers
$graph->Add($l1plot); 

//UNSUCCESSFUL numbers
$graph->AddY(0,$user_not_found_plot);
$graph->AddY(0,$acc_not_activated_plot);
$graph->AddY(0,$others_plot);
$graph->ynaxis[0]->SetColor('red');
$graph->ynaxis[0]->title->Set('The number of visit (UNSUCCESSFUL)');
$graph->ynaxis[0]->scale->SetGrace(80);
//As demo, set the specific date on x-axis
$graph->xaxis->SetLabelFormatString('d/m/Y',true);

$graph->xaxis->setTickLabels($dateArray);
// Display the graph
// Get the handler to prevent the library from sending the image to the          browser
$gdImgHandler = $graph->Stroke(_IMG_HANDLER);

// Default is PNG so use ".png" as suffix
$fileName = "pic/oul207_log_".$file_date.".png";
$graph->img->Stream($fileName);

?>`

1 个答案:

答案 0 :(得分:5)

我猜你不想要tick,所以这里是如何添加红色垂直线:

require_once ('jpgraph/jpgraph_plotline.php');
$v_plot = new PlotLine();
$v_plot->SetDirection(VERTICAL);
$v_plot->SetColor('red');
$v_plot->SetPosition(2);
$graph->AddLine($v_plot);

当然,你需要相对于x轴计算你的星期日,并相应地设置位置。