imyxiao opened a new issue, #18170:
URL: https://github.com/apache/echarts/issues/18170

   ### Version
   
   5.4.1
   
   ### Link to Minimal Reproduction
   
   https://echarts.apache.org/examples/en/editor.html?c=boxplot-light-velocity
   
   ### Steps to Reproduce
   
   I'm testing boxplot 
[here](https://echarts.apache.org/examples/en/editor.html?c=boxplot-light-velocity)
 by modifying the data to `[[10,20,30,40,50,60,70,80]]`
   
   the result is:
   
   <img width="400" alt="image" 
src="https://user-images.githubusercontent.com/4159448/212233784-d1e6200d-f0eb-4836-8105-fef5b78d70e1.png";>
   
   ### Current Behavior
   
   The values of q1 and q3 were not what I expected, and I didn't know how to 
display the exact values in the chart, so I looked through the source code and 
found the calculate method of q1 and q3. I calculate that q1 is 27.5 and q3 is 
62.5
   
   
https://github.com/apache/echarts/blob/master/src/chart/boxplot/prepareBoxplotData.ts
   
   ```ts
   const Q1 = quantile(ascList, 0.25);
   const Q2 = quantile(ascList, 0.5);
   const Q3 = quantile(ascList, 0.75);
   ```
   
   But I think q1 shoud be 25 (`(20 + 30)/2`) which is what I expected
   
   Now I change the data to an odd array:`10,20,30,40,50,60,70,80,90`. 
   
   There are two ways to calculate:
   
   - If the median is included in the calculation of q1/q3, then q1 is 30, q3 
is 70
   - If the median is not included in the calculation of q1/q3, then q1 is 25, 
q3 is 75
   
   Echart uses the first one, but I was expecting the second one
   
   
   ### Expected Behavior
   
   This library does it perfectly as I expected: 
https://github.com/plotly/plotly.js
   
   The relevant calculation codes are here: 
   
   https://github.com/plotly/plotly.js/blob/master/src/traces/box/calc.js
   
   ```ts
   if((N % 2) && (usesExclusive || usesInclusive)) {
       var lower;
       var upper;
   
       if(usesExclusive) {
           // do NOT include the median in either half
           lower = boxVals.slice(0, N / 2);
           upper = boxVals.slice(N / 2 + 1);
       } else if(usesInclusive) {
           // include the median in either half
           lower = boxVals.slice(0, N / 2 + 1);
           upper = boxVals.slice(N / 2);
       }
   
       cdi.q1 = Lib.interp(lower, 0.5);
       cdi.q3 = Lib.interp(upper, 0.5);
   } else {
       cdi.q1 = Lib.interp(boxVals, 0.25);
       cdi.q3 = Lib.interp(boxVals, 0.75);
   }
   ```
   
   They use this 
[property](https://plotly.com/javascript/reference/box/#box-quartilemethod) to 
determine the method for calculating q1 q3
   
   > Sets the method used to compute the sample's Q1 and Q3 quartiles. The 
"linear" method uses the 25th percentile for Q1 and 75th percentile for Q3 as 
computed using method #10 (listed on 
http://jse.amstat.org/v14n3/langford.html). The "exclusive" method uses the 
median to divide the ordered dataset into two halves if the sample is odd, it 
does not include the median in either half - Q1 is then the median of the lower 
half and Q3 the median of the upper half. The "inclusive" method also uses the 
median to divide the ordered dataset into two halves but if the sample is odd, 
it includes the median in both halves - Q1 is then the median of the lower half 
and Q3 the median of the upper half.
   
   Examples: https://codepen.io/imyxiao/pen/LYBLNoe
   
   
   ### Environment
   
   _No response_
   
   ### Any additional comments?
   
   _No response_


-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to