Commit 54e2baa67f79681ec5f63314112d6e1420d93fae

Authored by hitier
1 parent f25c2405

Add charge charts to Project page

app/db_mgr.py
... ... @@ -113,7 +113,7 @@ def charges_by_project_stacked(project_id, category="service"):
113 113 # build the charges line for the current period
114 114 category_charges = [period_name]
115 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 117 category_charges.append(category_rate)
118 118 all_charges.append(category_charges)
119 119  
... ...
app/main/static/css/charges.css
  1 +
1 2 #charge_table,
2 3 #charge_table th,
3 4 #charge_table td {
... ... @@ -11,7 +12,7 @@
11 12 padding: 0.8em;
12 13 }
13 14  
14   -#charge_div {
  15 +.charge_chart {
15 16 background-color: #fAfAfA;
16 17 border: 1pt solid black;
17 18 display: inline-block;
... ...
app/main/static/js/charges.js 0 → 100644
... ... @@ -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 5 {% endblock %}
6 6  
7 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 12 <table id="charge_table">
9 13 <thead>
10 14 <tr>
... ... @@ -26,4 +30,12 @@
26 30 {% endblock %}
27 31  
28 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 41 {% endblock %}
... ...