frappe-charts 2.1.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "frappe-charts",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "main": "dist/frappe-charts.esm.js",
5
5
  "module": "dist/frappe-charts.esm.js",
6
6
  "browser": "dist/frappe-charts.umd.js",
@@ -8,7 +8,7 @@ import { makeOverlay, updateOverlay, legendDot, makeHoverBand, FONT_SIZE, AXIS_T
8
8
  import {
9
9
  BASE_MEASURES, getExtraHeight, getTopOffset, getLeftOffset, MIN_BAR_PERCENT_HEIGHT, BAR_CHART_SPACE_RATIO,
10
10
  BAR_CHART_RADIUS, BAR_CHART_GAP, BAR_CHART_MAX_WIDTH, LINE_CHART_DOT_SIZE, LEGEND_DOT_SIZE, LEGEND_FONT_SIZE,
11
- LEGEND_CHAR_WIDTH, TOOLTIP_SIDE_GAP
11
+ LEGEND_CHAR_WIDTH, TOOLTIP_SIDE_GAP, DEFAULT_CHAR_WIDTH
12
12
  } from '../utils/constants';
13
13
  import { shortenLargeNumber } from '../utils/draw-utils';
14
14
  import { getColor } from '../utils/colors';
@@ -70,6 +70,7 @@ export default class AxisChart extends BaseChart {
70
70
 
71
71
  this.config.xAxisMode = options.axisOptions.xAxisMode || 'span';
72
72
  this.config.xOffset = options.axisOptions.xOffset;
73
+ this.config.xAxisType = options.axisOptions.xAxisType;
73
74
  this.config.yAxisMode = options.axisOptions.yAxisMode || 'span';
74
75
  this.config.xIsSeries = options.axisOptions.xIsSeries || 0;
75
76
  this.config.shortenYAxisNumbers = options.axisOptions.shortenYAxisNumbers || 0;
@@ -147,14 +148,37 @@ export default class AxisChart extends BaseChart {
147
148
  s.unitWidth = this.width / (edgeToEdge ? s.datasetLength - 1 : s.datasetLength);
148
149
  s.xOffset = edgeToEdge ? 0 : s.unitWidth / 2;
149
150
 
151
+ let times = !hasBars && this.config.xAxisType === 'time' ? this.getLabelTimes() : null;
152
+ let span = this.width - 2 * s.xOffset;
153
+
150
154
  s.xAxis = {
151
155
  labels: labels,
156
+ isTime: !!times,
152
157
  positions: labels.map((d, i) =>
153
- floatTwo(s.xOffset + i * s.unitWidth)
158
+ floatTwo(s.xOffset + (times
159
+ ? (times[i] - times[0]) / (times[times.length - 1] - times[0]) * span
160
+ : i * s.unitWidth))
154
161
  )
155
162
  };
156
163
  }
157
164
 
165
+ getSpacedLabels() {
166
+ let { labels, positions } = this.state.xAxis;
167
+ let lastEnd = -Infinity;
168
+ return labels.map((label, i) => {
169
+ let half = getStringWidth(label, DEFAULT_CHAR_WIDTH) / 2;
170
+ if (positions[i] - half < lastEnd) return '';
171
+ lastEnd = positions[i] + half + DEFAULT_CHAR_WIDTH;
172
+ return label + '';
173
+ });
174
+ }
175
+
176
+ getLabelTimes() {
177
+ let times = this.data.labels.map(d => new Date(d).getTime());
178
+ let valid = times.length > 1 && times.every((t, i) => !isNaN(t) && (i === 0 || t >= times[i - 1]));
179
+ return valid && times[times.length - 1] > times[0] ? times : null;
180
+ }
181
+
158
182
  // always a list, even when empty, so the marker layer exists for markers added by a later update
159
183
  calcXMarkers() {
160
184
  let s = this.state;
@@ -272,6 +296,12 @@ export default class AxisChart extends BaseChart {
272
296
  return [].concat(...allValueLists);
273
297
  }
274
298
 
299
+ formatValueLabel(value) {
300
+ return this.config.numberFormatter
301
+ ? this.config.numberFormatter(value)
302
+ : shortenLargeNumber(value);
303
+ }
304
+
275
305
  setupComponents() {
276
306
  let componentConfigs = [
277
307
  [
@@ -296,8 +326,10 @@ export default class AxisChart extends BaseChart {
296
326
  },
297
327
  function () {
298
328
  let s = this.state;
299
- s.xAxis.calcLabels = getShortenedLabels(this.width,
300
- s.xAxis.labels, this.config.xIsSeries,this.config.seriesLabelSpaceRatio);
329
+ s.xAxis.calcLabels = s.xAxis.isTime
330
+ ? this.getSpacedLabels()
331
+ : getShortenedLabels(this.width,
332
+ s.xAxis.labels, this.config.xIsSeries,this.config.seriesLabelSpaceRatio);
301
333
 
302
334
  return s.xAxis;
303
335
  }.bind(this)
@@ -379,9 +411,9 @@ export default class AxisChart extends BaseChart {
379
411
  let labels = new Array(s.datasetLength).fill('');
380
412
  if (this.config.valuesOverPoints) {
381
413
  if (!stacked) {
382
- labels = d.values.map(v => shortenLargeNumber(v));
414
+ labels = d.values.map(v => this.formatValueLabel(v));
383
415
  } else if (d.index === s.datasets.length - 1) {
384
- labels = d.cumulativeYs.map(v => shortenLargeNumber(v));
416
+ labels = d.cumulativeYs.map(v => this.formatValueLabel(v));
385
417
  }
386
418
  }
387
419
 
@@ -417,6 +449,7 @@ export default class AxisChart extends BaseChart {
417
449
 
418
450
  // same for all datasets
419
451
  valuesOverPoints: this.config.valuesOverPoints,
452
+ numberFormatter: this.config.numberFormatter,
420
453
  },
421
454
  function () {
422
455
  let s = this.state;
@@ -6,6 +6,10 @@ import {
6
6
  animateDot, animatePath, animatePathStr
7
7
  } from '../utils/animate';
8
8
 
9
+ function formatPointValue(value, numberFormatter) {
10
+ return numberFormatter && value !== null && value !== undefined ? numberFormatter(value) : value;
11
+ }
12
+
9
13
  class ChartComponent {
10
14
  constructor({
11
15
  layerClass = '',
@@ -434,7 +438,7 @@ let componentConfigs = {
434
438
  y,
435
439
  visible ? data.radius : 0,
436
440
  c.color,
437
- (c.valuesOverPoints ? data.values[j] : ''),
441
+ (c.valuesOverPoints ? formatPointValue(data.values[j], c.numberFormatter) : ''),
438
442
  j,
439
443
  o.hideDotBorder
440
444
  );
@@ -449,7 +453,7 @@ let componentConfigs = {
449
453
  data.yPositions[lastIndex],
450
454
  data.radius,
451
455
  c.color,
452
- (c.valuesOverPoints ? data.values[lastIndex] : ''),
456
+ (c.valuesOverPoints ? formatPointValue(data.values[lastIndex], c.numberFormatter) : ''),
453
457
  lastIndex,
454
458
  o.hideDotBorder
455
459
  );