Skip to content

Commit b3edc07

Browse files
committedDec 17, 2015
Touchups
1 parent 48c1481 commit b3edc07

File tree

4 files changed

+41
-71
lines changed

4 files changed

+41
-71
lines changed
 

‎panoramix/bin/panoramix

+11-60
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ from panoramix import app
1212
from flask.ext.migrate import MigrateCommand
1313
from panoramix import db
1414
from flask.ext.appbuilder import Base
15-
from sqlalchemy import Column, Integer, String, Table, DateTime, Float
15+
from sqlalchemy import Column, Integer, String, Table, DateTime
1616
from panoramix import models, utils
1717

1818
config = app.config
@@ -59,63 +59,6 @@ def init():
5959
def load_examples(sample):
6060
"""Loads a set of Slices and Dashboards and a supporting dataset """
6161
print("Loading examples into {}".format(db))
62-
session = db.session()
63-
Country = Table(
64-
"countries", Base.metadata,
65-
Column("id", Integer, primary_key=True),
66-
Column("ds", DateTime, default=datetime.now()),
67-
Column("name", String(255)),
68-
Column("cca2", String(2)),
69-
Column("cca3", String(3)),
70-
Column("cioc", String(3)),
71-
Column("capital", String(255)),
72-
Column("lat", Float),
73-
Column("lng", Float),
74-
Column("area", Float),
75-
)
76-
try:
77-
Country.drop(db.engine)
78-
except:
79-
pass
80-
Country.create(db.engine)
81-
82-
filepath = os.path.join(config.get("BASE_DIR"), 'data/countries.json')
83-
with open(filepath, 'r') as f:
84-
d = {}
85-
for c in json.load(f):
86-
if not c['latlng']:
87-
continue
88-
db.engine.execute(
89-
Country.insert(),
90-
name=c['name']['common'].encode('utf8'),
91-
cca2=c['cca2'],
92-
cca3=c['cca3'],
93-
cioc=c['cioc'],
94-
capital=c['capital'],
95-
lat=c['latlng'][0],
96-
lng=c['latlng'][1],
97-
area=c['area'],
98-
)
99-
d[c['cca3']] = dict(
100-
name=c['name']['common'],
101-
cca2=c['cca2'],
102-
cca3=c['cca3'],
103-
cioc=c['cioc'],
104-
capital=c['capital'],
105-
lat=c['latlng'][0],
106-
lng=c['latlng'][1],
107-
area=c['area'],
108-
)
109-
#print(json.dumps(d, indent=4))
110-
print("Creating database reference")
111-
DB = models.Database
112-
dbobj = session.query(DB).filter_by(database_name='main').first()
113-
if not dbobj:
114-
dbobj = DB(database_name="main")
115-
print(config.get("SQLALCHEMY_DATABASE_URI"))
116-
dbobj.sqlalchemy_uri = config.get("SQLALCHEMY_DATABASE_URI")
117-
session.add(dbobj)
118-
session.commit()
11962

12063

