Laravel中的Datepicker和chartjs(悬停时的旧图表数据)

时间:2019-03-22 11:29:28

标签: laravel chart.js

显示图表时出现问题。我有一个默认情况下今天会加载数据的函数。之后,我可以选择一个日期范围。但是,将鼠标悬停在标签上时会出现问题。他们加载旧的图表值和折线。我知道我需要使用销毁或更新功能。但是我不知道如何在我的代码中实现它。

脚本:

        <script type="text/javascript">
        $(document).ready(function() {
          getdata_chart();
          {{-- chartjs --}}
          function getdata_chart(start_date='', end_date='')
          {
            $.ajax({
              url: "{{ route('ajaxdata.getdata_chart') }}",
              method: "GET",
              data:{
                start_date:start_date, end_date:end_date
              },
              success: function(data) {
                console.log(data);
                var timeFormat = 'DD MMM YYYY г. kk:mm:ss ч.';
                var progress = document.getElementById('animationProgress');
                var dateANDtime = [];
                var Gblok9osx = [];
                var Gblok9osy = [];
                var Gblok11osx = [];
                var Gblok11osy = [];
                var Gfilblok10 = [];
                var Gfilblok11 = [];
                var Gcvn = [];
                var Gndk = [];
                var Gndk2 = [];

                for (var i in data) {
                  dateANDtime.push(data[i].timestamp);
                  Gblok9osx.push(data[i].blok9osx);
                  Gblok9osy.push(data[i].blok9osy);
                  Gblok11osx.push(data[i].blok11osx);
                  Gblok11osy.push(data[i].blok11osy);
                  Gfilblok10.push(data[i].filblok10);
                  Gfilblok11.push(data[i].filblok11);
                  Gcvn.push(data[i].cvn);
                  Gndk.push(data[i].ndk);
                  Gndk2.push(data[i].ndk2);
                }

                var chartdata = {
                  labels: dateANDtime,
                  datasets: [{
                    fill: false,
                    label: 'Отвес блок 9 x',
                    backgroundColor: 'rgba(199, 228, 238, 0.75)',
                    borderColor: 'rgba(1, 150, 200, 0.75)',
                    //hoverBackgroundColor: 'rgba(200, 200, 200, 0.75)',
                    hoverBorderColor: 'rgba(200, 200, 200, 1)',
                    data: Gblok9osx,
                    hidden: false
                  },
                  {
                    fill: false,
                    label: 'Отвес блок 9 y',
                    backgroundColor: 'rgba(163, 147, 222, 0.75)',
                    borderColor: 'rgba(50, 10, 200, 0.75)',
                    //hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                    hoverBorderColor: 'rgba(200, 200, 200, 1)',
                    data: Gblok9osy,
                    hidden: true
                  },
                  {
                    fill: false,
                    label: 'Отвес блок 11 x',
                    backgroundColor: 'rgba(221, 221, 241, 0.75)',
                    borderColor: 'rgba(100, 100, 200, 0.75)',
                    //hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                    hoverBorderColor: 'rgba(200, 200, 200, 1)',
                    data: Gblok11osx,
                    hidden: true
                  },
                  {
                    fill: false,
                    label: 'Отвес блок 11 y',
                    backgroundColor: 'rgba(147, 227, 227, 0.75)',
                    borderColor: 'rgba(1, 200, 200, 0.75)',
                    //hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                    hoverBorderColor: 'rgba(200, 200, 200, 1)',
                    data: Gblok11osy,
                    hidden: true
                  },
                  {
                    fill: false,
                    label: 'Филтрация блок 10',
                    backgroundColor: 'rgba(139, 105, 132, 0.75)',
                    borderColor: 'rgba(255, 1, 200, 0.75)',
                    //hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                    hoverBorderColor: 'rgba(200, 200, 200, 1)',
                    data: Gfilblok10,
                    hidden: true
                  },
                  {
                    fill: false,
                    label: 'Филтрация блок 11',
                    backgroundColor: 'rgba(0, 200, 1, 1)',
                    borderColor: 'rgba(0, 101,1, 0.75)',
                    hoverBackgroundColor: 'rgba(200, 200, 200, 1)',
                    hoverBorderColor: 'rgba(50, 50, 50, 1)',
                    data: Gfilblok11,
                    hidden: true
                  },
                  {
                    fill: false,
                    label: 'КВН',
                    backgroundColor: 'rgba(30, 100, 100, 1)',
                    borderColor: 'rgba(100, 200,1, 0.75)',
                    hoverBackgroundColor: 'rgba(101, 200, 200, 1)',
                    hoverBorderColor: 'rgba(50, 50, 50, 1)',
                    data: Gcvn,
                    hidden: true
                  },
                  {
                    fill: false,
                    label: 'КДК',
                    backgroundColor: 'rgba(30, 100, 100, 1)',
                    borderColor: 'rgba(100, 200,1, 0.75)',
                    hoverBackgroundColor: 'rgba(101, 200, 200, 1)',
                    hoverBorderColor: 'rgba(50, 50, 50, 1)',
                    data: Gndk,
                    hidden: true
                  },
                  {
                    fill: false,
                    label: 'НДК',
                    backgroundColor: 'rgba(30, 100, 100, 1)',
                    borderColor: 'rgba(100, 200,1, 0.75)',
                    hoverBackgroundColor: 'rgba(101, 200, 200, 1)',
                    hoverBorderColor: 'rgba(50, 50, 50, 1)',
                    data: Gndk2,
                    hidden: true
                  }
                ]
              };
              $('#reset_zoom').click(function() {
                barGraph.resetZoom();
              })
              $("#save-btn").click(function() {
                $("#mycanvas").get(0).toBlob(function(blob) {
                  saveAs(blob, "chart_1.png");
                });
              });
              var config = {
                type: 'line',
                data: chartdata,
                options: {
                  responsive: true,
                  title: {
                    display: true,
                    text: 'Диаграма: Отвеси и филтрации'
                  },
                  scales: {
                    yAxes: [{
                      type: type,
                      scaleLabel: {
                        display: true,
                        labelString: '[ mm ] ,[ m ], [ l/s ]'
                      },
                      ticks: {
                        beginAtZero: false
                      }
                    }],
                    xAxes: [{
                      type: 'time',
                      time: {
                        tooltipFormat: timeFormat,
                        displayFormats: {
                          'hour': timeFormat
                        }
                      },
                      scaleLabel: {
                        display: true,
                        labelString: 'Дата/Час'
                      },
                      ticks: {
                        beginAtZero: true
                      }
                    }]
                  },
                  // Container for pan options
                  pan: {
                    // Boolean to enable panning
                    enabled: true,

                    // Panning directions. Remove the appropriate direction to disable
                    // Eg. 'y' would only allow panning in the y direction
                    mode: 'y'
                  },

                  // Container for zoom options
                  zoom: {
                    // Boolean to enable zooming
                    enabled: true,

                    // Zooming directions. Remove the appropriate direction to disable
                    // Eg. 'y' would only allow zooming in the y direction
                    mode: 'xy'

                  },
                  animation: {
                    duration: 2000,
                    onProgress: function(animation) {
                      progress.value = animation.currentStep / animation.numSteps;
                    },
                    onComplete: function() {
                      window.setTimeout(function() {
                        progress.value = 0;
                      }, 2000);
                    }
                  },
                  legend: {
                    display: true,
                    labels: {
                      //fontColor: 'rgb(255, 99, 132)'
                      usePointStyle: true
                    }
                  }
                },
                plugins: [{
                  beforeDraw: function(c) {
                    var reset_zoom = document.getElementById("reset_zoom"); //reset button
                    var ticks = c.scales['x-axis-0', 'y-axis-0'].ticks.length; //x-axis ticks array
                    var labels = c.data.labels.length; //labels array
                    if (ticks < labels) reset_zoom.hidden = false;
                    else reset_zoom.hidden = true;
                  }
                }]
              };
              var type = 'linear';
              var ctx = document.getElementById("mycanvas");
              var barGraph = new Chart(ctx, config);
              window.onload = function() {
                var ctx = document.getElementById("mycanvas");
                window.myLine = barGraph;
              };

              document.getElementById('toggleScale').addEventListener('click', function() {
                type = type === 'linear' ? 'logarithmic' : 'linear';
                window.myLine.options.title.text = 'Диаграма('+type+'): Отвеси и филтрации';
                window.myLine.options.scales.yAxes[0] = {
                  display: true,
                  type: type
                };

                window.myLine.update();


              }); //end data
            },
            error: function(data) {
              console.log(data);
            }
          });
        }




  $('#search').click(function(){
    var start_date = $('#start_date').val();
    var end_date = $('#end_date').val();
    if(start_date != '' &&  end_date != '')
    {

      getdata_chart(start_date, end_date);

    }
    else
    {
      alert('Both Date is required');
    }
  });
    });
    </script>

进入控制器:

  function getdata_chart(Request $request)
  {
    $start_date = date('d-m-Y 00:00:00');
    $end_date = date('d-m-Y 23:59:59');
    if($request->start_date != '' && $request->end_date != '')
    {
      $dateScope = array($request->start_date ." 00:00:00", $request->end_date ." 23:59:59");
    } else {
      $dateScope = array($start_date, $end_date);
    };
    $students = otv_fil::whereBetween('timestamp', $dateScope)
    ->selectRaw('timestamp,blok9osx,blok9osy,blok11osx,blok11osy,filblok10,filblok11,cvn,ndk,ndk - ? as ndk2',[743.38])
    ->orderBy('timestamp', 'ASC')
    ->get();
    return response()->json($students);

  }

主体部分需要在搜索点击功能中正确吗?

0 个答案:

没有答案