Skip to content

Commit

Permalink
Touchups
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Dec 17, 2015
1 parent 48c1481 commit b3edc07
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 71 deletions.
71 changes: 11 additions & 60 deletions panoramix/bin/panoramix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ from panoramix import app
from flask.ext.migrate import MigrateCommand
from panoramix import db
from flask.ext.appbuilder import Base
from sqlalchemy import Column, Integer, String, Table, DateTime, Float
from sqlalchemy import Column, Integer, String, Table, DateTime
from panoramix import models, utils

config = app.config
Expand Down Expand Up @@ -59,63 +59,6 @@ def init():
def load_examples(sample):
"""Loads a set of Slices and Dashboards and a supporting dataset """
print("Loading examples into {}".format(db))
session = db.session()
Country = Table(
"countries", Base.metadata,
Column("id", Integer, primary_key=True),
Column("ds", DateTime, default=datetime.now()),
Column("name", String(255)),
Column("cca2", String(2)),
Column("cca3", String(3)),
Column("cioc", String(3)),
Column("capital", String(255)),
Column("lat", Float),
Column("lng", Float),
Column("area", Float),
)
try:
Country.drop(db.engine)
except:
pass
Country.create(db.engine)

filepath = os.path.join(config.get("BASE_DIR"), 'data/countries.json')
with open(filepath, 'r') as f:
d = {}
for c in json.load(f):
if not c['latlng']:
continue
db.engine.execute(
Country.insert(),
name=c['name']['common'].encode('utf8'),
cca2=c['cca2'],
cca3=c['cca3'],
cioc=c['cioc'],
capital=c['capital'],
lat=c['latlng'][0],
lng=c['latlng'][1],
area=c['area'],
)
d[c['cca3']] = dict(
name=c['name']['common'],
cca2=c['cca2'],
cca3=c['cca3'],
cioc=c['cioc'],
capital=c['capital'],
lat=c['latlng'][0],
lng=c['latlng'][1],
area=c['area'],
)
#print(json.dumps(d, indent=4))
print("Creating database reference")
DB = models.Database
dbobj = session.query(DB).filter_by(database_name='main').first()
if not dbobj:
dbobj = DB(database_name="main")
print(config.get("SQLALCHEMY_DATABASE_URI"))
dbobj.sqlalchemy_uri = config.get("SQLALCHEMY_DATABASE_URI")
session.add(dbobj)
session.commit()


BirthNames = Table(
Expand All @@ -136,8 +79,8 @@ def load_examples(sample):
pass

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

print("Creating database reference")
DB = models.Database
dbobj = session.query(DB).filter_by(database_name='main').first()
if not dbobj:
dbobj = DB(database_name="main")
print(config.get("SQLALCHEMY_DATABASE_URI"))
dbobj.sqlalchemy_uri = config.get("SQLALCHEMY_DATABASE_URI")
session.add(dbobj)
session.commit()

print("Creating table reference")
TBL = models.SqlaTable
Expand Down
12 changes: 12 additions & 0 deletions panoramix/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ def __init__(self, viz):
'90 days ago',
'1 year ago'])
),
'max_bubble_size': FreeFormSelectField(
'Max Bubble Size', default="25",
choices=self.choicify([
'5',
'10',
'15',
'25',
'50',
'75',
'100',
])
),
'row_limit':
FreeFormSelectField(
'Row limit',
Expand Down
20 changes: 13 additions & 7 deletions panoramix/static/widgets/viz_world_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,27 @@ function viz_world_map(data_attribute) {
return '';
done();
}
var ext = d3.extent(json.data, function(d){return d.metric});
var extRadius = d3.extent(json.data, function(d){return d.radius});
var ext = d3.extent(json.data, function(d){return d.m1});
var extRadius = d3.extent(json.data, function(d){return d.m2});
var radiusScale = d3.scale.linear()
.domain([extRadius[0], extRadius[1]])
.range([1, 40]);
.range([1, data_attribute.form_data.max_bubble_size]);
json.data.forEach(function(d){
d.radius = radiusScale(d.radius);
d.radius = radiusScale(d.m2);
})
var colorScale = d3.scale.linear()
.domain([ext[0], ext[1]])
.range(["#FFF", "black"]);
var d = {};
for (var i=0; i<json.data.length; i++){
var country = json.data[i];
d[country.country] = colorScale(country.metric);
country['fillColor'] = colorScale(country.m1);
d[country.country] = country;
}
f = d3.format('.3s');
var map = new Datamap({
element: document.getElementById(data_attribute.token),
data: json.data,
fills: {
defaultFill: 'grey'
},
Expand All @@ -48,15 +51,18 @@ function viz_world_map(data_attribute) {
highlightBorderColor: 'black',
highlightFillColor: '#005a63',
highlightBorderWidth: 1,
popupTemplate: function(geo, data) {
return '<div class="hoverinfo"><strong>' + data.name + '</strong><br>'+ f(data.m1) + '</div>';
},
},
bubblesConfig: {
borderWidth: 1,
borderOpacity: 1,
borderColor: '#005a63',
popupOnHover: true,
radius: null,
popupTemplate: function(geography, data) {
return '<div class="hoverinfo"><strong>' + data.country + '</strong></div>';
popupTemplate: function(geo, data) {
return '<div class="hoverinfo"><strong>' + data.name + '</strong><br>'+ f(data.m2) + '</div>';
},
fillOpacity: 0.5,
animate: true,
Expand Down
9 changes: 5 additions & 4 deletions panoramix/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,14 +982,14 @@ class WorldMapViz(BaseViz):
'entity',
'country_fieldtype',
'metric',
'secondary_metric',
)
},
{
'label': 'Bubbles',
'fields': (
('show_bubbles', None),
'secondary_metric',
'max_bubble_size',
)
})
form_overrides = {
Expand Down Expand Up @@ -1021,13 +1021,13 @@ def get_json_data(self):
secondary_metric = self.form_data.get('secondary_metric')
if metric == secondary_metric:
ndf = df[cols]
ndf['metric'] = df[metric]
ndf['radius'] = df[metric]
ndf['m1'] = df[metric]
ndf['m2'] = df[metric]
else:
cols += [metric, secondary_metric]
ndf = df[cols]
df = ndf
df.columns = ['country', 'metric', 'radius']
df.columns = ['country', 'm1', 'm2']
d = df.to_dict(orient='records')
for row in d:
country = countries.get(
Expand All @@ -1036,6 +1036,7 @@ def get_json_data(self):
row['country'] = country['cca3']
row['latitude'] = country['lat']
row['longitude'] = country['lng']
row['name'] = country['name']
else:
row['country'] = "XXX"
return dumps(d)
Expand Down

0 comments on commit b3edc07

Please sign in to comment.