0
votes

I'm new to Google Charts and I'm struggling to solve this.

I have a datatable (called "result" in the code)

Name   Liquidity percent
a        1.3      20%
b        2.0      20%
c        3.4      20%
d        4        20%
e        5        20%

My pie chart is set to show 5 segments of equal size - 20% - and each segment is blue

I have set a 'Number Range Filter' control wrapper to filter the liquidity - when i set the control to the range 1 to 4 the pie moves to 4 equal sized segments.

BUT... I don't want it to do this. Instead of 1 segment disappearing I want the 5 segments to remain visible and the colour of the filtered segment to change to be a different colour.

The aim being that I can see visually a total percentage that falls within the number filter.

EDIT: So I've had a mess about and this is as far as I've got incorporating dlaliberte's comment below.

function drawChart3(chartData3) {
  var result = google.visualization.arrayToDataTable(chartData3,false); // 'false' means that the first row contains labels, not data.
  var chart3 = new google.visualization.ChartWrapper({
  'chartType': 'PieChart',
  'containerId': 'chart3_div',
  'dataTable': result,
  'options': {
    'width': 500,
    'height': 500,
    'legend': {position: 'none'},
    'pieSliceText': 'none',
    'colors': ['blue']
     },
  'view': {'columns': [0 , 1]}
}); 




   var liquidityDT = new google.visualization.DataTable();
  // Declare columns
  liquidityDT.addColumn('number', 'Liquidity');
        // Add data.
  liquidityDT.addRows([
    [1],
    [2],                             
    [3],                           
    [4],
    [5],
    ]);

// Create a range slider, passing some options
  var liquidityRangeSlider = new google.visualization.ControlWrapper({
  'controlType': 'NumberRangeFilter',
  'containerId': 'filter3_div',
  'dataTable': liquidityDT,
  'options': {
    'filterColumnLabel': 'Liquidity',
    'minValue': 0,
    'maxValue': 5
  }
});

 liquidityRangeSlider.draw();
 chart3.draw();

 google.visualization.events.addListener(liquidityRangeSlider, 'statechange', setChartColor);

function setChartColor(){
  var state = liquidityRangeSlider.getState();
  var stateLowValue = state.lowValue;
  var stateHighValue = state.highValue;
  for (var i = 0; i < result.getNumberOfRows(); i++) {

    var testValue = result.getValue(i,2);
    if (testValue < stateLowValue || testValue > stateHighValue){
    alert("attempting to set colors")
    //this bit I have no clue how to change the color of the table row currently being iterated on 
    chart3.setOption({'colors' : ['red']});
    }
   }
   chart3.draw();
 }    

}

so it produces the pie chart with 5 blue segments. I can move the number filter and it fires the listener event but I can't get it to affect anything on the piechart (Chart3) - The code currently attempts just to change the whole chart to RED but that isn't even working never mind the just colouring the filtered segments

So how do I effect the changes into Chart3 and how do I only effect the filtered segments?.

any clues welcome?

thanks

1

1 Answers

0
votes

You can (and must, for your application) use a NumberRangeFilter outside of a Dashboard because it will otherwise always do the data filtering. Instead, just listen for the change events on the NumberRangeFilter, get its current state, and then iterate through the colors array for your chart to set the appropriate colors. You'll have to draw the chart again with the updated colors. Here is the loop to set the colors and redraw.

var colors = [];
for (var i = 0; i < result.getNumberOfRows(); i++) { 
  var color = (testValue < stateLowValue || testValue > stateHighValue) ? 'red' : 'blue';
  colors.push(color);
} 
chart3.setOption('colors', colors);
chart3.draw();