0

I am trying to construct a pie chart with flot, and getting the json data from server as below

[
  {"transactions": 7, "products__name": "mark"}, 
  {"transactions": 12, "products__name": "vicky"}, 
  {"transactions": 30, "products__name": "daniel"},   
  {"transactions": 6, "products__name": "hurray "}]

So i need to format the above list of dictionaries in to below format so that i can use that to directly insert to flot pie chat placeholder

data = [
    { label: "mark",  data: 7, color: "#4572A7"},
    { label: "vicky",  data: 12, color: "#80699B"},
    { label: "daniel",  data: 30, color: "#3D96AE"},
    { label: "hurray",  data: 6, color: "#89A54E"},
];

Actually i just want to do this with jquery, by using foreach or something else..

2
  • Where did the color values come from? Are they randomly chosen? Commented Nov 14, 2013 at 19:01
  • I actually i want to add the color manually, so since i know there will be only 5 items in the list for sure, manually i want to assign color to each item in the list Commented Nov 14, 2013 at 19:06

1 Answer 1

3
lst = [
  {"transactions": 7, "products__name": "mark"}, 
  {"transactions": 12, "products__name": "vicky"}, 
  {"transactions": 30, "products__name": "daniel"},   
  {"transactions": 6, "products__name": "hurray "}]
colors = ["#4572A7", "#80699B", "#3D96AE", "#89A54E"]
data = []

for i, c in zip(lst, colors):
   data.append({'label': i['products__name'], 'data': i["transactions"], 'color': c,})
Sign up to request clarification or add additional context in comments.

7 Comments

sorry i want to do this with jquery, i will get only five items in the list, so i want to arrange the color fixed to the records
I added colour to them.
i just want to do this logic with jquery not python
Why you add python tag to your question?
I added python because, i am getting the data in python right
|

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.