Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: adbc20e3ef94e0b5551f1b7b98d1580a340c3b8f (plain) (blame)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * Copyright (c) 2020 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     BSI Business Systems Integration AG - initial API and implementation
 */

import {Chart, ChartJsRenderer} from '../../src/index';

describe('ChartJsRendererSpec', () => {

  describe('_adjustGrid', () => {
    let renderer = new ChartJsRenderer({}),
      chartArea = {
        top: 0,
        bottom: 300,
        left: 0,
        right: 750
      },
      defaultConfig = {
        data: {
          datasets: [{
            data: [11, 13, 17, 31, 37, 41, 43],
            label: 'Dataset 1'
          }]
        }
      },
      defaultScalesConfig = $.extend(true, {}, defaultConfig, {
        options: {
          scales: {
            xAxes: [{}],
            yAxes: [{}]
          }
        }
      }),
      defaultScaleConfig = $.extend(true, {}, defaultConfig, {
        options: {
          scale: {}
        }
      });

    it('bar chart, min/max is set on y axis', () => {
      let config = $.extend(true, {}, defaultScalesConfig, {type: Chart.Type.BAR});

      renderer._adjustGrid(config, chartArea);

      expect(config.options.scales.xAxes[0]).toEqual({});
      expect(config.options.scales.yAxes[0]).toEqual({
        ticks: {
          maxTicksLimit: 8,
          suggestedMax: 45,
          suggestedMin: 10
        }
      });
    });

    it('horizontal bar chart, min/max is set on x axis', () => {
      let config = $.extend(true, {}, defaultScalesConfig, {type: Chart.Type.BAR_HORIZONTAL});

      renderer._adjustGrid(config, chartArea);

      expect(config.options.scales.xAxes[0]).toEqual({
        ticks: {
          maxTicksLimit: 5,
          suggestedMax: 45,
          suggestedMin: 10
        }
      });
      expect(config.options.scales.yAxes[0]).toEqual({});
    });

    it('polar area chart, min/max is set on scale', () => {
      let config = $.extend(true, {}, defaultScaleConfig, {type: Chart.Type.POLAR_AREA});

      renderer._adjustGrid(config, chartArea);

      expect(config.options.scale).toEqual({
        ticks: {
          maxTicksLimit: 4,
          suggestedMax: 45,
          suggestedMin: 10
        }
      });
    });

    it('bubble chart, min/max is set on x and y axis, axis without offset take max(r) into account', () => {
      let config = $.extend(true, {}, defaultScalesConfig, {
        type: Chart.Type.BUBBLE,
        options: {
          scales: {
            xAxes: [
              {
                offset: true
              }
            ]
          }
        }
      });

      config.data.datasets[0].data = [
        {x: 11, y: 37, r: 47},
        {x: 13, y: 17, r: 29},
        {x: 17, y: 41, r: 19},
        {x: 31, y: 11, r: 53},
        {x: 37, y: 31, r: 13},
        {x: 41, y: 43, r: 11},
        {x: 43, y: 13, r: 37}
      ];

      renderer._adjustGrid(config, chartArea);

      let height = Math.abs(chartArea.top - chartArea.bottom),
        padding = 53, // max(r)
        maxY = 43,
        minY = 11,
        yValuePerPixel = (maxY - minY) / (height - 2 * padding),
        yPaddingValue = yValuePerPixel * padding;

      expect(config.options.scales.xAxes[0]).toEqual({
        offset: true,
        ticks: {
          maxTicksLimit: 5,
          suggestedMax: 46,
          suggestedMin: 11
        }
      });
      expect(config.options.scales.yAxes[0]).toEqual({
        ticks: {
          maxTicksLimit: 8,
          suggestedMax: 43 + yPaddingValue,
          suggestedMin: 11 - yPaddingValue
        }
      });
    });

    it('bubble chart, min/max is set on x and y axis, axis without offset take max(r) into account, axis with labelMap calculate exact min/max', () => {

      let labelMap = {
          11: 'Label 11',
          13: 'Label 13',
          17: 'Label 17',
          31: 'Label 31',
          37: 'Label 37',
          41: 'Label 41',
          43: 'Label 43'
        },
        config = $.extend(true, {}, defaultScalesConfig, {
          type: Chart.Type.BUBBLE,
          options: {
            scales: {
              xLabelMap: labelMap,
              yLabelMap: labelMap,
              xAxes: [
                {
                  offset: true
                }
              ]
            }
          }
        });

      config.data.datasets[0].data = [
        {x: 11, y: 37, r: 47},
        {x: 13, y: 17, r: 29},
        {x: 17, y: 41, r: 19},
        {x: 31, y: 11, r: 53},
        {x: 37, y: 31, r: 13},
        {x: 41, y: 43, r: 11},
        {x: 43, y: 13, r: 37}
      ];

      renderer._adjustGrid(config, chartArea);

      expect(config.options.scales.xAxes[0]).toEqual({
        offset: true,
        ticks: {
          maxTicksLimit: 5,
          suggestedMax: 43,
          suggestedMin: 11
        }
      });
      expect(config.options.scales.yAxes[0]).toEqual({
        ticks: {
          maxTicksLimit: 8,
          suggestedMax: 52,
          suggestedMin: 2
        }
      });
    });
  });

  describe('_adjustBubbleSizes', () => {
    let renderer = new ChartJsRenderer({}),
      chartArea = {
        top: 0,
        bottom: 300,
        left: 0,
        right: 750
      },
      defaultConfig = {
        type: Chart.Type.BUBBLE,
        data: {
          datasets: [{
            data: [
              {z: 100},
              {z: 81},
              {z: 49},
              {z: 25},
              {z: 16}
            ],
            label: 'Dataset 1'
          }]
        },
        bubble: {}
      };

    it('neither sizeOfLargestBubble nor minBubbleSize is set', () => {
      let config = $.extend(true, {}, defaultConfig);

      renderer._adjustBubbleSizes(config, chartArea);

      expect(config.data.datasets[0].data).toEqual([
        {r: 10, z: 100},
        {r: 9, z: 81},
        {r: 7, z: 49},
        {r: 5, z: 25},
        {r: 4, z: 16}
      ]);
    });

    it('only sizeOfLargestBubble is set', () => {
      let config = $.extend(true, {}, defaultConfig);
      config.bubble.sizeOfLargestBubble = 20;

      renderer._adjustBubbleSizes(config, chartArea);

      expect(config.data.datasets[0].data).toEqual([
        {r: 20, z: 100},
        {r: 18, z: 81},
        {r: 14, z: 49},
        {r: 10, z: 25},
        {r: 8, z: 16}
      ]);
    });

    it('only sizeOfLargestBubble is set, maxR equals 0', () => {
      let config = $.extend(true, {}, defaultConfig);
      config.bubble.sizeOfLargestBubble = 20;

      config.data.datasets[0].data = [
        {z: 0},
        {z: 0},
        {z: 0}
      ];

      renderer._adjustBubbleSizes(config, chartArea);

      expect(config.data.datasets[0].data).toEqual([
        {r: 20, z: 0},
        {r: 20, z: 0},
        {r: 20, z: 0}
      ]);
    });

    it('only minBubbleSize is set', () => {
      let config = $.extend(true, {}, defaultConfig);
      config.bubble.minBubbleSize = 5;

      renderer._adjustBubbleSizes(config, chartArea);

      expect(config.data.datasets[0].data).toEqual([
        {r: 12.5, z: 100},
        {r: 11.25, z: 81},
        {r: 8.75, z: 49},
        {r: 6.25, z: 25},
        {r: 5, z: 16}
      ]);
    });

    it('only minBubbleSize is set, minR equals 0', () => {
      let config = $.extend(true, {}, defaultConfig);
      config.bubble.minBubbleSize = 5;

      config.data.datasets[0].data.push({z: 0});

      renderer._adjustBubbleSizes(config, chartArea);

      expect(config.data.datasets[0].data).toEqual([
        {r: 15, z: 100},
        {r: 14, z: 81},
        {r: 12, z: 49},
        {r: 10, z: 25},
        {r: 9, z: 16},
        {r: 5, z: 0}
      ]);
    });

    it('sizeOfLargestBubble and minBubbleSize are set, scaling is sufficient', () => {
      let config = $.extend(true, {}, defaultConfig);
      config.bubble.sizeOfLargestBubble = 20;
      config.bubble.minBubbleSize = 5;

      renderer._adjustBubbleSizes(config, chartArea);

      expect(config.data.datasets[0].data).toEqual([
        {r: 20, z: 100},
        {r: 18, z: 81},
        {r: 14, z: 49},
        {r: 10, z: 25},
        {r: 8, z: 16}
      ]);
    });

    it('sizeOfLargestBubble and minBubbleSize are set, scaling is not sufficient', () => {
      let config = $.extend(true, {}, defaultConfig);
      config.bubble.sizeOfLargestBubble = 20;
      config.bubble.minBubbleSize = 5;

      config.data.datasets[0].data.push({z: 1});

      renderer._adjustBubbleSizes(config, chartArea);

      expect(config.data.datasets[0].data).toEqual([
        {r: 20, z: 100},
        {r: 9 * (5 / 3) + (10 / 3), z: 81},
        {r: 15, z: 49},
        {r: 5 * (5 / 3) + (10 / 3), z: 25},
        {r: 10, z: 16},
        {r: 5, z: 1}
      ]);
    });
  });
});

Back to the top