使用ApexCharts时日期加时间比实际时间少8小时

使用ApexCharts时日期加时间比实际时间少8小时

原来的代码
html

<div id="chart-timeline">
<apexchart type="area" height="350" ref="chart" :options="chartOptions" :series="series"></apexchart>
</div>

js

var options = {
  chart: {
              id: 'area-datetime',
              type: 'area',
              height: 350,
              zoom: {
                autoScaleYaxis: true
              }
            },
  series: [{
    name: 'sales',
    data: [
              [new Date("2022-02-28 5:00:00"),30.95],
              [new Date("2022-02-28 10:00:00"),31.34],
              [new Date("2022-02-28 12:00:00"),31.18],
			  [new Date("2022-02-28 17:00:00"),20.18],
            ]
  }],
  markers: {
              size: 0,
              style: 'hollow',
            },
  xaxis: {
    type: 'datetime',
  },
  tooltip: {
              x: {
                format: 'yyyy-MM-dd HH:mm:ss'
              }
            },
}

var chart = new ApexCharts(document.querySelector("#chart-timeline"), options);
chart.render();

测试发现x轴的时间比我date中的时间少了八小时
在这里插入图片描述

解决

查阅资料后发现是时区问题,在官方文档中找到了
在这里插入图片描述
在xaxis中添加,完美解决问题

xaxis: {
              type: 'datetime',
			  labels: {
			  datetimeUTC : false,
			  }
            },

在这里插入图片描述

官方文档:https://apexcharts.com/javascript-chart-demos/area-charts/datetime-x-axis/
测试检验地址:https://codepen.io/apexcharts/pen/xYqyYm

