charts.css.py
provides a python API to convert your 2-dimension data lists into html snippet,
which will be rendered into charts when serving inside a browser.
Characteristic:
charts.css.py
is a normal HTML table.
Search engines and screen readers will be able to consume your data even when CSS rendering is unavailable.
charts.css.py
is built on top of its upstream project
Charts.css.
pip
:
pip install charts.css.pyIf your project is powered by Brython, you can choose to use our JS package. For example, this online document is using this javascript file, hosted in our Github Release page:
<script src="https://github.com/rayluo/charts.css.py/releases/download/x.y.z/charts.css.py-brython.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/charts.css/dist/charts.min.css"> <style> .charts-css { /* You have to use this class name */ height: 400px; /* Most charts need some height to operate */ width: 80%; /* Optional */ margin: 1em auto; /* Optional */ } .charts-css.legend.legend-inline { /* You have to use this class name */ height: 3em; /* Optional */ } </style>
charts.css.py
have a unified data format as a 2-dimension list,
optionally with headers in the first column and/or first row. They look like this:
data = [ ["Continent", "1st year", "2nd year", "3rd year", "4th year", "5th year"], ["Asia", 20.0, 30.0, 40.0, 50.0, 75.0], ["Euro", 40.0, 60.0, 75.0, 90.0, 100.0], ]Even when you are working with one-dimension data, you would still need to format them as 2-dimension list, which contains one column:
data = [ [20.0], [40.0], ]Some charts can also work with one row:
data = [ [20.0, 30.0, 40.0, 50.0, 75.0], ]
charts.css.py
have a unified data format as a 2-dimension list,
optionally with headers in the first column and/or first row.
The following sample has a 2-dimension list which contains real 2-dimension numeric data,
and then renders them as 4 different kind of charts.
Here come the different charts generated by the snippet above.
Get yourself familiar with their common characteristics:
Sometimes you are dealing with 1-dimension numeric data. You will have 2 options to organize your data: pack them as either a 1 horizontal row or 1 vertical column into the 2-dimension list.
This sample is showing the outcome of 1 horizontal row.
In such a one-dimension horizontal numeric data scenario, bar chart will look similar to the previous bar chart. It is just that its primary axis contains only one data point, and righfully so.charts.css.py
will treat it as one dataset with multiple data points.
Bar chart will be rendered differently:
The above 2 sections tell us that the 2-dimension data would be rendered differently, based on which dimension is organized as a row or as a column. Sometimes our raw data is already in one shape, but we may want to "flip" their row and column, i.e. "flip" their x-axis and y-axis, for them to be rendered to a chart that we want. How do we achieve that?
We got you covered.
That "flipping x-axis and y-axis" is also known as "to transpose a matrix" in math.
So, charts.css.py
provides a transpose(...)
.
In particular, the following sample shows
how to convert a "horizontally wide and vertically short" list of numeric data
into a "horizontally narrow but vertically long" 2-dimension list,
and render them as line chart.
bar()
and column()
support a boolean parameter stacked
.
Simply enable that, you will get a stacked bar chart (or column chart).
A percentage=True
can switch stacked column or bar chart to percentage view.
The sample below is stacked by value.
charts.css.py
's line chart and area chart
are designed to consume a "1-dimension vertical" matrix
rather than a "1-dimension horizontal" matrix,
it is roughly because the tall-and-thin, vertically-scrolling layout is more prevalent in CSV.