Text value in axis ticks using scaleBand()Which “href” value should I use for JavaScript links, “#” or “javascript:void(0)”?Set a default parameter value for a JavaScript functionHow can I get query string values in JavaScript?Get selected value in dropdown list using JavaScript?Sort array of objects by string property valueGet selected text from a drop-down list (select box) using jQueryCopy array by valuehow to format time on xAxis use d3.jsDraw the line use the same y values, plots line at the bottomWith text labels for ticks in linear scale, zoom causes lables to extend beyond axis

Language involving irrational number is not a CFL

What the heck is gets(stdin) on site coderbyte?

What does "Scientists rise up against statistical significance" mean? (Comment in Nature)

Limit max CPU usage SQL SERVER with WSRM

Quoting Keynes in a lecture

Sound waves in different octaves

How to make money from a browser who sees 5 seconds into the future of any web page?

When is "ei" a diphthong?

Visualizing the difference curve in a 2D plot?

Do I have to take mana from my deck or hand when tapping a dual land?

Can I say "fingers" when referring to toes?

Check if object is null and return null

Personal or impersonal in a technical resume

Mimic lecturing on blackboard, facing audience

How can I safely use "Thalidomide" in my novel while respecting the trademark?

Can you identify this lizard-like creature I observed in the UK?

How to make a list of partial sums using forEach

What (the heck) is a Super Worm Equinox Moon?

Grepping string, but include all non-blank lines following each grep match

Why does a 97 / 92 key piano exist by Bösendorfer?

Why the "ls" command is showing the permissions of files in a FAT32 partition?

What does "tick" mean in this sentence?

How to leave product feedback on macOS?

Pre-Employment Background Check With Consent For Future Checks



Text value in axis ticks using scaleBand()


Which “href” value should I use for JavaScript links, “#” or “javascript:void(0)”?Set a default parameter value for a JavaScript functionHow can I get query string values in JavaScript?Get selected value in dropdown list using JavaScript?Sort array of objects by string property valueGet selected text from a drop-down list (select box) using jQueryCopy array by valuehow to format time on xAxis use d3.jsDraw the line use the same y values, plots line at the bottomWith text labels for ticks in linear scale, zoom causes lables to extend beyond axis













0















I have created a barg graph using SVGs. I am currently attempting to have my tick values as strings along my x-axis. In my case, the x-axis will represent colors (represented as strings). I have scales and I have axes. I want an x-axis which displays string values corresponding to each bar. How might I go about this?



[
"color":"blue",
"num":"8"
,

"color":"green",
"num": "8"
,

"color":"red",
"num":"6"
,

"color":"black",
"num":"2"
,

"color":"purple",
"num":"3"
,

"color":"gold",
"num":"4"
,

"color":"teal",
"num":"1"
];

var arr = data.map(function(d)return d.color;);

// create scale for X-AXIS (1) //
var band = d3.scaleBand() //
.domain(d3.range(data.length))
.range([0,width])
.paddingInner(0.1);


// DEFINE X AXIS
var xAxis = d3.axisBottom(band)
.tickValues(d3.range(data.length))
.tickFormat(function(d,i) return d.color;);

// CREATE X-AXIS
d3.select(location).append("g")
.attr("class","axis") // set class for x-axis
.attr("transform","translate(0,"+height+")")
.call(xAxis);









share|improve this question
























  • What exactly is your question?

    – Azmisov
    Mar 7 at 3:47






  • 1





    "Scale band's domain will not accept an array as its input"... A scale only accepts an array as the input.

    – Gerardo Furtado
    Mar 7 at 4:37











  • My question is how do I get my x axis ticks to present as strings? What modifications would I need to make to my work? Why are those modifications necessary? @Azmisov

    – Michael Ramage MikeRamage
    Mar 7 at 5:13











  • @MichaelRamageMikeRamage D3 ticks are always strings, so it's not clear what's the issue here. How is your axis? What values do you have now, and what values do you want to show?

    – Gerardo Furtado
    Mar 7 at 5:32











  • I would like each tick on my x-axis to represent a color, which is a string in my dataset. Therefore, each tick should have the color labeled for its corresponding bar. @GerardoFurtado

    – Michael Ramage MikeRamage
    Mar 7 at 5:41















