Commit 54e2baa67f79681ec5f63314112d6e1420d93fae
1 parent
f25c2405
Exists in
master
and in
4 other branches
Add charge charts to Project page
Showing
4 changed files
with
95 additions
and
2 deletions
Show diff stats
app/db_mgr.py
@@ -113,7 +113,7 @@ def charges_by_project_stacked(project_id, category="service"): | @@ -113,7 +113,7 @@ def charges_by_project_stacked(project_id, category="service"): | ||
113 | # build the charges line for the current period | 113 | # build the charges line for the current period |
114 | category_charges = [period_name] | 114 | category_charges = [period_name] |
115 | for (category_rate,) in db.session.execute(charge_by_categorie_req): | 115 | for (category_rate,) in db.session.execute(charge_by_categorie_req): |
116 | - category_rate = '0' if category_rate is None else str(category_rate) | 116 | + category_rate = str(category_rate) if category_rate else '0' |
117 | category_charges.append(category_rate) | 117 | category_charges.append(category_rate) |
118 | all_charges.append(category_charges) | 118 | all_charges.append(category_charges) |
119 | 119 |
app/main/static/css/charges.css
1 | + | ||
1 | #charge_table, | 2 | #charge_table, |
2 | #charge_table th, | 3 | #charge_table th, |
3 | #charge_table td { | 4 | #charge_table td { |
@@ -11,7 +12,7 @@ | @@ -11,7 +12,7 @@ | ||
11 | padding: 0.8em; | 12 | padding: 0.8em; |
12 | } | 13 | } |
13 | 14 | ||
14 | -#charge_div { | 15 | +.charge_chart { |
15 | background-color: #fAfAfA; | 16 | background-color: #fAfAfA; |
16 | border: 1pt solid black; | 17 | border: 1pt solid black; |
17 | display: inline-block; | 18 | display: inline-block; |
@@ -0,0 +1,80 @@ | @@ -0,0 +1,80 @@ | ||
1 | +const margin = {top: 150, right: 40, bottom: 10, left: 40}, | ||
2 | + width = 1000 - margin.left - margin.right, | ||
3 | + height = 500 - margin.top - margin.bottom; | ||
4 | + | ||
5 | +const tooltip_offset = {dx: 300, dy: 120} | ||
6 | + | ||
7 | +const x = d3.scaleBand() | ||
8 | + .range([0, width]) | ||
9 | + .padding(0.2); | ||
10 | + | ||
11 | +const y = d3.scaleLinear() | ||
12 | + .range([height, 0]); | ||
13 | + | ||
14 | +var color = d3.scaleOrdinal() | ||
15 | + .range([ | ||
16 | + "#800000", | ||
17 | + "#FFFF00", | ||
18 | + "#808000", | ||
19 | + "#00FF00", | ||
20 | + "#008000", | ||
21 | + "#00FFFF", | ||
22 | + "#008080", | ||
23 | + "#0000FF", | ||
24 | + "#000080", | ||
25 | + "#FF00FF", | ||
26 | + "#800080", | ||
27 | + ]); | ||
28 | + | ||
29 | +function build_chart(div_selector, data_url) { | ||
30 | + | ||
31 | + const svg = d3.select(div_selector).append("svg") | ||
32 | + .attr("id", "svg") | ||
33 | + .attr("width", width + margin.left + margin.right) | ||
34 | + .attr("height", height + margin.top + margin.bottom) | ||
35 | + .append("g") | ||
36 | + .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | ||
37 | + | ||
38 | + d3.csv(data_url).then(data => { | ||
39 | + var services = data.columns.slice(1) | ||
40 | + var periods = d3.map(data, d => d.period) | ||
41 | + | ||
42 | + console.log(data) | ||
43 | + | ||
44 | + var stack = d3.stack() | ||
45 | + .keys(services) | ||
46 | + // .order(d3.stackOrderNone) | ||
47 | + // .offset(d3.stackOffsetNone); | ||
48 | + var stacked_data = stack(data) | ||
49 | + | ||
50 | + // console.log(stacked_data) | ||
51 | + const x = d3.scaleBand() | ||
52 | + .domain(periods) | ||
53 | + .range([0, width]) | ||
54 | + .padding(0.1); | ||
55 | + | ||
56 | + const y = d3.scaleLinear() | ||
57 | + .domain([0, 100]) | ||
58 | + .range([height, 0]); | ||
59 | + | ||
60 | + | ||
61 | + let groups = svg.selectAll("g.category") | ||
62 | + .data(stacked_data) | ||
63 | + .enter() | ||
64 | + .append("g") | ||
65 | + .style("fill", (d) => color(d.key)); | ||
66 | + | ||
67 | + let rect = groups.selectAll("rect") | ||
68 | + .data(d => d) | ||
69 | + .enter() | ||
70 | + .append("rect") | ||
71 | + .attr("x", d => x(d.data.period)) | ||
72 | + .attr("width", x.bandwidth()) | ||
73 | + // .attr("width", 5) | ||
74 | + .attr("y", d => y(d[1])) | ||
75 | + .attr("height", d => height - y(d[1] - d[0])) | ||
76 | + .attr("stroke", "black"); | ||
77 | + | ||
78 | + }); | ||
79 | + | ||
80 | +} |
app/main/templates/project.html
@@ -5,6 +5,10 @@ | @@ -5,6 +5,10 @@ | ||
5 | {% endblock %} | 5 | {% endblock %} |
6 | 6 | ||
7 | {% block content %} | 7 | {% block content %} |
8 | +<div id="project_services_chart" class="charge_chart"></div> | ||
9 | +<hr/> | ||
10 | +<div id="project_capacities_chart" class="charge_chart"></div> | ||
11 | +<hr/> | ||
8 | <table id="charge_table"> | 12 | <table id="charge_table"> |
9 | <thead> | 13 | <thead> |
10 | <tr> | 14 | <tr> |
@@ -26,4 +30,12 @@ | @@ -26,4 +30,12 @@ | ||
26 | {% endblock %} | 30 | {% endblock %} |
27 | 31 | ||
28 | {% block more_scripts %} | 32 | {% block more_scripts %} |
33 | +{% include 'd3js-includes.html' %} | ||
34 | +<script src="{{ url_for('main.static', filename='js/charges.js') }}" type="text/javascript"></script> | ||
35 | +<script> | ||
36 | + build_chart("#project_services_chart", | ||
37 | + "{{url_for('main.charge_project_csv', project_id=project.id, category='service')}}"); | ||
38 | + build_chart("#project_capacities_chart", | ||
39 | + "{{url_for('main.charge_project_csv', project_id=project.id, category='capacity')}}"); | ||
40 | +</script> | ||
29 | {% endblock %} | 41 | {% endblock %} |