放大 AMCharts 时丢失 Timeserie

2024-03-06

当我放大图表时,serie2 消失了...如果我通过从右侧选择光标进行缩放...但是从左侧缩放工作正常!

无论如何,我期待看到 2 系列,但似乎有时不是......

See my 截屏 https://i.stack.imgur.com/MXsX1.png

知道为什么吗?

我的组件如下所示:

import { Component, NgZone, OnDestroy, OnInit } from '@angular/core';
import * as am4core from '@amcharts/amcharts4/core';
import * as am4charts from '@amcharts/amcharts4/charts';
import am4themes_animated from '@amcharts/amcharts4/themes/animated';
import {DayAheadService} from '../../_services/day-ahead.service';
import { Subscription } from 'rxjs';

am4core.useTheme(am4themes_animated);

@Component({
  providers: [DayAheadService],
  selector: 'app-max',
  templateUrl: './maximize.component.html',
  styleUrls: ['./maximize.component.scss']
})
export class MaximizeComponent implements OnDestroy {
  private chart: am4charts.XYChart;
  private subscription: Subscription;
  clearingsSell: any = [];
  clearingsBuy: any = [];
  constructor(
    private dayAheadService: DayAheadService,
    private zone: NgZone) {}

    ngOnInit() {

    }

    ngAfterViewInit() {
    this.zone.runOutsideAngular(() => {
      let chart = am4core.create('chartdiv', am4charts.XYChart);
      chart.paddingRight = 20;
      let data1 = [];
      let data2 = [];
      this.subscription = this.dayAheadService.requestAggClearingsMada('2019-01-01', 'asset' , '2019-01-19' ).subscribe(x => {
        this.clearingsSell = x.filter(f => f.direction === 'Sell');
        this.clearingsBuy = x.filter(f => f.direction === 'Buy');
        for (let i = 1; i < this.clearingsSell.length; i++) {
          for (let j = 0; j < 24; j++) {
              data1.push({ category : 'Sell', date: new Date(2019, 0, i, j).setHours(j), value1: this.clearingsSell[i].profilesData[j].price });
          }
        }
        for (let i = 1; i < this.clearingsBuy.length; i++) {
          for (let j = 0; j < 24; j++) {
              data2.push({ category : 'Buy', date: new Date(2019, 0, i, j).setHours(j), value2: this.clearingsBuy[i].profilesData[j].price });
          }
        }
        chart.data = data1.concat(data2);
        console.log(chart.data);
        });

        chart.dateFormatter.inputDateFormat = 'YY-MM-DD HH';
        const dateAxis = chart.xAxes.push(new am4charts.DateAxis());
        dateAxis.renderer.minGridDistance = 90;
        dateAxis.startLocation = 0.5;
        dateAxis.endLocation = 0.5;
        const valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
        valueAxis.tooltip.disabled = true;
        const series = chart.series.push(new am4charts.StepLineSeries());
        series.dataFields.dateX = 'date';
        series.name = 'Sell Vol.';
        series.dataFields.valueY = 'value1';
        series.tooltipText = '[#000]{valueY.value}[/]';
        series.tooltip.background.fill = am4core.color('#FFF');
        series.tooltip.getStrokeFromObject = true;
        series.tooltip.background.strokeWidth = 3;
        series.tooltip.getFillFromObject = false;
        series.fillOpacity = 0.6;
        series.strokeWidth = 2;
        series.stacked = true;
        const series2 = chart.series.push(new am4charts.StepLineSeries());
        series2.name = 'Buy Vol.';
        series2.dataFields.dateX = 'date';
        series2.dataFields.valueY = 'value2';
        series2.tooltipText = '[#000]{valueY.value}[/]';
        series2.tooltip.background.fill = am4core.color('#FFF');
        series2.tooltip.getFillFromObject = false;
        series2.tooltip.getStrokeFromObject = true;
        series2.tooltip.background.strokeWidth = 3;
        series2.sequencedInterpolation = true;
        series2.fillOpacity = 0.6;
        series2.stacked = true;
        series2.strokeWidth = 2;
        // Add scrollbar
        chart.scrollbarX = new am4charts.XYChartScrollbar();
        // Add cursor
        chart.cursor = new am4charts.XYCursor();
        chart.cursor.xAxis = dateAxis;
        chart.cursor.snapToSeries = series;
        // Add a legend
        chart.legend = new am4charts.Legend();
        chart.legend.position = 'top';
    });
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
    this.zone.runOutsideAngular(() => {
      if (this.chart) {
        this.chart.dispose();
      }
    });
  }
}