0















I have created a barg graph using SVGs. I am currently attempting to have my tick values as strings along my x-axis. In my case, the x-axis will represent colors (represented as strings). I have scales and I have axes. I want an x-axis which displays string values corresponding to each bar. How might I go about this?



[
"color":"blue",
"num":"8"
,

"color":"green",
"num": "8"
,

"color":"red",
"num":"6"
,

"color":"black",
"num":"2"
,

"color":"purple",
"num":"3"
,

"color":"gold",
"num":"4"
,

"color":"teal",
"num":"1"
];

var arr = data.map(function(d)return d.color;);

// create scale for X-AXIS (1) //
var band = d3.scaleBand() //
.domain(d3.range(data.length))
.range([0,width])
.paddingInner(0.1);


// DEFINE X AXIS
var xAxis = d3.axisBottom(band)
.tickValues(d3.range(data.length))
.tickFormat(function(d,i) return d.color;);

// CREATE X-AXIS
d3.select(location).append("g")
.attr("class","axis") // set class for x-axis
.attr("transform","translate(0,"+height+")")
.call(xAxis);









share|improve this question
























  • What exactly is your question?

    – Azmisov
    Mar 7 at 3:47






  • 1





    "Scale band's domain will not accept an array as its input"... A scale only accepts an array as the input.

    – Gerardo Furtado
    Mar 7 at 4:37











  • My question is how do I get my x axis ticks to present as strings? What modifications would I need to make to my work? Why are those modifications necessary? @Azmisov

    – Michael Ramage MikeRamage
    Mar 7 at 5:13











  • @MichaelRamageMikeRamage D3 ticks are always strings, so it's not clear what's the issue here. How is your axis? What values do you have now, and what values do you want to show?

    – Gerardo Furtado
    Mar 7 at 5:32











  • I would like each tick on my x-axis to represent a color, which is a string in my dataset. Therefore, each tick should have the color labeled for its corresponding bar. @GerardoFurtado

    – Michael Ramage MikeRamage
    Mar 7 at 5:41













0












0








0








I have created a barg graph using SVGs. I am currently attempting to have my tick values as strings along my x-axis. In my case, the x-axis will represent colors (represented as strings). I have scales and I have axes. I want an x-axis which displays string values corresponding to each bar. How might I go about this?



[
"color":"blue",
"num":"8"
,

"color":"green",
"num": "8"
,

"color":"red",
"num":"6"
,

"color":"black",
"num":"2"
,

"color":"purple",
"num":"3"
,

"color":"gold",
"num":"4"
,

"color":"teal",
"num":"1"
];

var arr = data.map(function(d)return d.color;);

// create scale for X-AXIS (1) //
var band = d3.scaleBand() //
.domain(d3.range(data.length))
.range([0,width])
.paddingInner(0.1);


// DEFINE X AXIS
var xAxis = d3.axisBottom(band)
.tickValues(d3.range(data.length))
.tickFormat(function(d,i) return d.color;);

// CREATE X-AXIS
d3.select(location).append("g")
.attr("class","axis") // set class for x-axis
.attr("transform","translate(0,"+height+")")
.call(xAxis);









share|improve this question
















I have created a barg graph using SVGs. I am currently attempting to have my tick values as strings along my x-axis. In my case, the x-axis will represent colors (represented as strings). I have scales and I have axes. I want an x-axis which displays string values corresponding to each bar. How might I go about this?



[
"color":"blue",
"num":"8"
,

"color":"green",
"num": "8"
,

"color":"red",
"num":"6"
,

"color":"black",
"num":"2"
,

"color":"purple",
"num":"3"
,

"color":"gold",
"num":"4"
,

"color":"teal",
"num":"1"
];

var arr = data.map(function(d)return d.color;);

// create scale for X-AXIS (1) //
var band = d3.scaleBand() //
.domain(d3.range(data.length))
.range([0,width])
.paddingInner(0.1);


// DEFINE X AXIS
var xAxis = d3.axisBottom(band)
.tickValues(d3.range(data.length))
.tickFormat(function(d,i) return d.color;);

// CREATE X-AXIS
d3.select(location).append("g")
.attr("class","axis") // set class for x-axis
.attr("transform","translate(0,"+height+")")
.call(xAxis);






