martijnmelchers
martijnmelchers

Reputation: 51

Can't find file, chart does not appear

I have this code :

<script>
var area = new Morris.Area({
    element: 'revenue-chart',
    resize: true,
    data: [
        {y: '2011 Q1', item1: 2666, item2: 2666},
        {y: '2011 Q2', item1: 2778, item2: 2294},
        {y: '2011 Q3', item1: 4912, item2: 1969},
        {y: '2011 Q4', item1: 3767, item2: 3597},
        {y: '2012 Q1', item1: 6810, item2: 1914},
        {y: '2012 Q2', item1: 5670, item2: 4293},
        {y: '2012 Q3', item1: 4820, item2: 3795},
        {y: '2012 Q4', item1: 15073, item2: 5967},
        {y: '2013 Q1', item1: 10687, item2: 4460},
        {y: '2013 Q2', item1: <?php echo file_get_contents("../adminpanel/pagecounters/totalviewsDecember.txt")?>, item2: 5713}
    ],
    xkey: 'y',
    ykeys: ['item1', 'item2'],
    labels: ['Item 1', 'Item 2'],
    lineColors: ['#a0d0e0', '#3c8dbc'],
    hideHover: 'auto'
});
</script>

But the chart doesn't appear when I refresh the page. (Also the file EXISTS! I tested it.)

How can I fix it?

Upvotes: 0

Views: 55

Answers (1)

A. Rama
A. Rama

Reputation: 912

Your js code is correct, here it is with an arbitrary number substituted to the php:

http://jsbin.com/muzucozivu/1/edit?html,js,output

Hence there are only a few possible problems:

  • you don't include all the right libraries
  • the php fails for some reason and the page is truncated after that
  • the element 'revenue-chart' is not present or not visible in your page.

To be sure to have all the libraries, copy them from my jsbin code (that was cloned from the jsbin code I found on the MorrisJS page).

To check the second possibility, open the page source and look if the rest of the page is absent or if the PHP returned some spurious value (if the file_get_contents fails, it will return FALSE).

The last one should only take a visual inspection of your code or open FireBug in Firefox and look for the right element. Be sure it's spelled correctly. You could also check for other JS errors in the console.

However even if the file exists, the PHP could still fail. A common source of problems, with PHP file_get_contents, is misunderstanding where the "Working Directory" actually is, thus any relative paths end up being screwed. Use absolute file path to test for that.

Upvotes: 1

Related Questions