0

I have created a Google Pie Chart using the following code:

var data = google.visualization.arrayToDataTable(array);
var options = {
  title: 'Meta Share',
      is3D: true,
      sliceVisibilityThreshold: .04,
      slices: {  6 : {offset: 0.2},
      },
    };

    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}

I want to select a slice of the pie chart dynamically depending on what my user is doing. I now have the following code:

var slice = 8;

I would now like to use this variable in the above code, however; replacing the '6' with the variable 'slice' does not work.

Any suggestions? :)

1 Answer 1

1

You can't use variables as keys in object literals, you'd have to first create the object, then use brackets to use the variable as a key

var slice  = 8;
var slices = {};

slices[slice] = {offset: 0.2};

var data = google.visualization.arrayToDataTable(array);
var options = {
    title  : 'Meta Share',
    is3D   : true,
    sliceVisibilityThreshold : .04,
    slices : slices
};

var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    chart.draw(data, options);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Or with calculated property names: var slice = 8, slices = { [slice]: { offset: 0.2 } }
@Andreas - yeah, I love those, but they currently only work in Chrome and FF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.