javascript d3.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 10:12









Gerardo Furtado

66.7k65294




66.7k65294










asked Mar 7 at 3:24









Michael Ramage MikeRamageMichael Ramage MikeRamage

82




82












  • What exactly is your question?

    – Azmisov
    Mar 7 at 3:47






  • 1





    "Scale band's domain will not accept an array as its input"... A scale only accepts an array as the input.

    – Gerardo Furtado
    Mar 7 at 4:37











  • My question is how do I get my x axis ticks to present as strings? What modifications would I need to make to my work? Why are those modifications necessary? @Azmisov

    – Michael Ramage MikeRamage
    Mar 7 at 5:13











  • @MichaelRamageMikeRamage D3 ticks are always strings, so it's not clear what's the issue here. How is your axis? What values do you have now, and what values do you want to show?

    – Gerardo Furtado
    Mar 7 at 5:32











  • I would like each tick on my x-axis to represent a color, which is a string in my dataset. Therefore, each tick should have the color labeled for its corresponding bar. @GerardoFurtado

    – Michael Ramage MikeRamage
    Mar 7 at 5:41

















  • What exactly is your question?

    – Azmisov
    Mar 7 at 3:47






  • 1





    "Scale band's domain will not accept an array as its input"... A scale only accepts an array as the input.

    – Gerardo Furtado
    Mar 7 at 4:37











  • My question is how do I get my x axis ticks to present as strings? What modifications would I need to make to my work? Why are those modifications necessary? @Azmisov

    – Michael Ramage MikeRamage
    Mar 7 at 5:13











  • @MichaelRamageMikeRamage D3 ticks are always strings, so it's not clear what's the issue here. How is your axis? What values do you have now, and what values do you want to show?

    – Gerardo Furtado
    Mar 7 at 5:32











  • I would like each tick on my x-axis to represent a color, which is a string in my dataset. Therefore, each tick should have the color labeled for its corresponding bar. @GerardoFurtado

    – Michael Ramage MikeRamage
    Mar 7 at 5:41
















What exactly is your question?

– Azmisov
Mar 7 at 3:47





What exactly is your question?

– Azmisov
Mar 7 at 3:47




1




1





"Scale band's domain will not accept an array as its input"... A scale only accepts an array as the input.

– Gerardo Furtado
Mar 7 at 4:37





"Scale band's domain will not accept an array as its input"... A scale only accepts an array as the input.

– Gerardo Furtado
Mar 7 at 4:37













My question is how do I get my x axis ticks to present as strings? What modifications would I need to make to my work? Why are those modifications necessary? @Azmisov

– Michael Ramage MikeRamage
Mar 7 at 5:13





My question is how do I get my x axis ticks to present as strings? What modifications would I need to make to my work? Why are those modifications necessary? @Azmisov

– Michael Ramage MikeRamage
Mar 7 at 5:13













@MichaelRamageMikeRamage D3 ticks are always strings, so it's not clear what's the issue here. How is your axis? What values do you have now, and what values do you want to show?

– Gerardo Furtado
Mar 7 at 5:32





@MichaelRamageMikeRamage D3 ticks are always strings, so it's not clear what's the issue here. How is your axis? What values do you have now, and what values do you want to show?

– Gerardo Furtado
Mar 7 at 5:32













I would like each tick on my x-axis to represent a color, which is a string in my dataset. Therefore, each tick should have the color labeled for its corresponding bar. @GerardoFurtado

– Michael Ramage MikeRamage
Mar 7 at 5:41





I would like each tick on my x-axis to represent a color, which is a string in my dataset. Therefore, each tick should have the color labeled for its corresponding bar. @GerardoFurtado

– Michael Ramage MikeRamage
Mar 7 at 5:41












2 Answers
2






active

oldest

votes


















1














The problem lies here:



var band = d3.scaleBand()
.domain(d3.range(data.length))


As you can see, you're passing just a bunch of numbers (I'd guess you're using them as indices) to the scale. Here they are:






