Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kshitizmittal's avatar

JSON with Date object

function onChartClick(e){
        jQuery.ajax({
            url: "./pipsubchart",
            type: "GET",
            data: {
                _token: '{{csrf_token()}}',
                pip_type: e.dataPoint.name,
                currMonth: jQuery("#chartMonth").val(),
                currYear: jQuery("#chartYear").val()
            },
            success: function(data) {
                data.forEach(function(value) {
                    value.x = new Date(value.x);
                });
                document.getElementById("result").innerHTML = JSON.stringify(data);
            }
        });
    }

I am passiing this above JSON in canvas JS charts datapoints but, it asks for date object as a input value,

My concern is i have already converted the date value into object but when i used JSON.stringify() It again changed the value to string.

0 likes
3 replies
Vilfago's avatar

You must fill the chart with your json, don't you ? Not the DOM element.

Where is your chart instantiation ?

kshitizmittal's avatar

@VILFAGO -

data: [
            {
                color: "#393f63",
                markerSize: 0,
                type: "splineArea",
                yValueFormatString: "### Employee",
                dataPoints: [
                    { x: new Date("1 Jan 2015"), y: 11 },
                    { x: new Date("1 Feb 2015"), y: 12 },
                    { x: new Date("1 Mar 2015"), y: 15 },
                    { x: new Date("1 Apr 2015"), y: 20 },
                    { x: new Date("1 May 2015"), y: 8 },
                    { x: new Date("1 Jun 2015"), y: 58 },
                    { x: new Date("1 Jul 2015"), y: 6 },
                    { x: new Date("1 Aug 2015"), y: 14 },
                    { x: new Date("1 Sep 2015"), y: 12 },
                    { x: new Date("1 Oct 2015"), y: 13 },
                    { x: new Date("1 Nov 2015"), y: 28 },
                    { x: new Date("1 Dec 2015"), y: 27 }
                ]
            }
        ]

I am putting the above json to datapoints attribute.

Vilfago's avatar

Can we have full code ? With the html ?

Please or to participate in this conversation.