2016-08-30 46 views
1

因此,我正在開發一個代碼已經存在的項目。如何將谷歌圖表顏色設置從文件移動到另一個

在下面的2個文件中,我需要在IF語句中以某種方式將顏色設置從第一個文件移除到第二個文件。

原因是第一個文件將被填充/注入後端的信息,但爲了做到這一點,我必須從數據集propeties(標籤,fillColor,strokeColor,pointColor,pointStrokeColor,pointHighlightFill和pointHighlightStroke)數組中刪除在文件1到文件2,以便從將要注入的實際數據中分離樣式

我試着簡單地將數據集部分添加到第二個文件,但只是不顯示任何內容 - 是否可以將其添加到Chart.types .Line.extend對象或AJAX部分莫名其妙?

提前感謝

文件1:(將注入給它的數據):

{ 
    "labels":[ 
    "1 Feb",  
    "8 Feb",  
    "15 Feb",  
    "22 Feb",  
    "29 Feb",  
    "7 Mar",  
    "14 Mar",  
    "21 Mar",  
    "28 Mar",  
    "4 Apr",  
    "11 Apr",  
    "18 Apr",  
    "25 Apr" 
    ], 
    "datasets":[ 
    { 
     "label":"Tenders", 
     "fillColor":"rgba(253,0,20,0.2)", 
     "strokeColor":"rgba(253,0,20,1)", 
     "pointColor":"#fff", 
     "pointStrokeColor":"rgba(253,0,20,1)", 
     "pointHighlightFill":"#fff", 
     "pointHighlightStroke":"rgba(253,0,20,1)", 
     "data":[ 
     77, 
     55, 
     40, 
     65, 
     59, 
     80, 
     81, 
     56, 
     55, 
     65, 
     59, 
     80, 
     75    
     ] 
    } 
    ] 
} 

文件2:(我想要的數據集的屬性來定):

if (document.getElementById("chart_div_won")) { 

    Chart.types.Line.extend({ 
     name: "LineAlt", 
     initialize: function(data) { 
      Chart.types.Line.prototype.initialize.apply(this, arguments); 
      var xLabels = this.scale.xLabels; 
      var xLabelsLength = xLabels.length; 
      xLabels.forEach(function(label, i) { 
       if (i % 4 != 0 || i <= 1 || i == xLabelsLength - 1) 
        xLabels[i] = ''; 
      }) 
     } 
    }); 

    var form_data = {}; 
    $.ajax({ 
     type: "GET", 
     url: "../../../sample_data/chart1.json", 
     data: form_data, 
     success: function(response) { 
      var ctx = document.getElementById("chart_div_won").getContext("2d"); 
      var options = { 
       responsive: true, 
       maintainAspectRatio: true, 
       pointDotRadius: 5, 
       showXLabels: 5, 
      }; 
      var myLineChart = new Chart(ctx).LineAlt(response, options); 

     }, 
     error: function() { 
      $('div#chart-container').html('<div class="notification-body"><p class="notification-heading">Loading error...</p><p class="notification-description">Unfortunatley for some reason visual data failed to load.</p></div>'); 
     }, 
     dataType: "json", 
     contentType: "application/json; charset=utf-8", 
    }); 
} 
+0

我想我一些如何在第二個文件中定義的樣式集,然後將其推到在第一個文件中的數據集陣列 - 所以尋找到陣列並推入他們,如果有人在此期間可以提供任何幫助,將是真棒:) –

回答

0

所以我這就是我所得到的:

if (document.getElementById("chart_div_won")) { 


    Chart.types.Line.extend({ 
     name: "LineAlt", 
     initialize: function(data) { 
      Chart.types.Line.prototype.initialize.apply(this, arguments); 
      var xLabels = this.scale.xLabels; 
      var xLabelsLength = xLabels.length; 
      xLabels.forEach(function(label, i) { 
       if (i % 4 != 0 || i <= 1 || i == xLabelsLength - 1) 
        xLabels[i] = ''; 
      }) 
     } 
    }); 

    var datasets = [{ 
         "label":"Tenders", 
         "fillColor":"rgba(253,0,20,0.2)", 
         "strokeColor":"rgba(253,0,20,1)", 
         "pointColor":"#fff", 
         "pointStrokeColor":"rgba(253,0,20,1)", 
         "pointHighlightFill":"#fff", 
         "pointHighlightStroke":"rgba(253,0,20,1)"} 
     ]; 
    var form_data = {}; 

    $.ajax({ 
     type: "GET", 
     url: "../../../sample_data/chart1.json", 
     data: form_data.push(datasets), 

     success: function(response) { 
      var ctx = document.getElementById("chart_div_won").getContext("2d"); 
      var options = { 
       responsive: true, 
       maintainAspectRatio: true, 
       pointDotRadius: 5,             
       showXLabels: 5, 
      }; 
      var myLineChart = new Chart(ctx).LineAlt(response, options); 

     }, 
     error: function() { 
      $('div#chart-container').html('<div class="notification-body"><p class="notification-heading">Loading error...</p><p class="notification-description">Unfortunatley for some reason visual data failed to load.</p></div>'); 
     }, 
     data: form_data.push(datasets), 
     dataType: "json", 
     contentType: "application/json; charset=utf-8", 
    }); 
} 

我正在嘗試添加數據集數組,然後將其添加到數據集數組中的第一個文件中的數組。沒有運氣,但我認爲我的代碼在語法錯誤上面的這個答案

0

對於任何人有這個或類似的問題。最後很容易。我所要做的就是在數組的最後添加每個屬性。

代碼

myLineChart.datasets[0].fillColor = "rgba(253,0,20,0.2)"; 

等等

相關問題