简体   繁体   中英

How do i set the X-Axis as Date Array in highcharts.js simple highcharts.chart plot or .stockChart?

I can succesffuly chart the data in a table on mysql

    $sql = "SELECT TradeDate  as date, LScore2 as L FROM levermann_kurz WHERE Stock_short='MSFT' ORDER BY TradeDate desc";
    $stock2 = mysqli_query($mysqli,$sql);
    $stock2 = mysqli_fetch_all($stock2,MYSQLI_ASSOC);
    $stock2 = json_encode(array_column($stock2,'L'),JSON_NUMERIC_CHECK);

and the body

<script type="text/javascript">

// load("getdata.php")
var data_stock1 = <?php echo $stock1; ?>;
var data_stock2 = <?php echo $stock2; ?>;

                Highcharts.chart('container', {

                    title: {
                        text: 'Levermann Scores'
                    },

                    subtitle: {
                        text: 'TradeFlags'
                    },

//                    xAxis: {
//                        categories: dates
//                    },

                    yAxis: {
                        title: {
                            text: 'Levermann Score'
                        }
                    },
                    legend: {
                        layout: 'vertical',
                        align: 'right',
                        verticalAlign: 'middle'
                    },

                    plotOptions: {
                        series: {
                            label: {
                                connectorAllowed: false
                            },
                            //pointStart: 0
                        }
                    },

                    series: [{
                        name: 'AAPL',
                        data: data_stock1
                    }, {
                        name: 'MSFT',
                        data: data_stock2
 //                   }, {
 //                       name: 'External Data',
 //                       data: [null, null, 7988, 12169, 15112, 22452, 34400, 34227]
                    }],

                    responsive: {
                        rules: [{
                            condition: {
                                maxWidth: 500
                            },
                            chartOptions: {
                                legend: {
                                    layout: 'horizontal',
                                    align: 'center',
                                    verticalAlign: 'bottom'
                                }
                            }
                        }]
                    }

                });

        </script>               
    </body>

But just can't get it to display the Date array as the x-Axis It just has values 0,1,2,3,4,... etc.

What am i doing wrong? How do you set the x-axis array in the highchart plot?

I thought you could simply pass through a 2 column array with Dates in the first column and Values in the second column ?

Please help!

I have looked on highcharts documentation but it doesnt seem to explain to me how i should pass the data to the chart container. which format it should be?

CONSOLE LOG:

<!DOCTYPE html>
<html>
<head>
        <meta charset="UTF-8">
        <title></title>
        <script src="https://code.highcharts.com/modules/series-label.js"></script>
        <script src="https://code.highcharts.com/modules/exporting.js"></script>
        <script src="https://code.highcharts.com/modules/export-data.js"></script>
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
        <script src="https://code.highcharts.com/stock/highstock.js"></script>
</head>
<body>

<div id="container" style="height: 400px; max-width: 800px"></div>



<script type="text/javascript">

// load("getdata.php")
var data_stock1 = [3,2,2,3,3,3,3,3,3,3,3,3,3,3,3,2,3,3,2,3,2,3,2,2,1,-1,0,0,0,0,0,1,1,0,0,-1,0,-1,-1,-1,-1,-1,0,0];
var data_stock2 = [2,3,2,2,2,2,2,2,2,2,2,2,2,1,2,2,3,2,3,3,1,2,0,2,2,2,1,2,2,2,2,2,2,1,2,2,1,2,2,2,2,1,1,2];
var data_datearray = ["2018-05-25","2018-06-01","2018-06-08","2018-06-15","2018-06-22","2018-06-27","2018-07-06","2018-07-20","2018-07-27","2018-08-03","2018-08-10","2018-08-17","2018-08-24","2018-08-31","2018-09-07","2018-09-14","2018-09-21","2018-09-28","2018-10-05","2018-10-12","2018-10-19","2018-10-26","2018-11-02","2018-11-09","2018-11-16","2018-11-23","2018-11-30","2018-12-07","2018-12-14","2018-12-21","2018-12-28","2019-01-04","2019-01-11","2019-01-18","2019-01-25","2019-02-01","2019-02-08","2019-02-15","2019-02-22","2019-03-01","2019-03-08","2019-03-15","2019-03-22","2019-03-29"];
//var seriesOptions = [], seriesCounter = 0, names = ['MSFT', 'AAPL', 'GOOG'];

/**
 * Create the chart when all data is loaded
 * @returns {undefined}
 */
//function createChart() {

    Highcharts.stockChart('container', {
///


                            title: {
                                    text: 'Levermann Scores'
                                    },


                            xAxis: {
                                    type: 'datetime',
                                    categories: data_datearray
                    },


                    series: [{
                        name: 'AAPL',
                        data: data_stock1
                    }, {
                        name: 'MSFT',
                        data: data_stock2
                    }]

////                    
//                    responsive: {
//                        rules: [{
//                            condition: {
//                                maxWidth: 500
//                            },
//                            chartOptions: {
//                                legend: {
//                                    layout: 'horizontal',
//                                    align: 'center',
//                                    verticalAlign: 'bottom'
//                                }
//                            }
//                        }]
//                    }

        });
