test-scatterplot.html 1.76 KB
Newer Older
Sascha Herzinger's avatar
Sascha Herzinger committed
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Scatterplot</title>
    <script src="http://localhost:8080/ada-charts.js"></script>
</head>
<body>
    <div id="scatterplot-controls">
        <span>API examples:</span>
        <button onclick="update()">update()</button>
        <button onclick="toPNG()">toPNG()</button>
    </div>
    <div id="scatterplot-container"></div>
</body>
</html>

<script>

  /**
   * Helper function. Do not copy
   */

  function generateRandomData(numOfPoints, minValue, maxValue) {
    const values = [];
    for (let i = 0; i < numOfPoints; i++) {
      const x = Math.random() * (maxValue - minValue + 1) + minValue;
      const y = Math.random() * (maxValue - minValue + 1) + minValue;
Sascha Herzinger's avatar
Sascha Herzinger committed
29
      const z = Math.random() < 0.33 ? 0 : Math.random() > 0.66 ? 1 : 2;
Sascha Herzinger's avatar
Sascha Herzinger committed
30
      values.push([x, y, z]);
Sascha Herzinger's avatar
Sascha Herzinger committed
31
32
33
34
35
36
37
38
39
40
41
42
    }
    return values;
  }

  /**
   * API Documentation below this point
   */
  const container = document.querySelector('#scatterplot-container');
  const scatterplot = AdaCharts.chart({ chartType: 'scatterplot', container });

  function update() {
    scatterplot.update({
Sascha Herzinger's avatar
Sascha Herzinger committed
43
      values: generateRandomData(1000, -100, 100),
Sascha Herzinger's avatar
Sascha Herzinger committed
44
      categories: { name: 'Gender', 0: 'male male male male male male male male male male male', 2: 'female female female female female female female', 1: 'NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA'},
Sascha Herzinger's avatar
Sascha Herzinger committed
45
      callback: d => console.log(d),
Sascha Herzinger's avatar
Sascha Herzinger committed
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
    });
  }

  function toPNG() {
    scatterplot.toPNG();
  }

  document.addEventListener('DOMContentLoaded', update, false);
</script>

<style>
    body {
        display: flex;
        flex-direction: row;
    }

    #scatterplot-controls {
        display: flex;
        flex-direction: column;
    }

    #scatterplot-container {
        width: 50vw;
    }
</style>