Skip to content

Instantly share code, notes, and snippets.

@NapalmHorn
Last active August 29, 2015 14:04
Show Gist options
  • Save NapalmHorn/f4c325c0139abc21a657 to your computer and use it in GitHub Desktop.
Save NapalmHorn/f4c325c0139abc21a657 to your computer and use it in GitHub Desktop.
Create a local html file with a google charts 3d pie chart and open it
import os.path # used by makeChart to create new unnamed charts
import webbrowser # used by makeChart to open the freshly created chart.
def makeChart(chartableDict):
"""Takes a dictionary with the subset of data that we want and makes a chart
Input : chartableDict which can have special entries
"control chart title" => the title of the new chart,
if absent or blank title will be 'chart title'
"control chart options" => the options to be sent to google charts,
if absent or blank defaults will be used
all other keys are labels for the new chart, and correspond to data points.
'control open chart' => if present indicates chart should open automatically
Output: a file named whatever the chartableDict['control chart title'] + '.html',
or, if absent or blank, chart\d+.html w/ \d+ as lowest possible choice starting at zero
"""
htmlPriorToChart = """<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Label', 'Data'],"""
htmlFollowingChart = """
var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="piechart_3d" style="width: 900px; height: 500px;"></div>
</body>
</html>
"""
controlOpenChart = None #thus it does not open by default
controlChartTitle = ''# title of chart
controlChartOptions = '' # the options properly formatted to be loaded into the HTML file
htmlDataChart = '' # the working string of html of chart data
# check for control chart title and set it
if 'control open chart' in chartableDict.keys():
controlOpenChart = chartableDict['control open chart']
del chartableDict['control open chart']
if 'control chart title' in chartableDict.keys() :
if chartableDict['control chart title']:
controlChartTitle = chartableDict['control chart title'] # load it into controlChartTitle
else:
chartNumber = 1
while chartNumber :
if not os.path.isfile('chart' + str(chartNumber - 1) + '.html' ):
controlChartTitle = 'chart' + str(chartNumber - 1) + '.html'
chartNumber = 0
else:
chartNumber += 1
del chartableDict['control chart title'] # remove control key
else:
# if absent or blank pick first available generic chart name
chartNumber = 1
while chartNumber :
if not os.path.isfile('chart' + str(chartNumber - 1) + '.html' ):
controlChartTitle = 'chart' + str(chartNumber - 1) + '.html'
chartNumber = 0
else:
chartNumber += 1
# create the options strings from control chart options
if 'control chart options' in chartableDict.keys():
if chartableDict['control chart options'] :
controlChartOptions = chartableDict['control chart options']
del chartableDict['control chart options'] # remove control key
else:
None # leave it blank
# for each key remaining treat them as a label
for datum in chartableDict.keys():
# add a label and data row to the htmlDataChart
if htmlDataChart:
htmlDataChart +=',' # if its not blank put a comma at the end, saves unneeded trailing comma
htmlDataChart += "\n ['" + str(datum) + "', " + str(chartableDict[datum]) + ' ]'
htmlDataChart += '\n ]);\n' #close chart
# write all 4 parts of HTML to the file.
f = open( controlChartTitle ,'w')
f.write(htmlPriorToChart)
f.write(htmlDataChart)
f.write(controlChartOptions)
f.write(htmlFollowingChart)
f.close()
if controlOpenChart:
webbrowser.open_new_tab(controlChartTitle) # call web browser to open html file.
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment