【问题标题】:Adding customized straight line on x-axis by jpgrph通过jpgrph在x轴上添加自定义直线
【发布时间】:2017-12-08 21:06:09
【问题描述】:

我想问一下如何使用 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);

?>`

【问题讨论】:

  • 或者用PHP编辑方法,也许可以解决?

标签: charts scaling jpgraph


【解决方案1】:

我猜你不想要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 轴的星期日并相应地设置位置。

【讨论】:

  • 谢谢,它有效,太好了!为了解决我在星期日设置位置的问题,我们需要使用 foreach 循环来打印所有日期,并检查工作日是星期日 "(date('w', strtotime($date)) == 0)" .如果函数返回 true,“$v_plot->SetPosition([key_of_foreach_loop])”。
猜你喜欢
  • 2021-02-05
  • 2014-02-22
  • 1970-01-01
  • 2018-12-28
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多