AmCharts 希望基于日期的数据按日期分组,而不是串联,否则您将遇到未定义的行为,例如缩放问题。您需要重写您的订阅方法以正确聚合所有内容:

clearingsSell = x.filter(f => f.direction === "Sell");
clearingsBuy = x.filter(f => f.direction === "Buy");
//store everything into an object with the date as the key
for (let i = 1; i < clearingsSell.length; i++) {
  for (let j = 0; j < 24; j++) {
    let date = new Date(2019, 0, i, j).setHours(j);
    data[date] = {
      date: date,
      value1: clearingsSell[i].profilesData[j].price
    }
  }
}
for (let i = 1; i < clearingsBuy.length; i++) {
  for (let j = 0; j < 24; j++) {
    let date = new Date(2019, 0, i, j).setHours(j);
    if (data[date] === undefined) {
      data[date] = {date: date};
    }
    data[date].value2 = clearingsBuy[i].profilesData[j].price
  }
}
//convert grouped data into array, iterating by date
chart.data = Object.keys(data).map(function(date) { 
  return data[date];
})

请注意,我删除了这些类别,因为它们似乎与您的图表设置无关。这是基于您的代码的简化演示:

let chart = am4core.create("chartdiv", am4charts.XYChart);
chart.paddingRight = 20;
let data = {};
let data1 = [];
let data2 = [];

x = getData(); 
//--- modified subscribe code ---
clearingsSell = x.filter(f => f.direction === "Sell");
clearingsBuy = x.filter(f => f.direction === "Buy");
//store everything into an object with the date as the key
for (let i = 1; i < clearingsSell.length; i++) {
  for (let j = 0; j < 24; j++) {
    let date = new Date(2019, 0, i, j).setHours(j);
    data[date] = {
      date: date,
      value1: clearingsSell[i].profilesData[j].price
    }
  }
}
for (let i = 1; i < clearingsBuy.length; i++) {
  for (let j = 0; j < 24; j++) {
    let date = new Date(2019, 0, i, j).setHours(j);
    if (data[date] === undefined) {
      data[date] = {date: date};
    }
    data[date].value2 = clearingsBuy[i].profilesData[j].price
  }
}
//convert grouped data into array, iterating by date
chart.data = Object.keys(data).map(function(date) { 
  return data[date];
});
// --- end modified subscribe code --

chart.dateFormatter.inputDateFormat = "YY-MM-DD HH";
const dateAxis = chart.xAxes.push(new am4charts.DateAxis());
dateAxis.renderer.minGridDistance = 90;
dateAxis.startLocation = 0.5;
dateAxis.endLocation = 0.5;
const valueAxis = chart.yAxes.push(new am4charts.ValueAxis());
valueAxis.tooltip.disabled = true;
const series = chart.series.push(new am4charts.StepLineSeries());
series.dataFields.dateX = "date";
series.name = "Sell Vol.";
series.dataFields.valueY = "value1";
series.tooltipText = "[#000]{valueY.value}[/]";
series.tooltip.background.fill = am4core.color("#FFF");
series.tooltip.getStrokeFromObject = true;
series.tooltip.background.strokeWidth = 3;
series.tooltip.getFillFromObject = false;
series.fillOpacity = 0.6;
series.strokeWidth = 2;
series.stacked = true;
const series2 = chart.series.push(new am4charts.StepLineSeries());
series2.name = "Buy Vol.";
series2.dataFields.dateX = "date";
series2.dataFields.valueY = "value2";
series2.tooltipText = "[#000]{valueY.value}[/]";
series2.tooltip.background.fill = am4core.color("#FFF");
series2.tooltip.getFillFromObject = false;
series2.tooltip.getStrokeFromObject = true;
series2.tooltip.background.strokeWidth = 3;
series2.sequencedInterpolation = true;
series2.fillOpacity = 0.6;
series2.stacked = true;
series2.strokeWidth = 2;
// Add scrollbar
chart.scrollbarX = new am4charts.XYChartScrollbar();
// Add cursor
chart.cursor = new am4charts.XYCursor();
chart.cursor.xAxis = dateAxis;
chart.cursor.snapToSeries = series;
// Add a legend
chart.legend = new am4charts.Legend();
chart.legend.position = "top";

