apache / echarts

Apache ECharts is a powerful, interactive charting and data visualization library for browser
https://echarts.apache.org
Apache License 2.0
60.15k stars 19.6k forks source link

[Bug] bar chart x-axis incorrect when stacking multiple series #17522

Open maryhmhhu opened 2 years ago

maryhmhhu commented 2 years ago

Version

5.3.2

Link to Minimal Reproduction

Link to Chart

Expand for chart config ``` option = { "grid":{ "bottom":40, "containLabel":true, "left":34, "right":97 }, "toolbox":{ "feature":{ "dataView":{ "show":true }, "dataZoom":{ "show":true, "yAxisIndex":"all" }, "saveAsImage":{ "name":"image", "show":true } }, "show":true }, "textStyle":{ "fontFamily":"Inter" }, "dataZoom":{ "type":"inside", "zoomLock":true }, "dataset":{ "dimensions":[ "Week", "C", "L", "M", "B", "G", "CO" ], "source":[ { "Week":" 22", "G":1, "M":12, "C":3, "CO":2, "L":null, "B":null }, { "Week":" 23", "L":4, "C":1, "M":9, "B":1, "G":1, "CO":null }, { "Week":" 24", "C":17, "L":null, "M":null, "B":null, "G":null, "CO":null }, { "Week":" 25", "C":1, "L":null, "M":null, "B":null, "G":null, "CO":null } ] }, "legend":{ "icon":"rect", "itemHeight":16, "itemWidth":16, "show":true, "left":"right", "orient":"vertical", "top":50 }, "series":[ { "animation":false, "barGap":"0%", "encode":{ "x":"Week", "y":"C" }, "itemStyle":{ "color":"#3182EB" }, "label":{ "color":"#000", "fontFamily":"Inter", "fontSize":10, "position":"inside", "show":true }, "labelLayout":{ "hideOverlap":true }, "name":"C", "stack":"stack", "type":"bar" }, { "animation":false, "barGap":"0%", "encode":{ "x":"Week", "y":"L" }, "itemStyle":{ "color":"#FF7D1B" }, "label":{ "color":"#000", "fontFamily":"Inter", "fontSize":10, "position":"inside", "show":true }, "labelLayout":{ "hideOverlap":true }, "name":"L", "stack":"stack", "type":"bar" }, { "animation":false, "barGap":"0%", "encode":{ "x":"Week", "y":"M" }, "itemStyle":{ "color":"#54B74B" }, "label":{ "color":"#000", "fontFamily":"Inter", "fontSize":10, "position":"inside", "show":true }, "labelLayout":{ "hideOverlap":true }, "name":"M", "stack":"stack", "type":"bar" }, { "animation":false, "barGap":"0%", "encode":{ "x":"Week", "y":"B" }, "itemStyle":{ "color":"#E43D55" }, "label":{ "color":"#000", "fontFamily":"Inter", "fontSize":10, "position":"inside", "show":true }, "labelLayout":{ "hideOverlap":true }, "name":"B", "stack":"stack", "type":"bar" }, { "animation":false, "barGap":"0%", "encode":{ "x":"Week", "y":"G" }, "itemStyle":{ "color":"#AC44EA" }, "label":{ "color":"#000", "fontFamily":"Inter", "fontSize":10, "position":"inside", "show":true }, "labelLayout":{ "hideOverlap":true }, "name":"G", "stack":"stack", "type":"bar" }, { "animation":false, "barGap":"0%", "encode":{ "x":"Week", "y":"CO" }, "itemStyle":{ "color":"#6C4141" }, "label":{ "color":"#ffffff", "fontFamily":"Inter", "fontSize":10, "position":"inside", "show":true }, "labelLayout":{ "hideOverlap":true }, "name":"CO", "stack":"stack", "type":"bar" } ], "tooltip":{ }, "xAxis":{ "axisLabel":{ "fontFamily":"Inter", "fontSize":11, "hideOverlap":true, "interval":0, "rotate":0, "show":true }, "axisTick":{ "alignWithLabel":true, "interval":0 }, "max": 26, "min": 21, "boundaryGap":true, "name":"Week", "nameGap":40, "nameLocation":"middle", "nameTextStyle":{ "align":"center", "fontSize":14, "fontWeight":"bold", "padding":[ 0, 0, 0, 0 ], "verticalAlign":"top" }, "show":true, "type":"value" // "type":"category" }, "yAxis":{ "axisLabel":{ "fontFamily":"Inter", "fontSize":11, "show":true }, "name":"user_id", "nameGap":40, "nameLocation":"middle", "nameTextStyle":{ "fontSize":14, "fontWeight":"bold" }, "scale":false, "show":true, "type":"value" } } ```

