Skip to content

Commit

Permalink
component_charts: remove code duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
yolile committed Feb 10, 2024
1 parent 85e373a commit d5655b1
Show file tree
Hide file tree
Showing 7 changed files with 687 additions and 469 deletions.
106 changes: 39 additions & 67 deletions component_charts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,27 @@
"source": [
"import altair as alt\n",
"\n",
"\n",
"class MissingColumnsError(Exception):\n",
" def __init__(self, columns):\n",
" super().__init__(f\"The source data is missing one or more of these columns: {columns}\")\n",
"\n",
"\n",
"chart_properties = {\n",
" \"width\": 600,\n",
" \"height\": 350,\n",
" \"padding\": 50,\n",
" \"title\": alt.TitleParams(text=\"\", subtitle=[\"\"], fontSize=18),\n",
"}\n",
"chart_axis = {\n",
" \"titleFontSize\": 14,\n",
" \"labelFontSize\": 14,\n",
" \"labelPadding\": 5,\n",
" \"ticks\": False,\n",
" \"domain\": False,\n",
"}\n",
"\n",
"\n",
"def check_columns(columns, data):\n",
" # check if input contains the right columns\n",
" if not columns.issubset(data.columns):\n",
Expand Down Expand Up @@ -95,19 +112,8 @@
" alt.Tooltip(\"collection_id\", title=\"collection id\"),\n",
" ],\n",
" )\n",
" .properties(\n",
" width=600,\n",
" height=350,\n",
" padding=50,\n",
" title=alt.TitleParams(text=\"\", subtitle=[\"\"], fontSize=18),\n",
" )\n",
" .configure_axis(\n",
" titleFontSize=14,\n",
" labelFontSize=14,\n",
" labelPadding=5,\n",
" ticks=False,\n",
" domain=False,\n",
" )\n",
" .properties(**chart_properties)\n",
" .configure_axis(**chart_axis)\n",
" .configure_view(strokeWidth=0)\n",
" )\n",
"\n",
Expand Down Expand Up @@ -136,19 +142,8 @@
" alt.Tooltip(\"object_count\", title=\"number of objects\"),\n",
" ],\n",
" )\n",
" .properties(\n",
" width=600,\n",
" height=350,\n",
" padding=50,\n",
" title=alt.TitleParams(text=\"\", subtitle=[\"\"], fontSize=18),\n",
" )\n",
" .configure_axis(\n",
" titleFontSize=14,\n",
" labelFontSize=14,\n",
" labelPadding=5,\n",
" ticks=False,\n",
" domain=False,\n",
" )\n",
" .properties(**chart_properties)\n",
" .configure_axis(**chart_axis)\n",
" .configure_view(strokeWidth=0)\n",
" )\n",
"\n",
Expand Down Expand Up @@ -185,19 +180,8 @@
" alt.Tooltip(\"release_type\", title=\"release type\"),\n",
" ],\n",
" )\n",
" .properties(\n",
" width=600,\n",
" height=350,\n",
" padding=50,\n",
" title=alt.TitleParams(text=\"\", subtitle=[\"\"], fontSize=18),\n",
" )\n",
" .configure_axis(\n",
" titleFontSize=14,\n",
" labelFontSize=14,\n",
" labelPadding=5,\n",
" ticks=False,\n",
" domain=False,\n",
" )\n",
" .properties(**chart_properties)\n",
" .configure_axis(**chart_axis)\n",
" .configure_view(strokeWidth=0)\n",
" )\n",
"\n",
Expand Down Expand Up @@ -233,19 +217,8 @@
" alt.Tooltip(\"key\", title=\"object type\", type=\"nominal\"),\n",
" ],\n",
" )\n",
" .properties(\n",
" width=600,\n",
" height=350,\n",
" padding=50,\n",
" title=alt.TitleParams(text=\"\", subtitle=[\"\"], fontSize=18),\n",
" )\n",
" .configure_axis(\n",
" titleFontSize=14,\n",
" labelFontSize=14,\n",
" labelPadding=5,\n",
" ticks=False,\n",
" domain=False,\n",
" )\n",
" .properties(**chart_properties)\n",
" .configure_axis(**chart_axis)\n",
" .configure_view(strokeWidth=0)\n",
" )\n",
"\n",
Expand All @@ -272,27 +245,26 @@
" alt.Tooltip(\"total_tenders\", title=\"number of tenders\", type=\"quantitative\"),\n",
" ],\n",
" )\n",
" .properties(\n",
" width=600,\n",
" height=350,\n",
" padding=50,\n",
" title=alt.TitleParams(text=\"\", subtitle=[\"\"], fontSize=18),\n",
" )\n",
" .configure_axis(\n",
" titleFontSize=14,\n",
" labelFontSize=14,\n",
" labelPadding=5,\n",
" ticks=False,\n",
" domain=False,\n",
" )\n",
" .properties(**chart_properties)\n",
" .configure_axis(**chart_axis)\n",
" .configure_view(strokeWidth=0)\n",
" )"
],
"metadata": {
"id": "Bip37aP917XY"
},
"execution_count": null,
"outputs": []
"execution_count": 5,
"outputs": [
{
"data": {
"text/html": "\n<style>\n #altair-viz-0203ac87d45949e1a15c16f2e5cd36b8.vega-embed {\n width: 100%;\n display: flex;\n }\n\n #altair-viz-0203ac87d45949e1a15c16f2e5cd36b8.vega-embed details,\n #altair-viz-0203ac87d45949e1a15c16f2e5cd36b8.vega-embed details summary {\n position: relative;\n }\n</style>\n<div id=\"altair-viz-0203ac87d45949e1a15c16f2e5cd36b8\"></div>\n<script type=\"text/javascript\">\n var VEGA_DEBUG = (typeof VEGA_DEBUG == \"undefined\") ? {} : VEGA_DEBUG;\n (function(spec, embedOpt){\n let outputDiv = document.currentScript.previousElementSibling;\n if (outputDiv.id !== \"altair-viz-0203ac87d45949e1a15c16f2e5cd36b8\") {\n outputDiv = document.getElementById(\"altair-viz-0203ac87d45949e1a15c16f2e5cd36b8\");\n }\n const paths = {\n \"vega\": \"https://cdn.jsdelivr.net/npm/vega@5?noext\",\n \"vega-lib\": \"https://cdn.jsdelivr.net/npm/vega-lib?noext\",\n \"vega-lite\": \"https://cdn.jsdelivr.net/npm/[email protected]?noext\",\n \"vega-embed\": \"https://cdn.jsdelivr.net/npm/vega-embed@6?noext\",\n };\n\n function maybeLoadScript(lib, version) {\n var key = `${lib.replace(\"-\", \"\")}_version`;\n return (VEGA_DEBUG[key] == version) ?\n Promise.resolve(paths[lib]) :\n new Promise(function(resolve, reject) {\n var s = document.createElement('script');\n document.getElementsByTagName(\"head\")[0].appendChild(s);\n s.async = true;\n s.onload = () => {\n VEGA_DEBUG[key] = version;\n return resolve(paths[lib]);\n };\n s.onerror = () => reject(`Error loading script: ${paths[lib]}`);\n s.src = paths[lib];\n });\n }\n\n function showError(err) {\n outputDiv.innerHTML = `<div class=\"error\" style=\"color:red;\">${err}</div>`;\n throw err;\n }\n\n function displayChart(vegaEmbed) {\n vegaEmbed(outputDiv, spec, embedOpt)\n .catch(err => showError(`Javascript Error: ${err.message}<br>This usually means there's a typo in your chart specification. See the javascript console for the full traceback.`));\n }\n\n if(typeof define === \"function\" && define.amd) {\n requirejs.config({paths});\n require([\"vega-embed\"], displayChart, err => showError(`Error loading script: ${err.message}`));\n } else {\n maybeLoadScript(\"vega\", \"5\")\n .then(() => maybeLoadScript(\"vega-lite\", \"5.16.3\"))\n .then(() => maybeLoadScript(\"vega-embed\", \"6\"))\n .catch(showError)\n .then(() => displayChart(vegaEmbed));\n }\n })({\"config\": {\"view\": {\"continuousWidth\": 300, \"continuousHeight\": 300, \"strokeWidth\": 0}, \"axis\": {\"domain\": false, \"labelFontSize\": 14, \"labelPadding\": 5, \"ticks\": false, \"titleFontSize\": 14}}, \"data\": {\"name\": \"data-07cbcd36d244d73845d1b5238107e767\"}, \"mark\": {\"type\": \"bar\", \"fill\": \"#d6e100\"}, \"encoding\": {\"tooltip\": [{\"field\": \"stage\", \"title\": \"stage\", \"type\": \"nominal\"}, {\"field\": \"object_count\", \"title\": \"number of objects\", \"type\": \"quantitative\"}], \"x\": {\"axis\": {\"labelAngle\": 0, \"title\": \"stage\"}, \"field\": \"stage\", \"scale\": {\"domain\": [\"planning\", \"tender\", \"awards\", \"contracts\", \"implementation\"]}, \"sort\": [\"planning\", \"tender\", \"awards\", \"contracts\", \"implementation\"], \"type\": \"ordinal\"}, \"y\": {\"axis\": {\"format\": \"~s\", \"tickCount\": 5, \"title\": \"number of objects\"}, \"field\": \"object_count\", \"type\": \"quantitative\"}}, \"height\": 350, \"padding\": 50, \"title\": {\"text\": \"\", \"fontSize\": 18, \"subtitle\": [\"\"]}, \"width\": 600, \"$schema\": \"https://vega.github.io/schema/vega-lite/v5.16.3.json\", \"datasets\": {\"data-07cbcd36d244d73845d1b5238107e767\": [{\"stage\": \"planning\", \"object_count\": 100}, {\"stage\": \"tender\", \"object_count\": 80}, {\"stage\": \"awards\", \"object_count\": 70}, {\"stage\": \"contracts\", \"object_count\": 60}, {\"stage\": \"implementation\", \"object_count\": 50}]}}, {\"mode\": \"vega-lite\"});\n</script>",
"text/plain": "alt.Chart(...)"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
]
}
]
}
Loading

0 comments on commit d5655b1

Please sign in to comment.