doutao1171 2016-04-29 09:23
浏览 99

错误显示图表Highstock中的数据

I have a graph highstock that create a CSV coming from my database and then display that data in the chart but it turns out that the graph only recognizes CSV if I after the CSV is created open CSV and back to guard as CSV.

The php script that I have to create the csv is this:

 <?php
    // mysql database connection details
    $host = "localhost";
    $username = "user";
    $password = "pass";
    $dbname = "database";

    // open connection to mysql database
    $connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));

    // fetch mysql table rows
    $sql = "SELECT `tempo`, `s1`, `s2`, `s3` FROM `tabel`";
    $result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));
    $f = "perfiskwh.csv";
    $fp = fopen('perfiskwh.csv', 'w+');


    while($row = mysqli_fetch_assoc($result))
    {
        fputcsv($fp,$row);
    }
    // read into array
    $arr = file($f);

    // edit first line
    $arr[0] = 'Data,KWh1,KWh2,KWh3,'. "
";

    // write back to file
    file_put_contents($f, implode($arr));
    fclose($fp);
    //close the db connection
    mysqli_close($connection);

?>

As the graph have this code so far:

$.get('dados/perfiskwh.csv', function (csvFile) {
      var data = parseCSVData(csvFile);
      var highchartsOptions = Highcharts.setOptions({
        lang: {
          loading: 'Aguarde...',
          months: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
          weekdays: ['Domingo', 'Segunda-feira', 'Terça-feira', 'Quarta-feira', 'Quinta-feira', 'Sexta-feira', 'Sábado'],
          shortMonths: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
          exportButtonTitle: "Exportar",
          printButtonTitle: "Imprimir",
          rangeSelectorFrom: "De",
          rangeSelectorTo: "Até",
          rangeSelectorZoom: "Periodo",
          downloadPNG: 'Descarregar gráfico como imagem PNG',
          downloadJPEG: 'Descarregar gráfico como imagem JPEG',
          downloadSVG: 'Descarregar gráfico como imagem SVG',
          downloadPDF: 'Salvar em documento PDF',
          contextButtonTitle: 'Opções',
          noData: 'Sem dados para mostrar',
          printChart: 'Imprimir gráfico',
          numericSymbols: null,
          // resetZoom: "Reset",
          // resetZoomTitle: "Reset,
          thousandsSep: ".",
          decimalPoint: ','
          }
        }
      );
      $('#container').highcharts('StockChart', {
        navigation: {
          buttonOptions: {
          enabled: true,
          }
        },
        colors:[
     '#993333',
     '#404040',
     '#003366',
     '#006600',
     '#CC3300'
   ],
        credits: {
          enabled: false
        },
        rangeSelector : {
          selected: 1,
          allButtonsEnabled: true,
          buttons: [{
            type: 'day',
            count: 1,
            text: '1 dia'
          }, {
            type: 'week',
            count: 1,
            text: '1 Semana'
          }, {
            type: 'all',
            text: 'Tudo'
          }],
          buttonTheme: {
            width: 75,
            style: {
             color: '#333',
             fontWeight: 'bold',
             fontSize: '14px',
             fontFamily: 'Trebuchet MS, Verdana, sans-serif'

          },
          },

        },
        chart: {
          renderTo: 'container',
          backgroundColor: {
         linearGradient: [500, 400, 250, 0],
         stops: [
             [0, '#F0F7FF'],
             [1, '#F0F7FF']
         ]
     },
     style: {
      color: '#333',
      fontWeight: 'bold',
      fontSize: '14px',
      fontFamily: 'Trebuchet MS, Verdana, sans-serif'

   },
          type: 'spline'
        },

        title: {
          text: 'Perfis de Consumos',
          style: {
           color: '#333',
           fontWeight: 'bold',
           fontSize: '20px',
           fontFamily: 'Trebuchet MS, Verdana, sans-serif'

        }
        },
        xAxis: {
          labels: {
            style: {
              color: 'black',
              fontWeight: 'bold',
              fontSize: '12px',
              fontFamily: 'Trebuchet MS, Verdana, sans-serif'
            }
        },
          type: 'datetime',
          tickInterval: 3600 * 1000,
          categories: ['Mar','Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'Jan', 'Feb']

        },
        tooltip: {
          valueDecimals: 1,
          valueSuffix: ' KWh'
      },
        yAxis: {
          labels: {
            style: {
              color: 'black',
              fontWeight: 'bold',
              fontSize: '12px',
              fontFamily: 'Trebuchet MS, Verdana, sans-serif'
            }
        },
          opposite: false,
          title: {
            text: 'Consumos da Energia [KWh]',
            style: {
             color: '#333',
             fontWeight: 'bold',
             fontSize: '16px',
             fontFamily: 'Trebuchet MS, Verdana, sans-serif'
          }
          }
        },
        navigator:{
          xAxis: {
            style: {
             color: '#333',
             fontWeight: 'bold',
             fontSize: '16px',
             fontFamily: 'Trebuchet MS, Verdana, sans-serif'
          }
        },
          enabled: true,
          handles: {
              backgroundColor: 'Black',
              borderColor: 'White'
          },
            series: {
              color: '#0000FF',
              lineWidth: 1
          },
          outlineColor: 'black',
          outlineWidth: 1,
        },
        series: [{
          name:'Distribuidor',
          data: data[0],
        }, {
          name: 'Edificios',
          data: data[1]
        }, {
          name: 'Avac',
          data: data[2]
        }]
      });
    });

    function parseCSVData(csvFile) {
      //Array para armazenar dados do Gráfico
      var Geral= [];
      var Distribuidor= [];
      var AVAC= [];

      var Data_inicio = "January 1, 2014";
      var Data_hoje = new Date();

      //Separar os dados do CSV em linhas
      var lines= csvFile.split("
");

      //Verifica todas as linhas do CSV
      $.each(lines, function (lineNumber, line){
        if(lineNumber !=0){ //Salta a linha do cabeçalho
          var fields = line.split(",");
          if(fields.length==4 && Date.parse(fields[0]) >= Date.parse(Data_inicio) && Date.parse(fields[0]) <= Date.parse(Data_hoje)) {//Salta as linhas invalidas e começa na data especificada até ao presente dia
            var timestamp = Date.parse(fields[0]);
            var data1 = parseInt(fields[1]);
            var data2 = parseInt(fields[2]);
            var data3 = parseInt(fields[3]);

            Geral.push([timestamp, data1]);
            Distribuidor.push([timestamp, data2]);
            AVAC.push([timestamp, data3]);

          }
        }
      });
      return [Geral,Distribuidor,AVAC];
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题