chartist 1.3.1 → 1.4.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/dist/index.js CHANGED
@@ -3273,7 +3273,9 @@ class BarChart extends BaseChart {
3273
3273
  // Label direction can be 'neutral', 'explode' or 'implode'. The labels anchor will be positioned based on those settings as well as the fact if the labels are on the right or left side of the center of the chart. Usually explode is useful when labels are positioned far away from the center.
3274
3274
  labelDirection: "neutral",
3275
3275
  // If true empty values will be ignored to avoid drawing unnecessary slices and labels
3276
- ignoreEmptyValues: false
3276
+ ignoreEmptyValues: false,
3277
+ // If Nonzero check if a label has overlapping text then move it the number of pixels up and left (Should be half of label font size + 1 but you can tweak it as you prefer)
3278
+ preventOverlappingLabelOffset: 0
3277
3279
  };
3278
3280
  /**
3279
3281
  * Determines SVG anchor position based on direction and center parameter
@@ -3289,6 +3291,19 @@ class BarChart extends BaseChart {
3289
3291
  }
3290
3292
  class PieChart extends BaseChart {
3291
3293
  /**
3294
+ * Check if a label has overlapping text then move it the number of pixels up and left based on textSize.
3295
+ * @param labelPos - Label position that chartist will be checking does not overlap with the list of LabelPositions.
3296
+ * @param existingLabelPos - Label position that has already been placed that chartist will check against.
3297
+ * @param textOffset - this is configured with preventOverlappingLabelOffset option.
3298
+ * @param length - How many characters long the label is.
3299
+ */ moveLabel(labelPos, existingLabelPos, textOffset, length) {
3300
+ if (labelPos.y > existingLabelPos.y - textOffset && labelPos.y < existingLabelPos.y + textOffset && labelPos.x > existingLabelPos.x - length * textOffset && labelPos.x < existingLabelPos.x + length * textOffset) {
3301
+ labelPos.y -= textOffset;
3302
+ labelPos.x -= textOffset;
3303
+ this.moveLabel(labelPos, existingLabelPos, textOffset, length);
3304
+ }
3305
+ }
3306
+ /**
3292
3307
  * Creates the pie chart
3293
3308
  *
3294
3309
  * @param options
@@ -3297,6 +3312,7 @@ class PieChart extends BaseChart {
3297
3312
  const normalizedData = normalizeData(data);
3298
3313
  const seriesGroups = [];
3299
3314
  let labelsGroup;
3315
+ const labelPositions = [];
3300
3316
  let labelRadius;
3301
3317
  let startAngle = options.startAngle;
3302
3318
  // Create SVG.js draw
@@ -3440,6 +3456,14 @@ class PieChart extends BaseChart {
3440
3456
  }
3441
3457
  const interpolatedValue = options.labelInterpolationFnc(rawValue, index);
3442
3458
  if (interpolatedValue || interpolatedValue === 0) {
3459
+ if (options.preventOverlappingLabelOffset) {
3460
+ const textOffset = options.preventOverlappingLabelOffset;
3461
+ const length = String(normalizedData.labels[index]).length;
3462
+ labelPositions.forEach((item)=>{
3463
+ this.moveLabel(labelPosition, item, textOffset, length);
3464
+ });
3465
+ labelPositions.push(labelPosition);
3466
+ }
3443
3467
  const labelElement = labelsGroup.elem("text", {
3444
3468
  dx: labelPosition.x,
3445
3469
  dy: labelPosition.y,