在 highcharts 上生成散点图的 X 轴

问题描述 投票:0回答:1

我正在尝试基于日期的散点图。我的意思是我想在 x 轴上添加日期,但它没有添加日期。在我的代码中,我添加了一小部分数据。数据点超过 500000 个。最有效的方法是什么?

我的代码:

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>

如何让x轴显示日期?也适用于大型数据集。不知道如何优化。

小提琴链接:https://jsfiddle.net/nb_nb_nb/vymdkqzb/1/

javascript date charts highcharts scatter-plot
1个回答
0
投票

此处的问题是 Highcharts 可能无法识别数据中的日期格式。为了使其正常工作,请将您的数据更改为这种格式,例如:

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

演示: https://jsfiddle.net/BlackLabel/stxj1aqr/

© www.soinside.com 2019 - 2024. All rights reserved.