Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding customized straight line on x-axis by jpgrph

Add customized straight line on each Sunday I would like to ask how to add the customized line into chart by using jpgraph. For example, to identify SUNDAY on date which selecting from database, the jpgraph will draw a straight line with red color on each Sunday which will be showed on x-axis.

Does anyone meet the related issue and has been solved? Please tell me, thank you.

The code of my situation: `

$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('[email protected]');
$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('[email protected]');
$acc_not_activated_plot->SetLegend('(UNSUCCESSFUL) Account not activated');
$others_plot = new LinePlot($dataUnsuccess_others);
$others_plot->SetColor('lightred:1.2');
$others_plot->SetFillColor('[email protected]');
$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);

?>`
like image 369
Chris.C Avatar asked Jul 05 '17 09:07

Chris.C


1 Answers

I guess you don't want a tick, so here's how to add a red vertical line:

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);

Of course you'd need to calculate your sunday(s) relative to the x-axis and set the position(s) accordingly.

like image 91
simlev Avatar answered Sep 28 '22 13:09

simlev