Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
hannansatopay committed Dec 2, 2019
1 parent c631003 commit 652eefc
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 101 deletions.
67 changes: 44 additions & 23 deletions roughviz/roughviz.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,67 @@
from jinja2 import Template
from IPython.core.display import HTML
from IPython.core.display import display, HTML
import random
import string
import json
import pkgutil
import pandas as pd


def generate_template(data, labels, values, **kwargs):
def generate_template(data, labels, values, plot_svg, **kwargs):
template = Template(data.decode("utf-8"))
id_name = ''.join(random.choice(string.ascii_lowercase) for i in range(10))
output = template.render(id_name = id_name,
labels = labels.tolist(),
values = values.tolist(),
labels = labels,
values = values,
kwargs = kwargs)
return HTML(output)
if(plot_svg):
svg_id = "svg"+id_name
script = """
<style>
div.output_area img, div.output_area svg{
height: 100%!important;
}
</style>
<script>
var e = document.getElementById('"""+id_name+"""');
var divCheckingInterval = setInterval(function(){
if(e.getElementsByTagName('svg').length){
clearInterval(divCheckingInterval);
e.getElementsByTagName('svg')[0].setAttribute("id", '"""+svg_id+"""');
var svgElement = document.getElementById('"""+svg_id+"""');
var svgString = new XMLSerializer().serializeToString(svgElement);
var decoded = unescape(encodeURIComponent(svgString));
var base64 = btoa(decoded);
var imgSource = `data:image/svg+xml;base64,${base64}`;
e.innerHTML = "<img id='svgplot'>";
document.getElementById('svgplot').src = imgSource;
}}, 1);
</script>
"""
display(HTML(output))
display(HTML(script))
else:
display(HTML(output))

def bar(labels, values, **kwargs):
def bar(labels, values, plot_svg = False, **kwargs):
data = pkgutil.get_data(__package__, 'templates/bar.html')
return generate_template(data, labels, values, **kwargs)
generate_template(data, labels.tolist(), values.tolist(), plot_svg, **kwargs)

def barh(labels, values, **kwargs):
def barh(labels, values, plot_svg = False, **kwargs):
data = pkgutil.get_data(__package__, 'templates/barh.html')
return generate_template(data, labels, values, **kwargs)
generate_template(data, labels.tolist(), values.tolist(), plot_svg, **kwargs)

def pie(labels, values, **kwargs):
def pie(labels, values, plot_svg = False, **kwargs):
data = pkgutil.get_data(__package__, 'templates/pie.html')
return generate_template(data, labels, values, **kwargs)
generate_template(data, labels.tolist(), values.tolist(), plot_svg, **kwargs)

def donut(labels, values, **kwargs):
def donut(labels, values, plot_svg = False, **kwargs):
data = pkgutil.get_data(__package__, 'templates/donut.html')
return generate_template(data, labels, values, **kwargs)
generate_template(data, labels.tolist(), values.tolist(), plot_svg, **kwargs)

def stackedbar(labels, values, **kwargs):
def stackedbar(labels, values, plot_svg = False, **kwargs):
data = pkgutil.get_data(__package__, 'templates/stackedbar.html')
template = Template(data.decode("utf-8"))
id_name = ''.join(random.choice(string.ascii_lowercase) for i in range(10))
data = []
content = []
for index, row in pd.concat([labels,values],axis=1).iterrows():
data.append(row.to_dict())
output = template.render(id_name = id_name,
labels = labels.name,
values = data,
kwargs = kwargs)
return HTML(output)
content.append(row.to_dict())
generate_template(data, labels.name, content, plot_svg, **kwargs)
26 changes: 10 additions & 16 deletions roughviz/templates/bar.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
<html>
<head>
<style>
.xAxis{{id_name}} .tick text
{
text-anchor: middle!important;
transform: rotate(0)!important;
}
#{{id_name}}{
height: {{kwargs.height|default(400)|int - 20}}px!important;
}
#{{id_name}} svg{
height: {{kwargs.height|default(400)|int - 20}}px!important;
}
</style>
</head>
<body>

<div id={{id_name|tojson}}></div>
Expand Down Expand Up @@ -52,9 +37,18 @@
xLabel: {{kwargs.xLabel|default("")|tojson}},
yLabel: {{kwargs.yLabel|default("")|tojson}},
height: {{kwargs.height|default(400)}},
width: {{kwargs.width|default(700)}}
width: {{kwargs.width|default(700)}},
interactive: {{kwargs.interactive|default(true)|tojson}}
}
);
var text = document.querySelectorAll(".xAxis{{id_name}} .tick text");
var i;
for (i = 0; i < text.length; i++) {
text[i].setAttribute("transform", "rotate(0)");
text[i].style.textAnchor="middle";
}
document.getElementById("{{id_name}}").style.height = "{{kwargs.height|default(400)|int - 20}}px";
document.querySelector("#{{id_name}} svg").style.height = "{{kwargs.height|default(400)|int - 20}}px";
});
</script>

Expand Down
26 changes: 10 additions & 16 deletions roughviz/templates/barh.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
<html>
<head>
<style>
.xAxis{{id_name}} .tick text
{
text-anchor: middle!important;
transform: rotate(0)!important;
}
#{{id_name}}{
height: {{kwargs.height|default(400)|int - 20}}px!important;
}
#{{id_name}} svg{
height: {{kwargs.height|default(400)|int - 20}}px!important;
}
</style>
</head>
<body>

