2012-02-09 90 views
7

我試圖通過向爲列繪製的SVG rects添加漸變來向Google ColumnChart添加一些pizazz。下面的代碼會爲iframe svg> defs添加漸變,並在此刻關注的所有瀏覽器(Firefox,IE和Chrome的更高版本)中正確替換rects的fill屬性。向Google可視化添加漸變ColumnChart

我的問題是,只要我將鼠標懸停在上方或選擇一個條(或圖例),顏色就會重置爲原始顏色。我是SVG noob,我一直無法確定顏色的重置方式。

所以我的問題是沒有人知道如何(使用javascript/jquery)停止,覆蓋或某些操作重置顏色的代碼?如果可能,我寧願保持「交互式」部分完整(工具提示等)。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
    <title>Google Visualization API Sample</title> 
    <script type="text/javascript" src="http://www.google.com/jsapi"></script> 
    <script type="text/javascript"> 
     google.load('visualization', '1', {packages: ['corechart']}); 
     google.load("jquery", "1.7.1"); 
    </script> 
    <script type="text/javascript"> 
     function drawVisualization() { 
     // Create and populate the data table. 
     var data = new google.visualization.DataTable(); 
     var rowData = [['Year', 'North', 'West', 'South'], 
         ['2010', 197,  333,  298 ], 
         ['2011', 167,  261,  381 ]]; 
     var data = google.visualization.arrayToDataTable(rowData); 

     visualization = new google.visualization.ColumnChart(document.getElementById('visualization')); 

     google.visualization.events.addListener(visualization, 'ready', function(){ 
      var svgns = "http://www.w3.org/2000/svg"; 
      var gradients = [["red","#C0504D","#E6B8B7"], 
          ["green","#9BBB59","#D8E4BC"], 
          ["blue","#4F81BD","DCE6F1"]]; 
      var svg_defs = $("#visualization iframe").contents().find('defs'); 
      // add gradients to svg defs 
      for(var i = 0; i < gradients.length; i++){ 
      var grad = $(document.createElementNS(svgns, "linearGradient")). 
       attr({id:gradients[i][0],x1:"0%",x2:"0%",y1:"0%",y2:"100%"}); 
      var stopTop = $(document.createElementNS(svgns, "stop")). 
       attr({offset:"0%","stop-color":gradients[i][1]}); 
      var stopBottom = $(document.createElementNS(svgns, "stop")). 
       attr({offset:"100%","stop-color":gradients[i][2]}); 
      $(grad).append(stopTop).append(stopBottom); 
      svg_defs.append(grad); 
      } 
      // #3366cc, #dc3912, #ff9900 - replace default colors with gradients 
      $("#visualization iframe").contents().find('rect[fill="#3366cc"]').attr({'fill':'url(#blue)','stroke-width':0.4,'stroke':'#000000'}); 
      $("#visualization iframe").contents().find('rect[fill="#dc3912"]').attr({'fill':'url(#blue)','stroke-width':0.4,'stroke':'#000000'}); 
      $("#visualization iframe").contents().find('rect[fill="#ff9900"]').attr({'fill':'url(#blue)','stroke-width':0.4,'stroke':'#000000'}); 
     }); 
     // Create and draw the visualization. 
     visualization.draw(data,{width:600, height:400}); 
     } 
     google.setOnLoadCallback(drawVisualization); 
    </script> 
    </head> 
    <body style="font-family: Arial;border: 0 none;"> 
    <div id="visualization" style="width: 600px; height: 400px;"></div> 
    </body> 
</html> 

UPDATE

所以同時期待通過DOM來看看,如果我能找到在哪裏,這些顏色代碼可以被存儲(也有由發現使用它們的功能),我沒有發現這些(其中,當集將做我想做的):

 //fill 
     visualization.qa.l.e[0].Hm.O = "url(#blue)"; 
     visualization.qa.l.e[1].Hm.O = "url(#red)"; 
     visualization.qa.l.e[2].Hm.O = "url(#green)"; 

     // stroke 
     visualization.qa.l.e[0].Hm.Jb = "#000000"; 
     visualization.qa.l.e[1].Hm.Jb = "#000000"; 
     visualization.qa.l.e[2].Hm.Jb = "#000000"; 

     // fill-opacity 
     //visualization.qa.l.e[0].Hm.$b = 0.5; 
     //visualization.qa.l.e[1].Hm.$b = 0.5; 
     //visualization.qa.l.e[2].Hm.$b = 0.5; 

     // stroke-width 
     visualization.qa.l.e[0].Hm.H = 0.4; 
     visualization.qa.l.e[1].Hm.H = 0.4; 
     visualization.qa.l.e[2].Hm.H = 0.4; 

     // stroke-opacity 
     //visualization.qa.l.e[0].Hm.nc = 0.5; 
     //visualization.qa.l.e[1].Hm.nc = 0.5; 
     //visualization.qa.l.e[2].Hm.nc = 0.5; 

