nvd3 line chart with string values on x-axis
Asked Answered
K

2

18

i'm new to nvd3 charts. i need a line chart, with string-values on the x-axis the chart should be like this Bar Chart, but i need a line, instead of bars

my result looks like this Line Chart The values are all mapped to x=0

my code

nv.addGraph(function() {
    var chart = nv.models.lineChart()
    .useInteractiveGuideline(true) 
    .transitionDuration(350)
    .x(function(d) { return d.x}) 
    .y(function(d) { return d.y}) 
    .showLegend(true)
    .showYAxis(true)
    .showXAxis(true);

    chart.xAxis.tickValues(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]);

    d3.select(element + ' svg')
    .datum(data) 
    .call(chart);

    nv.utils.windowResize(chart.update);

    return chart;
});

and my data

[{"color":"#a215af","key":"products","values":[
    {"label":"Monday","y":0,"x":"Monday"},
    {"label":"Tuesday","y":0,"x":"Tuesday"},
    {"label":"Wednesday","y":1,"x":"Wednesday"},
    {"label":"Thursday","y":6,"x":"Thursday"},
    {"label":"Friday","y":2,"x":"Friday"},
    {"label":"Saturday","y":0,"x":"Saturday"},
    {"label":"Sunday","y":13,"x":"Sunday"}]}] 

i tried a lot, but have no idea what to do.

any help or suggestions would be great


Solution like dcclassics mentioned i took number values instead of strings and then used tickValues and tickFormat:

var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

chart.xAxis.tickValues([0, 1, 2, 3, 4, 5, 6])
.tickFormat(function(d){
    return days[d]
});
Keon answered 18/5, 2014 at 22:43 Comment(4)
If I remember correctly, what happens if you change your X axis values to numbers? (1-7) instead of days? Labels won't tell it where to plot data, it needs to know the value of the data.Boelter
thx! with numbers it works Chart But i need string values. is there a way to map labels to the numbers? with scale.ordinal, or something like that?Keon
I found a solution and updated the question! thx dcclassics!Keon
Please create an answer for your own question and mark is as accepter.Louielouis
K
19

like dcclassics mentioned i took number values instead of strings and then used tickValues and tickFormat:

var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

chart.xAxis.tickValues([0, 1, 2, 3, 4, 5, 6])
.tickFormat(function(d){
    return days[d]
});

this solutions worked for me

Keon answered 19/5, 2015 at 12:40 Comment(0)
B
5

Working solution:

var data = [{"color":"#a215af","key":"products","values":[
    {"y":0,"x":0},
    {"y":0,"x":1},
    {"y":1,"x":2},
    {"y":6,"x":3},
    {"y":2,"x":4},
    {"y":0,"x":5},
    {"y":13,"x":6}]}] 

nv.addGraph(function() {
  var chart = nv.models.lineChart()
    .useInteractiveGuideline(true) 
    .transitionDuration(350)
    .x(function(d) { return d.x}) 
    .y(function(d) { return d.y}) 
    .showLegend(true)
    .showYAxis(true)
    .showXAxis(true);

  var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

  chart.xAxis
    .tickValues([0, 1, 2, 3, 4, 5, 6])
    .tickFormat(function(d){
      return days[d]
    });

  d3.select(element + ' svg')
    .datum(data) 
    .call(chart);

  nv.utils.windowResize(chart.update);

  return chart;
});
Bawd answered 17/3, 2015 at 22:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.