function getData() {  
  return [
    {
      direction: "Buy",
      profilesData: [
        {
          price: 14
        },
        {
          price: 12
        },
        {
          price: 16
        },
        {
          price: 13
        },
        {
          price: 10
        },
        {
          price: 10
        },
        {
          price: 18
        },
        {
          price: 13
        },
        {
          price: 17
        },
        {
          price: 12
        },
        {
          price: 18
        },
        {
          price: 10
        },
        {
          price: 10
        },
        {
          price: 17
        },
        {
          price: 17
        },
        {
          price: 20
        },
        {
          price: 13
        },
        {
          price: 17
        },
        {
          price: 13
        },
        {
          price: 12
        },
        {
          price: 16
        },
        {
          price: 16
        },
        {
          price: 13
        },
        {
          price: 11
        }
      ]
    },
    {
      direction: "Buy",
      profilesData: [
        {
          price: 14
        },
        {
          price: 15
        },
        {
          price: 13
        },
        {
          price: 14
        },
        {
          price: 18
        },
        {
          price: 10
        },
        {
          price: 10
        },
        {
          price: 18
        },
        {
          price: 17
        },
        {
          price: 15
        },
        {
          price: 17
        },
        {
          price: 19
        },
        {
          price: 12
        },
        {
          price: 20
        },
        {
          price: 11
        },
        {
          price: 16
        },
        {
          price: 17
        },
        {
          price: 15
        },
        {
          price: 13
        },
        {
          price: 10
        },
        {
          price: 18
        },
        {
          price: 10
        },
        {
          price: 19
        },
        {
          price: 14
        }
      ]
    }, 
    {
      direction: "Sell",
      profilesData: [
        {
          price: 13
        },
        {
          price: 11
        },
        {
          price: 12
        },
        {
          price: 6
        },
        {
          price: 8
        },
        {
          price: 9
        },
        {
          price: 13
        },
        {
          price: 15
        },
        {
          price: 13
        },
        {
          price: 11
        },
        {
          price: 12
        },
        {
          price: 7
        },
        {
          price: 8
        },
        {
          price: 10
        },
        {
          price: 6
        },
        {
          price: 5
        },
        {
          price: 5
        },
        {
          price: 7
        },
        {
          price: 10
        },
        {
          price: 13
        },
        {
          price: 10
        },
        {
          price: 5
        },
        {
          price: 5
        },
        {
          price: 6
        }
      ]
    },
    {
      direction: "Sell",
      profilesData: [
        {
          price: 11
        },
        {
          price: 7
        },
        {
          price: 5
        },
        {
          price: 12
        },
        {
          price: 7
        },
        {
          price: 5
        },
        {
          price: 13
        },
        {
          price: 6
        },
        {
          price: 12
        },
        {
          price: 11
        },
        {
          price: 11
        },
        {
          price: 10
        },
        {
          price: 5
        },
        {
          price: 12
        },
        {
          price: 10
        },
        {
          price: 7
        },
        {
          price: 7
        },
        {
          price: 7
        },
        {
          price: 14
        },
        {
          price: 14
        },
        {
          price: 11
        },
        {
          price: 12
        },
        {
          price: 13
        },
        {
          price: 8
        }
      ]
    }
  ];
}
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

#chartdiv {
  width: 100%;
  height: 450px;
}
<script src="//www.amcharts.com/lib/4/core.js"></script>
<script src="//www.amcharts.com/lib/4/charts.js"></script>
<script src="//www.amcharts.com/lib/4/themes/animated.js"></script>
<div id="chartdiv"></div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

放大 AMCharts 时丢失 Timeserie 的相关文章

