django-chartit highcharts options object "chart" not displaying subtitle - python

I get different results in the displayed chart when I use the highcharts option "chart" in chart_options. Example 1 will display the subtitle but not the background color. Example 2 will show the background color but not the subtitle. Anyone else encountered this behavior?
Python v2.7.5
Django v1.10
django-chartit v0.2.7
django-highcharts v0.1.7
Example 1: displays subtitle, not backgroundColor
#Create the PivotChart object
site_prod_pivotcht = PivotChart(
datasource = site_prod_ds,
series_options = [
{'options':{
'type': 'column',
'stacking': False},
'terms': [
'prod_value',
'wx_adj_value']}
],
chart_options =
{'title': {
'text': 'Actual versus Wx Adjusted Production Data'},
'subtitle': {
'text': report_range},
'backgroundColor': '#7FFFD4',
'xAxis': {
'title': {
'text': 'Group:Sites'}}
}
Example 2: displays backgroundColor, not subtitle
#Create the PivotChart object
site_prod_pivotcht = PivotChart(
datasource = site_prod_ds,
series_options = [
{'options':{
'type': 'column',
'stacking': False},
'terms': [
'prod_value',
'wx_adj_value']}
],
chart_options =
{'chart':{
'title': {
'text': 'Actual versus Wx Adjusted Production Data'},
'subtitle': {
'text': report_range},
'backgroundColor': '#7FFFD4',
'xAxis': {
'title': {
'text': 'Group:Sites'}}}
}

After a bit more trial and error, I found the following works as intended.
chart_options =
{'chart':{
'backgroundColor': '#7FFFD4',
'title': {
'text': 'Actual versus Wx Adjusted Production Data'}},
'subtitle': {
'text': report_range},
'credits': {
'enabled': False},
'xAxis': {
'title': {
'text': 'Group:Sites'}}
}

Related

How can I print specific integer variables in a nested dictionary by using Python?

This is my first question :)
I loop over a nested dictionary to print specific values. I am using the following code.
for i in lizzo_top_tracks['tracks']:
print('Track Name: ' + i['name'])
It works for string variables, but does not work for other variables. For example, when I use the following code for the date variable:
for i in lizzo_top_tracks['tracks']:
print('Album Release Date: ' + i['release_date'])
I receive a message like this KeyError: 'release_date'
What should I do?
Here is a sample of my nested dictionary:
{'tracks': [{'album': {'album_type': 'album',
'artists': [{'external_urls': {'spotify': 'https://open.spotify.com/artist/56oDRnqbIiwx4mymNEv7dS'},
'href': 'https://api.spotify.com/v1/artists/56oDRnqbIiwx4mymNEv7dS',
'id': '56oDRnqbIiwx4mymNEv7dS',
'name': 'Lizzo',
'type': 'artist',
'uri': 'spotify:artist:56oDRnqbIiwx4mymNEv7dS'}],
'external_urls': {'spotify': 'https://open.spotify.com/album/74gSdSHe71q7urGWMMn3qB'},
'href': 'https://api.spotify.com/v1/albums/74gSdSHe71q7urGWMMn3qB',
'id': '74gSdSHe71q7urGWMMn3qB',
'images': [{'height': 640,
'width': 640}],
'name': 'Cuz I Love You (Deluxe)',
'release_date': '2019-05-03',
'release_date_precision': 'day',
'total_tracks': 14,
'type': 'album',
'uri': 'spotify:album:74gSdSHe71q7urGWMMn3qB'}]}
The code you posted isn't syntactically correct; running it through a Python interpreter gives a syntax error on the last line. It looks like you lost a curly brace somewhere toward the end. :)
I went through it and fixed up the white space to make the structure easier to see; the way you had it formatted made it hard to see which keys were at which level of nesting, but with consistent indentation it becomes much clearer:
lizzo_top_tracks = {
'tracks': [{
'album': {
'album_type': 'album',
'artists': [{
'external_urls': {
'spotify': 'https://open.spotify.com/artist/56oDRnqbIiwx4mymNEv7dS'
},
'href': 'https://api.spotify.com/v1/artists/56oDRnqbIiwx4mymNEv7dS',
'id': '56oDRnqbIiwx4mymNEv7dS',
'name': 'Lizzo',
'type': 'artist',
'uri': 'spotify:artist:56oDRnqbIiwx4mymNEv7dS'
}],
'external_urls': {
'spotify': 'https://open.spotify.com/album/74gSdSHe71q7urGWMMn3qB'
},
'href': 'https://api.spotify.com/v1/albums/74gSdSHe71q7urGWMMn3qB',
'id': '74gSdSHe71q7urGWMMn3qB',
'images': [{'height': 640, 'width': 640}],
'name': 'Cuz I Love You (Deluxe)',
'release_date': '2019-05-03',
'release_date_precision': 'day',
'total_tracks': 14,
'type': 'album',
'uri': 'spotify:album:74gSdSHe71q7urGWMMn3qB'
}
}]
}
So the first (and only) value you get for i in lizzo_top_tracks['tracks'] is going to be this dictionary:
i = {
'album': {
'album_type': 'album',
'artists': [{
'external_urls': {
'spotify': 'https://open.spotify.com/artist/56oDRnqbIiwx4mymNEv7dS'
},
'href': 'https://api.spotify.com/v1/artists/56oDRnqbIiwx4mymNEv7dS',
'id': '56oDRnqbIiwx4mymNEv7dS',
'name': 'Lizzo',
'type': 'artist',
'uri': 'spotify:artist:56oDRnqbIiwx4mymNEv7dS'
}],
'external_urls': {
'spotify': 'https://open.spotify.com/album/74gSdSHe71q7urGWMMn3qB'
},
'href': 'https://api.spotify.com/v1/albums/74gSdSHe71q7urGWMMn3qB',
'id': '74gSdSHe71q7urGWMMn3qB',
'images': [{'height': 640, 'width': 640}],
'name': 'Cuz I Love You (Deluxe)',
'release_date': '2019-05-03',
'release_date_precision': 'day',
'total_tracks': 14,
'type': 'album',
'uri': 'spotify:album:74gSdSHe71q7urGWMMn3qB'
}
}
The only key in this dictionary is 'album', the value of which is another dictionary that contains all the other information. If you want to print, say, the album release date and a list of the artists' names, you'd do:
for track in lizzo_top_tracks['tracks']:
print('Album Release Date: ' + track['album']['release_date'])
print('Artists: ' + str([artist['name'] for artist in track['album']['artists']]))
If these are dictionaries that you're building yourself, you might want to remove some of the nesting layers where there's only a single key, since they just make it harder to navigate the structure without giving you any additional information. For example:
lizzo_top_albums = [{
'album_type': 'album',
'artists': [{
'external_urls': {
'spotify': 'https://open.spotify.com/artist/56oDRnqbIiwx4mymNEv7dS'
},
'href': 'https://api.spotify.com/v1/artists/56oDRnqbIiwx4mymNEv7dS',
'id': '56oDRnqbIiwx4mymNEv7dS',
'name': 'Lizzo',
'type': 'artist',
'uri': 'spotify:artist:56oDRnqbIiwx4mymNEv7dS'
}],
'external_urls': {
'spotify': 'https://open.spotify.com/album/74gSdSHe71q7urGWMMn3qB'
},
'href': 'https://api.spotify.com/v1/albums/74gSdSHe71q7urGWMMn3qB',
'id': '74gSdSHe71q7urGWMMn3qB',
'images': [{'height': 640, 'width': 640}],
'name': 'Cuz I Love You (Deluxe)',
'release_date': '2019-05-03',
'release_date_precision': 'day',
'total_tracks': 14,
'type': 'album',
'uri': 'spotify:album:74gSdSHe71q7urGWMMn3qB'
}]
This structure allows you to write the query the way you were originally trying to do it:
for album in lizzo_top_albums:
print('Album Release Date: ' + album['release_date'])
print('Artists: ' + str([artist['name'] for artist in album['artists']]))
Much simpler, right? :)

How to select a specific dictionary from an API request?

I am able to get a dictionary from my request function. Here's what I get:
{
'collaborative': False,
'external_urls': {
'spotify': 'http://open.spotify.com/user/dashrif/playlist/3LEoetnegEv2Q8jdmB8TER'
},
'href': 'https://api.spotify.com/v1/users/dashrif/playlists/3LEoetnegEv2Q8jdmB8TER',
'id': '3LEoetnegEv2Q8jdmB8TER',
'images': [{
'height': 640,
'url': 'https://mosaic.scdn.co/640/0cd0508f78c5e5f6e2b01b3009753083c7977270527f35929eff151f80bcabec17b2fb9383da342b32d7d3432ff965abb01f706ec2efc38282a11b45d088e352f19eebb53874fcdc4366ff4249da45fe',
'width': 640
},
{
'height': 300,
'url': 'https://mosaic.scdn.co/300/0cd0508f78c5e5f6e2b01b3009753083c7977270527f35929eff151f80bcabec17b2fb9383da342b32d7d3432ff965abb01f706ec2efc38282a11b45d088e352f19eebb53874fcdc4366ff4249da45fe',
'width': 300
},
{
'height': 60,
'url': 'https://mosaic.scdn.co/60/0cd0508f78c5e5f6e2b01b3009753083c7977270527f35929eff151f80bcabec17b2fb9383da342b32d7d3432ff965abb01f706ec2efc38282a11b45d088e352f19eebb53874fcdc4366ff4249da45fe',
'width': 60
}],
'name': 'Life Playlist Vol. I: The Fuck You Getting Hype For? You Still Broke',
'owner': {
'external_urls': {
'spotify': 'http://open.spotify.com/user/dashrif'
},
'href': 'https://api.spotify.com/v1/users/dashrif',
'id': 'dashrif',
'type': 'user',
'uri': 'spotify:user:dashrif'
},
'public': True,
'snapshot_id': 'PCG8b/CxCfaCjX0mmFMZ3T9NUsJC1sz5MVAXfQf3aefQhcAi4Zdm2k+3rySb/HLw',
'tracks': {
'href': 'https://api.spotify.com/v1/users/dashrif/playlists/3LEoetnegEv2Q8jdmB8TER/tracks',
'total': 63
},
'type': 'playlist',
'uri': 'spotify:user:dashrif:playlist:3LEoetnegEv2Q8jdmB8TER'
}
At least I hope this is a dictionary. Honestly at this point I've gotten so many errors I'm not too sure. Here's the code in question:
playlist_api_endpoint = "{}/playlists".format(profile_data["href"])
playlists_response = requests.get(playlist_api_endpoint,
headers=authorization_header)
playlist_data = json.loads(playlists_response.text)
display_arr = [profile_data] + playlist_data["items"]
return render_template("index.html",sorted_array=display_arr)
Basically I want to be able to filter out the last uri object and any other uri objects when adding new playlists. I've tried .items(), filtered dictionary, and a couple of other things I can't remember. If anyone has an idea of where I'm going wrong or how to to achieve my goal, I'd love some help. Thanks!

Plotting pie chart using Django Cahrtit

I am trying to plot pie chart using Django chartit. Below is my code. I have a model racktestresult with fields PassNumber, FailNumber and Date. I am looking for a formula to plot a pie chart of only the sum of PassNumbers and sum of FailNumbers.
ds = DataPool(
series=[
{
'options': {
'source': racktestresult.objects.values('Date').annotate(
Pass=Sum('PassNumbers'), Fail=Sum('FailNumbers'))},
'terms': [
'Date', 'Pass', 'Fail'
]
}
]
)
cht3 = Chart(
datasource=ds,
series_options=[
{
'options': {
'type': 'pie', 'stacking': False,
'options3d': {'enabled': True, 'alpha': 45, 'beta': 0}
}, 'terms': {'Date': ['Fail']}
}
],
chart_options={
'title': {'text': 'Pass/Fail - Pie Chart'}
}
)
What I am trying to get is just PassNumbers and FailNumbers from racktestresult model like below. How can I achieve that?

different color line per category chartit django 1.6

I am creating a loop of charts and I would like to keep specified color for each category so in every chart the same category is always represented by same color, but I am completly lost. Any help, thanks
cht = Chart(
datasource = ventasdata,
series_options =
[{'options':{'type': 'line','stacking': False, 'colors': ['#00cc00', '#ff5050','#00cc00']}, # Para area cambiar a 'type': 'area'
'terms':{
'mes': [
# 'cantidad_total_mes_2015',
'cantidad_acumulado_2015'],
'mes_2014': [
# 'cantidad_total_mes_2014',
'cantidad_acumulado_2014'],
'mes_2016': [
# 'cantidad_total_mes_2016',
'cantidad_acumulado_2016']
}}],
chart_options =
# {'colors': ['#00cc00','#ff5050','#3333ff'], 'title': {
{'mes': {
'color': '#00cc00'},
'cantidad_acumulado_2014': {
'color': ['#ff5050']},
'cantidad_acumulado_2016': {
'color': ['#3333ff']},
'terms':{
# 'colors': ['#00cc00','#ff5050','#3333ff']},
'colors': [['#00cc00'],'#ff5050','#3333ff']},
'title': {
# {'title': {
'text': titulo},
'xAxis': {
'title': {'text': 'MES'}}},
x_sortf_mapf_mts = (None, monthname, False))
return cht

how to set chartit-django to render a 3d pie chart

i am trying to render a 3d pie chart with chartit_django. i am on Django 1.6.
my code in views.py :
def sales_pie_chart(request):
ds = DataPool(
series=[
{
'options': {
'source': Sale.objects.values('sales_client__client_name').annotate(
sales_total_quantity=Sum('sales_total_quantity'))},
'terms': [
'sales_client__client_name', 'sales_total_quantity'
]
}
]
)
cht = Chart(
datasource=ds,
series_options=[
{
'options': {
'type': 'pie', 'stacking': False,
'options3d': {'enabled': True, 'alpha': 45, 'beta': 0}
}, 'terms': {'sales_client__client_name': ['sales_total_quantity']}
}
],
chart_options={
'title': {'text': 'Sales Report - Pie Chart'}
}
)
return render(request, 'accounting/charts.html', {'cht': cht})
i am betting on the 'options3d' setting but cant seem to find any documentation online.
this code would only give a flat pie chart.

Categories

Resources