Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime PHP to JS for Timeline charts #9

Closed
tperroin opened this issue Nov 8, 2016 · 2 comments
Closed

DateTime PHP to JS for Timeline charts #9

tperroin opened this issue Nov 8, 2016 · 2 comments
Assignees
Labels

Comments

@tperroin
Copy link

tperroin commented Nov 8, 2016

Hello,

First, thank you for this bundle, it really helps us!

However, we face issues with DateTime from PHP to JS Date.

We want to create a timeline chart with only hours, minutes and seconds like this example from Google Chart Timeline documentation :

function drawChart() {

    var container = document.getElementById('example5.2');
    var chart = new google.visualization.Timeline(container);
    var dataTable = new google.visualization.DataTable();

    dataTable.addColumn({ type: 'string', id: 'Room' });
    dataTable.addColumn({ type: 'string', id: 'Name' });
    dataTable.addColumn({ type: 'date', id: 'Start' });
    dataTable.addColumn({ type: 'date', id: 'End' });
    dataTable.addRows([
      [ 'Magnolia Room',  'CSS Fundamentals',    new Date(0,0,0,12,0,0),  new Date(0,0,0,14,0,0) ],
      [ 'Magnolia Room',  'Intro JavaScript',    new Date(0,0,0,14,30,0), new Date(0,0,0,16,0,0) ],
      [ 'Magnolia Room',  'Advanced JavaScript', new Date(0,0,0,16,30,0), new Date(0,0,0,19,0,0) ],
      [ 'Gladiolus Room', 'Intermediate Perl',   new Date(0,0,0,12,30,0), new Date(0,0,0,14,0,0) ],
      [ 'Gladiolus Room', 'Advanced Perl',       new Date(0,0,0,14,30,0), new Date(0,0,0,16,0,0) ],
      [ 'Gladiolus Room', 'Applied Perl',        new Date(0,0,0,16,30,0), new Date(0,0,0,18,0,0) ],
      [ 'Petunia Room',   'Google Charts',       new Date(0,0,0,12,30,0), new Date(0,0,0,14,0,0) ],
      [ 'Petunia Room',   'Closure',             new Date(0,0,0,14,30,0), new Date(0,0,0,16,0,0) ],
      [ 'Petunia Room',   'App Engine',          new Date(0,0,0,16,30,0), new Date(0,0,0,18,30,0) ]]);

    var options = {
      timeline: { singleColor: '#8d8' },
    };

    chart.draw(dataTable, options);
  }

In fact in our application we have to generate a PHP DateTime from a string. But the Data class from your bundle doesn't implement hours, minutes and seconds.

It's pretty easy do add that functionality into the draw function :

public function draw($dataName)
    {
        $js = "var $dataName = new google.visualization.arrayToDataTable([";

        end($this->arrayToDataTable);
        $lastKeyRow = key($this->arrayToDataTable);
        foreach ($this->arrayToDataTable as $keyRow => $row) {
            $js .= '[';

            end($row);
            $lastKeyValue = key($row);
            foreach ($row as $key => $value) {
                if ($value instanceof \DateTime) {
                    $js .= 'new Date(' . $value->format('Y') . ', ' . ($value->format('n')-1) . ', ' .
                        $value->format('d') . ', '. $value->format('H')  .', '. $value->format('i').', '. $value->format('s') .')';
                } else {
                    $js .= json_encode($value);
                }

                if ($key != $lastKeyValue) {
                    $js .= ', ';
                }
            }
            unset($value);
            $js .= ']';

            if ($keyRow != $lastKeyRow) {
                $js .= ', ';
            }
        }
        unset($row);

        $this->firstRowIsData ? $js .= '], true);' : $js .= '], false);';

        return $js;
    }

There is any plan to add this features? Maybe can I create a PR for that?

Thank you!

@cmen
Copy link
Owner

cmen commented Nov 10, 2016

Hello,

It's not planned but you can create a PR.

@cmen cmen added the feature label Nov 10, 2016
cmen added a commit that referenced this issue Dec 23, 2016
@cmen cmen self-assigned this Dec 24, 2016
@cmen
Copy link
Owner

cmen commented Dec 24, 2016

405e883

@cmen cmen closed this as completed Dec 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants