This commit is contained in:
Maxime Beauchemin 2015-12-16 20:45:09 -08:00
parent 48c1481caa
commit b3edc077dc
4 changed files with 41 additions and 71 deletions

View File

@ -12,7 +12,7 @@ from panoramix import app
from flask.ext.migrate import MigrateCommand from flask.ext.migrate import MigrateCommand
from panoramix import db from panoramix import db
from flask.ext.appbuilder import Base 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 from panoramix import models, utils
config = app.config config = app.config
@ -59,63 +59,6 @@ def init():
def load_examples(sample): def load_examples(sample):
"""Loads a set of Slices and Dashboards and a supporting dataset """ """Loads a set of Slices and Dashboards and a supporting dataset """
print("Loading examples into {}".format(db)) 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( BirthNames = Table(
@ -136,8 +79,8 @@ def load_examples(sample):
pass pass
BirthNames.create(db.engine) BirthNames.create(db.engine)
session = db.session()
filepath = os.path.join(config.get("BASE_DIR"), 'data/birth_names.csv.gz') filepath = os.path.join(config.get("BASE_DIR"), 'data/birth_names.csv.gz')
'''
with gzip.open(filepath, mode='rt') as f: with gzip.open(filepath, mode='rt') as f:
bb_csv = csv.reader(f) bb_csv = csv.reader(f)
for i, (state, year, name, gender, num) in enumerate(bb_csv): for i, (state, year, name, gender, num) in enumerate(bb_csv):
@ -162,8 +105,16 @@ def load_examples(sample):
if sample and i>1000: break if sample and i>1000: break
print("Done loading table!") print("Done loading table!")
print("-" * 80) 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") print("Creating table reference")
TBL = models.SqlaTable TBL = models.SqlaTable

View File

@ -203,6 +203,18 @@ class FormFactory(object):
'90 days ago', '90 days ago',
'1 year 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': 'row_limit':
FreeFormSelectField( FreeFormSelectField(
'Row limit', 'Row limit',

View File

@ -19,13 +19,13 @@ function viz_world_map(data_attribute) {
return ''; return '';
done(); done();
} }
var ext = d3.extent(json.data, function(d){return d.metric}); var ext = d3.extent(json.data, function(d){return d.m1});
var extRadius = d3.extent(json.data, function(d){return d.radius}); var extRadius = d3.extent(json.data, function(d){return d.m2});
var radiusScale = d3.scale.linear() var radiusScale = d3.scale.linear()
.domain([extRadius[0], extRadius[1]]) .domain([extRadius[0], extRadius[1]])
.range([1, 40]); .range([1, data_attribute.form_data.max_bubble_size]);
json.data.forEach(function(d){ json.data.forEach(function(d){
d.radius = radiusScale(d.radius); d.radius = radiusScale(d.m2);
}) })
var colorScale = d3.scale.linear() var colorScale = d3.scale.linear()
.domain([ext[0], ext[1]]) .domain([ext[0], ext[1]])
@ -33,10 +33,13 @@ function viz_world_map(data_attribute) {
var d = {}; var d = {};
for (var i=0; i<json.data.length; i++){ for (var i=0; i<json.data.length; i++){
var country = json.data[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({ var map = new Datamap({
element: document.getElementById(data_attribute.token), element: document.getElementById(data_attribute.token),
data: json.data,
fills: { fills: {
defaultFill: 'grey' defaultFill: 'grey'
}, },
@ -48,6 +51,9 @@ function viz_world_map(data_attribute) {
highlightBorderColor: 'black', highlightBorderColor: 'black',
highlightFillColor: '#005a63', highlightFillColor: '#005a63',
highlightBorderWidth: 1, highlightBorderWidth: 1,
popupTemplate: function(geo, data) {
return '<div class="hoverinfo"><strong>' + data.name + '</strong><br>'+ f(data.m1) + '</div>';
},
}, },
bubblesConfig: { bubblesConfig: {
borderWidth: 1, borderWidth: 1,
@ -55,8 +61,8 @@ function viz_world_map(data_attribute) {
borderColor: '#005a63', borderColor: '#005a63',
popupOnHover: true, popupOnHover: true,
radius: null, radius: null,
popupTemplate: function(geography, data) { popupTemplate: function(geo, data) {
return '<div class="hoverinfo"><strong>' + data.country + '</strong></div>'; return '<div class="hoverinfo"><strong>' + data.name + '</strong><br>'+ f(data.m2) + '</div>';
}, },
fillOpacity: 0.5, fillOpacity: 0.5,
animate: true, animate: true,

View File

@ -982,7 +982,6 @@ class WorldMapViz(BaseViz):
'entity', 'entity',
'country_fieldtype', 'country_fieldtype',
'metric', 'metric',
'secondary_metric',
) )
}, },
{ {
@ -990,6 +989,7 @@ class WorldMapViz(BaseViz):
'fields': ( 'fields': (
('show_bubbles', None), ('show_bubbles', None),
'secondary_metric', 'secondary_metric',
'max_bubble_size',
) )
}) })
form_overrides = { form_overrides = {
@ -1021,13 +1021,13 @@ class WorldMapViz(BaseViz):
secondary_metric = self.form_data.get('secondary_metric') secondary_metric = self.form_data.get('secondary_metric')
if metric == secondary_metric: if metric == secondary_metric:
ndf = df[cols] ndf = df[cols]
ndf['metric'] = df[metric] ndf['m1'] = df[metric]
ndf['radius'] = df[metric] ndf['m2'] = df[metric]
else: else:
cols += [metric, secondary_metric] cols += [metric, secondary_metric]
ndf = df[cols] ndf = df[cols]
df = ndf df = ndf
df.columns = ['country', 'metric', 'radius'] df.columns = ['country', 'm1', 'm2']
d = df.to_dict(orient='records') d = df.to_dict(orient='records')
for row in d: for row in d:
country = countries.get( country = countries.get(
@ -1036,6 +1036,7 @@ class WorldMapViz(BaseViz):
row['country'] = country['cca3'] row['country'] = country['cca3']
row['latitude'] = country['lat'] row['latitude'] = country['lat']
row['longitude'] = country['lng'] row['longitude'] = country['lng']
row['name'] = country['name']
else: else:
row['country'] = "XXX" row['country'] = "XXX"
return dumps(d) return dumps(d)