<div id={{id_name|tojson}}></div>
Expand Down Expand Up @@ -52,9 +37,18 @@
xLabel: {{kwargs.xLabel|default("")|tojson}},
yLabel: {{kwargs.yLabel|default("")|tojson}},
height: {{kwargs.height|default(400)}},
width: {{kwargs.width|default(700)}}
width: {{kwargs.width|default(700)}},
interactive: {{kwargs.interactive|default(true)|tojson}}
}
);
var text = document.querySelectorAll(".xAxis{{id_name}} .tick text");
var i;
for (i = 0; i < text.length; i++) {
text[i].setAttribute("transform", "rotate(0)");
text[i].style.textAnchor="middle";
}
document.getElementById("{{id_name}}").style.height = "{{kwargs.height|default(400)|int - 20}}px";
document.querySelector("#{{id_name}} svg").style.height = "{{kwargs.height|default(400)|int - 20}}px";
});
</script>

Expand Down
24 changes: 9 additions & 15 deletions roughviz/templates/donut.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
<html>
<head>
<style>
.xAxis{{id_name}} .tick text
{
text-anchor: middle!important;
transform: rotate(0)!important;
}
#{{id_name}}{
height: {{kwargs.height|default(400)|int - 20}}px!important;
}
#{{id_name}} svg{
height: {{kwargs.height|default(400)|int - 20}}px!important;
}
</style>
</head>
<body>

<div id={{id_name|tojson}}></div>
Expand Down Expand Up @@ -42,8 +27,17 @@
title: {{kwargs.title|default("")|tojson}},
titleFontSize: {{kwargs.titleFontSize|default("3rem")|tojson}},
tooltipFontSize: {{kwargs.tooltipFontSize|default("3rem")|tojson}},
interactive: {{kwargs.interactive|default(true)|tojson}}
}
);
var text = document.querySelectorAll(".xAxis{{id_name}} .tick text");
var i;
for (i = 0; i < text.length; i++) {
text[i].setAttribute("transform", "rotate(0)");
text[i].style.textAnchor="middle";
}
document.getElementById("{{id_name}}").style.height = "{{kwargs.height|default(400)|int - 20}}px";
document.querySelector("#{{id_name}} svg").style.height = "{{kwargs.height|default(400)|int - 20}}px";
});
</script>

Expand Down
24 changes: 9 additions & 15 deletions roughviz/templates/pie.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
<html>
<head>
<style>
.xAxis{{id_name}} .tick text
{
text-anchor: middle!important;
transform: rotate(0)!important;
}
#{{id_name}}{
height: {{kwargs.height|default(400)|int - 20}}px!important;
}
#{{id_name}} svg{
height: {{kwargs.height|default(400)|int - 20}}px!important;
}
</style>
</head>
<body>

<div id={{id_name|tojson}}></div>
Expand Down Expand Up @@ -43,8 +28,17 @@
title: {{kwargs.title|default("")|tojson}},
titleFontSize: {{kwargs.titleFontSize|default("3rem")|tojson}},
tooltipFontSize: {{kwargs.tooltipFontSize|default("3rem")|tojson}},
interactive: {{kwargs.interactive|default(true)|tojson}}
}
);
var text = document.querySelectorAll(".xAxis{{id_name}} .tick text");
var i;
for (i = 0; i < text.length; i++) {
text[i].setAttribute("transform", "rotate(0)");
text[i].style.textAnchor="middle";
}
document.getElementById("{{id_name}}").style.height = "{{kwargs.height|default(400)|int - 20}}px";
document.querySelector("#{{id_name}} svg").style.height = "{{kwargs.height|default(400)|int - 20}}px";
});
</script>

Expand Down
26 changes: 10 additions & 16 deletions roughviz/templates/stackedbar.html
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
<html>
<head>
<style>
.xAxis{{id_name}} .tick text
{
text-anchor: middle!important;
transform: rotate(0)!important;
}
#{{id_name}}{
height: {{kwargs.height|default(400)|int - 20}}px!important;
}
#{{id_name}} svg{
height: {{kwargs.height|default(400)|int - 20}}px!important;
}
</style>
</head>
<body>

<div id={{id_name|tojson}}></div>
Expand Down Expand Up @@ -50,9 +35,18 @@
xLabel: {{kwargs.xLabel|default("")|tojson}},
yLabel: {{kwargs.yLabel|default("")|tojson}},
height: {{kwargs.height|default(400)}},
width: {{kwargs.width|default(700)}}
width: {{kwargs.width|default(700)}},
interactive: {{kwargs.interactive|default(true)|tojson}}
}
);
var text = document.querySelectorAll(".xAxis{{id_name}} .tick text");
var i;
for (i = 0; i < text.length; i++) {
text[i].setAttribute("transform", "rotate(0)");
text[i].style.textAnchor="middle";
}
document.getElementById("{{id_name}}").style.height = "{{kwargs.height|default(400)|int - 20}}px";
document.querySelector("#{{id_name}} svg").style.height = "{{kwargs.height|default(400)|int - 20}}px";
});
</script>

Expand Down

0 comments on commit 652eefc

Please sign in to comment.