var data = [
"color": "blue",
"num": "8"
,

"color": "green",
"num": "8"
,

"color": "red",
"num": "6"
,

"color": "black",
"num": "2"
,

"color": "purple",
"num": "3"
,

"color": "gold",
"num": "4"
,

"color": "teal",
"num": "1"

];

console.log(d3.range(data.length))

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>





Because of that, your axis will have just numbers (unless you do a cumbersome tickFormat acceding data again):






var data = [
"color": "blue",
"num": "8"
,

"color": "green",
"num": "8"
,

"color": "red",
"num": "6"
,

"color": "black",
"num": "2"
,

"color": "purple",
"num": "3"
,

"color": "gold",
"num": "4"
,

"color": "teal",
"num": "1"

];

var svg = d3.select("svg")
var scale = d3.scaleBand()
.domain(d3.range(data.length))
.range([20, 480]);
var axis = d3.axisBottom(scale);
axis(svg.append("g").attr("transform", "translate(0,50)"))

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width="500" height="100"></svg>





The idiomatic solution is passing the categorical variable you want to depict in the data visualisation to the band scale. This seems to be a bar chart or a similar visualisation type, in which the color is the categorical variable. That being the case, pass it to the scale:



.domain(data.map(d => d.color))


Here is how the axis will be:






var data = [
"color": "blue",
"num": "8"
,

"color": "green",
"num": "8"
,

"color": "red",
"num": "6"
,

"color": "black",
"num": "2"
,

"color": "purple",
"num": "3"
,

"color": "gold",
"num": "4"
,

"color": "teal",
"num": "1"

];

var svg = d3.select("svg")
var scale = d3.scaleBand()
.domain(data.map(d => d.color))
.range([20, 480]);
var axis = d3.axisBottom(scale);
axis(svg.append("g").attr("transform", "translate(0,50)"))

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg width="500" height="100"></svg>








