项目作者: gduverger

项目描述 :
📊 Text-based charting utility
高级语言: Python
项目地址: git://github.com/gduverger/plainchart.git
创建时间: 2018-08-26T15:31:05Z
项目社区:https://github.com/gduverger/plainchart

开源协议:MIT License

下载


PlainChart

A text-based, no-dependencies, pip-installable, open-source charting utility in Python.

Usage:

  1. >>> import plainchart
  2. >>> chart = plainchart.PlainChart([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9]) # 🥧
  3. >>> print(chart.render())
  4. ▌▌ ▌▌ ▌▌
  5. ▌▌ ▌▌ ▌▌
  6. ▌▌ ▌▌ ▌▌
  7. ▌▌ ▌▌▌▌▌
  8. ▌▌▌▌▌▌▌▌
  9. ▌▌▌▌▌▌▌▌▌▌▌▌

Installation

To install PlainChart, you can use pipenv or pip:

  1. $ pipenv install plainchart

Features

With PlainChart, you can:

  • render an array of values in a text-based chart
  • limit the height of the chart and have the values rendered accordingly
  • render a different style of chart, e.g., plainchart.PlainChart.bar or plainchart.PlainChart.scatter
  • implement your own style of chart, e.g., mean_html (see example below)

Examples

  1. >>> import plainchart
  2. >>> import random
  3. >>>
  4. >>> values = [random.randint(0, 10) for _ in range(100)]
  5. >>> chart = plainchart.PlainChart(values)
  6. >>>
  7. >>> print(chart.render())
  8. ▌▌
  9. ▌▌ ▌▌ ▌▌ ▌▌
  10. ▌▌ ▌▌ ▌▌▌ ▌▌ ▌▌▌ ▌▌ ▌▌
  11. ▌▌▌ ▌▌ ▌▌▌ ▌▌ ▌▌ ▌▌▌▌ ▌▌ ▌▌ ▌▌▌
  12. ▌▌▌ ▌▌ ▌▌▌ ▌▌ ▌▌ ▌▌▌▌ ▌▌ ▌▌ ▌▌ ▌▌ ▌▌▌▌▌▌▌ ▌▌▌
  13. ▌▌▌ ▌▌ ▌▌▌ ▌▌ ▌▌▌▌▌▌▌▌▌ ▌▌▌ ▌▌ ▌▌ ▌▌ ▌▌▌▌▌▌▌ ▌▌▌ ▌▌ ▌▌
  14. ▌▌▌▌ ▌▌▌ ▌▌▌▌ ▌▌▌▌▌▌▌▌▌▌▌▌ ▌▌▌ ▌▌▌ ▌▌ ▌▌▌ ▌▌▌ ▌▌ ▌▌▌▌▌▌▌▌▌ ▌▌▌ ▌▌ ▌▌
  15. ▌▌▌▌ ▌▌▌▌ ▌▌▌ ▌▌▌▌ ▌▌▌▌▌▌▌▌▌▌▌▌ ▌▌▌ ▌▌ ▌▌▌▌▌▌▌ ▌▌▌▌▌▌▌▌ ▌▌ ▌▌▌▌▌ ▌▌▌▌ ▌▌▌▌▌▌▌▌▌ ▌▌▌ ▌▌▌ ▌▌▌▌
  16. ▌▌▌▌▌ ▌▌▌▌ ▌▌▌▌▌▌▌▌ ▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌ ▌▌ ▌▌ ▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌▌ ▌▌▌▌▌▌▌▌▌▌▌▌▌▌ ▌▌▌▌▌▌▌▌▌▌▌▌▌
  1. >>> import plainchart
  2. >>> import math
  3. >>> import numpy as np
  4. >>>
  5. >>> values = [1.3 + math.sin(x) for x in np.linspace(0, 4 * math.pi, num=100)]
  6. >>> chart = plainchart.PlainChart(values, style=plainchart.PlainChart.scatter)
  7. >>>
  8. >>> print(chart.render())
  9. ×××××××× ×××××××
  10. ××× ××× ××× ×××
  11. ×× ×× ××× ××
  12. ×× ×× × ××
  13. ×× ×× ×× ×× ×
  14. × ×× ×× ××
  15. ×× ×× ×× ××
  16. ××× ×× ×× ××
  17. ××× ×××× ×××× ××××
  18. ××××× ××××

You can also implement your own style of chart. Below is an example of a HTML chart (mean_html.py) with different colors for values below and above the mean.

  1. import plainchart
  2. import random
  3. import statistics
  4. def mean_html(chart, value, y):
  5. mean = statistics.mean(chart.values)
  6. mean_y = chart.y(mean)
  7. value_y = chart.y(value)
  8. if value_y <= mean_y:
  9. if y <= value_y:
  10. return '<span style="color:green">▌</span>'
  11. return '<span style="color:white">▌</span>'
  12. else:
  13. if y <= mean_y:
  14. return '<span style="color:green">▌</span>'
  15. elif y <= value_y:
  16. return '<span style="color:red">▌</span>'
  17. return '<span style="color:white">▌</span>'
  18. values = [random.randint(0, 10) for _ in range(100)]
  19. chart = plainchart.PlainChart(values, style=mean_html)
  20. print(chart.render(new_line='<br>'))
  1. $ python mean_html.py > mean.html

Mean HTML chart

Contribute

Please feel free to open an issue to propose a new feature or point out a bug. You can also fork the PlainChart repository and submit a pull request.

Support

PlainChart is free and under the MIT License. To support its development, you can make a donation to cash.me/$gduverger.