1

I am trying to make election map so I can draw chart and label with same chart color. So I have six election zone and running candidates may be up-to six or less. So if some election zone might have only two candidates so it should label only 2 not the empty value. Labeling should be stacked and should match with the color code of the charts. Here is my code which worked if all the column has value. On candidate 4 which has only 4 won,t label it. All the document is attached.

def FindLabel ( [Sheet1$.Candiate1], [Sheet1$.Candiate2], [Sheet1$.Candiate3], [Sheet1$.Candiate4], [Sheet1$.Candiate5], [Sheet1$.Candiate6], [Sheet1$.VoteCount1],[Sheet1$.VoteCount2],[Sheet1$.VoteCount3],[Sheet1$.VoteCount4],[Sheet1$.VoteCount5],[Sheet1$.VoteCount6] ):

if [Sheet1$.VoteCount1]> 0: [Sheet1$.Candiate1]= [Sheet1$.Candiate1]+" Vote Count= "+Sheet1$.VoteCount1]

if [Sheet1$.VoteCount2] > 0: [Sheet1$.Candiate2]= [Sheet1$.Candiate2]+" Vote Count= "+sheet1$.VoteCount2]

return "" +[Sheet1$.Candiate1]+"" +'\n'+""+[Sheet1$.Candiate2]+ ""enter image description hereenter image description here

1 Answer 1

1

It's a little unclear what you're asking, but if you're looking for labels to include all candidates with non-zero vote counts, something like this should work.

def FindLabel ( [Sheet1$.Candiate1], [Sheet1$.Candiate2], [Sheet1$.Candiate3], [Sheet1$.Candiate4], [Sheet1$.Candiate5], [Sheet1$.Candiate6], [Sheet1$.VoteCount1],[Sheet1$.VoteCount2],[Sheet1$.VoteCount3],[Sheet1$.VoteCount4],[Sheet1$.VoteCount5],[Sheet1$.VoteCount6] ):
    results = []
    if [Sheet1$.VoteCount1] > 0:
        results.append('{} Vote Count = {}'.format([Sheet1$.Candiate1], [Sheet1$.VoteCount1]))
    if [Sheet1$.VoteCount2] > 0:
        results.append('{} Vote Count = {}'.format([Sheet1$.Candiate2], [Sheet1$.VoteCount2]))
    if [Sheet1$.VoteCount3] > 0:
        results.append('{} Vote Count = {}'.format([Sheet1$.Candiate3], [Sheet1$.VoteCount3]))
    if [Sheet1$.VoteCount4] > 0:
        results.append('{} Vote Count = {}'.format([Sheet1$.Candiate4], [Sheet1$.VoteCount4]))
    if [Sheet1$.VoteCount5] > 0:
        results.append('{} Vote Count = {}'.format([Sheet1$.Candiate5], [Sheet1$.VoteCount5]))
    if [Sheet1$.VoteCount6] > 0:
        results.append('{} Vote Count = {}'.format([Sheet1$.Candiate6], [Sheet1$.VoteCount6]))
    return '\n'.join(results)

There are neater ways to do it, but hopefully this will do the job!

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.