Search code examples
phppchart

pChart, how to display week start or end date with month like "12/2-8" on x-axis?


Right now my pchart week graph display like this way,

enter image description here

But I want to display week on x-axis 12/2-8, 12/9-15 ('month' followed by '/' followed by 'start date', followed by '-', then 'end date')"


Solution

  • You generate the legend you can have

    $weekLegend = getWeekLegend();
    var_dump($weekLegend); // for debug purpose 
    

    Output

    array (size=8)
      0 => string '12/09-15' (length=8)
      1 => string '12/16-22' (length=8)
      2 => string '12/23-29' (length=8)
      3 => string '12/30-05' (length=8)
      4 => string '01/06-12' (length=8)
      5 => string '01/13-19' (length=8)
      6 => string '01/20-26' (length=8)
      7 => string '01/27-02' (length=8)
    

    Adding it to your dataset defination

    // Dataset definition
    $DataSet = new pData;
    $DataSet->AddPoint($data,"Serie1");
    $DataSet->AddPoint(getWeekLegend(),"Serie2");
    $DataSet->AddAllSeries();
    $DataSet->SetAbsciseLabelSerie("Serie2"); //<------ set pChat to use legend
    

    Function Used

    function getWeekLegend($week = 8) {
        $start = new DateTime();
        $start->modify("last sunday");
        while ( $week > 0 ) {
            $date = $start->format("m");
            $date .= "/";
            $date .= $start->format("d");
            $date .= "-";
            $start->modify("+6 day");
            $date .= $start->format("d");
            $start->modify("+1 day");
            $weekLegend[] = $date;
            $week --;
        }
        return $weekLegend ;
    }