widget.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. use yii\helpers\Html;
  3. use yii\helpers\Url;
  4. use app\assets\AppAsset;
  5. $this->title = $model->text;
  6. $this->params['breadcrumbs'][] = ['text' => 'Опросы', 'url' =>'/inquirer'];
  7. $url = '/inquirer/'.$model->id;
  8. $widget = isset( $_REQUEST['widget'] )?$_REQUEST['widget']*1:1;
  9. AppAsset::register($this);
  10. // \yii\web\JqueryAsset::register($this);
  11. // \yii\bootstrap\BootstrapAsset::register($this);
  12. $do = $model->getPart();
  13. ?>
  14. <?php $this->beginPage() ?>
  15. <!DOCTYPE html>
  16. <html lang="ru" class="h-100" prefix="og: http://ogp.me/ns# article: http://ogp.me/ns/article# profile: http://ogp.me/ns/profile# fb: http://ogp.me/ns/fb#">
  17. <head>
  18. <meta charset="UTF-8">
  19. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  20. <meta http-equiv="x-dns-prefetch-control" content="on">
  21. <link rel="manifest" href="/manifest.json">
  22. <title><?= Html::encode($model->text)?></title>
  23. <meta name="title" content="<?= Html::encode($model->text)?>">
  24. <meta name="description" content="<?= preg_replace('/\s+/', ' ', str_replace( ["\n","\r"],' ',Html::encode( 'Опрос: '.$model->text)))?>">
  25. <meta name="author" content="ИА Амител" />
  26. <meta name="copyright" content="Амител">
  27. <link rel="canonical" href="<?=$url?>">
  28. <?php $this->registerCsrfMetaTags() ?>
  29. <?php $this->head() ?>
  30. </head>
  31. <body>
  32. <?php $this->beginBody() ?>
  33. <div >
  34. <section class="text">
  35. <?
  36. if( $model->show == 'N' || $do){
  37. ?>
  38. <h1>Результат опроса: <?=$model->text?></h1>
  39. <?
  40. $answers = $model->getAnswers();
  41. $sum = 0;
  42. foreach($answers as $item){
  43. $sum += $item->count;
  44. }
  45. if( $widget != 3 ){
  46. /*
  47. круговые диаграммы
  48. */
  49. ?>
  50. <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  51. <?
  52. $s = '';
  53. foreach($answers as $item){
  54. $s .="['".$item['text']."', ".$item['count']."],";
  55. }
  56. $typech = "{'packages':['corechart']}";
  57. if( $widget == 2 ){
  58. $type = "is3D: false,pieHole: 0.4,legend: {position: 'right', textStyle: {color: 'black', fontSize: 12}}";
  59. }elseif($widget == 3){
  60. $type = 'bar: { groupWidth: "100%" },bars: "horizontal"';
  61. $typech = "{'packages':['bar']}";
  62. }else{
  63. $type = "is3D: true,legend: {position: 'right', textStyle: {color: 'black', fontSize: 12}}";
  64. }
  65. $this->registerJs(
  66. <<<JS
  67. google.charts.load('current', $typech);
  68. google.charts.setOnLoadCallback(drawChart);
  69. function drawChart() {
  70. var data = new google.visualization.DataTable();
  71. data.addColumn('string', 'Ответы');
  72. data.addColumn('number', 'Голоса');
  73. data.addRows([$s]);
  74. var options = {'title':'график',
  75. chartArea:{left:0,top:10,width:'100%',height:'100%'},
  76. tooltip: {textStyle: {color: '#000000'}, showColorCode: false},$type
  77. };
  78. if( $widget == 3){
  79. var chart = new google.charts.Bar(document.getElementById('chart_div'));
  80. }else{
  81. var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  82. google.visualization.events.addListener(chart, 'ready', function () {
  83. png = chart.getImageURI();
  84. // chart_div.innerHTML = '<img src="' + png + '">';
  85. // console.log(png);
  86. dwnload = '<a href="' + png + '" download>Скачать</a>';
  87. chart_png.innerHTML = dwnload;
  88. });
  89. }
  90. chart.draw(data, options);
  91. }
  92. JS
  93. ,
  94. $this::POS_END,
  95. 'chart'
  96. );
  97. ?>
  98. <div id="chart_div" style="width: 100%; height: 400px;" class="p-0"></div>
  99. <div id="chart_png"></div>
  100. <?
  101. }else{
  102. /*
  103. столбчатая гистограмма на html
  104. */
  105. foreach($answers as $item){
  106. $percent = ($sum)?round($item->count/$sum*100):0;
  107. ?>
  108. <div class="shadow-sm p-2 mt-3 bg-light rounded">
  109. <h4><?=$item->text?></h4>
  110. <div class="progress" style="height: 20px;">
  111. <div class="progress-bar bg-info" role="progressbar" style="width: <?=$percent?>%;" aria-valuenow="<?=$percent?>" aria-valuemin="0" aria-valuemax="100"><?=$percent?>%</div>
  112. </div>
  113. </div>
  114. <?
  115. }
  116. }
  117. }else{
  118. ?>
  119. <h1>Опрос: <?=$model->text?></h1>
  120. <?
  121. echo yii\base\View::render("poll",['id'=>$model->id, 'model'=>$model]);
  122. }
  123. ?>
  124. </section>
  125. </div>
  126. <style>
  127. input[type=radio]:not(:checked) {
  128. content: "";
  129. background-color: #ff00ff;
  130. border: 2px solid #36d7d7;
  131. }
  132. </style>
  133. <?php $this->endBody() ?>
  134. <body>
  135. </html>
  136. <?php $this->endPage() ?>
  137. <?