// end of container }];
//}

console.log(data_stock1)
        </script>               
    </body>

i can get the date into this format with two columns but it wont work in the chart

var data_dual = [{"date":"2018-05-25","L":"3"},{"date":"2018-06-01","L":"2"},{"date":"2018-06-08","L":"2"},{"date":"2018-06-15","L":"3"},{"date":"2018-06-22","L":"3"},{"date":"2018-06-27","L":"3"},{"date":"2018-07-06","L":"3"},{"date":"2018-07-20","L":"3"},{"date":"2018-07-27","L":"3"},{"date":"2018-08-03","L":"3"},{"date":"2018-08-10","L":"3"},{"date":"2018-08-17","L":"3"},{"date":"2018-08-24","L":"3"},{"date":"2018-08-31","L":"3"},{"date":"2018-09-07","L":"3"},{"date":"2018-09-14","L":"2"},{"date":"2018-09-21","L":"3"},{"date":"2018-09-28","L":"3"},{"date":"2018-10-05","L":"2"},{"date":"2018-10-12","L":"3"},{"date":"2018-10-19","L":"2"},{"date":"2018-10-26","L":"3"},{"date":"2018-11-02","L":"2"},{"date":"2018-11-09","L":"2"},{"date":"2018-11-16","L":"1"},{"date":"2018-11-23","L":"-1"},{"date":"2018-11-30","L":"0"},{"date":"2018-12-07","L":"0"},{"date":"2018-12-14","L":"0"},{"date":"2018-12-21","L":"0"},{"date":"2018-12-28","L":"0"},{"date":"2019-01-04","L":"1"},{"date":"2019-01-11","L":"1"},{"date":"2019-01-18","L":"0"},{"date":"2019-01-25","L":"0"},{"date":"2019-02-01","L":"-1"},{"date":"2019-02-08","L":"0"},{"date":"2019-02-15","L":"-1"},{"date":"2019-02-22","L":"-1"},{"date":"2019-03-01","L":"-1"},{"date":"2019-03-08","L":"-1"},{"date":"2019-03-15","L":"-1"},{"date":"2019-03-22","L":"0"},{"date":"2019-03-29","L":"0"}];

or using UNIX_TIMESTAMP i converted the date format into timestamps:

var data_dual = [{"date":"1527206400","L":"3"},{"date":"1527811200","L":"2"},{"date":"1528416000","L":"2"},{"date":"1529020800","L":"3"},{"date":"1529625600","L":"3"},{"date":"1530057600","L":"3"},{"date":"1530835200","L":"3"},{"date":"1532044800","L":"3"},{"date":"1532649600","L":"3"},{"date":"1533254400","L":"3"},{"date":"1533859200","L":"3"},{"date":"1534464000","L":"3"},{"date":"1535068800","L":"3"},{"date":"1535673600","L":"3"},{"date":"1536278400","L":"3"},{"date":"1536883200","L":"2"},{"date":"1537488000","L":"3"},{"date":"1538092800","L":"3"},{"date":"1538697600","L":"2"},{"date":"1539302400","L":"3"},{"date":"1539907200","L":"2"},{"date":"1540512000","L":"3"},{"date":"1541116800","L":"2"},{"date":"1541721600","L":"2"},{"date":"1542326400","L":"1"},{"date":"1542931200","L":"-1"},{"date":"1543536000","L":"0"},{"date":"1544140800","L":"0"},{"date":"1544745600","L":"0"},{"date":"1545350400","L":"0"},{"date":"1545955200","L":"0"},{"date":"1546560000","L":"1"},{"date":"1547164800","L":"1"},{"date":"1547769600","L":"0"},{"date":"1548374400","L":"0"},{"date":"1548979200","L":"-1"},{"date":"1549584000","L":"0"},{"date":"1550188800","L":"-1"},{"date":"1550793600","L":"-1"},{"date":"1551398400","L":"-1"},{"date":"1552003200","L":"-1"},{"date":"1552608000","L":"-1"},{"date":"1553212800","L":"0"},{"date":"1553817600","L":"0"}];

but using this array data_dual in the series will just render a chart with blank.

You need to use a datetime axis and provide dates as timestamps or as a Date objects, in the first elemets of the data array:

Highcharts.chart('container', {
    xAxis: {
        type: 'datetime'
    },
    series: [{
        data: [
            [1262304000000, 29.9],
            [1267401600000, 71.5],
            [1270080000000, 106.4]
        ]
    }]
});

Or as an object:

series: [{
    data: [{
            x: 1262304000000,
            y: 29.9
        },
        {
            x: 1267401600000,
            y: 71.5
        },
        {
            x: 1270080000000,
            y: 106.4
        }
    ]
}]

Live demo: http://jsfiddle.net/BlackLabel/7tov823n/

API Reference: https://api.highcharts.com/highcharts/series.column.data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM