0

I don't know where I am making an error in this code.
What I need to do is to have a HighCharts drawing with the values from the data base. The values could change at anytime.
- The x-axis should be labeled like " Nov 30 " as the last point down to " Oct 30 " as the first point, that means 30 days ( I want to display the chart on 30 days only ). This is an example , the date can vary in time. - The JS is sending the request via ajax and the result is returned in json format. This is one of the row : {"time":"2012-08-07 21:15:05","value_1":"1.241110","value_2":"1.241103"} The graph has two lines ( value_1 and value_2 ) and all correspond to the same Time on the x-axis.
I read somewhere that I have to convert the format of the date to UTC. This is the function

function get_and_display_data(symbol_1,price_1,period_1,bars_1,constraints_1){

var highchartsOptions = Highcharts.setOptions(Highcharts.theme);

var options = {
    chart: {
        renderTo: 'graph',
        type: 'line',
        marginRight: 130,
        marginBottom: 25,
        reflow: false,
        borderWidth: 0
    },
    title: {
        text: 'Data',
        x: -20 //center
    },
    xAxis: {
        type:'datetime',
        dateTimeLabelFormats: {
            year: '%b \'%y'
        }
    },
    yAxis: {
        title: {
            text: 'This is a text !'
        },
        plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
        }]
    },
    tooltip: {
        formatter: function() {
            return '<b>'+ this.series.name +'</b><br/>'+
            Highcharts.dateFormat ('%A, %b %e, %Y', this.point.x) +': '+ this.y;
        }
    },
    legend: {
        layout: 'vertical',
        align: 'right',
        verticalAlign: 'top',
        x: -10,
        y: 100,
        borderWidth: 0
    },
    plotOptions: {
        series: {
            marker: {
                enabled: false,
                states: {
                    hover: {
                        enabled: true,
                        radius: 3
                    }
                }
            }
        }
    },
    series: [{
        name: ''
        }, {
        name: ''
    }]
}
$.ajax({
    url: url_base+"show_page/graph_data.do",
    type: 'POST',
    data: 'symbol='+symbol_1+"&price="+price_1+"&period="+period_1+"&bars="+bars_1+"&constraints="+constraints_1,
    cache: false,
    dataType: "json",
    success: function(html) {
        var data_array = [];
        var data_1 = [];
        var data_2 = [];
        var i = 0;
        var time_x_axis = [];
        var sLegend = [];
        try{
            data_array = html;
            sLegend = price_1.split(',');
            if(html == null){
                alert('No data was found. Try it again ');
            }else{
                for(i=0;i<data_array.length;i++)
                {
                    time_x_axis[i] = data_array[i]['time'];
                    data_1[i] = '['+Date.UTC(time_x_axis[i].substr(0, 4), time_x_axis[i].substr(5,2), time_x_axis[i].substr(8,2), time_x_axis[i].substr(11,2), time_x_axis[i].substr(14,2), time_x_axis[i].substr(17,2))+','+parseFloat(data_array[i]['value_1'])+']';
                    data_2[i] = '['+Date.UTC(time_x_axis[i].substr(0, 4), time_x_axis[i].substr(5,2), time_x_axis[i].substr(8,2), time_x_axis[i].substr(11,2), time_x_axis[i].substr(14,2), time_x_axis[i].substr(17,2))+','+parseFloat(data_array[i]['value_2'])+']';                        
                }
            }
        }catch(e){ }
        options.series[0].data = '['+data_1+']';
        options.series[0].name = sLegend[1];
        options.series[1].data = data_2;
        options.series[1].name = sLegend[2];
        chart = new Highcharts.Chart(options);
    }
});}

Here I get result in html variable and affect them to Data_array[] . Because the series data should be in the format Data:[[],[],[]..[]] , that is why I put *options.series[0].data = '['+data_1+']';* .
For *data_1[i]*, I have to substr the time to get the Year, Month, Day, Hour, Min and Sec.
What am I missing inside the code ?

DeathCoder
  • 145
  • 1
  • 4
  • 19

1 Answers1

0

Can you try the following changes?

Make the data_1[i] & data_2[i] as arrays by using [...,...] instead of '[' + ... + ',' + ... + ']' (This actually results in a string and not array)

                data_1[i] = [parseInt(Date.UTC(time_x_axis[i].substr(0, 4), time_x_axis[i].substr(5,2), time_x_axis[i].substr(8,2), time_x_axis[i].substr(11,2), time_x_axis[i].substr(14,2), time_x_axis[i].substr(17,2))),parseFloat(data_array[i]['value_1'])];
                data_2[i] = [parseInt(Date.UTC(time_x_axis[i].substr(0, 4), time_x_axis[i].substr(5,2), time_x_axis[i].substr(8,2), time_x_axis[i].substr(11,2), time_x_axis[i].substr(14,2), time_x_axis[i].substr(17,2))),parseFloat(data_array[i]['value_2'])];         

AND

Remove the '[' + ... + ']' from series data, since data_1 is already an array of array, you don't need to add the square brackets using any concatenation. Your approach would anyway again result in a string because of concatenation.

    options.series[0].data = data_1;               

Related: Highcharts returning error 14

Community
  • 1
  • 1
Jugal Thakkar
  • 13,432
  • 4
  • 61
  • 79
  • yes this one seems to be working but the other problem I have is based on the x-axis. I want to have labels with 30 days. Starting from the first entry from the database minuis 30 days. that is if the date is today 01 Nov , i want the a-axis to be labeled from 01 Oct 02Oct 03Oct .. 01Nov – DeathCoder Oct 31 '12 at 23:17
  • Another problem is that, when I point the mouse on the line chart, the only combined data I can have are the first and the last point at the boundaries. When I try to get the cordinates of any point on the line, it is not showed. – DeathCoder Nov 01 '12 at 00:57
  • It sometimes becomes difficult to understand, reproduce, debug and answer questions from words, a [jsFiddle](http://jsfiddle.net/) repro really helps, can you do one for us? If the questions are independent and isolated, consider asking them as separate questions against comments, as they can be seen by people who haven't answered the first question but may be able to help with the next – Jugal Thakkar Nov 01 '12 at 05:24