【问题标题】:Drill Down (Highcharts) not working with dynamic data in Angular向下钻取(Highcharts)不适用于Angular中的动态数据
【发布时间】:2021-07-01 05:45:50
【问题描述】:

我在我的 Angular 项目中使用高图表 API,我的问题是当我获得动态数据时,我成功生成了高图表,但是当我点击任何状态时,我的向下钻取功能没有被命中或不工作。在我的场景中需要使用带有钻取的 highchart,因此经过大约 5 个小时的搜索和开发,我没有从谷歌或 highcharts 自己的论坛获得任何解决方案。

这是我的代码:

generateMap() {
this.Highcharts = Highcharts;

const usMapData = require("@highcharts/map-collection/countries/us/us-all.geo.json");
const usMap = Highcharts.geojson(usMapData);

this.chartOptions = {
  chart: {
    height: (8 / 16) * 100 + "%",
  },
  events: {
    drilldown: function (e) {
      this.methodMap(e.point.splitName);
      const chart = this as any;
      const mapKey = "countries/us/" + e.point.drilldown + "-all";
      const mapData = require(`@highcharts/map-collection/${mapKey}.geo.json`);
      const provinceData = Highcharts.geojson(mapData);

      chart.addSeriesAsDrilldown(e.point, {
        name: e.point.name,
        data: provinceData,

        dataLabels: {
          enabled: true,
          format: '{point.name}'
        }
      });

      chart.setTitle(null, { text: e.point.name });
    },
    drillup() {
      const chart = this as any;
    }
  },
  title: {
    text: ""
  },
  colorAxis: {
    min: 0,
    minColor: "#E6E7E8",
    maxColor: "#417BCC"
  },

  mapNavigation: {
    enabled: true,
    buttonOptions: {
      verticalAlign: "bottom"
    }
  },
  plotOptions: {
    map: {
      states: {
        hover: {
          color: "#F8BA03"
        }
      }
    },
    series: {
      point: {
        events: {
          click: function (e) {
          }
        }
      }
    }
  },
  series: [
    {
      animation: {
        duration: 1000
      },
      name: "United States",
      data: null,
      dataLabels: {
        enabled: true,
        color: '#FFFFFF',
        format: `{point.splitName}`,
        style: {
          textTransform: 'uppercase',
        }
      },
    }
  ],
  drilldown: {}
};

this.CurrentVendorService.getAllVendorStates().pipe(
  tap(result => {
    this.chart.showLoading();
    
    usMap.forEach((el: any, i) => {
      el.splitName = el.properties["hc-key"].split('-')[1].toUpperCase();
      el.drilldown = el.properties["hc-key"];
      const getFirstMatchedVendor = result.data.find(vendorObj => vendorObj.State_Code == el.splitName);
      if (getFirstMatchedVendor) {
        el.value = getFirstMatchedVendor.Vendor_Count;
      }
    });
    
    this.chartOptions.series = [
      {
        data: usMap
      }
    ];
    
    this.updateFromInput = true;
    this.chart.hideLoading();
  },
    (error: any) => {
      this.gifLoader = false
      this.errorMessage = error.error.message;
      this.snackBar.open(this.errorMessage, '', {
        duration: 2000,
      });
      console.log(`error on retriving all vendors state list : ${error}`);
    }
  ),
  finalize(() => {})).subscribe();
}

组件.Html

<highcharts-chart
          [Highcharts]="Highcharts"
          [constructorType]="chartConstructor"
          [callbackFunction]="chartCallback"
          [options]="chartOptions"
          style="width: 100%; height: 400px; display: block;"
          [(update)]="updateFromInput"
        ></highcharts-chart>

generateMap() 致电ngOnInit()。如果我使用静态数据而不是服务。它的作用就像一个魅力,但这里的情况并非如此。我需要对我的动态数据进行深入研究。请帮我解决我做错的事情。