Steps to Reproduce

  1. Go to link
  2. See that the xAxis max value is multiple times the actual max value

It seems de-selecting series from the chart changes the xAxis max value.

Current Behavior

xAxis max value is 150 when it should be 25, the chart is unreadable:

Screen Shot 2022-08-15 at 3 47 52 PM

When min and max on the xAxis are specified, only one series is shown at a time:

Screen Shot 2022-08-15 at 3 48 04 PM

Expected Behavior

It stacks properly when x-axis type is category instead of value:

Screen Shot 2022-08-15 at 3 47 15 PM

Environment

- OS:
- Browser:
- Framework:

Any additional comments?

No response

Ovilia commented 2 years ago

Please try to minify your data to make it easier to read and understand.

maryhmhhu commented 2 years ago

Hi @Ovilia , this is a simplified version of the example with just 2 series.

Same problems of xAxis max value is 50 when it should be 25; when min and max on the xAxis are specified (as 21 and 26), only one series is shown at a time; and it stacks properly only when x-axis type is category instead of value.

x-axis type is value:

Screen Shot 2022-08-17 at 10 07 29 AM

x-axis type is category:

Screen Shot 2022-08-17 at 10 07 44 AM
Ovilia commented 2 years ago

The default max value of the xAxis (when max is not set) seems to be weird.

rephus commented 2 years ago

I can also replicate this bug on my charts when I simply apply stack on my series when xAxis type is set to value

Peek 2022-08-25 13-48

Here is our open issue: https://github.com/lightdash/lightdash/issues/3076

Here is a modified stacked bar chart example

image

as you can see, the max value on xAxis should be 1320, not 3000

This only happens when xAxis type is value (not category)

I noticed that if you remove any of the series, the maxValue will be reduced as well, like maxValue is the maxValue * number of series whe stack is applied.

we're using

    "echarts": "^5.3.0",
    "echarts-for-react": "^3.0.2",
huangluyu commented 1 year ago

It seams like stacked xAxis max value example:

option = {
  xAxis: {
    type: 'value'
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      data: [
        [10, 50],
        [15, 100]
        ],
      type: 'bar',
      stack: 'jrx'
    },
    {
      data: [
        [12, 50],
        [14, 100]
        ],
      type: 'bar',
      stack: 'jrx'
    }
  ]
};

image And It's stack strategy like the line,but show the bar as it own way,without stacked and accurately. image

huangluyu commented 1 year ago

Another question, the "value type xAxis" with "bar series" doesn‘t stack well

image

huangluyu commented 1 year ago

Another question, the "value type xAxis" with "bar series" doesn‘t stack well

image

Expected:

image

huangluyu commented 1 year ago
// Expected for value type x-axis with bar series stacked
const xStackMap = {};
option.series.forEach((series, index) => {
  series.z = option.series.length - index;
  series.data.forEach((d, i) => {
    const xValue = d[0];
    const yValue = d[1];
    const stackedValue = xStackMap[xValue];
    if (stackedValue === undefined) {
      xStackMap[xValue] = yValue;
    } else {
      d[1] = yValue + stackedValue;
      xStackMap[xValue] = d[1];
    }
  });
});

image

huangluyu commented 1 year ago

New Version!

  1. support multi stack target
  2. fix z-level error
    const stackMap = {}
    option.series.forEach((series, index) => {
    if (series.stack == null) {
    series.z = option.series.length
    return
    }
    series.z = option.series.length - index
    const xStackMap = stackMap[series.stack] || {}
    stackMap[series.stack] = xStackMap
    series.data.forEach((d, i) => {
    const xValue = d[0]
    const yValue = d[1]
    const stackedValue = xStackMap[xValue]
    if (stackedValue === undefined) {
      xStackMap[xValue] = yValue
    } else {
      d[1] = yValue + stackedValue
      xStackMap[xValue] = d[1]
    }
    })
    })