Search code examples
javascriptjquerycanvasjs

Graph using CanvasJS is empty when I use specific dateTime format


I am using CanvasJS to show a graph in my page. The x contains dates and y numbers..

I set:

xValueType: "dateTime",
xValueFormatString: "YYYY-MM-DD HH:mm",

to format my dates e.g. '2018-11-08 13:11' but the graph is empty.. Something wrong is with the dateFormat but I can't figure out what is it...

window.onload = function() {

  var chart = new CanvasJS.Chart("grafima1", {
    animationEnabled: true,
    title: {
      text: "title"
    },
    axisX: {
      title: "date"
    },
    axisY: {
      title: "Num",
      suffix: "",
      stripLines: [{
        value: 3,
        label: "3"
      }]
    },
    data: [{
      type: "line",
      name: "Ώρες",
      connectNullData: true,
      xValueType: "dateTime",
      xValueFormatString: "YYYY-MM-DD HH:mm",
      yValueFormatString: "#,##0.##",
      dataPoints: [{
          x: '2018-11-08 13:11',
          y: 2.0
        },
        {
          x: '2018-11-05 15:23',
          y: 5.0
        }

      ]
    }]
  });
  chart.render();


};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>

<div id="grafima1" style="height: 370px; width: 100%;"></div>


Solution

  • window.onload = function () {
    
            var chart = new CanvasJS.Chart("grafima1", {
                animationEnabled: true,
                title: {
                    text: "title"
                },
                axisX: {
                    title: "date"
                },
                axisY: {
                    title: "Num",
                    suffix: "",
                    stripLines: [{
                        value: 3,
                        label: "3"
                    }]
                },
                data: [{
                    type: "line",
                    name: "Ώρες",
                    connectNullData: true,
                    xValueType: "dateTime",
                    xValueFormatString: "YYYY-MM-DD HH:mm",
                    yValueFormatString: "#,##0.##",
                    dataPoints: [{
                        x: new Date("2018-11-08 13:11"),
                        y: 2.0
                    },
                    {
                        x: new Date("2018-11-05 15:23"),
                        y: 5.0
                    }
                    ]
                }]
            });
            chart.render();
    
        };
    <script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="grafima1" style="height: 370px; width: 100%;"></div>

    enter image description here