0

I'm trying to create a line chart using the Morris.js source but I'm looking to get months put into the chart rather than the example years. Now the problem is when I change the years field the chart no longer works I've added the two examples below the first is the working years version while the second is the non-working months version.

If someone can see where I'm going wrong or if i'm missing something that'd be very helpful.

Thanks in advance.

Chart Years

new Morris.Line({
  // ID of the element in which to draw the chart.
  element: 'monthlyReport',
  // Chart data records -- each entry in this array corresponds to a point on
  // the chart.
  data: [{
      year: '2008',
      value: 20
    },
    {
      year: '2009',
      value: 10
    },
    {
      year: '2010',
      value: 5
    },
    {
      year: '2011',
      value: 5
    },
    {
      year: '2012',
      value: 20
    }
  ],
  // The name of the data record attribute that contains x-values.
  xkey: 'year',
  // A list of names of data record attributes that contain y-values.
  ykeys: ['value'],
  // Labels for the ykeys -- will be displayed when you hover over the
  // chart.
  labels: ['Value']
});
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
<div id="monthlyReport" style="height: 500px;width:50%;margin-left:25%;margin-bottom:100px;"></div>

Chart Months

new Morris.Line({
  // ID of the element in which to draw the chart.
  element: 'monthlyReport',
  // Chart data records -- each entry in this array corresponds to a point on
  // the chart.
  data: [{
      month: 'Jan',
      value: 20
    },
    {
      month: 'Feb',
      value: 10
    },
    {
      month: 'Mar',
      value: 5
    },
    {
      month: 'Apr',
      value: 5
    },
    {
      month: 'May',
      value: 20
    }
  ],
  // The name of the data record attribute that contains x-values.
  xkey: 'month',
  // A list of names of data record attributes that contain y-values.
  ykeys: ['value'],
  // Labels for the ykeys -- will be displayed when you hover over the
  // chart.
  labels: ['Value']
});
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.min.js"></script>
<div id="monthlyReport" style="height: 500px;width:50%;margin-left:25%;margin-bottom:100px;"></div>
ben topper
  • 13
  • 1
  • 4

2 Answers2

1

You can try to set the option parseTime to false.

Pierre
  • 1,044
  • 15
  • 27
0

This is because the key values must be numeric. For year, you're using numeric values (2008-2012), but in the second sample, you're using a string for each month. Replace the key values with numbers and it will work.

Use the following guide to configure Morris.line to show your month names: https://morrisjs.github.io/morris.js/lines.html

Alexander van Oostenrijk
  • 4,644
  • 3
  • 23
  • 37