如何重写Google地理图表的代码以将其与angular(angular-google-charts)一起使用?

我开始在一个简单的html文件中处理google-charts,实际上得到了我一直想要的结果。为此,我使用了很多“ google .....”方法。现在,我想在我的Angular-Application(带有angular-google-charts)中使用此代码,为此,我不得不转换我的代码。

我的html文件:

<html>
  <head>
    <style>
      .google-visualization-tooltip { 
        width: 150px !important;
        background-color: #515152 !important;
        position: absolute !important;
        color: white;
      }
    </style>
    <!--================================================================================================-->
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current',{
        'packages':['geochart'],'mapsApiKey': 'myKey'
      });
      google.charts.setOnLoadCallback(drawRegionsMap);

      function drawRegionsMap() {
        var dataTable = new google.visualization.DataTable();
        dataTable.addColumn('string','Country');
        dataTable.addColumn('number','amount');
        dataTable.addColumn({type: 'string',role: 'tooltip'})

        dataTable.addRows([
          ['Country',''],['India',2,'visited'],['Australia',4,'want to visit'],['Germany',1,'homecountry'],['United States',['Brazil',['Canada',3,'favorite destination'],['France',['Russia','visited']
        ]);

    var options = {
      backgroundColor: '#17263c',datalessRegionColor: '#242f3e',/*legend: {
        textStyle: {
          color: 'blue',fontSize: '16'
        }
      },*/
      legend: 'none',tooltip: {
        isHtml: true,textStyle: {
          color: 'white'
        }
      },colors: ['#46127A','#1102BB','#1633C4','#3185CE']
    };

      var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));

      chart.draw(dataTable,options);
    }
    </script>
    <!--================================================================================================-->
  </head>
  <body>
    <div id="regions_div" style="width: 100%; height: 100%;"></div>
  </body>
</html>

这就是我已经拥有的:

<google-chart #chart
   [title]="title"
   [type]="type"
   [data]="data"
   [columnNames]="columnNames"
   [options]="options"
   [firstRowIsData]="false"
   style="width: 100%; height: 100%;">
</google-chart>
type="GeoChart";
  data = [
    ['Country','amount',{type: 'string',role: 'tooltip'}],['Country','visited']      
  ];
  options = {
    backgroundColor: '#17263c',legend: 'none',tooltip: {
      isHtml: true,textStyle: {
        color: 'black'
      }
    },'#3185CE']
  };

现在我不知道如何为图表定义3.列,我可以在其中设置工具提示,也不知道如何添加CSS。因为这个谷歌很多。 ...我以为我必须从“我不知道在哪里”导入{google},那时我真的不知道如何使用它。

如果有人可以给我一个例子,我会很高兴 1.允许我创建3列的图表数据(与第一个代码段相同),并且 2.允许我使用CSS(.google-visualization-tooltip {})

编辑:我在上面的代码中添加了一些内容来获取3.列,现在出现了这个错误:

core.js:9110 ERROR Error: Unknown type of value in 0,2
    at gvjs_Rba (jsapi_compiled_default_module.js:130)
    at Object.gvjs_Eo [as arrayToDataTable] (jsapi_compiled_default_module.js:130)
    at GoogleChartComponent.updateChart (angular-google-charts.js:266)
    at SafeSubscriber._next (angular-google-charts.js:473)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:185)
    at SafeSubscriber.next (Subscriber.js:124)
    at Subscriber._next (Subscriber.js:72)
    at Subscriber.next (Subscriber.js:49)
    at angular-google-charts.js:89
    at ZoneDelegate.invoke (zone-evergreen.js:359)

谢谢!

adi2589900 回答:如何重写Google地理图表的代码以将其与angular(angular-google-charts)一起使用?

您可以将列标题作为数据的一部分传递...

data = [
  ['Country','amount',{type: 'string',role: 'tooltip'}],['Country',''],['India',2,'visited'],['Australia',4,'want to visit'],['Germany',1,'homecountry'],['United States',['Brazil',['Canada',3,'favorite destination'],['France',['Russia','visited']      
];

传递列标题时,有一个属性(firstRowIsData)必须为false。
false是默认值,因此,如果在某处将其设置为true,则将其删除...

<raw-chart [firstRowIsData]="true"></raw-chart>

对于CSS,您可以像往常一样在HTML中添加...

,

我现在找到了一种方法: 我没有使用angular-google-charts库。为此,我在intex.html中添加了脚本:

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

在component.html中,我基本上是这样写的:

<div id="regions_div" style="width: 100%; height: 100%;"></div>

在component.ts中,我为此使用了ngOnInit-Method:

ngOnInit() {

google.charts.load('current',{
  'packages':['geochart'],'mapsApiKey': 'myKey'
});
google.charts.setOnLoadCallback(drawRegionsMap);

function drawRegionsMap() {
  var dataTable = new google.visualization.DataTable();
  dataTable.addColumn('string','Country');
  dataTable.addColumn('number','amount');
  dataTable.addColumn({type: 'string',role: 'tooltip'})

  dataTable.addRows([
    ['Country','visited']
  ]);

var options = {
backgroundColor: '#17263c',datalessRegionColor: '#242f3e',legend: 'none',tooltip: {
  isHtml: true,textStyle: {
    color: 'black'
  }
},colors: ['#46127A','#1102BB','#1633C4','#3185CE']
};

var chart = new google.visualization.GeoChart(document.getElementById('regions_div'));

chart.draw(dataTable,options);
}

现在编译时出现错误:

 ERROR in src/app/components/chart/chart.component.ts(65,27): error TS2345: Argument of type '{ backgroundColor: string; datalessRegionColor: string; legend: string; tooltip: { isHtml: boolean; textStyle: { color: string; }; }; colors: string[]; }' is not assignable to parameter of type 'GeoChartOptions'.
  Types of property 'legend' are incompatible.
    Type 'string' is not assignable to type '"none" | ChartLegend'.

但这基本上只是告诉我我的选项不正确,但是在页面视图中,所有内容仍然正确显示。

本文链接:https://www.f2er.com/3141252.html

大家都在问