set height of chart based on number of categories

  • 1
  • Question
  • Updated 2 years ago
  • Answered
I've seen examples of before render snippets used to control attributes of a chart, but it does not look like chart height is one of the available attributes.

We have a bar chart used to display activities of various types for each rep.  I'd like the height of the chart to be dynamic, so that it doesn't hide the names of the sales reps (chart category) when too many are displayed for the static chart height.
Photo of Jared Jones

Jared Jones

  • 2,214 Points 2k badge 2x thumb

Posted 2 years ago

  • 1
Photo of Peter Hale

Peter Hale, Employee

  • 330 Points 250 badge 2x thumb
There isn't a way to do it declaritvely, but you can do it via JS, maybe in a snippet.  In the advanced tab of the chart, you can set the unique id of the element, and then in JS, do something like:

$(#"my-chart-id").height(myCalculatedHeight);

You can get the calculated height by using the length of the sales reps model
Photo of Jared Jones

Jared Jones

  • 2,214 Points 2k badge 2x thumb
I have this in place now, but it isn't having a permanent impact on the way the categories are rendered.  And it doesn't have any impact the first time the chart is rendered.

I'm using this code in the chart's before render snippet:

var params = arguments[0],	$ = skuid.$;
var repCount;
repCount = params.xAxis[0].categories.length;
dynHeight = repCount*18;
$('#taskChart').height(dynHeight);

When the filters are refreshed, it animates the draw at the correct size, then pops to the default chart height of 400.


I added a couple more css overrides, but the result is the same.  The first two are reflected in the code, the last one gets ignored, even after adding !important

var params = arguments[0],	$ = skuid.$;
var repCount;
repCount = params.xAxis[0].categories.length;
dynHeight = repCount*18;
$('#taskChart').height(dynHeight);
$('.sk-chart-wrapper').css('height',dynHeight);
$('.highcharts-container').css('height',dynHeight);

Resulting page:



Would sure be nice to have the chart adjust based on the content.  Or have the option to increase the size of the chart so that category labels are never hidden.
Photo of Jared Jones

Jared Jones

  • 2,214 Points 2k badge 2x thumb
Ok - this works now, allowing for a minimum height of 400:

var params = arguments[0],	$ = skuid.$;
var repCount;
repCount = params.xAxis[0].categories.length;
dynHeight = Math.max(repCount*20,400);
$('#taskChart').height(dynHeight);
$('.sk-chart-wrapper').css('height',dynHeight);