victory icon indicating copy to clipboard operation
victory copied to clipboard

Categories are not working properly with horizontal stacked bars

Open acarl opened this issue 9 years ago • 8 comments

I am trying to build a horizontal bar chart and am having issues with the automatic calculation of tickValues for the VictoryAxis.

Here is an image of the issue in question. chart-without-tick-values

Clearly I am having an issue telling Victory which dataset to use for the automatic calculation of the tickValues, but I am not sure how to correctly change it.

Below is a simplification of the code I am using.

var React = require('react');
var VictoryBar = require('victory').VictoryBar;
var VictoryChart = require('victory').VictoryChart;
var VictoryAxis = require('victory').VictoryAxis;
var VictoryStack = require('victory').VictoryStack;

var StackedTest = function(props) {

  var data = [
    {
      user: 'John',
      apples: 50,
      oranges: 10
    },
    {
      user: 'Jane',
      apples: 5,
      oranges: 20
    }
  ];

  return (
    <VictoryChart domainPadding={20}>

      <VictoryAxis />
      <VictoryAxis dependentAxis />

      <VictoryStack
        horizontal={true}
        categories={data.map(function(datum) {return datum.user})}
        >

        <VictoryBar
          horizontal={true}
          data={data}
          x="user"
          y="apples"
          />

        <VictoryBar
          horizontal={true}
          data={data}
          x="user"
          y="oranges"
          />

      </VictoryStack>

    </VictoryChart>

  );

};

module.exports = StackedTest;

I was able to get it working by hard coding the tick values.

<VictoryAxis tickValues={[20, 40, 60]} />
<VictoryAxis dependentAxis />

chart-with-tick-values

Is there an easier way to do this or something I am missing in the definition of VictoryAxis? I really don't want to have to dynamically calculate the tick values outside of Victory for every chart.

acarl avatar Apr 17 '17 14:04 acarl

@acarl categories should be given as an object with x and y. https://jsfiddle.net/boygirl/5ohsfz6c/

However, it does look like there's a bug with horizontal data with categories. I will change the title of this issue to reflect that.

boygirl avatar Apr 20 '17 21:04 boygirl

Thanks! I can see the bug you are talking about when I change your example to a horizontal bar chart. https://jsfiddle.net/5ohsfz6c/2/ It only shows data for John.

I didn't realize you needed to specify categories as an object. The example given in the documentation has an array of strings. https://formidable.com/open-source/victory/docs/victory-stack/#categories

In the mean time would you recommend I calculate the tickValues manually or is there a better workaround before a bug fix?

acarl avatar Apr 21 '17 13:04 acarl

@acarl you can do something like this in the meantime: https://jsfiddle.net/boygirl/5ohsfz6c/3/

I'll open an issue in our docs repo to fix the issue you mention.

boygirl avatar Apr 21 '17 15:04 boygirl

That should work nicely. Thanks for your help.

acarl avatar Apr 22 '17 01:04 acarl

This bug is still there but I am not able to access fiddle with workaround, can you repost it please?

aleksey-shmatov avatar Jun 26 '17 20:06 aleksey-shmatov

I see the same issue with the JSFiddle. Here is a working version based on my first example. I can't remember if this was different from the JSFiddle, but it works.

var React = require('react');
var VictoryBar = require('victory').VictoryBar;
var VictoryChart = require('victory').VictoryChart;
var VictoryAxis = require('victory').VictoryAxis;
var VictoryStack = require('victory').VictoryStack;

var StackedTest = function(props) {

  var data = [
    {
      user: 'John',
      apples: 50,
      oranges: 10
    },
    {
      user: 'Jane',
      apples: 5,
      oranges: 20
    }
  ];

  return (
    <VictoryChart domainPadding={20}>

      <VictoryAxis
        tickFormat={function(tick) {return tick}}
        />

      <VictoryStack horizontal>

        <VictoryBar
          data={data}
          x="user"
          y="apples"
        />

        <VictoryBar
          data={data}
          x="user"
          y="oranges"
        />

      </VictoryStack>

      <VictoryAxis
        dependentAxis
        tickValues={[1, 2]}
        tickFormat={['John','Jane']}
        />


    </VictoryChart>

  );

};

module.exports = StackedTest;

acarl avatar Jun 27 '17 16:06 acarl

we should use https://codesandbox.io/ instead of jsfiddle

sibelius avatar May 07 '18 19:05 sibelius

I believe this is fixed in the latest version, so I'm going to close this issue.

boygirl avatar Oct 03 '18 04:10 boygirl