2016-06-30 22 views
0

Ich arbeite an einer Grafik, aber es ist nicht sichtbar.Highcharts Grafik nicht sichtbar

In meinem Code gibt es zwei Zeilen, eine, die funktioniert und eine, die nicht funktioniert. (ich habe sie kommentiert)

//this line does not work 
//data3.push([new Date(d.timestamp).getTime(),data.data.risk); 

//this line works 
data3.push([new Date(d.timestamp).getTime(),data.data.threshold[0].amber_threshold]); 

Mein Ziel: ich die Zeile ausgeführt werden soll, die nicht funktioniert.

Ich glaube, ich gebe die Daten nicht richtig weiter. Ich möchte das Array risk übergeben, um das Diagramm zu erstellen.

Kopieren Sie einfach den Code in eine Datei einfügen und es sollte funktionieren.

<!DOCTYPE html> 
<html> 

<head> 

<script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
<script src="https://code.highcharts.com/highcharts.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 

</head> 

<body style="background:#212224;"> 

<div id="container2" style="max-width: 1666px; margin: 0 auto"></div> 

<script type="text/javascript"> 

    $.getJSON('https://dl.dropboxusercontent.com/u/76618626/data2.json', function (data) { 
     console.log("data is : "); 
     console.log(data); 

        var minX = _.min(data.data.risk, function (d) { 
         return new Date(d.timestamp).getTime(); 
        }); 
        var maxX = _.max(data.data.risk, function (d) { 
         return new Date(d.timestamp).getTime(); 
        }); 


     var data3 = []; 

      $.each(data.data.risk, function (i, d) { 

         //this line does not work 
         //data3.push([new Date(d.timestamp).getTime(),data.data.risk); 

         //this line works 
         data3.push([new Date(d.timestamp).getTime(),data.data.threshold[0].amber_threshold]); 
       }); 




     $('#container2').highcharts({ 

      chart: { 
      backgroundColor: '#000000', 
       }, 

      title: { 
       text: 'Test Graph', 
       style: { 
       color: '#FFFFFF', 
       fontWeight: 'bold' 
       } 
      }, 
      xAxis: { 
       type: 'datetime', 
       title: { 
        text: 'Time Stamp' 
       }, 
       gridLineColor: 'grey', 
       gridLineWidth: 1, 
       lineWidth:1 

      }, 
      yAxis: { 
       title: { 
        text: 'Value' 
       }, 
       gridLineColor: 'grey', 
       gridLineWidth: 1, 
       lineWidth:1 
      }, 
      legend: { 
       enabled: true 
      }, 

      exporting: false, 



      plotOptions: { 
       line: {     
        lineColor: 'red', 
        fillOpacity: 1,      
        lineWidth: 2, 
        states: { 
         hover: { 
          lineWidth: 2 
         } 
        }, 
        threshold: null, 
        marker: { 
         fillColor: '#e57255' 
         } 


       }, 


      }, 

      series: [{ 
       name: 'Graph', 
       data: data3 
      }] 
     }); 
    }); 

</script> 
</body> 
</html> 
+0

Bekommen Sie einen Fehler in der Konsole? – abarisone

Antwort

1

Ich nehme an, Sie die von riskvalue ‚s wollen. Sie haben bereits durch die einzelnen Elemente von risk in dieser Zeile iteriert: $.each(data.data.risk, function (i, d) {

Um Werte von risk die Zeile verwenden: data3.push([new Date(d.timestamp).getTime(),d.value]);

Sie können das folgende Beispiel überprüfen.

<!DOCTYPE html> 
    <html> 

    <head> 

    <script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
    <script src="https://code.highcharts.com/highcharts.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> 

    </head> 

    <body style="background:#212224;"> 

    <div id="container2" style="max-width: 1666px; margin: 0 auto"></div> 

    <script type="text/javascript"> 

     $.getJSON('https://dl.dropboxusercontent.com/u/76618626/data2.json', function (data) { 
      console.log("data is : "); 
      console.log(data); 

         var minX = _.min(data.data.risk, function (d) { 
          return new Date(d.timestamp).getTime(); 
         }); 
         var maxX = _.max(data.data.risk, function (d) { 
          return new Date(d.timestamp).getTime(); 
         }); 


      var data3 = []; 

       $.each(data.data.risk, function (i, d) { 

          //this line does not works 
          data3.push([new Date(d.timestamp).getTime(),d.value]); 

          //this line works 
          // data3.push([new Date(d.timestamp).getTime(),data.data.threshold[0].amber_threshold]); 
        }); 




      $('#container2').highcharts({ 

       chart: { 
       backgroundColor: '#000000', 
        }, 

       title: { 
        text: 'Test Graph', 
        style: { 
        color: '#FFFFFF', 
        fontWeight: 'bold' 
        } 
       }, 
       xAxis: { 
        type: 'datetime', 
        title: { 
         text: 'Time Stamp' 
        }, 
        gridLineColor: 'grey', 
        gridLineWidth: 1, 
        lineWidth:1 

       }, 
       yAxis: { 
        title: { 
         text: 'Value' 
        }, 
        gridLineColor: 'grey', 
        gridLineWidth: 1, 
        lineWidth:1 
       }, 
       legend: { 
        enabled: true 
       }, 

       exporting: false, 



       plotOptions: { 
        line: {     
         lineColor: 'red', 
         fillOpacity: 1,      
         lineWidth: 2, 
         states: { 
          hover: { 
           lineWidth: 2 
          } 
         }, 
         threshold: null, 
         marker: { 
          fillColor: '#e57255' 
          } 


        }, 


       }, 

       series: [{ 
        name: 'Graph', 
        data: data3 
       }] 
      }); 
     }); 

    </script> 
    </body> 
    </html> 

Ausgang: enter image description here

+0

Okay danke ... es funktioniert ... eine Idee, warum es so lange dauert, um das Diagramm zu machen? – Simon

+0

Bitte kopieren und fügen Sie den vollständigen Code ein. und warte ein paar Minuten. Ihre JSON haben eine riesige Daten –

+0

Es ist nur 8000 Punkte ... so eine Idee, warum es so lange dauert? Ich habe Graphen mit mehr als 10.000 Punkten gezeichnet und sie rendern schnell als das ... – Simon