eloquent_poltergeist
eloquent_poltergeist

Reputation: 195

How to align Highcharts datetime x-axis with data I pass to it?

Here is jsfiddle

https://jsfiddle.net/Lp3tjpuv/

If you move your cursor to the right corner of the chart, you can see the tooltip display that the last piece of data is from 2015-09-06, but the x-axis label shows dates that don't align with the actual data.

How can I make x-axis (its labels) align with the actual data I pass to it?

For reference, I pass to it data only on dates 2015-09-03 and 2015-09-06, so the chart shouldn't look like this. The x-axis distorts the chart.

Code below:

(function () {
    Highcharts.setOptions({
        lang: {
            months: ['Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec'],
            weekdays: ['Neděle', 'Pondělí', 'Úterý', 'Středa', 'Čtvrtek', 'Pátek', 'Sobota']
        }
    });

    window.ChartData = JSON.parse('{"collected":[{"created":"1441287599000","amount":"1"},{"created":"1441373999000","amount":"1"},{"created":"1441463999000","amount":"1"},{"created":"1441546799000","amount":"5"}],"challenges":[{"created":"1441287599000","amount":"1"},{"created":"1441291199000","amount":"2"},{"created":"1441291799000","amount":"3"},{"created":"1441553999000","amount":"4"},{"created":"1441561199000","amount":"5"},{"created":"1441568399000","amount":"6"}],"participants":[{"created":"2015-09-03 15:39:59","amount":"1"},{"created":"2015-09-03 16:39:59","amount":"2"},{"created":"2015-09-03 16:49:59","amount":"2"},{"created":"2015-09-06 17:39:59","amount":"3"},{"created":"2015-09-06 19:39:59","amount":"8"},{"created":"2015-09-06 21:39:59","amount":"18"}],"initialUnixMilliseconds":1441287599000,"pointsInterval":86400000}');

    $(function () {
        $('#container').highcharts({
            chart: {
                type: 'area',
                marginTop: 100
            },
            credits: false,
            title: {
                text: ''
            },
            subtitle: {
                text: ''  //can be html
            },
            xAxis: {
                type: 'datetime',
                labels: {
                    formatter: function () {
                        return moment(this.value).format('D. M.');
                    }
                }
            },
            yAxis: {
                title: {
                    text: ''
                },
                labels: {
                    formatter: function () {
                        return this.value;
                    }
                }
            },
            legend: {
                symbolRadius: 130,
                symbolWidth: 10,
                symbolHeight: 10,
                align: 'center',
                verticalAlign: 'top',
                floating: true,
                x: 0,
                y: 0
            },
            tooltip: {
                pointFormat: '{series.name} produced <b>{point.y:,.0f}</b><br/>warheads in {point.x}'
            },
            plotOptions: {
                area: {
                    marker: {
                        enabled: false,
                        symbol: 'circle',
                        radius: 2,
                        states: {
                            hover: {
                                enabled: true
                            }
                        }
                    }
                }
            },
            series: [
                //{
                //  name: 'test',
                //  color: '#499814',
                //  data: [
                //      [Date.UTC(2015, 9, 21), 1],
                //      [Date.UTC(2015, 9, 22), 1],
                //      [Date.UTC(2015, 9, 23), 1],
                //      [Date.UTC(2015, 9, 24), 1],
                //      [Date.UTC(2015, 9, 25), 5]
                //  ],
                //  pointStart: Date.UTC(2015, 9, 21),
                //  pointInterval: ChartData.pointsInterval
                //},


                //{
                //  name: 'počet výzev',
                //  color: '#499814',
                //  data: processData(ChartData.challenges),
                //  pointStart: ChartData.initialUnixMilliseconds,
                //  pointInterval: ChartData.pointsInterval
                //},
                {
                    name: 'počet účastníků',
                    color: '#F4E134',
                    data: processData(ChartData.participants),
                    pointStart: ChartData.initialUnixMilliseconds,
                    pointInterval: ChartData.pointsInterval
                },
                // {
                //  name: 'počet  odeslaných DMS',
                //  color: '#D34F6A',
                //  data: shuffle(range(240)),
                //  pointStart: ChartData.initialUnixMilliseconds,
                //  pointInterval: ChartData.pointsInterval
                //},
                //{
                //  name: 'vybraných prostředků',
                //  color: '#66ADFF',
                //  data: processData(ChartData.collected),
                //  pointStart: ChartData.initialUnixMilliseconds,
                //  pointInterval: ChartData.pointsInterval
                //}
            ]
        });
    });

    function processData(data) {
        var result = [];
        $.map(data, function (obj) {
            result.push([obj.created, Number(obj.amount)]);
        });
        console.log(result);
        return result;
    }

    function range(n) {
        return Array.apply(null, Array(n)).map(function (_, i) {
            return i;
        })
    }

    function shuffle(array) {
        var counter = array.length, temp, index;

        // While there are elements in the array
        while (counter > 0) {
            // Pick a random index
            index = Math.floor(Math.random() * counter);

            // Decrease counter by 1
            counter--;

            // And swap the last element with it
            temp = array[counter];
            array[counter] = array[index];
            array[index] = temp;
        }

        return array;
    }
}());

//$.each(ChartData.challenges, function (i, v) {
//  console.log(new Date(Number(v.created)));
//});

<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>

Upvotes: 2

Views: 1494

Answers (2)

Paweł Fus
Paweł Fus

Reputation: 45079

To achieve that, use Axis.tickPositioner, for example:

            tickPositioner: function(min, max) {
                return this.series[0].xData.slice();
            },

And demo: https://jsfiddle.net/Lp3tjpuv/2/

Upvotes: 1

Nishith Kant Chaturvedi
Nishith Kant Chaturvedi

Reputation: 4769

I have updated your fiddle at https://jsfiddle.net/Nishith/Lp3tjpuv/1/ removing below section

   labels: {
                    formatter: function () {
                    return moment(this.value).format('D. M.');
                    }
            }

Upvotes: 1

Related Questions