Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set tick count for y-axis? #560

Closed
sweatherall opened this issue Sep 13, 2014 · 10 comments
Closed

set tick count for y-axis? #560

sweatherall opened this issue Sep 13, 2014 · 10 comments
Labels
C-feature-request Category: A feature request or an enhancement question resolved maybe

Comments

@sweatherall
Copy link

Is there any way to set the tick count for the y-axis like there is for x-axis?

I have y-values that do not make sense to be floats, yet when there are only 2 the chart gives me floating values between 0-1, 1-2.

Any suggestions are welcome, thank you.

@AnSavvides
Copy link
Contributor

It doesn't look like c3 supports setting the count for y axis ticks looking at the library's reference/options, it should be trivial to implement though!

@sweatherall
Copy link
Author

Good to hear, AnSavvides!
Could you give me some direction as to how to implement this? I would love to.
Would it just be some d3 code (I have done this in d3 elsewhere)? And if so, where would it go?
Thank you

@AnSavvides
Copy link
Contributor

You'll need to extend c3 itself - generateTickValues is responsible for generating tick values for the x axis, which in turn is used by redraw as follows:

// update axis tick values according to options
if (!config.axis_x_tick_values && (config.axis_x_tick_fit || config.axis_x_tick_count)) {
    tickValues = $$.generateTickValues($$.mapTargetsToUniqueXs(targetsToShow), config.axis_x_tick_count);
    $$.xAxis.tickValues(tickValues);
    $$.subXAxis.tickValues(tickValues);
}

So you could for example rename generateTickValues into generateTickValuesX and then create a generateTickValuesY function which would be used in a similar manner.

@sweatherall
Copy link
Author

Ah, great!
I very much appreciate your help, AnSavvides.

@AnSavvides
Copy link
Contributor

No worries Steph, let me know if you need a hand :)

@threesided
Copy link

Hey, so there is a far easier way to set the number of ticks to use:

In the generate() function you can set axis.y.ticks to whatever value you would like. c3 will use this in place of the default 10.
object path eg:

var chart = c3.generate({
     axis: {
          y: {
              ticks: 3, 
          }
      }
    });

No need to extend or override the base functionality for it, it's already there.

@masayuki0812
Copy link
Member

Hi, I added axis.y.tick.values and axis.y.tick.count so that we can control tick on y axis as well as y axis. And I removed axis.y.ticks because this cannot control ticks properly and it's more intuitive to specify new options than using axis.y.ticks. Thanks.

@masayuki0812 masayuki0812 added question resolved maybe C-feature-request Category: A feature request or an enhancement labels Sep 28, 2014
@sweatherall
Copy link
Author

thank you much @masayuki0812 you are awesome

@MayankProgenG
Copy link

In the generate() function you can set axis.y.tick to whatever value you would like. c3 will use this in place of the default 10.
object path eg:

var chart = c3.generate({
axis: {
y: {
tick: {
count: 4
}
}
}
});
After tick of y axis is 4.

@WoodrowShigeru
Copy link

I find the axis.y.tick behavior lackluster. I.e., for a dataset like ["apples", 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] and a tick setting of 10, it generates a y axis of [0,0,0,0,0,0,1,1,1,1,1,1].

Here is a workaround:

c3_config.axis.y.tick.count = Math.min(
  [
    // get unique values.
    ...new Map(
      c3_config.data.columns
        // skip x row if present.
        .filter((dataset) => dataset[0] !== 'x')

        // skip dataset key.
        .map((dataset) => dataset.slice(1))

        // merge all datasets into one set.
        .flat()

        // prepare for map constructor.
        .map((value) => [value, value])
    ).values()
  ].length,
  10
);

It basically uses either the preferred 10 or, if lower, the amount of unique values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-feature-request Category: A feature request or an enhancement question resolved maybe
Projects
None yet
Development

No branches or pull requests

6 participants