0

I want a stacked area chart of several datasets. possibly unusually I want to use the y axis to clarify what each set represents and its starting value.

i.e. The Y-axis doesn't need to be a regular tick over the range of the data.

for the sets:

[
{key: 'the first set', values: [x: 0, y: 4]},
{key: 'the second set', values: [x: 0, y: 10]},
]

the y-axis should have a tick at 4 labelled: 'the first set: 4', and another at 10 labelled: 'the second set: 10'

nv.addGraph(function() {
  var chart = nv.models.stackedAreaChart()
    .showLegend(false)
    .showControls(false);

  var data = [
    {
      key: 'first',
      values: [
        { x: 0, y: 2 }, 
        { x: 2, y: 4 }, 
        { x: 4, y: 6 }, 
        { x: 6, y: 8 }
      ]
    }, 
    {
      key: 'second',
      values: [
        { x: 0, y: 4 }, 
        { x: 2, y: 6 }, 
        { x: 4, y: 8 }, 
        { x: 6, y: 10 }
      ]
    } 
  ];

  var firstItemsLabels = ['', 'first', 'second', ''];
  var firstItemsValues = [0, 2, 4, 10];
  //the below doesn't seem to make any difference
  //var firstItemsLabels = ['first', 'second'];
  //var firstItemsValues = [2, 4];

  chart.yAxis.tickValues(firstItemsLabels);
  chart.yAxis.tickFormat(function(d, i) {
    console.log('getting tick format for d: "' + d + '" at i ' + i);
    console.log('for i: ' + i + ' = ' + firstItemsValues[i]);
    var result = firstItemsValues[i];
    return result;
  });

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

  nv.utils.windowResize(chart.update);
  return chart;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.3.13/d3.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/nvd3/1.8.1/nv.d3.css" rel="stylesheet">
<div id="chart">
  <svg></svg>
</div>

I thought that the code in this snippet should achieve that but it doesn't

The console log output from that snippet...

getting tick format for d: "" at i 0
for i: 0 = 0
getting tick format for d: "first" at i 1
for i: 1 = 2
Error: Invalid value for <g> attribute transform="translate(0,NaN)
getting tick format for d: "0" at i undefined
for i: undefined = undefined
getting tick format for d: "18" at i undefined
for i: undefined = undefined

...confuses me because tickformat is looking for a value of 18 at one point which isn't in the dataset.

Am I trying to achieve something impossible? If not, since I'm clearly doing it wrong, how can it be done?

Paul D'Ambra
  • 7,629
  • 3
  • 51
  • 96

1 Answers1

1

If I understand your goal correctly, you can use:

  chart.yAxis.tickValues([4,10]);
  chart.yAxis.tickFormat(function(d, i) {
    if (d === 4 || d == 10) {
      return "the first set: " + d
    }
  });

See this Plunk for a working example: http://plnkr.co/edit/sdM14ebvv8cYCvOtX8em?p=preview

Lucas
  • 1,359
  • 7
  • 16
  • ah, that's not quite what I'm after but I think it helps me take the next step. The i variable clearly isn't doing what I think it is. – Paul D'Ambra Sep 12 '15 at 20:55