{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Kernalはpythonでないとinitialize_javascriptが動かない\n",
"ここではKernelをpython2とし、load_extを使ってsageの機能を後付けします。\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The sage extension is already loaded. To reload it, use:\n",
" %reload_ext sage\n"
]
}
],
"source": [
"%load_ext sage"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"$.getScript(\"https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.7.0/nv.d3.min.js\")"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"$.getScript(\"https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js\", function() {\n",
" $.getScript(\"https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.7.0/nv.d3.min.js\", function() {})});"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.core.display import HTML\n",
"import nvd3\n",
"nvd3.ipynb.initialize_javascript(use_remote=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"配列は、sageのままではシリアライズに失敗するので、numpyの配列で定義します。"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" \n",
"\n",
"\n",
" \n",
"\n"
]
}
],
"source": [
"from nvd3 import pieChart\n",
"import numpy as np\n",
"\n",
"type = 'pieChart'\n",
"chart = pieChart(name=type, color_category='category20c', height=450, width=450)\n",
"xdata = np.array([\"Orange\", \"Banana\", \"Pear\", \"Kiwi\", \"Apple\", \"Strawberry\", \"Pineapple\"]) \n",
"ydata = np.array([3, 4, 0, 1, 5, 7, 3])\n",
"extra_serie = {\"tooltip\": {\"y_start\": \"\", \"y_end\": \" cal\"}}\n",
"chart.add_serie(y=ydata, x=xdata, extra=extra_serie)\n",
"chart.buildcontent()\n",
"#HTML(chart.htmlcontent)\n",
"print chart.htmlcontent"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"\n",
"\n",
" \n",
"\n",
" \n",
""
],
"text/plain": [
""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chart"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"\n",
"scatterChart
\n",
"\n",
"\n",
"\n",
"\n",
" \n",
"\n",
" \n",
""
],
"text/plain": [
""
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from nvd3 import scatterChart\n",
"import random\n",
"\n",
"type = \"scatterChart\"\n",
"chart = scatterChart(name=type, height=350, width=800, x_is_date=False)\n",
"chart.set_containerheader(\"\\n\\n\" + type + \"
\\n\\n\")\n",
"nb_element = 50\n",
"xdata = np.array([i + random.randint(1, 10) for i in range(nb_element)])\n",
"ydata = np.array([i * random.randint(1, 10) for i in range(nb_element)])\n",
"ydata2 = np.array([x * 2 for x in ydata])\n",
"ydata3 = np.array([x * 5 for x in ydata])\n",
"\n",
"kwargs1 = {'shape': 'circle', 'size': '1'}\n",
"kwargs2 = {'shape': 'cross', 'size': '10'}\n",
"kwargs3 = {'shape': 'triangle-up', 'size': '100'}\n",
"\n",
"extra_serie = {\"tooltip\": {\"y_start\": \"\", \"y_end\": \" calls\"}}\n",
"chart.add_serie(name=\"serie 1\", y=ydata, x=xdata, extra=extra_serie, **kwargs1)\n",
"chart.add_serie(name=\"serie 2\", y=ydata2, x=xdata, extra=extra_serie, **kwargs2)\n",
"chart.add_serie(name=\"serie 3\", y=ydata3, x=xdata, extra=extra_serie, **kwargs3)\n",
"\n",
"chart.buildhtml()\n",
"chart"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 0
}