Skip to content

Commit

Permalink
feat: use base chart component when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
yolile committed Feb 10, 2024
1 parent 3260a5b commit 30d7829
Show file tree
Hide file tree
Showing 13 changed files with 1,846 additions and 259 deletions.
132 changes: 61 additions & 71 deletions component_charts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@
"source": [
"import altair as alt\n",
"\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",
" if not set([\"collection_id\", \"release_type\", \"release_count\", \"ocid_count\"]).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",
Expand Down Expand Up @@ -103,66 +102,63 @@
"\n",
" return chart\n",
"\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",
" # 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",
" if not set([\"date\", \"collection_id\", \"release_type\", \"release_count\"]).issubset(release_dates.columns):\n",
" raise ValueError(\"Data must contain columns 'date', 'collection_id', 'release_type', 'release_count'\")\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",
Expand All @@ -172,9 +168,7 @@
" 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",
" x=alt.X(\"date\", timeUnit=\"yearmonth\", axis=alt.Axis(title=\"year and month\")),\n",
" y=alt.Y(\n",
" \"release_count\",\n",
" type=\"quantitative\",\n",
Expand All @@ -190,9 +184,7 @@
" ),\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_count\", aggregate=\"sum\", title=\"number of releases\"),\n",
" alt.Tooltip(\"release_type\", title=\"release type\"),\n",
" ],\n",
" )\n",
Expand All @@ -213,6 +205,7 @@
" )\n",
" return chart\n",
"\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",
Expand All @@ -238,9 +231,7 @@
" \"key\",\n",
" type=\"nominal\",\n",
" title=\"object type\",\n",
" scale=alt.Scale(\n",
" domain=[\"tenders\", \"awards\"], range=[\"#D6E100\", \"#FB6045\"]\n",
" ),\n",
" scale=alt.Scale(domain=[\"tenders\", \"awards\"], range=[\"#D6E100\", \"#FB6045\"]),\n",
" ),\n",
" tooltip=[\n",
" alt.Tooltip(\"year\", title=\"year\", type=\"quantitative\"),\n",
Expand All @@ -265,6 +256,7 @@
" )\n",
" return chart\n",
"\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",
Expand All @@ -287,9 +279,7 @@
" ),\n",
" tooltip=[\n",
" alt.Tooltip(\"name\", title=\"buyer\", type=\"nominal\"),\n",
" alt.Tooltip(\n",
" \"total_tenders\", title=\"number of tenders\", type=\"quantitative\"\n",
" ),\n",
" alt.Tooltip(\"total_tenders\", title=\"number of tenders\", type=\"quantitative\"),\n",
" ],\n",
" )\n",
" .properties(\n",
Expand All @@ -316,4 +306,4 @@
"outputs": []
}
]
}
}
9 changes: 1 addition & 8 deletions component_check_quality.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -547,14 +547,7 @@
"id": "yeBOaDsO6uDS"
},
"source": [
"release_count_chart = sns.catplot(\n",
" x=\"release_count\", y=\"ocid_count\", kind=\"bar\", col=\"collection_id\", hue=\"release_type\", data=release_counts\n",
").set_xticklabels(rotation=90)\n",
"\n",
"for ax in release_count_chart.axes.flat:\n",
" format_thousands(ax.yaxis)\n",
"\n",
"plt.show(release_count_chart)"
"plot_release_count(release_counts)"
],
"execution_count": null,
"outputs": []
Expand Down
2 changes: 1 addition & 1 deletion component_environment.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@
"outputs": []
}
]
}
}
15 changes: 2 additions & 13 deletions component_scope_kingfisher.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,7 @@
"id": "mKo6Q4HimvQZ"
},
"source": [
"objects_per_stage_chart = sns.catplot(x=\"stage\", y=\"object_count\", kind=\"bar\", data=objects_per_stage).set_xticklabels(\n",
" rotation=90\n",
")\n",
"\n",
"for ax in objects_per_stage_chart.axes.flat:\n",
" format_thousands(ax.yaxis)\n",
"\n",
"objects_per_stage"
"plot_objects_per_stage(objects_per_stage)"
],
"execution_count": null,
"outputs": []
Expand Down Expand Up @@ -374,11 +367,7 @@
"release_dates = release_dates.set_index(\"date\")\n",
"release_dates = release_dates.groupby([\"collection_id\", \"release_type\"]).resample(\"M\").sum()\n",
"\n",
"fig, ax = plt.subplots(figsize=[15, 5])\n",
"sns.lineplot(data=release_dates, x=\"date\", y=\"release_count\", hue=\"collection_id\", style=\"release_type\")\n",
"\n",
"format_thousands(ax.yaxis)\n",
"sns.despine()"
"plot_releases_by_month(release_dates)"
],
"execution_count": null,
"outputs": []
Expand Down
45 changes: 3 additions & 42 deletions component_scope_usability.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,7 @@
{
"cell_type": "code",
"source": [
"fig = px.bar(\n",
" stages,\n",
" x=\"stage\",\n",
" y=\"object_count\",\n",
" title=\"Stages covered\",\n",
" template=\"plotly_white\",\n",
" text=\"object_count\",\n",
" labels={\"stage\": \"Stage\", \"object_count\": \"Number of releases\"},\n",
")\n",
"fig.update_traces(marker_color=\"#D6E100\")\n",
"fig.update_layout(width=400, height=350, bargap=0.5)\n",
"fig.show()"
"plot_objects_per_stage(stages)"
],
"metadata": {
"id": "zGNrN_occdW5"
Expand Down Expand Up @@ -242,22 +231,7 @@
{
"cell_type": "code",
"source": [
"final = pd.melt(dates, id_vars=[\"year\"], value_vars=[\"tenders\", \"awards\"])\n",
"fig = px.bar(\n",
" final,\n",
" x=\"year\",\n",
" y=\"value\",\n",
" color=\"variable\",\n",
" barmode=\"group\",\n",
" title=\"Tenders and awards by year\",\n",
" template=\"plotly_white\",\n",
" text=\"value\",\n",
" color_discrete_sequence=[\"#D6E100\", \"#6c75e1\"],\n",
" labels={\"value\": \"Tenders and awards\"},\n",
")\n",
"fig.update_layout(width=600, height=350, bargap=0.5)\n",
"\n",
"fig.show()"
"plot_objects_per_year(dates)"
],
"metadata": {
"id": "ZFUPPW51dF4m"
Expand Down Expand Up @@ -371,20 +345,7 @@
{
"cell_type": "code",
"source": [
"fig = px.bar(\n",
" buyers.head(10),\n",
" y=\"name\",\n",
" x=\"total_tenders\",\n",
" title=\"Top 10 buyers\",\n",
" template=\"plotly_white\",\n",
" text=\"total_tenders\",\n",
" labels={\"total_tenders\": \"Number of procedures\", \"name\": \"buyer\"},\n",
" color_discrete_sequence=[\"#D6E100\", \"#6c75e1\"],\n",
" orientation=\"h\",\n",
")\n",
"# fig.update_traces()\n",
"fig.update_layout(width=850, height=450, bargap=0.4, yaxis={\"categoryorder\": \"total ascending\"})\n",
"fig.show()"
"plot_top_buyers(buyers.head(10))"
],
"metadata": {
"id": "KmgHVCXaiCeG"
Expand Down
6 changes: 6 additions & 0 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,22 @@
],
"template_publisher_analysis": [
"component_environment",
"component_charts",
"component_setup_kingfisher",
"component_errors_kingfisher",
"component_scope_kingfisher",
],
"template_structure_and_format_feedback": [
"component_environment",
"component_charts",
"component_setup_kingfisher",
"component_errors_kingfisher",
"component_scope_kingfisher",
"component_check_structure",
],
"template_data_quality_feedback": [
"component_environment",
"component_charts",
"component_setup_kingfisher",
"component_errors_kingfisher",
"component_scope_kingfisher",
Expand All @@ -39,17 +42,20 @@
],
"template_usability_checks": [
"component_environment",
"component_charts",
"component_setup_kingfisher",
"component_scope_usability",
"component_check_usability",
],
"template_usability_checks_fieldlist": [
"component_environment",
"component_charts",
"component_setup_fieldlist",
"component_check_usability",
],
"template_usability_checks_registry": [
"component_environment",
"component_charts",
"component_setup_registry",
"component_check_usability",
],
Expand Down
Loading

0 comments on commit 30d7829

Please sign in to comment.