Solved

Controlling colour of pie chart slices

Posted on 2008-10-17
8
846 Views
Last Modified: 2013-11-27
I'm using a microsoft graph object to display a pie chart on a form to represent the proportion of red amber and green and black indicators in a table.  The red slice needs to be coloured red, the green slice needs to be green etc... The problem is getting the slices to appear as the right colours.  Either they come up as different colours when the form is loaded on different occaisions, or the displayed colours have no bearing to the RGB colour value that is being specified.   The code snippet below, which is run on the current event (have tried load activate etc) shows the technique I'm trying to use, basically display the data labels, identify the text of the label, apply the corresponding colour and then hide the labels again... sounds easy doesn't it ?
The technique I'm trying at the moment is to define the colour palette and then use the colorindex property to specify the required colour.  However this doesn't work at all.

My Previous attempt used the .Interior.Color property of the point object and this just about worked when I was using raw RGB colours e.g. RGB(255,0,0) for red etc, however even this didn't always work i.e. when the routine was first run the colours where wrong and it had to be run twice for the colours to be corrected.  In any case these colours are too intense, so I defined my own colours
Public Const kpiRed = 8947967
Public Const kpiGreen = 10678678
Public Const kpiAmber = 7972351
However when these were applied to the .Interior.Color the results were complete rubbish, so I'm wondering if there is a better way ...

Sub SetPie()
Dim x As Long
Dim p As Graph.Point
 
Me.Graph3.Object.Refresh
Me.Graph3.Object.Colors(1) = kpiRed
Me.Graph3.Object.Colors(2) = kpiGreen
Me.Graph3.Object.Colors(3) = kpiAmber
Me.Graph3.Object.Colors(4) = 0
 
DoEvents
 
For x = 1 To Me.Graph3.SeriesCollection.Count
 
    For Each p In Me.Graph3.SeriesCollection(x).Points
        p.ApplyDataLabels ShowCategoryName:=True
        'MsgBox p.DataLabel.Text
        Select Case p.DataLabel.Text
        Case "red"
            'p.Interior.Color = kpiRed
            'p.Interior.Color = 255
            p.Interior.ColorIndex = 1
        Case "amber"
            p.Interior.ColorIndex = 3
        Case "green"
            p.Interior.ColorIndex = 2
        Case "black"
            p.Interior.ColorIndex = 4
        End Select
            
        p.HasDataLabel = False
    DoEvents
    Next
    
 
Next
 
 
End Sub

Open in new window

0
Comment
Question by:Natchiket
  • 4
  • 3
8 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 400 total points
ID: 22739859
try using the qbcolor function

p.Interior.Color =qbcolor(4)    'red


The color argument has these settings:

Number Color Number Color
0 Black 8 Gray
1 Blue 9 Light Blue
2 Green 10 Light Green
3 Cyan 11 Light Cyan
4 Red 12 Light Red
5 Magenta 13 Light Magenta
6 Yellow 14 Light Yellow
7 White 15 Bright White
0
 
LVL 17

Author Comment

by:Natchiket
ID: 22740333
hmmm well it kind of works (like the RGB function) although when the form first opens one of the colours (black) is wrong, although it comes right when I re run the function.  At least qbcolor allows the colours to be a bit  muted which is an improvement ...
0
 
LVL 17

Author Comment

by:Natchiket
ID: 22740731
ok slight progress.  I've set the timer interval to 500
the following code corrects the graph before anyone's had a good time to look at it

Private Sub Form_Timer()
SetPie
Me.TimerInterval = 0
End Sub

Although I'm surprised it allows the interval to be set back to 0 but seems to work
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 17

Author Comment

by:Natchiket
ID: 22740839
Interestingly an initial timer interval much less than 500, e.g. 100, 200 etc doesn't seem to work
0
 
LVL 74

Assisted Solution

by:Jeffrey Coachman
Jeffrey Coachman earned 100 total points
ID: 22747109
Why not use the chart Wizard in Access?

It always keeps the same color scheme.

JeffCoachman
0
 
LVL 17

Author Comment

by:Natchiket
ID: 22795199
Hi Jeff it may keep the same colour scheme, but the colours it allocates to pie chart points will vary according to wether the value data exists or not, etc..  To test this create a simple one column table with a text field and enter the values Red, Red, Green, Blue etc .  If you then create a pie chart using the wizard, with the slice size depending on the count of each colour and get it to colour the points according their names, it looks great.  However if you then start modifying the data so that all the reds are replaced by green etc, you'll soon find that it switches the slice colours ... not what I'm after.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22798408
Oh,

OK, I misunderstood.

Jeff
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 22799676
Natchiket,

Thanks buddy, but you know me, ..if my post did not explicitly help you out then you really don't have to give me any points.

If you want to change this, and give Cap all the points, I have no problem.
;-)

Enjoy your weekend.
;-)

Jeff
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Some sers suddenly getting error popup msg 28 89
Report with several filters - Issue with query? 3 23
is Microsoft Access going to Die? 9 49
Filter a form 8 15
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question