但我敢肯定,下一次谷歌更新了可視化代碼,這些變量名稱將發生變化,這將僅僅是一個臨時的解決方案(我不認爲有人選擇這些有意使用的壓縮器/混淆器可能會在下次選擇不同的變量名 - 但是然後誰知道 - 也許它不會)。

所以,如果有人知道一個更加永久的方式,不依賴於手動查找和設置變量名稱,我會喜歡它。否則,這可能是我現在最好的選擇。

UPDATE2(2012年3月1日)

典型的例子。現在的變量是感動:

 //fill 
     visualization.da.C.d[0].en.S = "url(#blue)"; 

回答

0

可以使用MutationObserver知道什麼時候更改了SVG的製作

移動從'ready'事件偵聽器的代碼到observer
覆蓋碼該復位顏色

如下面的代碼片段...

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
    <title>Google Visualization API Sample</title> 
    <script type="text/javascript" src="http://www.google.com/jsapi"></script> 
    <script type="text/javascript"> 
     google.load('visualization', '1', {packages: ['corechart']}); 
     google.load("jquery", "1.7.1"); 
    </script> 
    <script type="text/javascript"> 
     function drawVisualization() { 
     // Create and populate the data table. 
     var data = new google.visualization.DataTable(); 
     var rowData = [['Year', 'North', 'West', 'South'], 
         ['2010', 197,  333,  298 ], 
         ['2011', 167,  261,  381 ]]; 
     var data = google.visualization.arrayToDataTable(rowData); 

     var chartDiv = document.getElementById('visualization'); 
     visualization = new google.visualization.ColumnChart(); 

     // observe changes to the chart container 
     var observer = new MutationObserver(function() { 
      var svgns = "http://www.w3.org/2000/svg"; 
      var gradients = [["red","#C0504D","#E6B8B7"], 
          ["green","#9BBB59","#D8E4BC"], 
          ["blue","#4F81BD","DCE6F1"]]; 
      var svg_defs = $("#visualization iframe").contents().find('defs'); 
      // add gradients to svg defs 
      for(var i = 0; i < gradients.length; i++){ 
      var grad = $(document.createElementNS(svgns, "linearGradient")). 
       attr({id:gradients[i][0],x1:"0%",x2:"0%",y1:"0%",y2:"100%"}); 
      var stopTop = $(document.createElementNS(svgns, "stop")). 
       attr({offset:"0%","stop-color":gradients[i][1]}); 
      var stopBottom = $(document.createElementNS(svgns, "stop")). 
       attr({offset:"100%","stop-color":gradients[i][2]}); 
      $(grad).append(stopTop).append(stopBottom); 
      svg_defs.append(grad); 
      } 
      // #3366cc, #dc3912, #ff9900 - replace default colors with gradients 
      $("#visualization iframe").contents().find('rect[fill="#3366cc"]').attr({'fill':'url(#blue)','stroke-width':0.4,'stroke':'#000000'}); 
      $("#visualization iframe").contents().find('rect[fill="#dc3912"]').attr({'fill':'url(#blue)','stroke-width':0.4,'stroke':'#000000'}); 
      $("#visualization iframe").contents().find('rect[fill="#ff9900"]').attr({'fill':'url(#blue)','stroke-width':0.4,'stroke':'#000000'}); 
     }); 

     observer.observe(chartDiv, { 
      childList: true, 
      subtree: true 
     }); 

     // Create and draw the visualization. 
     visualization.draw(data,{width:600, height:400}); 
     } 
     google.setOnLoadCallback(drawVisualization); 
    </script> 
    </head> 
    <body style="font-family: Arial;border: 0 none;"> 
    <div id="visualization" style="width: 600px; height: 400px;"></div> 
    </body> 
</html> 
+0

[這裏是一個工作示例,使用MutationObserver更改甘特圖上的條形顏色](http://stackoverflow.com/a/40655754/5090771) – WhiteHat 2016-11-19 00:17:23