0

I am using yii2, and in that I need to use temperatures.csv file to get a graph, while using normal php its working fine but in yii2, the path to the csv file causes an error.

My view file code is this:

<div id="graphdiv3"
 style="width:500px; height:300px;"></div>
<script type="text/javascript">
var filename =  "Date,Temperature\n" +
   "2008-05-07,75\n" +
   "2008-05-08,70\n" +
   "2008-05-09,80\n";
console.log(filename);
 g3 = new Dygraph(
   document.getElementById("graphdiv3"),
  "///var/www/devicesadmin/yii-app/views/data-chart/temperatures.csv",
   {
 legend: 'always',
 title: 'NYC vs. SF',
 showRoller: true,
 rollPeriod: 14,
 ylabel: 'Temperature (F)',
   }
 );
</script>

My csv file is in the view folder of this same file folder. filename Javascript variable is a sample of that csv file where it's working fine, but calling the file directly, causes it to return an error.

P.S using widgets is not fair as many options are not working.

  • no, its working fine without yii2 framework, but unfortunately i need in yii2,here where to keep the file and how to refer is the concern. – pavi THRAN Dec 04 '19 at 13:20
  • [https://www.yiiframework.com/doc/guide/2.0/en/concept-aliases](https://www.yiiframework.com/doc/guide/2.0/en/concept-aliases) @views/data-chart/temperatures.csv? Using alias? – Sfili_81 Dec 04 '19 at 13:31
  • @Sfili_81 It is also not working. – pavi THRAN Dec 04 '19 at 13:34
  • why have you placed it inside the views folder? is there any special reason for doing that why not place it inside the web accessible directory and just use path relative to the web directory like if the file is in the root of the `web` folder then you will provide path in javascript like `'/temperatures.csv'` – Muhammad Omer Aslam Dec 04 '19 at 21:51

1 Answers1

1
  1. I dont think there is any special reason to place the file inside the views folder neither it should be there, you should place it inside the web directory of your project and use the path relative to the web directory.

  2. What is the point of declaring a variable with line breaks at the end?

    var filename =  "Date,Temperature\n" +
       "2008-05-07,75\n" +
       "2008-05-08,70\n" +
       "2008-05-09,80\n";
    

    That is not a valid javascript code and would simply result in error like

    Uncaught SyntaxError: Invalid or unexpected token

  3. You should run the javascript on DOM ready so that the source library is available before calling the Dygraph constructor, and for writing the javascript code inside the view i would prefer using HEREDOC for more readability

Your code inside the view should look like below

<?php
    use yii\web\View;

    $js = <<<JS

    g3 = new Dygraph(
        document.getElementById("graphdiv3"),
        "/temperatures.csv",
        {
        legend: 'always',
        title: 'NYC vs. SF',
        showRoller: true,
        rollPeriod: 14,
        ylabel: 'Temperature (F)',
        }
    );
JS;

    $this->registerJs($js, View::POS_READY);
?>
<div id="graphdiv3" style="width:500px; height:300px;"></div>
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68