Skip to content
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

Automatically generated chart component #112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ django~=3.1.0
pymongo==3.11
PyMySQL==1.0.2
django-currentuser
django-python3_ldap==0.11.4
django-python3_ldap==0.11.4
plotly==5.13.0
kaleido==0.2.1
54 changes: 54 additions & 0 deletions writehat/components/ChartComponent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from .base import *
import plotly.graph_objects as go
from base64 import b64encode


class ChartComponentForm(ComponentForm):

name = forms.CharField(label='Title', required=False)
pageBreakBefore = forms.BooleanField(
label='Start On New Page?', required=False)


class Component(BaseComponent):
default_name = 'Chart'
formClass = ChartComponentForm
htmlTemplate = 'componentTemplates/ChartComponent.html'
iconType = 'far fa-chart-bar'
iconColor = '#fff'

def preprocess(self, context):
findings = context["report"].findings
data = {
"Critical": 0,
"High": 0,
"Medium": 0,
"Low": 0,
"Informational": 0
}
for finding in findings:
data[finding.severity] += 1

x = list(data.keys())
y = list(data.values())

fig = go.Figure(
data=[go.Bar(x=x,
y=y,
text=y,
textposition="auto",
insidetextanchor="middle",
insidetextfont_size=16,
marker_color=["#a600ff", "#FF0000",
"#ff711e", "#ffc803", "#4894e0"],
textfont=dict(color="white"),
width=0.5
)],
# layout_title_text="Number of Findings",
)
maxValue = 5 if max(data.values()) < 5 else max(data.values()) + 2
fig.update_yaxes(range=[0, maxValue], dtick=1)
b64image = b64encode(fig.to_image(format="svg", width=720))
context['chartb64'] = b64image.decode()

return context
3 changes: 3 additions & 0 deletions writehat/templates/componentTemplates/ChartComponent.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<img src="data:image/svg+xml;base64,{{ chartb64 }}">
</div>