【问题讨论】:

    标签: angular highcharts angular2-highcharts


    【解决方案1】:

    经过几个小时的努力,我找到了自己的问题答案。 我仍在学习和提高对编程问题细节的关注度。

    解决方案

    让我用勺子喂你,你只需复制粘贴即可完成。

    一些全局声明:

    Highcharts;
    chartConstructor = "mapChart";
    chartOptions: Highcharts.Options;
    updateFromInput = false;
    chart;
    chartCallback;
    

    将下面的代码 sn-p 放入你的组件构造函数中

    this.chartCallback = chart => {
        this.chart = chart;
    };
    

    将下面的代码放入ngOnInit()

    this.generateMap();
    

    现在是主要部分:

    generateMap() {
        const self = this;
        self.Highcharts = Highcharts;
    
        const usMapData = require("@highcharts/map-collection/countries/us/us-all.geo.json");
        const usMap = Highcharts.geojson(usMapData);
    
        self.chartOptions = {
            chart: {
                height: (8 / 16) * 100 + "%",
                events: {
                    drilldown: function (e: any) {
                        setTimeout(() => {
                            self.methodMap(e.point.splitName);
                            const mapKey = "countries/us/" + e.point.drilldown + "-all";
                            const mapData = require(`@highcharts/map-collection/${mapKey}.geo.json`);
                            const provinceData = Highcharts.geojson(mapData);
                            self.chart.hideLoading();
                            self.chart.addSeriesAsDrilldown(e.point, {
                                name: e.point.name,
                                data: provinceData,
    
                                dataLabels: {
                                    enabled: true,
                                    format: '{point.name}'
                                }
                            } as Highcharts.SeriesOptionsType);
    
                            self.chart.setTitle(null, { text: e.point.name });
                        }, 100);
                    },
                    drillup() {
                        self.resetMethod();
                    }
                }
            },
            title: {
                text: ""
            },
            colorAxis: {
                min: 0,
                minColor: "#E6E7E8",
                maxColor: "#417BCC"
            },
    
            mapNavigation: {
                enabled: true,
                buttonOptions: {
                    verticalAlign: "bottom"
                }
            },
            plotOptions: {
                map: {
                    states: {
                        hover: {
                            color: "#F8BA03"
                        }
                    }
                }
            },
            series: [
                {
                    type: "map",
                    name: "United States",
                    data: null,
                    dataLabels: {
                        enabled: true,
                        color: '#FFFFFF',
                        format: `{point.splitName}`,
                        style: {
                            textTransform: 'uppercase',
                        }
                    },
                }
            ],
            drilldown: {}
        };
    
        self.CurrentVendorService.getAllVendorStates().pipe(
            tap(result => {
                self.chart.showLoading();
    
                usMap.forEach((el: any, i) => {
                    el.splitName = el.properties["hc-key"].split('-')[1].toUpperCase();
                    el.drilldown = el.properties["hc-key"];
                    const getFirstMatchedVendor = result.data.find(vendorObj => vendorObj.State_Code == el.splitName);
                    if (getFirstMatchedVendor) {
                        el.value = getFirstMatchedVendor.Vendor_Count;
                    }
                });
    
                self.chartOptions.series = [
                    {
                        type: "map",
                        data: usMap
                    }
                ];
    
                self.updateFromInput = true;
                self.chart.hideLoading();
            },
                (error: any) => {
                    self.gifLoader = false
                    self.errorMessage = error.error.message;
                    self.snackBar.open(self.errorMessage, '', {
                        duration: 2000,
                    });
                    console.log(`error on retriving all vendors state list : ${error}`);
                }
            ),
            finalize(() => { })).subscribe();
    }
    

    是的,您将根据需要自定义上述业务逻辑。但这就是我想要的。我对self.chartOptions 对象做了一些修改并添加了setTimeout

    干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 1970-01-01
      • 2023-03-12
      • 2015-12-15
      • 1970-01-01
      相关资源
      最近更新 更多