12164
BirthNames = Table(
@@ -136,8 +79,8 @@ def load_examples(sample):
13679
pass
13780

13881
BirthNames.create(db.engine)
82+
session = db.session()
13983
filepath = os.path.join(config.get("BASE_DIR"), 'data/birth_names.csv.gz')
140-
'''
14184
with gzip.open(filepath, mode='rt') as f:
14285
bb_csv = csv.reader(f)
14386
for i, (state, year, name, gender, num) in enumerate(bb_csv):
@@ -162,8 +105,16 @@ def load_examples(sample):
162105
if sample and i>1000: break
163106
print("Done loading table!")
164107
print("-" * 80)
165-
'''
166108

109+
print("Creating database reference")
110+
DB = models.Database
111+
dbobj = session.query(DB).filter_by(database_name='main').first()
112+
if not dbobj:
113+
dbobj = DB(database_name="main")
114+
print(config.get("SQLALCHEMY_DATABASE_URI"))
115+
dbobj.sqlalchemy_uri = config.get("SQLALCHEMY_DATABASE_URI")
116+
session.add(dbobj)
117+
session.commit()
167118

168119
print("Creating table reference")
169120
TBL = models.SqlaTable

‎panoramix/forms.py

+12
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@ def __init__(self, viz):
203203
'90 days ago',
204204
'1 year ago'])
205205
),
206+
'max_bubble_size': FreeFormSelectField(
207+
'Max Bubble Size', default="25",
208+
choices=self.choicify([
209+
'5',
210+
'10',
211+
'15',
212+
'25',
213+
'50',
214+
'75',
215+
'100',
216+
])
217+
),
206218
'row_limit':
207219
FreeFormSelectField(
208220
'Row limit',

‎panoramix/static/widgets/viz_world_map.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,27 @@ function viz_world_map(data_attribute) {
1919
return '';
2020
done();
2121
}
22-
var ext = d3.extent(json.data, function(d){return d.metric});
23-
var extRadius = d3.extent(json.data, function(d){return d.radius});
22+
var ext = d3.extent(json.data, function(d){return d.m1});
23+
var extRadius = d3.extent(json.data, function(d){return d.m2});
2424
var radiusScale = d3.scale.linear()
2525
.domain([extRadius[0], extRadius[1]])
26-
.range([1, 40]);
26+
.range([1, data_attribute.form_data.max_bubble_size]);
2727
json.data.forEach(function(d){
28-
d.radius = radiusScale(d.radius);
28+
d.radius = radiusScale(d.m2);
2929
})
3030
var colorScale = d3.scale.linear()
3131
.domain([ext[0], ext[1]])
3232
.range(["#FFF", "black"]);
3333
var d = {};
3434
for (var i=0; i<json.data.length; i++){
3535
var country = json.data[i];
36-
d[country.country] = colorScale(country.metric);
36+
country['fillColor'] = colorScale(country.m1);
37+
d[country.country] = country;
3738
}
39+
f = d3.format('.3s');
3840
var map = new Datamap({
3941
element: document.getElementById(data_attribute.token),
42+
data: json.data,
4043
fills: {
4144
defaultFill: 'grey'
4245
},
@@ -48,15 +51,18 @@ function viz_world_map(data_attribute) {
4851
highlightBorderColor: 'black',
4952
highlightFillColor: '#005a63',
5053
highlightBorderWidth: 1,
54+
popupTemplate: function(geo, data) {
55+
return '<div class="hoverinfo"><strong>' + data.name + '</strong><br>'+ f(data.m1) + '</div>';
56+
},
5157
},
5258
bubblesConfig: {
5359
borderWidth: 1,
5460
borderOpacity: 1,
5561
borderColor: '#005a63',
5662
popupOnHover: true,
5763
radius: null,
58-
popupTemplate: function(geography, data) {
59-
return '<div class="hoverinfo"><strong>' + data.country + '</strong></div>';
64+
popupTemplate: function(geo, data) {
65+
return '<div class="hoverinfo"><strong>' + data.name + '</strong><br>'+ f(data.m2) + '</div>';
6066
},
6167
fillOpacity: 0.5,
6268
animate: true,

‎panoramix/viz.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -982,14 +982,14 @@ class WorldMapViz(BaseViz):
982982
'entity',
983983
'country_fieldtype',
984984
'metric',
985-
'secondary_metric',
986985
)
987986
},
988987
{
989988
'label': 'Bubbles',
990989
'fields': (
991990
('show_bubbles', None),
992991
'secondary_metric',
992+
'max_bubble_size',
993993
)
994994
})
995995
form_overrides = {
@@ -1021,13 +1021,13 @@ def get_json_data(self):
10211021
secondary_metric = self.form_data.get('secondary_metric')
10221022
if metric == secondary_metric:
10231023
ndf = df[cols]
1024-
ndf['metric'] = df[metric]
1025-
ndf['radius'] = df[metric]
1024+
ndf['m1'] = df[metric]
1025+
ndf['m2'] = df[metric]
10261026
else:
10271027
cols += [metric, secondary_metric]
10281028
ndf = df[cols]
10291029
df = ndf
1030-
df.columns = ['country', 'metric', 'radius']
1030+
df.columns = ['country', 'm1', 'm2']
10311031
d = df.to_dict(orient='records')
10321032
for row in d:
10331033
country = countries.get(
@@ -1036,6 +1036,7 @@ def get_json_data(self):
10361036
row['country'] = country['cca3']
10371037
row['latitude'] = country['lat']
10381038
row['longitude'] = country['lng']
1039+
row['name'] = country['name']
10391040
else:
10401041
row['country'] = "XXX"
10411042
return dumps(d)

0 commit comments

Comments
 (0)
Please sign in to comment.