Permalink
Browse files

more demosite stuff

  • Loading branch information...
1 parent d3fcdda commit 4b043809932162187cb4f4aa50e0fbcb328f1ba1 @rhblind committed Oct 9, 2012
View
@@ -9,6 +9,7 @@
*.pyo
*.so
*.log
+*.db
.svn
_svn
.DS_Store
View
Binary file not shown.
@@ -32,14 +32,14 @@ def handle_noargs(self, **options):
# This data is really random and by no means correct!
p = os.path.join(settings.PROJECT_PATH, "demosite", "countries.txt")
with open(p, "r") as fname:
- countries = fname.readlines()
+ countries = map(lambda s: s.rstrip("\n"), fname.readlines())
for _, c in enumerate(countries):
code, name = c.split(":")
data = {
"country_name": name,
"country_code": code,
- "population": random.randint(1, 1000),
+ "population": random.randint(1, 300),
"fertility_rate": random.uniform(0.7, 7.2)
}
GeoData.objects.create(**data)
@@ -53,13 +53,14 @@ def handle_noargs(self, **options):
p = os.path.join(settings.PROJECT_PATH, "demosite", "names.txt")
with open(p, "r") as fname:
- names = fname.readlines()
+ names = map(lambda s: s.rstrip("\n"), fname.readlines())
for d in daterange(otherday, today):
for n in names:
data = {
"name": n,
- "number": random.randint(10, 100),
+ "number1": random.randint(10, 100),
+ "number2": random.randint(10, 100),
"date": d
}
OtherData.objects.create(**data)
View
@@ -13,7 +13,7 @@ class GeoData(models.Model):
country_name = models.CharField(max_length=100)
country_code = models.CharField(max_length=2)
population = models.PositiveIntegerField()
- fertility_rate = models.DecimalField(max_digits=4, decimal_places=3)
+ fertility_rate = models.FloatField()
class OtherData(models.Model):
@@ -25,6 +25,7 @@ class OtherData(models.Model):
objects = models.Manager()
name = models.CharField(max_length=20)
- number = models.IntegerField()
+ number1 = models.IntegerField()
+ number2 = models.IntegerField()
date = models.DateField()
@@ -29,10 +29,9 @@ h2 {
}
p {
- border: 1px solid #666;
overflow: hidden;
padding: 10px 0;
- text-align: center;
+ text-align: left;
}
.container_12,
@@ -42,3 +41,28 @@ p {
background-repeat: repeat-y;
margin-bottom: 20px;
}
+
+ul {
+ list-style-type: none;
+ padding: 0px;
+ margin: 0px;
+}
+
+ul li {
+ padding-left: 10px;
+}
+
+.note {
+ position: relative;
+ padding: 10px 15px 50px 15px;
+ margin: 0 auto;
+ background-color: #fff;
+ -webkit-box-shadow: 0 0 4px rgba(0, 0, 0, 0.2), inset 0 0 50px rgba(0, 0, 0, 0.1);
+ -moz-box-shadow: 0 0 4px rgba(0, 0, 0, 0.2), inset 0 0 50px rgba(0, 0, 0, 0.1);
+ box-shadow: 0 0 5px rgba(0, 0, 0, 0.2), inset 0 0 50px rgba(0, 0, 0, 0.1);
+}
+
+.note a {
+ color: #000;
+ text-decoration: underline;
+}
@@ -9,7 +9,6 @@
<title>django-gcharts demo site</title>
<meta name="description" content="django-gcharts demo site" />
- <meta name="viewport" content="width=device-width; initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="/static/960.css" media="all">
<link rel="stylesheet" type="text/css" href="/static/demosite.css" media="all">
@@ -30,40 +29,164 @@
height: 300,
};
- <!-- cloned option and adapted for "line_opt" -->
- line_opt = _clone(options);
- line_opt.title = "LineChart test";
+ <!-- Options for GeoChart -->
+ geo_opt = _clone(options);
+ geo_opt.width = "100%";
+ geo_opt.height = "100%";
- {% options line_opt %}
- kind: "LineChart",
- options: line_opt,
- {% endoptions %}
-
+ {% options geo_opt %}
+ kind: "GeoChart",
+ options: geo_opt,
+ {% endoptions %}
+
+ <!-- Options for AreaChart -->
+ area_opt = _clone(options);
+ area_opt.title = "Charting some random numbers"
+
+ {% options area_opt %}
+ kind: "AreaChart",
+ options: area_opt,
+ {% endoptions %}
+
+ <!-- Options for BarChart -->
+ bar_opt = _clone(area_opt)
+
+ {% options bar_opt %}
+ kind: "BarChart",
+ options: bar_opt,
+ {% endoptions %}
+
+ <!-- Options for LineChart -->
+ line_opt = _clone(area_opt)
+
+ {% options line_opt %}
+ kind: "LineChart",
+ options: line_opt,
+ {% endoptions %}
+
+ <!-- Options for ColumnChart -->
+ column_opt = _clone(area_opt)
+
+ {% options column_opt %}
+ kind: "ColumnChart",
+ options: column_opt,
+ {% endoptions %}
+
+ <!-- Options for ComboChart -->
+ combo_opt = _clone(area_opt)
+ combo_opt.seriesType = "bars";
+ combo_opt.series = {1: {type: "line"}};
+
+ {% options combo_opt %}
+ kind: "ComboChart",
+ options: combo_opt,
+ {% endoptions %}
+
+ <!-- Options for PieChart -->
+ pie_opt = _clone(options);
+
+ {% options pie_opt %}
+ kind: "PieChart",
+ options: pie_opt,
+ {% endoptions %}
+
+ <!-- Options for Table -->
+ table_opt = _clone(options);
+ table_opt.page = "enable";
+ table_opt.pageSize = "15";
+
+ {% options table_opt %}
+ kind: "Table",
+ options: table_opt,
+ {% endoptions %}
+
+ <!-- Render all charts -->
+ {% render "geo_chart" "geo_data" "geo_opt" %}
+ {% render "area_chart" "area_data" "area_opt" %}
+ {% render "pie_chart" "pie_data" "pie_opt" %}
+ {% render "bar_chart" "bar_data" "bar_opt" %}
+ {% render "line_chart" "line_data" "line_opt" %}
+ {% render "column_chart" "column_data" "column_opt" %}
+ {% render "combo_chart" "combo_data" "combo_opt" %}
+ {% render "table_chart" "table_data" "table_opt" %}
+
{% endgcharts %}
- <h2>django-gcharts demo site</h2>
+ <h1>Demo site</h1>
<div class="grid_12">
- <h2>GeoChart</h2>
+ <h2>GeoChart - Top 100 world pretend population</h2>
+ <div id="geo_chart">
+ <!-- Container for geo_chart -->
+ </div>
</div>
<div class="grid_6">
<h2>AreaChart</h2>
+ <div id="area_chart">
+ <!-- Container for area_chart -->
+ </div>
+ </div>
+
+ <div class="grid_6">
+ <h2>PieChart</h2>
+ <div id="pie_chart">
+ <!-- Container for pie_chart -->
+ </div>
</div>
<div class="grid_6">
<h2>BarChart</h2>
+ <div id="bar_chart">
+ <!-- Container for bar_chart -->
+ </div>
</div>
<div class="grid_6">
<h2>LineChart</h2>
+ <div id="line_chart">
+ <!-- Container for line_chart -->
+ </div>
</div>
<div class="grid_6">
<h2>ColumnChart</h2>
+ <div id="column_chart">
+ <!-- Container for column_chart -->
+ </div>
</div>
+ <div class="grid_6">
+ <h2>ComboChart</h2>
+ <div id="combo_chart">
+ <!-- Container for combo_chart -->
+ </div>
+ </div>
+
+ <div class="grid_6">
+ <h2>Table - Rat cake statistics</h2>
+ <div id="table_chart">
+ <!-- Container for table_chart -->
+ </div>
+ </div>
+
+ <div class="grid_6">
+ <h2>And so on..</h2>
+ <div class="note">
+ <h4>More resources</h4>
+ <ul>
+ <li><a href="https://developers.google.com/chart/interactive/docs/index">
+ Google Visualization API documentation</a></li>
+ <li><a href="https://github.com/rhblind/django-gcharts">
+ django-gcharts home at github</a></li>
+ </ul>
+ <p>
+ By now you probably has a pretty good idea about what this is about.
+ <br />Thanks for downloading django-gcharts =)
+ </p>
+ </div>
+ </div>
</div>
</div>
View
@@ -1,6 +1,56 @@
# Create your views here.
+from datetime import datetime
+from dateutil.relativedelta import relativedelta
from django.shortcuts import render_to_response
-
+from demosite.models import GeoData, OtherData
+from django.template.context import RequestContext
+from django.db.models.aggregates import Sum
def home(request):
- return render_to_response("home.html")
+ """
+ Gather some data for the demo site
+ """
+ if request.method == "GET":
+
+ # GeoChart demo
+ geo_qset = GeoData.gcharts.order_by("-population").all()[:100]
+ geo_data = geo_qset.values("country_name", "population", "fertility_rate") \
+ .to_json(labels={"country_name": "Country",
+ "population": "Population",
+ "fertility_rate": "Birth rate"},
+ formatting={"population": "{0:d} millions",
+ "fertility_rate": "{0:.3f}"},
+ order=("country_name", "population", "fertility_rate"))
+
+ # AreaChart
+ area_series_age = datetime.today() - relativedelta(months=1)
+ area_qset = OtherData.gcharts.filter(date__gte=area_series_age) \
+ .values("date").annotate(Sum("number1")) \
+ .annotate(Sum("number2")).order_by()
+ area_data = area_qset.order_by("-date").to_json(order=("date", "number1__sum",
+ "number2__sum"),
+ labels={"number1__sum": "A number",
+ "number2__sum": "Another number"})
+
+ # PieChart
+ pie_qset = OtherData.gcharts.values("name").annotate(Sum("number1")).order_by()
+ pie_data = pie_qset.order_by("name").to_json(order=("name", "number1__sum"),
+ formatting={"number1__sum": "{0:d} Sum total"})
+
+ # You get the idea....
+ bar_data = line_data = column_data = combo_data = area_data
+
+ # Table
+ table_series_age = datetime.today() - relativedelta(months=3)
+ table_qset = OtherData.gcharts.filter(date__gte=table_series_age) \
+ .values("date").annotate(num_baked=Sum("number1")) \
+ .annotate(num_eaten=Sum("number2")).order_by("-date")
+ table_data = table_qset.to_json(labels={"date": "Date",
+ "num_baked": "Rat cakes baked",
+ "num_eaten": "Rat cakes eaten"},
+ order=("date", "num_baked", "num_eaten"),
+ formatting={"num_baked": "{0:d} Kg",
+ "num_eaten": "{0:d} Kg"})
+
+ return render_to_response("home.html", locals(),
+ context_instance=RequestContext(request))
@@ -162,5 +162,5 @@
# django-gcharts settings
GOOGLECHARTS_API = '1.1'
-GOOGLECHARTS_PACKAGES = ['corechart']
+GOOGLECHARTS_PACKAGES = ["corechart", "gauge", "geochart", "table", "treemap"]

0 comments on commit 4b04380

Please sign in to comment.