简体   繁体   English

如何使用 Angular 在 amCharts 中创建图例?

[英]How to create a legend in amCharts with Angular?

I have an application in which complex charts are displayed.我有一个显示复杂图表的应用程序。 I would like to add a legend to these, but I have no idea how to do it.我想为这些添加一个图例,但我不知道该怎么做。

Inside my component I import amcharts like this:在我的组件中,我像这样导入amchart

import { AmChartsService } from '@amcharts/amcharts3-angular';

Then I call this function:然后我调用这个函数:

  getCharts(id: string) {
    this.charts.forEach(chart => {
      const amChart = this.AmChartsService.makeChart(chart.id, this.chartService.getById(MyEnum.config));
    });
  }

The config I pass into the function above is defined in my chart service like this:我传递给上述函数的配置在我的图表服务中定义如下:

const charts: Chart[] = [
  { 
    id: MyEnum.config,
    data: {
      ...
      },
      valueAxes: [{
      ...
      }],
      balloon: {
      ...
      },
      chartCursor: {
      ...
      },
      legend: {
        position: 'bottom',
        bottom: 10,
        left: 15,
        width: 'auto',
        autoMargins: false,
        data: []
      },
    }
  }
];

The legend is only displayed if I remove data:[] from the config.仅当我从配置中删除data:[] 时才会显示图例。 But then only the markers of the legend are shown.但随后只显示图例的标记。

I would like a legend on the bottom of my charts with markers and labels to illustrate the meaning of the lines.我希望图表底部有一个带有标记和标签的图例来说明线条的含义。

The data property is used for creating your own custom markers and cannot be used with auto-generated legends by default, so you need to remove the data:[] line. data属性用于创建您自己的自定义标记,默认情况下不能与自动生成的图例一起使用,因此您需要删除data:[]行。 To add a label to the auto-generated legend markers, set your graphs' title properties to an appropriate value as it uses the title value by default :要将标签添加到自动生成的图例标记,请将图表的title属性设置为适当的值,因为它默认使用title值:

"graphs": [{
  "title": "graph 1",
  // ...
},{ 
  "title": "graph 2",
  // ...
}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM