pissang commented on a change in pull request #11479: fix(time): bar bandWidth 
with time axis #11145
URL: 
https://github.com/apache/incubator-echarts/pull/11479#discussion_r338905248
 
 

 ##########
 File path: src/layout/barGrid.js
 ##########
 @@ -87,16 +87,91 @@ export function prepareLayoutBarSeries(seriesType, 
ecModel) {
     return seriesModels;
 }
 
+
+/**
+ * Map from axis.index to min gap of two adjacent time values.
+ * For a single time axis, return value is in the form like
+ * [[1000000]].
+ * The value of 1000000 is in milliseconds.
+ */
+function getTimeAxesMinGaps(barSeries) {
+    /**
+     * Map from axis.index to time values.
+     * For a single time axis, axisValues is in the form like
+     * [[1495555200000, 1495641600000, 1495728000000]].
+     * Items in axisValues[x], e.g. 1495555200000, are time values of all
+     * series.
+     */
+    var axisValues = [];
+    zrUtil.each(barSeries, function (seriesModel) {
+        var cartesian = seriesModel.coordinateSystem;
+        var baseAxis = cartesian.getBaseAxis();
+        if (baseAxis.type !== 'time') {
+            return;
+        }
+
+        var data = seriesModel.getData();
+        var axisId = baseAxis.index;
+        for (var i = 0, cnt = data.count(); i < cnt; ++i) {
+            var value = data.get(baseAxis.dim, i);
+            if (!axisValues[axisId]) {
+                // No time value for the time axis
+                axisValues[axisId] = [value];
+            }
+            else if (axisValues[axisId].indexOf(value) < 0) {
+                // No time value in previous series
+                axisValues[axisId].push(value);
+            }
+            // Ignore duplicated time values in the same time axis
+        }
+    });
+
+    var axisMinGaps = [];
+    for (var i = 0; i < axisValues.length; ++i) {
+        if (axisValues[i]) {
+            // Sort time values into ascending order to calculate gaps
+            axisValues[i] = axisValues[i].sort(function (a, b) {
+                return a - b;
+            });
+
+            var min = Number.MAX_VALUE;
+            for (var j = 1; j < axisValues[i].length; ++j) {
+                min = Math.min(min, axisValues[i][j] - axisValues[i][j - 1]);
+            }
+            // Set to null if only have one data
+            axisMinGaps[i] = min === Number.MAX_VALUE ? null : min;
+        }
+    }
+    return axisMinGaps;
+}
+
 export function makeColumnLayout(barSeries) {
+    var axisMinGaps = getTimeAxesMinGaps(barSeries);
+
     var seriesInfoList = [];
     zrUtil.each(barSeries, function (seriesModel) {
         var data = seriesModel.getData();
         var cartesian = seriesModel.coordinateSystem;
         var baseAxis = cartesian.getBaseAxis();
         var axisExtent = baseAxis.getExtent();
-        var bandWidth = baseAxis.type === 'category'
-            ? baseAxis.getBandWidth()
-            : (Math.abs(axisExtent[1] - axisExtent[0]) / data.count());
+
+        var bandWidth;
+        switch (baseAxis.type) {
+            case 'category':
+                bandWidth = baseAxis.getBandWidth();
+                break;
+            case 'time':
+                var minGap = axisMinGaps[baseAxis.index];
+                var extentSpan = Math.abs(axisExtent[1] - axisExtent[0]);
+                var scale = baseAxis.scale.getExtent();
+                var scaleSpan = Math.abs(scale[1] - scale[0]);
+                bandWidth = minGap
+                    ? extentSpan / scaleSpan * minGap
+                    : extentSpan; // When there is only one data value
+                break;
+            default:
+                bandWidth = Math.abs(axisExtent[1] - axisExtent[0]) / 
data.count();
 
 Review comment:
   Can we use the same strategy on the value axis?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to