随机推荐

  • Rails 3:如何用英语以外的语言格式化日期?

    格式化一个Date用英语我做 Date today to s long ordinal gt September 28th 2011 如何用俄语 或任何其他语言 格式化日期 有的是Rails 中的国际化 API http guides ru
  • django-allauth - 使用 Gmail 帐户发送电子邮件验证

    我已经设置 allauth 为每个新注册用户发送一封电子邮件 以便可以验证他们的电子邮件 现在我使用 email backend 以便将电子邮件发送到终端 一切正常 但现在我想可以将其全部设置好 以便发送电子邮件 并且因为我 可能 在主机服
  • 如何使用 jQuery 在带有动画的表格中添加新行?

    这就是我正在做的在表中添加新行的操作 function expandAll myTableID gt tbody gt tr gt td nth child 2 gt div nth child 2 each function html t
  • android同时显示SIM卡和手机联系人

    在我的代码中 我应该只显示电话联系人 我遵循了之前的帖子 但仍然显示电话和 SIM 卡联系人 这是我的代码 Uri uri ContactsContract CommonDataKinds Phone CONTENT URI String
  • DateTime 为空字符串或 null?如何检查?

    Q 我想检查日期时间null value如果日期时间为空 则清空报告中的单元格 但我不知道如何执行此操作 它看起来像这样1 1 0001如果它是空的 我希望它是空单元格 这是我的数据集中的数据类型 这是我的列的表达式值 FormatDate
  • 是否有 IntelliJ Java Profiler [关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 IntelliJ 是否有像 Matlab 那样的 Profiler 假设你有这个代码 a true i 0 while a if a i
  • ORA-04084: 无法更改此触发器类型的新值

    我正在尝试打开 pl sql 触发器 该触发器在故事更改时计算表中某些单元格的总数 这是代码 ALTER session SET nls date format dd mm yyyy CREATE OR REPLACE TRIGGER TO
  • Go 中可以有函数指针吗?

    我正在学习 Go 中的指针 并设法写出类似的东西 func hello fmt Println Hello World func main pfunc hello pfunc is a pointer to the function hel
  • 2 轴 Reportlab 图

    我通过重叠条形图和线罐 成功在 ReportLab 中创建了一个 2 轴图 对于对类似内容感兴趣的任何人来说 这里是代码 from reportlab graphics shapes import Drawing colors from r
  • 让 htmlParse 与希伯来语一起工作?

    我希望 htmlParse 能够很好地处理希伯来语 但它不断地扰乱我输入的页面中的希伯来语文本 例如 why can t I parse the Hebrew correctly library RCurl library XML u ht
  • 如何使用 Codeigniter PHP 将 CSV 导入数据库?

    我正在尝试使用 csv 插入记录 我想将 csv 上传到我的应用程序并希望将其导入数据库 现在我有一个用户表 所以我想通过导入用户的 csv 文件来创建用户 我对文件上传了解一点 但对将其导入数据库一无所知 请帮助 请参阅我已使用以下命令完
  • Jetpack compose - 当应用程序返回前台时如何刷新屏幕

    当应用程序返回前台时 我需要自动刷新 Android Compose 屏幕 我有一个需要权限和位置服务的 如果用户关闭了其中任何一项 则会绘制一个需要更改的项目列表 当用户转到 设置 并且应用程序返回前台时 我希望刷新列表以反映更改 我正在
  • Android WebView 硬件加速 Artefact 解决方法

    Android 中的 WebView 硬件加速存在一个已知的错误 例如 请参见此处 https code google com p android issues detail id 17352 https code google com p
  • 每次 React 向 Express 发送请求时,都会生成一个新的会话

    I use React作为客户端发送请求Express with proxy and express session设置 但每次 React 向 Express 服务器发出请求时 都会创建一个新会话 因此 我通过手动访问相同的 api ur
  • chrome.tabs.update() 重定向到 'chrome-extension://invalid/'

    我编写了一个 chrome 扩展 它根据作为内容脚本注入的计时器的值重定向当前选项卡 后台脚本通过每隔一段时间轮询每个计时器来跟踪所有打开的选项卡的运行时间 如果在特定站点上花费的时间超过给定限制 则将活动选项卡重定向到间隙页面 并提供重置
  • 如何使用SBT和IntelliJ IDEA管理多个相互依赖的模块?

    我正在开发几个相互依赖的模块 并且希望在一个 IDEA 项目中将它们一起使用 我在用着sbt idea https github com mpeltonen sbt idea从 sbt 构建定义生成 IDEA 项目 这对于单个项目非常有用
  • 如何阻止 Reporting Services 报表在启动时自动呈现?

    我注意到 如果报表的所有参数都指定了默认值 那么它会在启动时自动呈现 我怎样才能防止这种情况发生 也就是说 我不希望在用户单击 查看报告 按钮之前呈现报告 如果所有参数都有默认值 则无法停止报表呈现 自动停止报表呈现的唯一方法是至少有一个没
  • Mac 命令行 - 列出可用的串行端口?

    在我的 Mac 上 我目前有可用的串行端口 dev tty usbserial A700dYoR dev cu usbserial A700dYoR dev tty 蓝牙 PDA 同步 dev cu 蓝牙 PDA 同步 dev tty 蓝牙
  • Java:在for循环init中初始化多个变量?

    我想要两个不同类型的循环变量 有什么办法可以让这个工作吗 Override public T get int index throws IndexOutOfBoundsException syntax error on first int
  • 放大 AMCharts 时丢失 Timeserie

    当我放大图表时 serie2 消失了 如果我通过从右侧选择光标进行缩放 但是从左侧缩放工作正常 无论如何 我期待看到 2 系列 但似乎有时不是 See my 截屏 https i stack imgur com MXsX1 png 知道为什