Reputation: 81
I would like to be able to control the color of the sections in a pie chart programmically. Ideally my chart would be based on a 3-column table with the columns being: The Data Value, The Label, and the Pie Chart Color Value. The color values would be that same numbers one sees in Access form properties.
Thanks,
Steve
Upvotes: 8
Views: 6779
Reputation: 675
Maybe it can be done in using VBA, but here I propose another solution that let you do this using JavaScript. You could use a free Excel add-in called Funfun that allows you to use JavaScript code directly in Excel. That being said, you are able to use libraries like HighCharts.js or D3.js to draw a pie chart and control the color individually. Here is an example that I made based on your description.
The colors of different parts of the pie chart are determined by the data of the third column in the spreadsheet. In this example, I used HighCharts.js to control draw this chart. The specific code that controls the color could be seen as below.
var pieColors =[];
for(var i=1;i<data.length;i++){
switch(parseInt(data[i][2])){
case 0:
pieColors.push('blue');
break;
case 1:
pieColors.push('green');
break;
case 2:
pieColors.push('orange');
break;
case 3:
pieColors.push('purple');
break;
case 4:
pieColors.push('red');
break;
case 5:
pieColors.push('brown');
break;
}
}
In this example, I just manually picked six colors, but you could easily generate random color or color in a specific range by JavaScript code.
The Funfun add-in also has an online editor in which you could test with your code and result. You could check the detailed code and this example on the link below.
https://www.funfun.io/1/#/edit/5a4f4680c3a8a526caeec989
Once you are satisfied with your result, you could easily load the result into your Excel using the link above. But of course, first, you need to add the Funfun add-in into your Excel by Insert - Office Add-ins. Here are some screenshots showing how you load the example into you Excel.
Disclosure: I'm a developer of Funfun
Upvotes: 0
Reputation: 5071
First off, I know it can be done. BeGraphic have a freebie Excel add-in with a feature to set the chart element colours in the spreadhseet. See:
This use an add-in. I must say the results are impressive. For myself I needed something more Excel-native, many others there will find this option top-shelf.
While the VB option from Remou and begraphic's add-in do the deed. For me, or the current project at least, I need an Excel based solution (as much as is practical and is possible). In the meantime, you fine people and myself can find work-arounds via VB, VBA, .Net and/or macros. Visit Jon P's site for available options.
If I discover 'more' -- I'll pass it back on this query.
aloha,
Will
Upvotes: 0
Reputation: 91376
Perhaps something on these lines?
Dim ws As Worksheet
Dim sh As Shape
Set ws = Sheet2
Set sh = ws.Shapes.AddChart '.Select
With sh.Chart
.ChartType = xlPie
.SetSourceData Source:=Range("Sheet1!$A$1:$B$3")
For i = 1 To .SeriesCollection(1).Points.Count
With .SeriesCollection(1).Points(i).Format.Fill
.ForeColor.RGB = Range("C" & i).Interior.Color
.Solid
End With
Next
End With
This will allow you to add a colour to a cell using the fill button and have it used for a segment.
Upvotes: 2
Reputation: 61056
Sub a()
ActiveSheet.ChartObjects("Chart 1").Activate
ActiveChart.SeriesCollection(1).Points.Item(1).Interior.ColorIndex = 7
End Sub
You can learn the color represented by ColorIndex with the trick I posted in This Other Answer
Upvotes: 2