aboutsummaryrefslogtreecommitdiffstats
path: root/web/plot.js
blob: 9095ec3abc50fce0f1b63ee06f7326c7dafbf86b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import './lib/d3.v7.min.js';

let POINTS = [];

const x = d3.scaleLinear([0, 120]).nice(30);
const y = d3.scaleLinear([0, 350]);
const line = d3.line(d => x(d[0]), d => y(d[1]));

const margin = 2 * parseFloat(getComputedStyle(document.documentElement).fontSize);

const svg = d3.select('#plot');

// Add the x-axis.
const xa = svg.append('g');

// Add the y-axis.
const ya = svg.append('g');

const PLOTS = {};

const sec = d3.format('02');
const min = d3.format(' ');
const minsec = v => `${min(Math.floor(v / 60))}:${sec(Math.floor(v % 60))}`;
const degc = v => `${Math.floor(v)}°C`;

export const addPlot = (name, color, data=[], interactive=false) => {
  const path = svg.append('path')
    // .attr('cursor', 'copy')
    .attr('fill', 'none')
    .attr('stroke', color)
    .attr('stroke-width', '2');

  const points = interactive && (
    svg.append('g')
    .attr('cursor', 'grab')
  );

  PLOTS[name] = {
    color,
    data,
    points,
    path,
  };
  return PLOTS[name];
};

export const update = (transition=true) => {
  const times = d3.merge(Object.values(PLOTS).map(p => p.data.map(d => d[0])));
  const temps = d3.merge(Object.values(PLOTS).map(p => p.data.map(d => d[1])));

  x.domain([0, d3.max([...times, 120])]).nice(30);
  y.domain([0, d3.max([...temps, 200])]).nice(25);

  const dur = transition ? null : 0;

  xa.transition().duration(dur).call(d3.axisBottom(x).tickFormat(minsec));
  ya.transition().duration(dur).call(d3.axisLeft(y).tickFormat(degc));
  for (const { path, points, data, color } of Object.values(PLOTS)) {
    path.transition().duration(dur).attr('d', line(data));
    points && points.selectAll('circle')
      .data(data)
      .join('circle')
      .attr('fill', color)
      .attr('r', 5)
      .attr('cx', (d) => x(d[0]))
      .attr('cy', (d) => y(d[1]))
      .on('dblclick', function (e, d) {
        e.stopPropagation();
        if (d[0] === 0) return;

        const i = data.indexOf(d);
        data.splice(i, 1);
        update(false);
      })
      .call(
        d3.drag()
          .on('start', () => points.attr('cursor', 'grabbing'))
          .on('drag', function (e, d) {
            const [time, temp] = [x.invert(e.x), y.invert(e.y)];

            const ci = data.indexOf(d);
            if (data[ci-1] && data[ci-1][0] > time) {
              data.splice(ci-1, 0, ...data.splice(ci, 1));
            } else if (data[ci+1] && data[ci+1][0] < time) {
              data.splice(ci+1, 0, ...data.splice(ci, 1));
            }

            if (d[0] !== 0) d[0] = time;
            d[1] = temp;

            d3.select(this)
              .attr('cx', (d) => x(d[0]))
              .attr('cy', (d) => y(d[1]));
            update(false);
          })
          .on('end', () => points.attr('cursor', 'grab'))
      );
  }
};

const onresize = () => {
  const width = svg.node().clientWidth;
  const height = svg.node().clientHeight;
  svg
    .attr('width', width)
    .attr('height', height);

  x.range([margin, width - margin]);
  y.range([height - margin, margin]);
  xa.transition().call(d3.axisBottom(x).tickFormat(minsec));
  ya.transition().call(d3.axisLeft(y).tickFormat(degc));
  for (const { path, data, points } of Object.values(PLOTS)) {
    path.transition().attr('d', line(data));
    points && points.transition().selectAll('circle')
      .attr('cx', (d) => x(d[0]))
      .attr('cy', (d) => y(d[1]));
  }

  xa.attr('transform', `translate(0, ${height - margin})`);
  ya.attr('transform', `translate(${margin}, 0)`);
};
window.addEventListener('resize', onresize);

const crosshair = svg.append('g')
  .attr('display', 'none')
  .attr('color', '#00000080')
  .attr('font-size', 10)
  .attr('font-family', 'sans-serif')
  .attr('stroke-width', 0.5);
crosshair.append('text')
  .attr('id', 'ctextx')
  .attr('y', margin)
  .attr('dx', '0.32em')
  .attr('dy', '1em')
  .attr('fill', 'currentColor');
crosshair.append('text')
  .attr('id', 'ctexty')
  .attr('x', margin)
  .attr('dx', '0.32em')
  .attr('dy', '-0.32em')
  .attr('fill', 'currentColor');;
crosshair.append('line')
  .attr('id', 'clinex')
  .attr('stroke', 'currentColor');
crosshair.append('line')
  .attr('id', 'cliney')
  .attr('stroke', 'currentColor');

svg
  .on('mousemove', (e) => {
    const [px, py] = d3.pointer(e);
    const [time, temp] = [x.invert(px), y.invert(py)];

    const width = svg.node().clientWidth;
    const height = svg.node().clientHeight;
    const inRange = margin < px && px < width - margin &&
      margin < py && py < height - margin;

    crosshair.attr('display', inRange ? null : 'none').lower();
    crosshair.select('#ctextx').text(minsec(time)).attr('x', px);
    crosshair.select('#ctexty').text(degc(temp)).attr('y', py);
    crosshair.select('#clinex').attr('x1', px).attr('x2', px).attr('y1', margin).attr('y2', height - margin);
    crosshair.select('#cliney').attr('y1', py).attr('y2', py).attr('x1', margin).attr('x2', width - margin);
  })
  .on('dblclick', (e) => {
    const [px, py] = d3.pointer(e);
    const [time, temp] = [x.invert(px), y.invert(py)];

    const current = PLOTS.setpoint;

    const ni = current.data.findLastIndex(([tt, tp]) => tt <= time);
    if (current.data[ni][0] === time) {
      current.data[ni][1] = temp;
    } else {
      current.data.splice(ni + 1, 0, [time, temp]);
    }
    update(false);
  });

onresize();
update();