question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

min/max on yAxis for bar charts, makes the bars go outside limits

See original GitHub issue

One-line summary [问题简述]

Setting custom min/max values on the yAxis for bar charts, makes the bars go below the lowest limit of x axis and above the highest limit of y axis.

Version & Environment [版本及环境]

  • ECharts version [ECharts 版本]: 4.1.0
  • Browser version [浏览器类型和版本]: 68.0.3440.84
  • OS Version [操作系统类型和版本]: macOS High Sierra 10.13.3

Expected behaviour [期望结果]

Without min/max on bar chart I get, bar_without_min_max

When I set min/max on bar chart I get, bar_with_min_max

I do not face this issue if I change the chart type to line,

Line without min/max: line_without_min_max

Line with min/max: line_with_min_max

What can I do to make min/max for bar behave like line ? Is there some way I can prevent the bars from going outside limits ?

ECharts option [ECharts配置项]

option = {
    xAxis: {
        type: 'category',
        data: ['Foo', 'Bar', 'Baz']
    },
    yAxis: {
        type: 'value',
        min: 200,
        max: 250
    },
    series: [{
        data: [100, 200, 300],
        type: 'line' // 'bar'
    }]
};

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:8
  • Comments:19 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
suremicrocommented, Oct 26, 2020

You example doesn’t include the layout clipping function??? You need to add the function and register it using the global echarts function. If you try the code below in your jsfiddle example it will clip the bars 😉

echarts.registerLayout(chartLayout);
var container = document.getElementById('main');
var chart = echarts.init(container);

const min = "2020-11-25T06:10";
const max = "2020-11-25T08:10";
function getData() {
      const data = [];
      let now = +new Date(2020, 10, 25);
      const oneDay = 10 * 60 * 1000;
      const randomData = ()=> {
        now = new Date(+now + oneDay);
        return {
          value: [
            now.toISOString().substring(0,16),
            Math.floor(Math.random() * 100),
          ],
        };
      };
      for (let i = 0; i < 72; i++) {
        data.push(randomData());
      }
      return data;
}

            function chartLayout(ec) {

                ec.eachSeries(seriesComponent => {

                    var data = seriesComponent.getData();

                    data.each(idx => {

                        var layout = data.getItemLayout(idx);

                        if (layout) {

                            var rect = seriesComponent.coordinateSystem.grid.getRect();
                            
                            if (layout.x < rect.x) {
                                layout.width = layout.width - (rect.x - layout.x);
                                layout.x = rect.x;
                            }

                            if (layout.x + layout.width > rect.x + rect.width) {
                                layout.width = rect.x + rect.width - layout.x;
                            }

                            if (layout.width < 0) layout.width = 0;

                            if (layout.y > rect.y + rect.height) {
                                layout.height = layout.height - (rect.y + rect.height - layout.y);
                                layout.y = rect.y + rect.height;
                            }

                            var absY = (rect.y + rect.height) - layout.y;

                            if (absY + Math.abs(layout.height) + 65 > rect.y + rect.height) {
                                layout.height = -(rect.y + rect.height - 65 - absY);
                            }

                            if (layout.height > 0) layout.height = 0;

                        }
                    });
                });
            }
            
const data = getData();
chart.setOption({
    xAxis: [
      {
      	splitNumber: 24,
        scale: false,
        min, 
        max,
        type: "time",
        axisLabel: {
          formatter(value) {
            return moment(value).format("HH");
          }
        }
      }
    ],
    yAxis: [
      {
        type: "value"
      }
    ],
    series: [
      {
        data,
        type:'bar',
      }
    ]
  });
2reactions
jonavilacommented, Jul 29, 2019

@suremicro No need to go the custom chart route. I’ve solved this using a similar solution to yours but instead of creating a custom chart, I update each bar’s layout properties. You can update each bar’s layout by registering a layout function before initializing your chart.

Example:

echarts.registerLayout(myCustomLayoutFunction);
echarts.init(myChartContainer);

function myCustomLayoutFunction(ecModel) {
  ecModel.eachSeries(seriesComponent => {
    const data = seriesComponent.getData();
    data.each(idx => {
      const layout = data.getItemLayout(idx);
      
      if (layout) {
        const newLayout = echarts.graphic.clipRectByRect(
          layout,
          seriesComponent.coordinateSystem.grid.getRect(),
        );

        if (newLayout) {
          ({ x: layout.x, y: layout.y, width: layout.width, height: layout.height } = newLayout);
        }  
      }
    });
  }
}

I use this technique of registering layout functions for my charts quite often to do things like dynamically showing or hiding series labels depending if they fit.

Read more comments on GitHub >

github_iconTop Results From Across the Web

setting custom min/max value of y-axis on bar charts in echarts ...
My problem is i want to give the minimum on y-axis as 0 but maximum as either the addition of maximum of two...
Read more >
Automatically setting Y-axis limits for a bar graph using ...
To set Y-axis limit, we can use ylim() method and put maximum and minimum limit values. Steps. Set the figure size and adjust...
Read more >
Set Minimum and Maximum Values on Value Axis ... - Indezine
Now you can see that the maximum and minimum values on the value axis have changed to new values. If you compare the...
Read more >
yAxis.max | highcharts API Reference
In case of an inverted column chart or a bar chart the label is placed to the right of positive bars and to...
Read more >
Bar Charts - NCL Graphics
Bar charts (in this context) are simply XY plots that are drawn with bars for each X,Y point. To get bars instead of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found