test-scatterplot.html 1.75 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
<!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++) {
Sascha Herzinger's avatar
Sascha Herzinger committed
27
28
      const x = parseInt(Math.random() * (maxValue - minValue + 1) + minValue, 10);
      const y = parseInt(Math.random() * (maxValue - minValue + 1) + minValue, 10);
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
44
      values: generateRandomData(2000, -10000, 10000),
      categories: { name: 'Gender', 0: 'male', 1: 'female', 2: 'N/A'},
Sascha Herzinger's avatar
Sascha Herzinger committed
45
      title: 'Scatterplot',
46
47
      xAxisLabel: 'X Axis Values',
      yAxisLabel: 'Y Axis Values',
Sascha Herzinger's avatar
Sascha Herzinger committed
48
      callback: d => console.log(d),
Sascha Herzinger's avatar
Sascha Herzinger committed
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
    });
  }

  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>