2017-02-10 4 views
1

Ich habe ein Google Combo-Diagramm, das einen Wert als Balken und einen Wert für eine Zeile hat, wollte ich auch eine Linie, um den Durchschnittswert der Balken zu zeigen, so fand ich Code, der das aber tun würde, wenn meine andere Zeile implementiert wurde.Google-Diagramm, durchschnittliche Linie entfernt meine andere Linie

Das ist, was mein Diagramm vorher aussah.

enter image description here

Und das ist, nachdem ich die durchschnittliche Linie, meine andere Linie verschwunden umgesetzt.

enter image description here

Ich weiß nicht, wie sie beide Show zu machen, zu tun?

Diese Linie scheinen Dinge zu haben, mit, alles zu tun, dv zurück zu data Ändern werden zeigen, wie meine Grafik wie auf dem ersten Bild sah, aber ich denke, es ist etwas mehr ich brauche es alle Arbeit zu machen zu ändern ?

chart.draw(dv, options); 

Hier ist der Code.

<script> 
    google.charts.load('current', { 
     'packages': ['corechart'] 
    }); 
    google.charts.setOnLoadCallback(drawVisualization); 


    function drawVisualization() { 
     var data = google.visualization.arrayToDataTable([ 
      ['Day', 'Repetitions', 'Sets'], 
      @foreach (var c in db.Query(Query, inputDate, endDate, baselift)) 
      { 
       var totAvg = c.avg; 
       var allReps = c.reps; 
       var realAvg = (totAvg/allReps) * 100; 

       //Writes out the data that will be shown in the chart. 
       <text>['@c.date', @c.reps, @c.sets],</text> 
      } 
     ]); 

      // Create a DataView that adds another column which is all the same (empty-string) to be able to aggregate on. 
      var viewWithKey = new google.visualization.DataView(data); 
        viewWithKey.setColumns([0, 1, { 
         type: 'string', 
         label: '', 
         calc: function (d, r) { 
          return '' 
         } 
        }]) 

        // Aggregate the previous view to calculate the average. This table should be a single table that looks like: 
        // [['', AVERAGE]], so you can get the Average with .getValue(0,1) 
        var group = google.visualization.data.group(viewWithKey, [2], [{ 
         column: 1, 
         id: 'avg', 
         label: 'Average', 
         aggregation: google.visualization.data.avg, 
         'type': 'number' 
        }]); 

        // Create a DataView where the third column is the average. 
        var dv = new google.visualization.DataView(data); 
        dv.setColumns([0, 1, { 
         type: 'number', 
         label: 'Average', 
         calc: function (dt, row) { 
          return group.getValue(0, 1); 
         } 
        }]); 

        var options = {        
         title: 'Daily Repetition Statistics', 
         backgroundColor: { fill: 'transparent' }, 
         explorer: { axis: 'horizontal' }, 
         vAxes: { 

          0: { logScale: false, viewWindow: { min: 0 } }, 
          1: { logScale: false, maxValue: 2 } 

         }, 
         hAxis: { title: 'Day' }, 
         seriesType: 'bars', 
         curveType: 'function', 
         series: { 
         0: { 
           targetAxisIndex: 0, 
           color: 'orange'          
          }, 
         1: { targetAxisIndex: 1 }, 
         1: { targetAxisIndex: 1, type: "line" }, 
         2: { targetAxisIndex: 1, type: "line" } 
        } 
       }; 

       var chart = new google.visualization.ComboChart(document.getElementById('chart_div')); 
       chart.draw(dv, options); 
       } 
      </script> 

Antwort

1

der Spaltenindex für 'sets' vorgesehen ist, nicht zu setColumns

Änderung dieser ...

var dv = new google.visualization.DataView(data); 
dv.setColumns([0, 1, { 
    type: 'number', 
    label: 'Average', 
    calc: function (dt, row) { 
     return group.getValue(0, 1); 
    } 
}]); 

zu ..

var dv = new google.visualization.DataView(data); 
dv.setColumns([0, 1, 2, { // <-- add 'sets' column index 2 
    type: 'number', 
    label: 'Average', 
    calc: function (dt, row) { 
     return group.getValue(0, 1); 
    } 
}]); 
+0

oh, habe ich versucht, ein paar Zahlen zu ändern hier und da wusste ich nicht, was ich tat, aber ich dachte nicht daran, eines hinzuzufügen! Das hat funktioniert, vielen Dank! –

+1

Prost! froh zu helfen, jederzeit ... – WhiteHat