X axis on scatter plot being generated on highcharts

31 views Asked by At

I am trying to a date-based scatter plot. I mean I want to add dates on the x-axis but it is not adding the dates. In my code, I have added a small portion of the data. The data points is over 500000. What is the most efficient way to do that?

My code:

const series = [{"name":"Page Views","id":"ABC-page_views","data":[["2024-02-26T00:00:00.000Z",1],["2024-02-28T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-28T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-28T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",3],["2024-02-29T00:00:00.000Z",1],["2024-02-29T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",7],["2024-03-01T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-03-01T00:00:00.000Z",1],["2024-02-29T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-28T00:00:00.000Z",1],["2024-02-28T00:00:00.000Z",5],["2024-02-28T00:00:00.000Z",1],["2024-02-28T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-27T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1],["2024-02-26T00:00:00.000Z",1]]}]

Highcharts.chart('container', {
  chart: {
    type: 'scatter',
    zoomType: 'xy'
  },
  xAxis: {
    type: 'datetime', 
    lineColor: "#000",
    lineWidth: 3,
    labels: {
      style: {
        color: "#000",
      },
    },
  },
  yAxis: {
    gridLineColor: "#283347",
    lineColor: "#fff",
    lineWidth: 3,
    labels: {
      style: {
        color: "#000",
      },
    },
    title: {
      text: "",
    },
    opposite: false,
  },
  legend: {
    enabled: true
  },
  plotOptions: {
    scatter: {
      marker: {
        radius: 2.5,
        symbol: 'circle',
        states: {
          hover: {
            enabled: true,
            lineColor: 'rgb(100,100,100)'
          }
        }
      },
      states: {
        hover: {
          marker: {
            enabled: false
          }
        }
      },
      jitter: {
        x: 0.005
      }
    }
  },
  tooltip: {
    formatter: function () {
      return 'Date: ' + Highcharts.dateFormat('%b %d, %Y', new Date(this.x)) + '<br/>PV: ' + this.y;
    }
  },
  series: series
});
#container {
    max-width: 800px;
    margin: auto;
}

.highcharts-figure,
.highcharts-data-table table {
    min-width: 360px;
    max-width: 800px;
    margin: 1em auto;
}

.highcharts-data-table table {
    font-family: Verdana, sans-serif;
    border-collapse: collapse;
    border: 1px solid #ebebeb;
    margin: 10px auto;
    text-align: center;
    width: 100%;
    max-width: 500px;
}

.highcharts-data-table caption {
    padding: 1em 0;
    font-size: 1.2em;
    color: #555;
}

.highcharts-data-table th {
    font-weight: 600;
    padding: 0.5em;
}

.highcharts-data-table td,
.highcharts-data-table th,
.highcharts-data-table caption {
    padding: 0.5em;
}

.highcharts-data-table thead tr,
.highcharts-data-table tr:nth-child(even) {
    background: #f8f8f8;
}

.highcharts-data-table tr:hover {
    background: #f1f7ff;
}
<script src="https://code.highcharts.com/highcharts.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.highcharts.com/modules/accessibility.js"></script>

<figure class="highcharts-figure">
    <div id="container"></div>
    
</figure>

How do I make the x-axis display the dates? Also for a large dataset. Not sure how to optimize it.

Fiddle link: https://jsfiddle.net/nb_nb_nb/vymdkqzb/1/

1

There are 1 answers

0
Dominik Chudy On BEST ANSWER

The issue here is that Highcharts might not recognize the date format in your data. To make it work change your data to this format for example:

 data:[
  [new Date('2017-01-01T00:00:00Z').getTime(), 1],
  [new Date('2017-01-03T00:00:00Z').getTime(), 2]
 ]

Demo: https://jsfiddle.net/BlackLabel/stxj1aqr/