chartist 1.2.1 → 1.3.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/README.md CHANGED
@@ -173,6 +173,7 @@ import type {
173
173
  - [Multi-line labels](http://codesandbox.io/s/github/chartist-js/chartist/main/sandboxes/bar/multiline)
174
174
  - [Overlapping bars on mobile](http://codesandbox.io/s/github/chartist-js/chartist/main/sandboxes/bar/overlapping-bars)
175
175
  - [Stacked bar chart](http://codesandbox.io/s/github/chartist-js/chartist/main/sandboxes/bar/stacked)
176
+ - [Stacked bar chart with accumulate-relative stack mode](http://codesandbox.io/s/github/chartist-js/chartist/main/sandboxes/bar/stacked-accumulate-relative)
176
177
  - [Add peak circles using the draw events](http://codesandbox.io/s/github/chartist-js/chartist/main/sandboxes/bar/with-circle-modify-drawing)
177
178
 
178
179
  </details>
@@ -34,7 +34,7 @@ $ct-text-color: rgba(0, 0, 0, 0.4) !default;
34
34
  $ct-text-size: 0.75rem !default;
35
35
  $ct-text-align: flex-start !default;
36
36
  $ct-text-justify: flex-start !default;
37
- $ct-text-line-height: 1;
37
+ $ct-text-line-height: 1 !default;
38
38
 
39
39
  // Grid styles
40
40
  $ct-grid-color: rgba(0, 0, 0, 0.2) !default;
@@ -1 +1 @@
1
- {"version":3,"file":"BarChart.d.ts","sourceRoot":"","sources":["../../../src/charts/BarChart/BarChart.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,eAAe,EACf,2BAA2B,EAG3B,mBAAmB,EACpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAEV,iBAAiB,EAElB,MAAM,YAAY,CAAC;AAoBpB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAkHzC,qBAAa,QAAS,SAAQ,SAAS,CAAC,mBAAmB,CAAC;cAyCrC,IAAI,EAAE,YAAY;IAxCvC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAqCG;gBAED,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,EACX,IAAI,EAAE,YAAY,EACrC,OAAO,CAAC,EAAE,eAAe,EACzB,iBAAiB,CAAC,EAAE,iBAAiB,CAAC,eAAe,CAAC;IAWxD;;OAEG;IACH,WAAW,CAAC,OAAO,EAAE,2BAA2B;CAwYjD"}
1
+ {"version":3,"file":"BarChart.d.ts","sourceRoot":"","sources":["../../../src/charts/BarChart/BarChart.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,eAAe,EACf,2BAA2B,EAG3B,mBAAmB,EACpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,KAAK,EAEV,iBAAiB,EAElB,MAAM,YAAY,CAAC;AAoBpB,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAmHzC,qBAAa,QAAS,SAAQ,SAAS,CAAC,mBAAmB,CAAC;cAyCrC,IAAI,EAAE,YAAY;IAxCvC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAqCG;gBAED,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,EACX,IAAI,EAAE,YAAY,EACrC,OAAO,CAAC,EAAE,eAAe,EACzB,iBAAiB,CAAC,EAAE,iBAAiB,CAAC,eAAe,CAAC;IAWxD;;OAEG;IACH,WAAW,CAAC,OAAO,EAAE,2BAA2B;CAoZjD"}
@@ -30,10 +30,11 @@ export interface BarChartOptions<TXAxisOptions = AxisOptions, TYAxisOptions = TX
30
30
  */
31
31
  stackBars?: boolean;
32
32
  /**
33
- * If set to 'overlap' this property will force the stacked bars to draw from the zero line.
33
+ * If set to true this property will force the stacked bars to draw from the zero line.
34
34
  * If set to 'accumulate' this property will form a total for each series point. This will also influence the y-axis and the overall bounds of the chart. In stacked mode the seriesBarDistance property will have no effect.
35
+ * If set to 'accumulate-relative' positive and negative values will be handled separately.
35
36
  */
36
- stackMode?: 'accumulate' | boolean;
37
+ stackMode?: 'accumulate' | 'accumulate-relative' | boolean;
37
38
  /**
38
39
  * Inverts the axes of the bar chart in order to draw a horizontal bar chart. Be aware that you also need to invert your axis settings as the Y Axis will now display the labels and the X Axis the values.
39
40
  */
@@ -1 +1 @@
1
- {"version":3,"file":"BarChart.types.d.ts","sourceRoot":"","sources":["../../../src/charts/BarChart/BarChart.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,WAAW,EACX,IAAI,EACJ,YAAY,EACZ,SAAS,EACT,eAAe,EACf,aAAa,EACd,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAErD,oBAAY,YAAY,GAAG,IAAI,CAAC;AAEhC,MAAM,WAAW,eAAe,CAC9B,aAAa,GAAG,WAAW,EAC3B,aAAa,GAAG,aAAa,CAC7B,SAAQ,OAAO,CAAC,aAAa,EAAE,aAAa,CAAC;IAC7C;;OAEG;IACH,UAAU,CAAC,EAAE;QACX,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,cAAc,CAAC,EAAE,MAAM,CAAC;QACxB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,cAAc,CAAC,EAAE,MAAM,CAAC;QACxB,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,GAAG,CAAC,EAAE,MAAM,CAAC;KACd,CAAC;IACF;;OAEG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;;OAGG;IACH,SAAS,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC;IACnC;;OAEG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;OAEG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B;;OAEG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB;;OAEG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B;AAED,oBAAY,2BAA2B,GAAG,YAAY,CACpD,eAAe,CACb,YAAY,CACV,WAAW,EACT,QAAQ,GACR,UAAU,GACV,aAAa,GACb,WAAW,GACX,UAAU,GACV,uBAAuB,GACvB,eAAe,CAClB,EACD,YAAY,CACV,WAAW,EACT,QAAQ,GACR,UAAU,GACV,aAAa,GACb,WAAW,GACX,UAAU,GACV,uBAAuB,GACvB,eAAe,CAClB,CACF,EACC,gBAAgB,GAChB,cAAc,GACd,mBAAmB,GACnB,WAAW,GACX,OAAO,GACP,OAAO,EACT,YAAY,CACb,CAAC;AAEF,oBAAY,oBAAoB,GAAG,YAAY,CAAC,eAAe,CAAC,CAAC;AAEjE,MAAM,WAAW,YAAa,SAAQ,SAAS;IAC7C,IAAI,EAAE,KAAK,CAAC;IACZ,KAAK,EAAE,MAAM,GAAG,eAAe,CAAC;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,oBAAY,mBAAmB,GAAG,oBAAoB,CACpD,oBAAoB,EACpB,aAAa,GAAG,YAAY,CAC7B,CAAC"}
1
+ {"version":3,"file":"BarChart.types.d.ts","sourceRoot":"","sources":["../../../src/charts/BarChart/BarChart.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,WAAW,EACX,IAAI,EACJ,YAAY,EACZ,SAAS,EACT,eAAe,EACf,aAAa,EACd,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EAAE,YAAY,EAAE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,GACb,WAAW,GACX,UAAU,GACV,uBAAuB,GACvB,eAAe,CAClB,EACD,YAAY,CACV,WAAW,EACT,QAAQ,GACR,UAAU,GACV,aAAa,GACb,WAAW,GACX,UAAU,GACV,uBAAuB,GACvB,eAAe,CAClB,CACF,EACC,gBAAgB,GAChB,cAAc,GACd,mBAAmB,GACnB,WAAW,GACX,OAAO,GACP,OAAO,EACT,YAAY,CACb,CAAC;AAEF,oBAAY,oBAAoB,GAAG,YAAY,CAAC,eAAe,CAAC,CAAC;AAEjE,MAAM,WAAW,YAAa,SAAQ,SAAS;IAC7C,IAAI,EAAE,KAAK,CAAC;IACZ,KAAK,EAAE,MAAM,GAAG,eAAe,CAAC;IAChC,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,oBAAY,mBAAmB,GAAG,oBAAoB,CACpD,oBAAoB,EACpB,aAAa,GAAG,YAAY,CAC7B,CAAC"}
package/dist/index.cjs CHANGED
@@ -2919,8 +2919,9 @@ function getSerialSums(series) {
2919
2919
  seriesBarDistance: 15,
2920
2920
  // If set to true this property will cause the series bars to be stacked. Check the `stackMode` option for further stacking options.
2921
2921
  stackBars: false,
2922
- // If set to 'overlap' this property will force the stacked bars to draw from the zero line.
2922
+ // If set to true this property will force the stacked bars to draw from the zero line.
2923
2923
  // If set to 'accumulate' this property will form a total for each series point. This will also influence the y-axis and the overall bounds of the chart. In stacked mode the seriesBarDistance property will have no effect.
2924
+ // If set to 'accumulate-relative' positive and negative values will be handled separately.
2924
2925
  stackMode: "accumulate",
2925
2926
  // Inverts the axes of the bar chart in order to draw a horizontal bar chart. Be aware that you also need to invert your axis settings as the Y Axis will now display the labels and the X Axis the values.
2926
2927
  horizontalBars: false,
@@ -3029,8 +3030,12 @@ class BarChart extends BaseChart {
3029
3030
  }
3030
3031
  // Projected 0 point
3031
3032
  const zeroPoint = options.horizontalBars ? chartRect.x1 + valueAxis.projectValue(0) : chartRect.y1 - valueAxis.projectValue(0);
3033
+ const isAccumulateStackMode = options.stackMode === "accumulate";
3034
+ const isAccumulateRelativeStackMode = options.stackMode === "accumulate-relative";
3032
3035
  // Used to track the screen coordinates of stacked bars
3033
- const stackedBarValues = [];
3036
+ const posStackedBarValues = [];
3037
+ const negStackedBarValues = [];
3038
+ let stackedBarValues = posStackedBarValues;
3034
3039
  labelAxis.createGridAndLabels(gridGroup, labelGroup, options, this.eventEmitter);
3035
3040
  valueAxis.createGridAndLabels(gridGroup, labelGroup, options, this.eventEmitter);
3036
3041
  if (options.showGridBackground) {
@@ -3077,6 +3082,8 @@ class BarChart extends BaseChart {
3077
3082
  seriesClassName || "".concat(options.classNames.series, "-").concat(alphaNumerate(seriesIndex))
3078
3083
  ].join(" "));
3079
3084
  normalizedData.series[seriesIndex].forEach((value, valueIndex)=>{
3085
+ const valueX = safeHasProperty(value, "x") && value.x;
3086
+ const valueY = safeHasProperty(value, "y") && value.y;
3080
3087
  let labelAxisValueIndex;
3081
3088
  // We need to set labelAxisValueIndex based on some options combinations
3082
3089
  if (options.distributeSeries && !options.stackBars) {
@@ -3095,13 +3102,13 @@ class BarChart extends BaseChart {
3095
3102
  // We need to transform coordinates differently based on the chart layout
3096
3103
  if (options.horizontalBars) {
3097
3104
  projected = {
3098
- x: chartRect.x1 + valueAxis.projectValue(safeHasProperty(value, "x") ? value.x : 0, valueIndex, normalizedData.series[seriesIndex]),
3099
- y: chartRect.y1 - labelAxis.projectValue(safeHasProperty(value, "y") ? value.y : 0, labelAxisValueIndex, normalizedData.series[seriesIndex])
3105
+ x: chartRect.x1 + valueAxis.projectValue(valueX || 0, valueIndex, normalizedData.series[seriesIndex]),
3106
+ y: chartRect.y1 - labelAxis.projectValue(valueY || 0, labelAxisValueIndex, normalizedData.series[seriesIndex])
3100
3107
  };
3101
3108
  } else {
3102
3109
  projected = {
3103
- x: chartRect.x1 + labelAxis.projectValue(safeHasProperty(value, "x") ? value.x : 0, labelAxisValueIndex, normalizedData.series[seriesIndex]),
3104
- y: chartRect.y1 - valueAxis.projectValue(safeHasProperty(value, "y") ? value.y : 0, valueIndex, normalizedData.series[seriesIndex])
3110
+ x: chartRect.x1 + labelAxis.projectValue(valueX || 0, labelAxisValueIndex, normalizedData.series[seriesIndex]),
3111
+ y: chartRect.y1 - valueAxis.projectValue(valueY || 0, valueIndex, normalizedData.series[seriesIndex])
3105
3112
  };
3106
3113
  }
3107
3114
  // If the label axis is a step based axis we will offset the bar into the middle of between two steps using
@@ -3116,6 +3123,10 @@ class BarChart extends BaseChart {
3116
3123
  // Using bi-polar offset for multiple series if no stacked bars or series distribution is used
3117
3124
  projected[labelAxis.units.pos] += options.stackBars || options.distributeSeries ? 0 : biPol * options.seriesBarDistance * (options.horizontalBars ? -1 : 1);
3118
3125
  }
3126
+ // distinguish between positive and negative values in relative stack mode
3127
+ if (isAccumulateRelativeStackMode) {
3128
+ stackedBarValues = valueY >= 0 || valueX >= 0 ? posStackedBarValues : negStackedBarValues;
3129
+ }
3119
3130
  // Enter value in stacked bar values used to remember previous screen value for stacking up bars
3120
3131
  const previousStack = stackedBarValues[valueIndex] || zeroPoint;
3121
3132
  stackedBarValues[valueIndex] = previousStack - (zeroPoint - projected[labelAxis.counterUnits.pos]);
@@ -3127,7 +3138,7 @@ class BarChart extends BaseChart {
3127
3138
  ["".concat(labelAxis.units.pos, "1")]: projected[labelAxis.units.pos],
3128
3139
  ["".concat(labelAxis.units.pos, "2")]: projected[labelAxis.units.pos]
3129
3140
  };
3130
- if (options.stackBars && (options.stackMode === "accumulate" || !options.stackMode)) {
3141
+ if (options.stackBars && (isAccumulateStackMode || isAccumulateRelativeStackMode || !options.stackMode)) {
3131
3142
  // Stack mode: accumulate (default)
3132
3143
  // If bars are stacked we use the stackedBarValues reference and otherwise base all bars off the zero line
3133
3144
  // We want backwards compatibility, so the expected fallback without the 'stackMode' option
@@ -3149,8 +3160,8 @@ class BarChart extends BaseChart {
3149
3160
  // Create bar element
3150
3161
  const bar = seriesElement.elem("line", positions, options.classNames.bar).attr({
3151
3162
  "ct:value": [
3152
- safeHasProperty(value, "x") && value.x,
3153
- safeHasProperty(value, "y") && value.y
3163
+ valueX,
3164
+ valueY
3154
3165
  ].filter(isNumeric).join(","),
3155
3166
  "ct:meta": serialize(metaData)
3156
3167
  });