簡體   English   中英

谷歌餅圖動畫的切片顏色

[英]Google pie chart animation of the slices colors

我有一個Google餅圖,嘗試使用簡單的JavaScript代碼制作動畫。

我想更改餅圖的顏色。 為什么我的代碼無法正常工作?

google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);   

function drawChart() {
  var data1 = google.visualization.arrayToDataTable([
  ['Task', 'Hours per Day'],
  ['A1', 0],
  ['Failed',1],
  ['A2', 0],
  ['Passed', 3],     
]);

 var colors1 = ['#ef7777', '#ef7777', '#b2d284', '#b2d284', '#f6c7b6'];
 var colors2 =  ['#ff00ff', '#ff00ff', '#02d2ff', '#02d2ff', '#f6c7b6'];
 var colors3 = colors2;

var options1 = {'title':'Logic', 'width':'50%', 'height':'50%', legend:{position:'none'}, 'is3D':true,  
chartArea: {width: '70%', height: '70%'},  
colors: colors3,    
    'backgroundColor': '#fef7f8',
    pieSliceTextStyle: {
            color: '#000000',
            bold: true,
            fontSize:16
        }
  }; 

var chart1 = new google.visualization.PieChart(document.getElementById('piechart1')); 

  chart1.draw(data1, options1);
  var percent = 0;
        var handler = setInterval(function(){
            // values increment
            percent += 1;
            if (percent%2 == 1) {
            colors3 = colors1;
            }
            else
            {
            colors3 = colors2;
            }
            chart1.draw(data1, options1);

            if (percent > 74)

                clearInterval(handler);
        }, 333);  
}

因此,我在這里為餅圖設置2個帶有顏色集的數組。 第一個具有紅色和綠色,第二個具有藍色和紫色。

我希望使用“ setInterval”功能在這些顏色之間連續切換。

顏色選項對象的關鍵。 setInterval調用的回調函數將更改一個名為colors3的變量,但您不會將其分配給原始對象,因此將永遠不會使用它。

    if (percent % 2 == 1) {
      colors3 = colors1;
    } else {
      colors3 = colors2;
    }
    options1.colors = colors3; // here we're assigning it!
    chart1.draw(data1, options1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM