-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chart experiments #316
base: master
Are you sure you want to change the base?
Chart experiments #316
Changes from 9 commits
c3c19a2
9ec5ccc
cafb5c4
9ff7282
bc19e7c
6d8909a
da9fe50
976f55b
a2196f4
51bc8f9
c21bf11
e50363f
8adad83
e69a3d2
5ba77ca
3b5cd41
96fde3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<template> | ||
<ul> | ||
<li v-bind:key="chartConfig.title" v-for="chartConfig in chartConfigs"> | ||
<router-link | ||
:to="{ | ||
name: 'chart', | ||
params: { chartSlug: chartConfig.chart_slug } | ||
}" | ||
class="subtle" | ||
> | ||
<font-awesome-icon icon="level-up-alt" class="rotated90" /> | ||
<span>{{ chartConfig.title }}</span> | ||
</router-link> | ||
</li> | ||
</ul> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import Vue from "vue" | ||
|
||
export default Vue.extend({ | ||
computed: { | ||
chartConfigs() { | ||
return this.$store.state.chartConfigs | ||
} | ||
}, | ||
async mounted() { | ||
await this.$store.dispatch("fetchChartConfigs") | ||
} | ||
}) | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<template> | ||
<div> | ||
<h3>{{ chartConfigs.title }}</h3> | ||
<pie-chart | ||
width="650px" | ||
height="450px" | ||
:download="{ background: '#ffffff' }" | ||
v-if="chartConfigs.chart_type == 'Pie'" | ||
:data="chartConfigs.data" | ||
></pie-chart> | ||
<line-chart | ||
width="650px" | ||
height="450px" | ||
:download="{ background: '#ffffff' }" | ||
v-else-if="chartConfigs.chart_type == 'Line'" | ||
:data="chartConfigs.data" | ||
></line-chart> | ||
<column-chart | ||
width="650px" | ||
height="450px" | ||
:download="{ background: '#ffffff' }" | ||
v-else-if="chartConfigs.chart_type == 'Column'" | ||
:data="chartConfigs.data" | ||
></column-chart> | ||
<bar-chart | ||
width="650px" | ||
height="450px" | ||
:download="{ background: '#ffffff' }" | ||
v-else-if="chartConfigs.chart_type == 'Bar'" | ||
:data="chartConfigs.data" | ||
></bar-chart> | ||
<area-chart | ||
width="650px" | ||
height="450px" | ||
:download="{ background: '#ffffff' }" | ||
v-else-if="chartConfigs.chart_type == 'Area'" | ||
:data="chartConfigs.data" | ||
></area-chart> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import Vue from "vue" | ||
|
||
export default Vue.extend({ | ||
computed: { | ||
chartConfigs() { | ||
return this.$store.state.chartConfigs | ||
} | ||
} | ||
}) | ||
</script> | ||
|
||
<style scoped lang="less"> | ||
h3 { | ||
text-transform: capitalize; | ||
text-align: center; | ||
} | ||
</style> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<template> | ||
<div> | ||
<div class="sidebar_wrapper"> | ||
<ul class="table_list"> | ||
<TableNavItem | ||
v-bind:key="tableName" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<template> | ||
<DetailViewBase> | ||
<ChartsPage :chartSlug="chartSlug" /> | ||
</DetailViewBase> | ||
</template> | ||
|
||
|
||
<script lang="ts"> | ||
import Vue from "vue" | ||
import ChartsPage from "../components/ChartsPage.vue" | ||
import DetailViewBase from "../components/DetailViewBase.vue" | ||
|
||
export default Vue.extend({ | ||
props: { | ||
chartSlug: String | ||
}, | ||
components: { | ||
ChartsPage, | ||
DetailViewBase | ||
}, | ||
async mounted() { | ||
await this.$store.dispatch("fetchChartConfig", this.chartSlug) | ||
} | ||
}) | ||
</script> | ||
|
||
|
||
<style scoped lang="less"> | ||
</style> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import asyncio | ||
|
||
from fastapi import FastAPI | ||
from fastapi.routing import Mount | ||
from home.tables import Director, Movie | ||
|
||
from piccolo_admin.endpoints import ChartConfig, create_admin | ||
|
||
|
||
# count directors movies | ||
async def director_movie_count(): | ||
movies = await Movie.select( | ||
Movie.director.name.as_alias("director"), | ||
Count(Movie.id), | ||
).group_by(Movie.director) | ||
# Flatten the response so it's a list of lists | ||
# like [['George Lucas', 3], ...] | ||
return [[i["director"], i["count"]] for i in movies] | ||
|
||
|
||
chart_data = asyncio.run(director_movie_count()) | ||
|
||
|
||
director_chart = ChartConfig( | ||
title="Movie count", | ||
chart_type="Pie", | ||
data=chart_data, | ||
) | ||
|
||
|
||
|
||
app = FastAPI( | ||
routes=[ | ||
Mount( | ||
"/admin/", | ||
create_admin( | ||
tables=[Director, Movie], | ||
charts=[director_chart], | ||
), | ||
), | ||
], | ||
) | ||
|
||
# For Starlette it is identical, just `app = Starlette(...)` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from piccolo.columns.column_types import Varchar, ForeignKey | ||
from piccolo.table import Table | ||
|
||
class Director(Table): | ||
name = Varchar() | ||
|
||
class Movie(Table): | ||
title = Varchar() | ||
director = ForeignKey(references=Director) |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now I noticed that the image in the docs is wrong (use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sinisaos OK will change that |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
.. _Charts: | ||
|
||
Charts | ||
====== | ||
|
||
Piccolo Admin can display different types of charts based on your | ||
data. Five chart types are supported: ``Pie``, ``Line``, ``Column``, | ||
``Bar`` and ``Area``. | ||
|
||
Here's an example of charts usage, using FastAPI: | ||
|
||
.. literalinclude:: ./examples/app.py | ||
|
||
Piccolo Admin will then show a chart in the UI. | ||
|
||
.. image:: ./images/charts_sidebar.png | ||
|
||
.. image:: ./images/chart.png | ||
|
||
.. hint:: | ||
|
||
The data format must be a list of lists | ||
(e.g. ``[["Male", 7], ["Female", 3]]``). | ||
|
||
------------------------------------------------------------------------------- | ||
|
||
Source | ||
------ | ||
|
||
.. currentmodule:: piccolo_admin.endpoints | ||
|
||
.. autoclass:: ChartConfig |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a way to change Chart.js colors in dark mode, but unfortunately it doesn't work without refreshing the page. I'm sorry I didn't check before posting the suggested change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, it's still useful - there might be a way to reload the chart without reloading the entire page.