share|improve this answer






























    0














    I think the problem is with your tickFormat, not the scaleBand. The format function should be something like:



    .tickFormat((i) => return data[i].color; );


    I'm not sure what the num field in your data is supposed to represent, if relevant. Maybe you need to cleanup up your data representation to fit d3 better.






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55035574%2ftext-value-in-axis-ticks-using-scaleband%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      The problem lies here:



      var band = d3.scaleBand()
      .domain(d3.range(data.length))


      As you can see, you're passing just a bunch of numbers (I'd guess you're using them as indices) to the scale. Here they are:






      var data = [
      "color": "blue",
      "num": "8"
      ,

      "color": "green",
      "num": "8"
      ,

      "color": "red",
      "num": "6"
      ,

      "color": "black",
      "num": "2"
      ,

      "color": "purple",
      "num": "3"
      ,

      "color": "gold",
      "num": "4"
      ,

      "color": "teal",
      "num": "1"

      ];

      console.log(d3.range(data.length))

      <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>





      Because of that, your axis will have just numbers (unless you do a cumbersome tickFormat acceding data again):






      var data = [
      "color": "blue",
      "num": "8"
      ,

      "color": "green",
      "num": "8"
      ,

      "color": "red",
      "num": "6"
      ,

      "color": "black",
      "num": "2"
      ,

      "color": "purple",
      "num": "3"
      ,

      "color": "gold",
      "num": "4"
      ,

      "color": "teal",
      "num": "1"

      ];

      var svg = d3.select("svg")
      var scale = d3.scaleBand()
      .domain(d3.range(data.length))
      .range([20, 480]);
      var axis = d3.axisBottom(scale);
      axis(svg.append("g").attr("transform", "translate(0,50)"))

      <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
      <svg width="500" height="100"></svg>





      The idiomatic solution is passing the categorical variable you want to depict in the data visualisation to the band scale. This seems to be a bar chart or a similar visualisation type, in which the color is the categorical variable. That being the case, pass it to the scale:



      .domain(data.map(d => d.color))


      Here is how the axis will be:






      var data = [
      "color": "blue",
      "num": "8"
      ,

      "color": "green",
      "num": "8"
      ,

      "color": "red",
      "num": "6"
      ,

      "color": "black",
      "num": "2"
      ,

      "color": "purple",
      "num": "3"
      ,

      "color": "gold",
      "num": "4"
      ,

      "color": "teal",
      "num": "1"

      ];

      var svg = d3.select("svg")
      var scale = d3.scaleBand()
      .domain(data.map(d => d.color))
      .range([20, 480]);
      var axis = d3.axisBottom(scale);
      axis(svg.append("g").attr("transform", "translate(0,50)"))

      <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
      <svg width="500" height="100"></svg>








      share|improve this answer



























        1














        The problem lies here:



        var band = d3.scaleBand()
        .domain(d3.range(data.length))


        As you can see, you're passing just a bunch of numbers (I'd guess you're using them as indices) to the scale. Here they are:






        var data = [
        "color": "blue",
        "num": "8"
        ,

        "color": "green",
        "num": "8"
        ,

        "color": "red",
        "num": "6"
        ,

        "color": "black",
        "num": "2"
        ,

        "color": "purple",
        "num": "3"
        ,

        "color": "gold",
        "num": "4"
        ,

        "color": "teal",
        "num": "1"

        ];

        console.log(d3.range(data.length))

        <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>





        Because of that, your axis will have just numbers (unless you do a cumbersome tickFormat acceding data again):






        var data = [
        "color": "blue",
        "num": "8"
        ,

        "color": "green",
        "num": "8"
        ,

        "color": "red",
        "num": "6"
        ,

        "color": "black",
        "num": "2"
        ,

        "color": "purple",
        "num": "3"
        ,

        "color": "gold",
        "num": "4"
        ,

        "color": "teal",
        "num": "1"

        ];

        var svg = d3.select("svg")
        var scale = d3.scaleBand()
        .domain(d3.range(data.length))
        .range([20, 480]);
        var axis = d3.axisBottom(scale);
        axis(svg.append("g").attr("transform", "translate(0,50)"))

        <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
        <svg width="500" height="100"></svg>





        The idiomatic solution is passing the categorical variable you want to depict in the data visualisation to the band scale. This seems to be a bar chart or a similar visualisation type, in which the color is the categorical variable. That being the case, pass it to the scale:



        .domain(data.map(d => d.color))


        Here is how the axis will be:






        var data = [
        "color": "blue",
        "num": "8"
        ,

        "color": "green",
        "num": "8"
        ,

        "color": "red",
        "num": "6"
        ,

        "color": "black",
        "num": "2"
        ,

        "color": "purple",
        "num": "3"
        ,

        "color": "gold",
        "num": "4"
        ,

        "color": "teal",
        "num": "1"

        ];

        var svg = d3.select("svg")
        var scale = d3.scaleBand()
        .domain(data.map(d => d.color))
        .range([20, 480]);
        var axis = d3.axisBottom(scale);
        axis(svg.append("g").attr("transform", "translate(0,50)"))

        <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
        <svg width="500" height="100"></svg>








        share|improve this answer

























          1












          1








          1







          The problem lies here:



          var band = d3.scaleBand()
          .domain(d3.range(data.length))


          As you can see, you're passing just a bunch of numbers (I'd guess you're using them as indices) to the scale. Here they are:






          var data = [
          "color": "blue",
          "num": "8"
          ,

          "color": "green",
          "num": "8"
          ,

          "color": "red",
          "num": "6"
          ,

          "color": "black",
          "num": "2"
          ,

          "color": "purple",
          "num": "3"
          ,

          "color": "gold",
          "num": "4"
          ,

          "color": "teal",
          "num": "1"

          ];

          console.log(d3.range(data.length))

          <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>





          Because of that, your axis will have just numbers (unless you do a cumbersome tickFormat acceding data again):






          var data = [
          "color": "blue",
          "num": "8"
          ,

          "color": "green",
          "num": "8"
          ,

          "color": "red",
          "num": "6"
          ,

          "color": "black",
          "num": "2"
          ,

          "color": "purple",
          "num": "3"
          ,

          "color": "gold",
          "num": "4"
          ,

          "color": "teal",
          "num": "1"

          ];

          var svg = d3.select("svg")
          var scale = d3.scaleBand()
          .domain(d3.range(data.length))
          .range([20, 480]);
          var axis = d3.axisBottom(scale);
          axis(svg.append("g").attr("transform", "translate(0,50)"))

          <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
          <svg width="500" height="100"></svg>





          The idiomatic solution is passing the categorical variable you want to depict in the data visualisation to the band scale. This seems to be a bar chart or a similar visualisation type, in which the color is the categorical variable. That being the case, pass it to the scale:



          .domain(data.map(d => d.color))


          Here is how the axis will be:






          var data = [
          "color": "blue",
          "num": "8"
          ,

          "color": "green",
          "num": "8"
          ,

          "color": "red",
          "num": "6"
          ,

          "color": "black",
          "num": "2"
          ,

          "color": "purple",
          "num": "3"
          ,

          "color": "gold",
          "num": "4"
          ,

          "color": "teal",
          "num": "1"

          ];

          var svg = d3.select("svg")
          var scale = d3.scaleBand()
          .domain(data.map(d => d.color))
          .range([20, 480]);
          var axis = d3.axisBottom(scale);
          axis(svg.append("g").attr("transform", "translate(0,50)"))

          <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
          <svg width="500" height="100"></svg>








          share|improve this answer













          The problem lies here:



          var band = d3.scaleBand()
          .domain(d3.range(data.length))


          As you can see, you're passing just a bunch of numbers (I'd guess you're using them as indices) to the scale. Here they are:






          var data = [
          "color": "blue",
          "num": "8"
          ,

          "color": "green",
          "num": "8"
          ,

          "color": "red",
          "num": "6"
          ,

          "color": "black",
          "num": "2"
          ,

          "color": "purple",
          "num": "3"
          ,

          "color": "gold",
          "num": "4"
          ,

          "color": "teal",
          "num": "1"

          ];

          console.log(d3.range(data.length))

          <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>





          Because of that, your axis will have just numbers (unless you do a cumbersome tickFormat acceding data again):






          var data = [
          "color": "blue",
          "num": "8"
          ,

          "color": "green",
          "num": "8"
          ,

          "color": "red",
          "num": "6"
          ,

          "color": "black",
          "num": "2"
          ,

          "color": "purple",
          "num": "3"
          ,

          "color": "gold",
          "num": "4"
          ,

          "color": "teal",
          "num": "1"

          ];

          var svg = d3.select("svg")
          var scale = d3.scaleBand()
          .domain(d3.range(data.length))
          .range([20, 480]);
          var axis = d3.axisBottom(scale);
          axis(svg.append("g").attr("transform", "translate(0,50)"))

          <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
          <svg width="500" height="100"></svg>





          The idiomatic solution is passing the categorical variable you want to depict in the data visualisation to the band scale. This seems to be a bar chart or a similar visualisation type, in which the color is the categorical variable. That being the case, pass it to the scale:



          .domain(data.map(d => d.color))


          Here is how the axis will be:






          var data = [
          "color": "blue",
          "num": "8"
          ,

          "color": "green",
          "num": "8"
          ,

          "color": "red",
          "num": "6"
          ,

          "color": "black",
          "num": "2"
          ,

          "color": "purple",
          "num": "3"
          ,

          "color": "gold",
          "num": "4"
          ,

          "color": "teal",
          "num": "1"

          ];

          var svg = d3.select("svg")
          var scale = d3.scaleBand()
          .domain(data.map(d => d.color))
          .range([20, 480]);
          var axis = d3.axisBottom(scale);
          axis(svg.append("g").attr("transform", "translate(0,50)"))

          <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
          <svg width="500" height="100"></svg>








          var data = [
          "color": "blue",
          "num": "8"
          ,

          "color": "green",
          "num": "8"
          ,

          "color": "red",
          "num": "6"
          ,

          "color": "black",
          "num": "2"
          ,

          "color": "purple",
          "num": "3"
          ,

          "color": "gold",
          "num": "4"
          ,

          "color": "teal",
          "num": "1"

          ];

          console.log(d3.range(data.length))

          <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>





          var data = [
          "color": "blue",
          "num": "8"
          ,

          "color": "green",
          "num": "8"
          ,

          "color": "red",
          "num": "6"
          ,

          "color": "black",
          "num": "2"
          ,

          "color": "purple",
          "num": "3"
          ,

          "color": "gold",
          "num": "4"
          ,

          "color": "teal",
          "num": "1"

          ];

          console.log(d3.range(data.length))

          <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>





          var data = [
          "color": "blue",
          "num": "8"
          ,

          "color": "green",
          "num": "8"
          ,

          "color": "red",
          "num": "6"
          ,

          "color": "black",
          "num": "2"
          ,

          "color": "purple",
          "num": "3"
          ,

          "color": "gold",
          "num": "4"
          ,

          "color": "teal",
          "num": "1"

          ];

          var svg = d3.select("svg")
          var scale = d3.scaleBand()
          .domain(d3.range(data.length))
          .range([20, 480]);
          var axis = d3.axisBottom(scale);
          axis(svg.append("g").attr("transform", "translate(0,50)"))

          <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
          <svg width="500" height="100"></svg>





          var data = [
          "color": "blue",
          "num": "8"
          ,

          "color": "green",
          "num": "8"
          ,

          "color": "red",
          "num": "6"
          ,

          "color": "black",
          "num": "2"
          ,

          "color": "purple",
          "num": "3"
          ,

          "color": "gold",
          "num": "4"
          ,

          "color": "teal",
          "num": "1"

          ];

          var svg = d3.select("svg")
          var scale = d3.scaleBand()
          .domain(d3.range(data.length))
          .range([20, 480]);
          var axis = d3.axisBottom(scale);
          axis(svg.append("g").attr("transform", "translate(0,50)"))

          <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
          <svg width="500" height="100"></svg>





          var data = [
          "color": "blue",
          "num": "8"
          ,

          "color": "green",
          "num": "8"
          ,

          "color": "red",
          "num": "6"
          ,

          "color": "black",
          "num": "2"
          ,

          "color": "purple",
          "num": "3"
          ,

          "color": "gold",
          "num": "4"
          ,

          "color": "teal",
          "num": "1"

          ];

          var svg = d3.select("svg")
          var scale = d3.scaleBand()
          .domain(data.map(d => d.color))
          .range([20, 480]);
          var axis = d3.axisBottom(scale);
          axis(svg.append("g").attr("transform", "translate(0,50)"))

          <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
          <svg width="500" height="100"></svg>





          var data = [
          "color": "blue",
          "num": "8"
          ,

          "color": "green",
          "num": "8"
          ,

          "color": "red",
          "num": "6"
          ,

          "color": "black",
          "num": "2"
          ,

          "color": "purple",
          "num": "3"
          ,

          "color": "gold",
          "num": "4"
          ,

          "color": "teal",
          "num": "1"

          ];

          var svg = d3.select("svg")
          var scale = d3.scaleBand()
          .domain(data.map(d => d.color))
          .range([20, 480]);
          var axis = d3.axisBottom(scale);
          axis(svg.append("g").attr("transform", "translate(0,50)"))

          <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
          <svg width="500" height="100"></svg>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 10:11









          Gerardo FurtadoGerardo Furtado

          66.7k65294




          66.7k65294























              0














              I think the problem is with your tickFormat, not the scaleBand. The format function should be something like:



              .tickFormat((i) => return data[i].color; );


              I'm not sure what the num field in your data is supposed to represent, if relevant. Maybe you need to cleanup up your data representation to fit d3 better.






              share|improve this answer



























                0














                I think the problem is with your tickFormat, not the scaleBand. The format function should be something like:



                .tickFormat((i) => return data[i].color; );


                I'm not sure what the num field in your data is supposed to represent, if relevant. Maybe you need to cleanup up your data representation to fit d3 better.






                share|improve this answer

























                  0












                  0








                  0







                  I think the problem is with your tickFormat, not the scaleBand. The format function should be something like:



                  .tickFormat((i) => return data[i].color; );


                  I'm not sure what the num field in your data is supposed to represent, if relevant. Maybe you need to cleanup up your data representation to fit d3 better.






                  share|improve this answer













                  I think the problem is with your tickFormat, not the scaleBand. The format function should be something like:



                  .tickFormat((i) => return data[i].color; );


                  I'm not sure what the num field in your data is supposed to represent, if relevant. Maybe you need to cleanup up your data representation to fit d3 better.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 7 at 7:41









                  AzmisovAzmisov

                  2,35842953




                  2,35842953



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55035574%2ftext-value-in-axis-ticks-using-scaleband%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                      Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                      Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved