chartist 1.3.0 → 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
@@ -225,7 +225,11 @@ function extend() {
225
225
  }
226
226
  for(let i = 0; i < sources.length; i++){
227
227
  const source = sources[i];
228
+ const targetProto = Object.getPrototypeOf(target);
228
229
  for(const prop in source){
230
+ if (targetProto !== null && prop in targetProto) {
231
+ continue; // prevent prototype pollution
232
+ }
229
233
  const sourceProp = source[prop];
230
234
  if (typeof sourceProp === "object" && sourceProp !== null && !(sourceProp instanceof Array)) {
231
235
  target[prop] = extend(target[prop], sourceProp);
@@ -1123,16 +1127,16 @@ function createAnimation(element, attribute, animationDefinition) {
1123
1127
  return this;
1124
1128
  }
1125
1129
  /**
1126
- * Get element height using `getBoundingClientRect`
1130
+ * Get element height using `clientHeight`
1127
1131
  * @return The elements height in pixels
1128
1132
  */ height() {
1129
- return this._node.getBoundingClientRect().height;
1133
+ return this._node.clientHeight;
1130
1134
  }
1131
1135
  /**
1132
- * Get element width using `getBoundingClientRect`
1136
+ * Get element width using `clientWidth`
1133
1137
  * @return The elements width in pixels
1134
1138
  */ width() {
1135
- return this._node.getBoundingClientRect().width;
1139
+ return this._node.clientWidth;
1136
1140
  }
1137
1141
  /**
1138
1142
  * The animate function lets you animate the current element with SMIL animations. You can add animations for multiple attributes at the same time by using an animation definition object. This object should contain SMIL animation attributes. Please refer to http://www.w3.org/TR/SVG/animate.html for a detailed specification about the available animation attributes. Additionally an easing property can be passed in the animation definition object. This can be a string with a name of an easing function in `Svg.Easing` or an array with four numbers specifying a cubic Bézier curve.
@@ -1234,6 +1238,9 @@ function createAnimation(element, attribute, animationDefinition) {
1234
1238
  * @return The created/reinitialized SVG element
1235
1239
  */ function createSvg(container) {
1236
1240
  let width = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "100%", height = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : "100%", className = arguments.length > 3 ? arguments[3] : void 0;
1241
+ if (!container) {
1242
+ throw new Error("Container element is not found");
1243
+ }
1237
1244
  // Check if there is a previous SVG element in the container that contains the Chartist XML namespace and remove it
1238
1245
  // Since the DOM API does not support namespaces we need to manually search the returned list http://www.w3.org/TR/selectors-api/
1239
1246
  Array.from(container.querySelectorAll("svg")).filter((svg)=>svg.getAttributeNS(namespaces.xmlns, "ct")
@@ -1417,7 +1424,7 @@ function createAnimation(element, attribute, animationDefinition) {
1417
1424
  responsiveOptions.forEach((responsiveOption)=>{
1418
1425
  const mql = window.matchMedia(responsiveOption[0]);
1419
1426
  if (mql.matches) {
1420
- currentOptions = extend(currentOptions, responsiveOption[1]);
1427
+ currentOptions = extend({}, currentOptions, responsiveOption[1]);
1421
1428
  }
1422
1429
  });
1423
1430
  }
@@ -2266,7 +2273,7 @@ class BaseChart {
2266
2273
  , 0);
2267
2274
  const container = typeof query === "string" ? document.querySelector(query) : query;
2268
2275
  if (!container) {
2269
- throw new Error("Target element is not found");
2276
+ throw new Error("Target element ".concat(typeof query === "string" ? '"'.concat(query, '"') : "", " is not found"));
2270
2277
  }
2271
2278
  this.container = container;
2272
2279
  const prevInstance = instances.get(container);
@@ -3266,7 +3273,9 @@ class BarChart extends BaseChart {
3266
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.
3267
3274
  labelDirection: "neutral",
3268
3275
  // If true empty values will be ignored to avoid drawing unnecessary slices and labels
3269
- 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
3270
3279
  };
3271
3280
  /**
3272
3281
  * Determines SVG anchor position based on direction and center parameter
@@ -3282,6 +3291,19 @@ class BarChart extends BaseChart {
3282
3291
  }
3283
3292
  class PieChart extends BaseChart {
3284
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
+ /**
3285
3307
  * Creates the pie chart
3286
3308
  *
3287
3309
  * @param options
@@ -3290,6 +3312,7 @@ class PieChart extends BaseChart {
3290
3312
  const normalizedData = normalizeData(data);
3291
3313
  const seriesGroups = [];
3292
3314
  let labelsGroup;
3315
+ const labelPositions = [];
3293
3316
  let labelRadius;
3294
3317
  let startAngle = options.startAngle;
3295
3318
  // Create SVG.js draw
@@ -3433,6 +3456,14 @@ class PieChart extends BaseChart {
3433
3456
  }
3434
3457
  const interpolatedValue = options.labelInterpolationFnc(rawValue, index);
3435
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
+ }
3436
3467
  const labelElement = labelsGroup.elem("text", {
3437
3468
  dx: labelPosition.x,
3438
3469
  dy: labelPosition.y,