-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
charts: add component notebook with all chart functions
- Loading branch information
Showing
1 changed file
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,319 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"provenance": [], | ||
"authorship_tag": "ABX9TyPcAYVVDyOnEVxw6WKYfB8l" | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"## Charts Setup\n", | ||
"*You must run the cells in this section each time you connect to a new runtime. For example, when you return to the notebook after an idle timeout, when the runtime crashes, or when you restart or factory reset the runtime.*" | ||
], | ||
"metadata": { | ||
"id": "IhnbdjqU1e6p" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"id": "elpUvMf61Ym6" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"! pip install --upgrade altair >> pip.log" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"Import chart packages and define chart functions:" | ||
], | ||
"metadata": { | ||
"id": "P1aenztz1zK3" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"import altair as alt\n", | ||
"\n", | ||
"def plot_release_count(release_counts):\n", | ||
" # check if input contains the right columns\n", | ||
" if not set(\n", | ||
" [\"collection_id\", \"release_type\", \"release_count\", \"ocid_count\"]\n", | ||
" ).issubset(release_counts.columns):\n", | ||
" raise ValueError(\n", | ||
" \"Input data must contain the following columns: collection_id, release_type, release_count, ocid_count\"\n", | ||
" )\n", | ||
" chart = (\n", | ||
" alt.Chart(release_counts)\n", | ||
" .mark_bar()\n", | ||
" .encode(\n", | ||
" x=alt.X(\n", | ||
" \"release_count\",\n", | ||
" type=\"ordinal\",\n", | ||
" axis=alt.Axis(title=\"release count\", labelAngle=0),\n", | ||
" ),\n", | ||
" y=alt.Y(\n", | ||
" \"ocid_count\",\n", | ||
" type=\"quantitative\",\n", | ||
" axis=alt.Axis(title=\"ocid count\", format=\"~s\", tickCount=5),\n", | ||
" ),\n", | ||
" color=alt.Color(\n", | ||
" \"release_type\",\n", | ||
" type=\"nominal\",\n", | ||
" title=\"release type\",\n", | ||
" scale=alt.Scale(range=[\"#D6E100\", \"#FB6045\", \"#23B2A7\", \"#6C75E1\"]),\n", | ||
" ),\n", | ||
" tooltip=[\n", | ||
" alt.Tooltip(\"release_count\", title=\"release count\"),\n", | ||
" alt.Tooltip(\"ocid_count\", title=\"ocid count\", format=\"~s\"),\n", | ||
" alt.Tooltip(\"release_type\", title=\"release type\"),\n", | ||
" 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", | ||
" .configure_view(strokeWidth=0)\n", | ||
" )\n", | ||
"\n", | ||
" return chart\n", | ||
"\n", | ||
"def plot_objects_per_stage(objects_per_stage):\n", | ||
" # check if input contains the right columns\n", | ||
" if not set([\"stage\", \"object_count\"]).issubset(objects_per_stage.columns):\n", | ||
" raise ValueError(\"Data must contain columns 'stage' and 'object_count'\")\n", | ||
" # draw chart\n", | ||
" chart = (\n", | ||
" alt.Chart(objects_per_stage)\n", | ||
" .mark_bar(fill=\"#d6e100\")\n", | ||
" .encode(\n", | ||
" x=alt.X(\n", | ||
" \"stage\",\n", | ||
" type=\"ordinal\",\n", | ||
" scale=alt.Scale(\n", | ||
" domain=[\n", | ||
" \"planning\",\n", | ||
" \"tender\",\n", | ||
" \"awards\",\n", | ||
" \"contracts\",\n", | ||
" \"implementation\",\n", | ||
" ]\n", | ||
" ),\n", | ||
" sort=[\"planning\", \"tender\", \"awards\", \"contracts\", \"implementation\"],\n", | ||
" axis=alt.Axis(title=\"stage\", labelAngle=0),\n", | ||
" ),\n", | ||
" y=alt.Y(\n", | ||
" \"object_count\",\n", | ||
" type=\"quantitative\",\n", | ||
" axis=alt.Axis(title=\"number of objects\", format=\"~s\", tickCount=5),\n", | ||
" ),\n", | ||
" tooltip=[\n", | ||
" alt.Tooltip(\"stage\", title=\"stage\"),\n", | ||
" 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", | ||
" .configure_view(strokeWidth=0)\n", | ||
" )\n", | ||
" return chart\n", | ||
"\n", | ||
"\n", | ||
"def plot_releases_by_month(release_dates):\n", | ||
" # check if input contains the right columns\n", | ||
" if not set([\"date\", \"collection_id\", \"release_type\", \"release_count\"]).issubset(\n", | ||
" release_dates.columns\n", | ||
" ):\n", | ||
" raise ValueError(\n", | ||
" \"Data must contain columns 'date', 'collection_id', 'release_type', 'release_count'\"\n", | ||
" )\n", | ||
" # check if number of rows is more than 5000\n", | ||
" if release_dates.shape[0] > 5000:\n", | ||
" alt.data_transformers.disable_max_rows()\n", | ||
"\n", | ||
" # draw chart\n", | ||
" chart = (\n", | ||
" alt.Chart(release_dates)\n", | ||
" .mark_line(strokeWidth=3)\n", | ||
" .encode(\n", | ||
" x=alt.X(\n", | ||
" \"date\", timeUnit=\"yearmonth\", axis=alt.Axis(title=\"year and month\")\n", | ||
" ),\n", | ||
" y=alt.Y(\n", | ||
" \"release_count\",\n", | ||
" type=\"quantitative\",\n", | ||
" aggregate=\"sum\",\n", | ||
" axis=alt.Axis(title=\"number of releases\", format=\"~s\", tickCount=5),\n", | ||
" scale=alt.Scale(zero=False),\n", | ||
" ),\n", | ||
" color=alt.Color(\n", | ||
" \"release_type\",\n", | ||
" type=\"nominal\",\n", | ||
" scale=alt.Scale(range=[\"#D6E100\", \"#FB6045\", \"#23B2A7\", \"#6C75E1\"]),\n", | ||
" legend=alt.Legend(title=\"release type\"),\n", | ||
" ),\n", | ||
" tooltip=[\n", | ||
" alt.Tooltip(\"date\", timeUnit=\"yearmonth\", title=\"date\"),\n", | ||
" alt.Tooltip(\n", | ||
" \"release_count\", aggregate=\"sum\", title=\"number of releases\"\n", | ||
" ),\n", | ||
" 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", | ||
" .configure_view(strokeWidth=0)\n", | ||
" )\n", | ||
" return chart\n", | ||
"\n", | ||
"def plot_objects_per_year(objects_per_year):\n", | ||
" # check if input contains the right columns\n", | ||
" if not set([\"year\", \"tenders\", \"awards\"]).issubset(objects_per_year.columns):\n", | ||
" raise ValueError(\"Data must contain columns 'year', 'tenders' and 'awards'\")\n", | ||
" # draw chart\n", | ||
" chart = (\n", | ||
" alt.Chart(objects_per_year)\n", | ||
" .transform_fold([\"tenders\", \"awards\"])\n", | ||
" .mark_line(strokeWidth=3)\n", | ||
" .encode(\n", | ||
" x=alt.X(\n", | ||
" \"year\",\n", | ||
" type=\"quantitative\",\n", | ||
" axis=alt.Axis(title=\"year\", format=\".0f\", tickCount=dates.shape[0]),\n", | ||
" ),\n", | ||
" y=alt.Y(\n", | ||
" \"value\",\n", | ||
" type=\"quantitative\",\n", | ||
" axis=alt.Axis(title=\"number of objects\", format=\"~s\", tickCount=5),\n", | ||
" scale=alt.Scale(zero=False),\n", | ||
" ),\n", | ||
" color=alt.Color(\n", | ||
" \"key\",\n", | ||
" type=\"nominal\",\n", | ||
" title=\"object type\",\n", | ||
" scale=alt.Scale(\n", | ||
" domain=[\"tenders\", \"awards\"], range=[\"#D6E100\", \"#FB6045\"]\n", | ||
" ),\n", | ||
" ),\n", | ||
" tooltip=[\n", | ||
" alt.Tooltip(\"year\", title=\"year\", type=\"quantitative\"),\n", | ||
" alt.Tooltip(\"value\", title=\"number of objects\", type=\"quantitative\"),\n", | ||
" 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", | ||
" .configure_view(strokeWidth=0)\n", | ||
" )\n", | ||
" return chart\n", | ||
"\n", | ||
"def plot_top_buyers(buyers):\n", | ||
" # check if input contains the right columns\n", | ||
" if not set([\"name\", \"total_tenders\"]).issubset(buyers.columns):\n", | ||
" raise ValueError(\"Data must contain columns 'name' and 'total_tenders'\")\n", | ||
" # draw chart\n", | ||
" chart = (\n", | ||
" alt.Chart(buyers)\n", | ||
" .mark_bar(fill=\"#d6e100\")\n", | ||
" .encode(\n", | ||
" x=alt.X(\n", | ||
" \"total_tenders\",\n", | ||
" type=\"quantitative\",\n", | ||
" axis=alt.Axis(title=\"number of tenders\", format=\"~s\", tickCount=5),\n", | ||
" ),\n", | ||
" y=alt.Y(\n", | ||
" \"name\",\n", | ||
" type=\"ordinal\",\n", | ||
" axis=alt.Axis(title=\"buyer\", labelAngle=0),\n", | ||
" sort=alt.SortField(\"total_tenders\", order=\"descending\"),\n", | ||
" ),\n", | ||
" tooltip=[\n", | ||
" alt.Tooltip(\"name\", title=\"buyer\", type=\"nominal\"),\n", | ||
" alt.Tooltip(\n", | ||
" \"total_tenders\", title=\"number of tenders\", type=\"quantitative\"\n", | ||
" ),\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", | ||
" .configure_view(strokeWidth=0)\n", | ||
" )\n", | ||
" return chart" | ||
], | ||
"metadata": { | ||
"id": "Bip37aP917XY" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
} | ||
] | ||
} |