Search code examples
javascriptdygraphs

Dygraphs: No Label on X Axis when data have only one record


I start to use Dygraphs recently, and I found that if data have only one record, there is no label show on the x-axis under the graph.

For Example, using the code from tutorial of Dygraphs site :

<html>
<head>
<script type="text/javascript"
  src="dygraph-combined-dev.js"></script>
</head>
<body>
<div id="graphdiv"></div>
<script type="text/javascript">
  g = new Dygraph(

    // containing div
    document.getElementById("graphdiv"),

    // CSV or path to a CSV file.
    "Date,Temperature\n" +
    "2008-05-07,75\n" +
    "2008-05-08,70\n" +
    "2008-05-09,80\n"

  );
</script>
</body>
</html>

I change the data to one record:

// CSV or path to a CSV file.
"Date,Temperature\n" +
"2008-05-07,75\n"

No label is shown on x-axis.

I have tried to have a look on the options reference of Dygraphs, but seems no option is for drawing the label when data have only one record.

Any idea how to draw the label when only one record is being inputted?


Solution

  • With just one value, dygraphs has no sense for the scale of your data. Should the x-axis span a year? A month? A century?

    The solution is to set the scale explicitly:

    g = new Dygraph(
      // containing div
      "graphdiv",
      // CSV or path to a CSV file.
      "Date,Temperature\n" +
      "2008-05-07,75\n",
      {
        dateWindow: [new Date(2008, 0, 0), new Date(2008, 11, 30)]
      }
    );