').text(\n",
" `Sample size: ${sample_size} out of ${total} ${ adjusted ? \"(adjusted)\" : \"\" }`));\n",
" }());\n",
" "
],
"text/html": [
"
"
]
},
"metadata": {
"finalized": true
},
"output_type": "display_data"
}
],
"source": [
"%load_ext jupyter_require"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-19T11:01:23.831507Z",
"start_time": "2019-07-19T11:01:23.651055Z"
},
"require": [
""
]
},
"outputs": [
{
"data": {
"text/html": [
"{\n",
" \"arc\": {\n",
" \"backgroundColor\": \"rgba(0,0,0,0.1)\",\n",
" \"borderColor\": \"#fff\",\n",
" \"borderWidth\": 2,\n",
" \"borderAlign\": \"center\"\n",
" },\n",
" \"line\": {\n",
" \"tension\": 0.4,\n",
" \"backgroundColor\": \"rgba(0,0,0,0.1)\",\n",
" \"borderWidth\": 3,\n",
" \"borderColor\": \"rgba(0,0,0,0.1)\",\n",
" \"borderCapStyle\": \"butt\",\n",
" \"borderDash\": [],\n",
" \"borderDashOffset\": 0,\n",
" \"borderJoinStyle\": \"miter\",\n",
" \"capBezierPoints\": true,\n",
" \"fill\": true\n",
" },\n",
" \"point\": {\n",
" \"radius\": 3,\n",
" \"pointStyle\": \"circle\",\n",
" \"backgroundColor\": \"rgba(0,0,0,0.1)\",\n",
" \"borderColor\": \"rgba(0,0,0,0.1)\",\n",
" \"borderWidth\": 1,\n",
" \"hitRadius\": 1,\n",
" \"hoverRadius\": 4,\n",
" \"hoverBorderWidth\": 1\n",
" },\n",
" \"rectangle\": {\n",
" \"backgroundColor\": \"rgba(0,0,0,0.1)\",\n",
" \"borderColor\": \"rgba(0,0,0,0.1)\",\n",
" \"borderSkipped\": \"bottom\",\n",
" \"borderWidth\": 0\n",
" }\n",
"}
"
],
"text/plain": [
""
]
},
"metadata": {
"finalized": true,
"frozen": true
},
"output_type": "display_data"
}
],
"source": [
"%%requirejs\n",
"\n",
"let defaultElementConfig = $(\"\").html(JSON.stringify(Chart.defaults.global.elements, null, 4))\n",
"\n",
"element.append(defaultElementConfig)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Check out [ChartJS](https://www.chartjs.org/docs/latest/general/) docs for more information about default settings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Custom Graph Objects"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can create your custom GraphObjects by implementing a function of the following specification:\n",
"\n",
"```ts\n",
"interface Index {\n",
" data: Array,\n",
" dtype: string\n",
"}\n",
"\n",
"function(data: Array, index: Array, dtype: string): Chart\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Suppose we wanna plot colours and we want a special kind of plot for that"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-19T11:01:27.457855Z",
"start_time": "2019-07-19T11:01:27.303409Z"
},
"require": [
"chartjs"
]
},
"outputs": [],
"source": [
"%%requirejs chartjs\n",
"\n",
"let isValidColour = function(colour) {\n",
" let s = new Option().style\n",
" s.color = colour\n",
" \n",
" return s.color !== '' || console.debug(`Invalid CSS colour: '${colour}'.`)\n",
"}\n",
"\n",
"let ColorPalette = function(data, index, dtype) {\n",
" const canvas = document.createElement('canvas')\n",
" const ctx = canvas.getContext('2d')\n",
" \n",
" // perform check if the pattern is correct\n",
" if ( !data.every( d => typeof(d) === 'string' && isValidColour(d) ) ) {\n",
" console.debug(\"Data does not match colour pattern.\")\n",
" return\n",
" }\n",
" \n",
" // evenly slice the Pie chart by number of colours\n",
" const slices = new Array(data.length).fill(Number(1 / data.length).toFixed(2))\n",
" const labels = index[0].data\n",
" \n",
" let chart = new Chart(ctx, {\n",
" type: 'pie',\n",
" data: {\n",
" labels: labels,\n",
" datasets: [{\n",
" data: slices,\n",
" backgroundColor: data,\n",
" }]\n",
" },\n",
" })\n",
" \n",
" return chart\n",
"}\n",
"\n",
"// Register the new chart\n",
"$.fn.dataTable.defaults.graphObjects['ColorPalette'] = ColorPalette"
]
},
{
"cell_type": "markdown",
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-18T19:55:59.832938Z",
"start_time": "2019-07-18T19:55:59.718074Z"
},
"require": [
"chartjs"
]
},
"source": [
"And set it as default for the dtype you wanna use it for (in this case `string`):\n",
"\n",
" The default setting is:\n",
" \n",
"```\n",
" { \n",
" boolean: ['CategoricalBar', 'Histogram'],\n",
" date: ['CategoricalBar', 'Histogram'],\n",
" num: ['Histogram', 'CategoricalBar', 'Bar', 'Line'],\n",
" string: ['CategoricalBar', 'Histogram'],\n",
"\n",
" undefined: ['Bar']\n",
" }\n",
" \n",
"```\n",
"\n",
" The order specifies fallback plots."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-19T11:01:32.045247Z",
"start_time": "2019-07-19T11:01:31.875373Z"
},
"require": [
""
]
},
"outputs": [],
"source": [
"%%requirejs\n",
"\n",
"$.fn.dataTable.defaults.dTypePlotMap['string'].unshift('ColorPalette')"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-19T11:01:32.085258Z",
"start_time": "2019-07-19T11:01:32.048433Z"
},
"require": [
"base/js/events",
"datatables.net",
"d3",
"chartjs",
"graph-objects",
"jupyter-datatables"
]
},
"outputs": [
{
"data": {
"text/html": [
"
Showing 1 to 4 of 4 entries
"
],
"text/plain": [
""
]
},
"metadata": {
"finalized": true,
"frozen": true
},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"{ \n",
" setTimeout(() => {\n",
" const table_id = 'b4d8ba45e1c0da84f169b0b118c3f1a212add1155a154489417842dc99a6a2ba';\n",
" const table = $(`#${table_id}_wrapper`);\n",
" \n",
" let scrollHead = table.find('div.dataTables_scrollHead');\n",
" let scrollBody = table.find('div.dataTables_scrollBody');\n",
" \n",
" $(scrollBody).on(\n",
" 'scroll',\n",
" (e) => {\n",
" scrollHead.scrollLeft(scrollBody.scrollLeft());\n",
" },\n",
" );\n",
" }, 200);\n",
" }"
],
"text/plain": [
""
]
},
"metadata": {
"finalized": true
},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"\n",
" (function() {\n",
" const sample_size = Number(4).toLocaleString();\n",
" const adjusted = Boolean('False' == 'True')\n",
"\n",
" const total = Number(4).toLocaleString();\n",
"\n",
" element.append($('').text(\n",
" `Sample size: ${sample_size} out of ${total} ${ adjusted ? \"(adjusted)\" : \"\" }`));\n",
" }());\n",
" "
],
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" value | \n",
"
\n",
" \n",
" \n",
" \n",
" red | \n",
" rgb(255, 99, 132) | \n",
"
\n",
" \n",
" blue | \n",
" rgb(54, 162, 235) | \n",
"
\n",
" \n",
" lightyellow | \n",
" rgba(255, 205, 86, 0.3) | \n",
"
\n",
" \n",
" darkorange | \n",
" darkorange | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" value\n",
"red rgb(255, 99, 132)\n",
"blue rgb(54, 162, 235)\n",
"lightyellow rgba(255, 205, 86, 0.3)\n",
"darkorange darkorange"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_colours = pd.DataFrame([\n",
" {\n",
" \"colour\": \"red\",\n",
" \"value\" : \"rgb(255, 99, 132)\",\n",
" },\n",
" {\n",
" \"colour\": \"blue\",\n",
" \"value\" : \"rgb(54, 162, 235)\"\n",
" },\n",
" {\n",
" \"colour\": \"lightyellow\",\n",
" \"value\" : \"rgba(255, 205, 86, 0.3)\" # alpha values via `rgba()`\n",
" },\n",
" {\n",
" \"colour\": \"darkorange\",\n",
" \"value\" : \"darkorange\" # any valid CSS specifier\n",
" }\n",
"])\n",
"\n",
"df_colours.set_index(\"colour\", inplace=True)\n",
"\n",
"# As of v0.3.0, DataTables do not support index names properly\n",
"del df_colours.index.name\n",
"\n",
"df_colours"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We fall back to the default chart if the colour value is invalid based on our check and use the second chart in order:"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"ExecuteTime": {
"end_time": "2019-07-19T11:01:32.145900Z",
"start_time": "2019-07-19T11:01:32.108737Z"
},
"require": [
"base/js/events",
"datatables.net",
"d3",
"chartjs",
"graph-objects",
"jupyter-datatables"
],
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"
Showing 1 to 4 of 4 entries
"
],
"text/plain": [
""
]
},
"metadata": {
"finalized": true,
"frozen": true
},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"{ \n",
" setTimeout(() => {\n",
" const table_id = 'eb22dfbf1b7818b07d1a12c99fd78870baebd42e8e41a36270ab854006f95877';\n",
" const table = $(`#${table_id}_wrapper`);\n",
" \n",
" let scrollHead = table.find('div.dataTables_scrollHead');\n",
" let scrollBody = table.find('div.dataTables_scrollBody');\n",
" \n",
" $(scrollBody).on(\n",
" 'scroll',\n",
" (e) => {\n",
" scrollHead.scrollLeft(scrollBody.scrollLeft());\n",
" },\n",
" );\n",
" }, 200);\n",
" }"
],
"text/plain": [
""
]
},
"metadata": {
"finalized": true
},
"output_type": "display_data"
},
{
"data": {
"application/javascript": [
"\n",
" (function() {\n",
" const sample_size = Number(4).toLocaleString();\n",
" const adjusted = Boolean('False' == 'True')\n",
"\n",
" const total = Number(4).toLocaleString();\n",
"\n",
" element.append($('').text(\n",
" `Sample size: ${sample_size} out of ${total} ${ adjusted ? \"(adjusted)\" : \"\" }`));\n",
" }());\n",
" "
],
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" value | \n",
"
\n",
" \n",
" \n",
" \n",
" red | \n",
" red | \n",
"
\n",
" \n",
" green | \n",
" invalid | \n",
"
\n",
" \n",
" blue | \n",
" blue | \n",
"
\n",
" \n",
" other | \n",
" invalid | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" value\n",
"red red\n",
"green invalid\n",
"blue blue\n",
"other invalid"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_other = pd.DataFrame([\n",
" {\n",
" \"colour\": \"red\",\n",
" \"value\" : \"red\",\n",
" },\n",
" {\n",
" \"colour\": \"green\",\n",
" \"value\" : \"invalid\",\n",
" },\n",
" {\n",
" \"colour\": \"blue\",\n",
" \"value\" : \"blue\",\n",
" },\n",
" {\n",
" \"colour\": \"other\",\n",
" \"value\" : \"invalid\",\n",
" }\n",
"])\n",
"\n",
"df_other.set_index(\"colour\", inplace=True)\n",
"\n",
"del df_other.index.name\n",
"\n",
"df_other"
]
}
],
"metadata": {
"finalized": {
"timestamp": 1563534100374,
"trusted": true
},
"hide_input": false,
"kernelspec": {
"display_name": "jupyter-datatables",
"language": "python",
"name": "jupyter-datatables"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
},
"require": {
"paths": {
"buttons.colvis": "https://cdn.datatables.net/buttons/1.5.6/js/buttons.colVis.min",
"buttons.flash": "https://cdn.datatables.net/buttons/1.5.6/js/buttons.flash.min",
"buttons.html5": "https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min",
"buttons.print": "https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min",
"chartjs": "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart",
"d3": "https://d3js.org/d3.v5.min",
"d3-array": "https://d3js.org/d3-array.v2.min",
"datatables.net": "https://cdn.datatables.net/1.10.18/js/jquery.dataTables",
"datatables.net-buttons": "https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min",
"datatables.responsive": "https://cdn.datatables.net/responsive/2.2.2/js/dataTables.responsive.min",
"datatables.scroller": "https://cdn.datatables.net/scroller/2.0.0/js/dataTables.scroller.min",
"datatables.select": "https://cdn.datatables.net/select/1.3.0/js/dataTables.select.min",
"jszip": "https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min",
"moment": "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.0/moment",
"pdfmake": "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min",
"vfsfonts": "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts"
},
"shim": {
"buttons.colvis": {
"deps": [
"jszip",
"datatables.net-buttons"
]
},
"buttons.flash": {
"deps": [
"jszip",
"datatables.net-buttons"
]
},
"buttons.html5": {
"deps": [
"jszip",
"datatables.net-buttons"
]
},
"buttons.print": {
"deps": [
"jszip",
"datatables.net-buttons"
]
},
"chartjs": {
"deps": [
"moment"
]
},
"datatables.net": {
"exports": "$.fn.dataTable"
},
"datatables.net-buttons": {
"deps": [
"datatables.net"
]
},
"pdfmake": {
"deps": [
"datatables.net"
]
},
"vfsfonts": {
"deps": [
"datatables.net"
]
}
}
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": true,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}