HighCharts 未在 Polymer 页面上呈现

HighCharts not rendering on Polymer page

我将 Polymer 与 HighCharts 结合用于我的项目。 我使用的代码是:

HTML :

<div class="container" layout vertical center>
        <paper-shadow z="1" class="span-shadow">
          <post-card id="card1">
            <img width="70" height="70" src="../images/graphex.jpg">
            <h2>Some Graph</h2>
            <div id="container2" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
          </post-card>
        </paper-shadow>

对于 JS:

$(function () {
    $('#container2').highcharts({
        title: {
            text: 'Monthly Average Temperature',
            x: -20 //center
        },
        subtitle: {
            text: 'Source: WorldClimate.com',
            x: -20
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },
        yAxis: {
            title: {
                text: 'Temperature (°C)'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            valueSuffix: '°C'
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle',
            borderWidth: 0
        },
        series: [{
            name: 'Tokyo',
            data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
        }, {
            name: 'New York',
            data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
        }, {
            name: 'Berlin',
            data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
        }, {
            name: 'London',
            data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
        }]
    });
});

但是虽然卡片生成正确,但里面的图表不可见,我得到这个错误:

TypeError: undefined is not an object (evaluating 'HighchartsAdapter.addEvent') (anonymous function)highcharts.js:304:237 (anonymous function)highcharts.js:306:162 global codehighcharts.js:322 exporting.js:9

ReferenceError: Can't find variable: $

如何修复此错误?

谢谢!

Highcharts在你的页面上没有定义,所以把Highcharts的脚本放在你的页面顶部。然后使用:

var chart = new Highcharts.Chart({
        chart: {
            renderTo: 'container2'
        }

而不是:

$('#container2').highcharts({ .. });

这是工作 fiddle

您可以使用 Highcharts-Chart,它允许您做同样的事情,但会为您管理依赖项。 并且还支持数据绑定和事件支持。

针对您的问题:

<head>
  <link rel="import" href="bower_components/highcharts-chart/highcharts-chart.html">
</head>
<body>
  <template is="dom-bind" id="app">
    <highcharts-chart id="Chart" x-axis='{"categories": [[arr2str(chartInfo.categories)]]}' type="spline" title="Monthly Average Temperature" subtitle="Source: WorldClimate.com" x-label="Months" tooltip-options='{"valueSuffix": "&deg;C"}' on-after-print="getData" y-label="Average"></highcharts-chart>
  </template>
  <script>
    var app = document.querySelector("#app")
    app.chartInfo = {
      categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
      data: [
        {name: "Tokyo", series: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]},
        {name: "New York", series: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]},
        {name: "Berlin", series: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]},
        {name: "London", series: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]},
      ]
    }
    app.arr2str = (arr)=>{return JSON.stringify(arr)}
    app.async(()=>{
       app.$.Chart._chart.xAxis[0].update({categories: app.chartInfo.categories})
       app.chartInfo.data.forEach((el)=>{
         app.$.Chart.addSeries(el.name,el.series)
       })
    },500)
  </script>
</body>

就是这样!更多示例 here.

编辑:

这是一种更优化的新方法:

<base href="https://user-content-dot-custom-elements.appspot.com/avdaredevil/highcharts-chart/v2.0.1/highcharts-chart/">
<link rel="import" href="highcharts-chart.html">
<body>
  <template is="dom-bind" id="app">
    <highcharts-chart id="Chart" x-axis='[[makeCategorical]]' type="spline" title="Monthly Average Temperature" subtitle="Source: WorldClimate.com" x-label="Months" tooltip-options='{"valueSuffix": "&deg;C"}' data="[[chartData]]" y-label="Average"></highcharts-chart>
  </template>
  <script>
    var app = document.querySelector("#app")
    app.makeCategorical = {categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']}

    app.chartData = [
      {name: "Tokyo", data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]},
      {name: "New York", data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]},
      {name: "Berlin", data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]},
      {name: "London", data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]},
    ]
  </script>
</body>

点击运行代码段查看图表!