HighCharts不在Polymer页面上呈现

时间:2015-02-24 11:05:55

标签: javascript jquery html highcharts polymer

我在项目中将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不是对象(评估   'HighchartsAdapter.addEvent')(匿名   function)highcharts.js:304:237(匿名   function)highcharts.js:306:162全局codehighcharts.js:322   exporting.js:9

  

ReferenceError:找不到变量:$

如何解决此错误?

谢谢!

2 个答案:

答案 0 :(得分:2)

您的页面上未定义

Highcharts ,因此请将Highcharts脚本放在页面顶部。然后使用:

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

而不是:

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

这是工作fiddle

答案 1 :(得分:1)

您可以使用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

编辑:

以下是新的更优化的方法:

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

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