Plotting pie chart using Django Cahrtit - python

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?

Related

Changing Dash chart series colors

I'm just starting to learn to use Dash and currently have a chart up displaying user engagement on my website. However,
I can't seem to figure out how to style Dash components when it comes to colors and the Dash documentation doesn't seem to have anything on the topic. How do I change a series' colors from the default blue/orange/green? The code for the graph in question is below`
dcc.Graph(
id='average_engagement_graph',
figure={
'data': [
{'x': list(avg_df['day']), 'y': list(avg_df['countMessageIn']),
'type': 'bar', 'name': 'Incoming Messages from Users'},
{'x': list(avg_df['day']), 'y': list(avg_df['countMessageOut']),
'type': 'bar', 'name': 'Outgoing Message by Bot'},
],
'layout': {
'title': 'Average User-Bot Engagement by Day of Week',
'xaxis': {
'title': 'Day of the Week'
},
'yaxis': {
'title': 'Average Number of Messages'
},
}
}
),
For bar, you've got to add it to a key named marker.
dcc.Graph(
id='average_engagement_graph',
figure={
'data': [
{'x': list(avg_df['day']), 'y': list(avg_df['countMessageIn']),
'type': 'bar', 'name': 'Incoming Messages from Users'},
'marker' : { "color" : your_color_array}
{'x': list(avg_df['day']), 'y': list(avg_df['countMessageOut']),
'type': 'bar', 'name': 'Outgoing Message by Bot'},
'marker' : { "color" : your_color_array}
],
'layout': {
'title': 'Average User-Bot Engagement by Day of Week',
'xaxis': {
'title': 'Day of the Week',
},
'yaxis': {
'title': 'Average Number of Messages'
},
}
}
),
EDIT
It seems they have done a lot of edit and you can also do this...
colors = ['lightslategray',] * 5
colors[1] = 'crimson'
fig = go.Figure(data=[go.Bar(
x=['Feature A', 'Feature B', 'Feature C',
'Feature D', 'Feature E'],
y=[20, 14, 23, 25, 22],
marker_color=colors # marker color can be a single color value or an iterable
)])

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

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'}}
}

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 change the series name using django-chartit?

I just got crazy about changing the series name using django-chartit. I have google the solutions but i cannot find it.
Here's my views.py. I even didn't know which place should i add the series' name attribute. So i just guess that might should be placed in the series_option . But as you have guessed, nothing has changed. And the series' name still remained "click time"
def userdata_chart_view(request):
userdata = \
DataPool(
series=
[{'options': {
'source': User_Data.objects.filter(user=request.user.username)},
'terms': [
'user',
'word',
'click_times']}
])
cht = Chart(
datasource= userdata,
series_options =
[{'options':{
'type': 'column',
'stacking': False},
'terms':{
'word': ['click_times']
},
'name': '搜索次数',
}],
chart_options=
{
'title': {'text': '搜索频率'},
'xAxis': {'title': {'text': '词条'}},
'yAxis': {'title': {'text': '频率'}},
}
)
content1 = {'user_data_chart': cht}
return render(request, 'yigu/charts.html', content1)
I had the exact same difficulty, and found a way that I share with you : based on this example from the chartit demo, it looks that you can simply rename a field during the DataPool creation. I used the syntax from the example (though based on a PivotDataPool, but it looks ok for any DataPool...) by changing the 'terms' value from a list to a dictionnary. In this dictionnary keys are the customized names used in the charts objects, and values are the original field names.
For your example, it might look like this :
userdata = \
DataPool(
series=
[{'options': {
'source': User_Data.objects.filter(user=request.user.username)},
'terms': {
'user': 'user',
'word': 'word',
'<your customized name>': 'click_times'}}
])
cht = Chart(
datasource= userdata,
series_options =
[{'options':{
'type': 'column',
'stacking': False},
'terms':{
'word': ['<your customized name>']
},
...
I hope this works for you, I'll be happy to know if there are more conventional ways to do it...
PolRaguénès' answer pointed me in the right direction. It was just the bracketing in the DataPool definition for terms that had to be fixed:
userdata = \
DataPool(
series=
[{'options': {
'source': User_Data.objects.filter(user=request.user.username)},
'terms': [
'user': 'user',
'word': 'word',
{'<your customized name>': 'click_times'}]}
])

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