// 数据数组声明 let AUCarr = Array(100).fill(null); let ACCarr = Array(100).fill(null); let dataPointIndex = 0; // 跟踪当前数据点索引 let dataInterval = null; let accChart = null; let aucChart = null; // 新增:epoch数组,1~400 let epochArr = Array.from({length: 100}, (_, i) => (i + 1) * 4); console.log(epochArr); // 基础图表配置 const baseConfig = { chart: { type: 'line', width: '100%', height: '400px', animations: { enabled: false }, toolbar: { show: false }, events: { mounted: function(ctx) { // 图表载完成后调整布局 ctx.updateOptions({ grid: { padding: { bottom: 30 } } }, false, false); } } }, grid: { padding: { left: 20, right: 20, bottom: 30 } }, dataLabels: { enabled: false }, stroke: { curve: 'smooth', width: 3 }, markers: { size: 6, shape: 'circle', strokeWidth: 2, strokeColor: '#fff' }, yaxis: { min: 0, max: 1, labels: { formatter: function(value) { return value.toFixed(2); } } }, xaxis: { labels: { style: { fontSize: '12px', cssClass: 'apexcharts-xaxis-label' }, offsetY: 5, show: true, tickAmount: 20 }, axisBorder: { show: true }, axisTicks: { show: true } }, tooltip: { enabled: true, } }; // ACC图表配置 const accoptions = { ...baseConfig, series: [{ name: 'ACC', data: ACCarr, }], labels: { show: true, // 确保显示标签 style: { fontSize: '12px' }, offsetY: 5 }, xaxis: { type: 'category', // 关键:强制按 category 处理 categories: epochArr, tickAmount: 0, // 关闭自动刻度 title: { text: 'epoch', style: { fontSize: '14px', fontWeight: 600 } }, labels: { style: { fontSize: '12px' }, offsetY: 8, formatter: function (value, index) { // epochArr 每 4 位出现一次,即 20 epoch return index % 4 === 0 ? value : ''; } } }, colors: ['#00E396'], markers: { ...baseConfig.markers, fillColors: ['#00E396'] } }; // AUC图表配置 const aucoptions = { ...baseConfig, series: [{ name: 'AUC', data: AUCarr, }], xaxis: { type: 'category', // 关键:强制按 category 处理 categories: epochArr, tickAmount: 0, // 关闭自动刻度 title: { text: 'epoch', style: { fontSize: '14px', fontWeight: 600 } }, labels: { style: { fontSize: '12px' }, offsetY: 8, formatter: function (value, index) { // epochArr 每 4 位出现一次,即 20 epoch return index % 5 === 0 ? value : ''; } } }, colors: ['#FEB019'], markers: { ...baseConfig.markers, fillColors: ['#FEB019'] } }; // 数据数组 const ACCarrlist = [ // 0–59 非线性增长 + 波动 0.0010,0.0083,0.0174,0.0284,0.0413,0.0562,0.0729,0.0915,0.1119,0.1340, 0.1577,0.1828,0.2092,0.2367,0.2651,0.2942,0.3238,0.3537,0.3836,0.4133, 0.4425,0.4709,0.4983,0.5244,0.5490,0.5719,0.5929,0.6118,0.6285,0.6430, 0.6552,0.6652,0.6731,0.6790,0.6831,0.6856,0.6966,0.6863,0.6950,0.6928, 0.7000,0.7068,0.7134,0.7201,0.7272,0.7349,0.7434,0.7529,0.7695,0.7754, 0.7786,0.7832,0.7992,0.7966,0.7954,0.8055,0.8069,0.8195,0.8133,0.8281, 0.8239,0.8305,0.8478,0.8556,0.8538,0.8621,0.8804,0.9084,0.9160,0.9228, // 70–99 平稳 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920, 0.920 ]; const AUCarrlist = [ // 0–59 非线性增长 + 波动 0.0010,0.0089,0.0191,0.0316,0.0463,0.0632,0.0823,0.1035,0.1267,0.1517, 0.1784,0.2066,0.2361,0.2667,0.2982,0.3303,0.3628,0.3955,0.4280,0.4602, 0.4918,0.5225,0.5520,0.5800,0.6063,0.6306,0.6528,0.6728,0.6804,0.6856, 0.7084,0.7089,0.7173,0.7237,0.7484,0.7415,0.7433,0.7540,0.7539,0.7632, 0.7622,0.7811,0.8002,0.8298,0.8401,0.8613,0.8737,0.8754,0.8726,0.8893, 0.8975,0.9072,0.9184,0.9210,0.9349,0.9400,0.9462,0.9534,0.9514,0.9600, // 60–99 平稳 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976, 0.976 ]; // 数据生成函数 function generateNextDataPoint() { if (dataPointIndex >= ACCarrlist.length || dataPointIndex >= epochArr.length) { clearInterval(dataInterval); dataInterval = null; return; } // 获取当前数据点 const aucValue = parseFloat(AUCarrlist[dataPointIndex].toFixed(4)); const accValue = parseFloat(ACCarrlist[dataPointIndex].toFixed(4)); AUCarr[dataPointIndex] = aucValue; ACCarr[dataPointIndex] = accValue; // 更新图表 accChart.updateSeries([{ data: ACCarr }], false); aucChart.updateSeries([{ data: AUCarr }], false); dataPointIndex++; } // 开始训练按钮事件 function startTraining() { // 重置数据状态 AUCarr = Array(100).fill(null); ACCarr = Array(100).fill(null); dataPointIndex = 0; // 清除旧定器 if (dataInterval) { clearInterval(dataInterval); dataInterval = null; } // 初始化或重置图表 if (!accChart) { accChart = new ApexCharts(document.querySelector('#acc'), accoptions); accChart.render(); } else { accChart.updateSeries([{ data: ACCarr }], false); } if (!aucChart) { aucChart = new ApexCharts(document.querySelector('#auc'), aucoptions); aucChart.render(); } else { aucChart.updateSeries([{ data: AUCarr }], false); } // 立即生成第一个数据点 generateNextDataPoint(); // 设置定器生成后续数据点 dataInterval = setInterval(generateNextDataPoint, 300); }为什么图表的横坐标没有正常显示
07-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值